1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.config.plugins.processor;
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.net.URL;
27 import java.util.Enumeration;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30
31
32
33
34 public class PluginCache {
35 private final Map<String, Map<String, PluginEntry>> categories =
36 new LinkedHashMap<>();
37
38
39
40
41
42
43
44 public Map<String, Map<String, PluginEntry>> getAllCategories() {
45 return categories;
46 }
47
48
49
50
51
52
53
54 public Map<String, PluginEntry> getCategory(final String category) {
55 final String key = category.toLowerCase();
56 if (!categories.containsKey(key)) {
57 categories.put(key, new LinkedHashMap<String, PluginEntry>());
58 }
59 return categories.get(key);
60 }
61
62
63
64
65
66
67
68
69 public void writeCache(final OutputStream os) throws IOException {
70 try (final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os))) {
71
72
73 out.writeInt(categories.size());
74 for (final Map.Entry<String, Map<String, PluginEntry>> category : categories.entrySet()) {
75 out.writeUTF(category.getKey());
76 final Map<String, PluginEntry> m = category.getValue();
77 out.writeInt(m.size());
78 for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) {
79 final PluginEntry plugin = entry.getValue();
80 out.writeUTF(plugin.getKey());
81 out.writeUTF(plugin.getClassName());
82 out.writeUTF(plugin.getName());
83 out.writeBoolean(plugin.isPrintable());
84 out.writeBoolean(plugin.isDefer());
85 }
86 }
87 }
88 }
89
90
91
92
93
94
95
96 public void loadCacheFiles(final Enumeration<URL> resources) throws IOException {
97 categories.clear();
98 while (resources.hasMoreElements()) {
99 final URL url = resources.nextElement();
100 try (final DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()))) {
101 final int count = in.readInt();
102 for (int i = 0; i < count; i++) {
103 final String category = in.readUTF();
104 final Map<String, PluginEntry> m = getCategory(category);
105 final int entries = in.readInt();
106 for (int j = 0; j < entries; j++) {
107 final PluginEntry entry = new PluginEntry();
108 entry.setKey(in.readUTF());
109 entry.setClassName(in.readUTF());
110 entry.setName(in.readUTF());
111 entry.setPrintable(in.readBoolean());
112 entry.setDefer(in.readBoolean());
113 entry.setCategory(category);
114 if (!m.containsKey(entry.getKey())) {
115 m.put(entry.getKey(), entry);
116 }
117 }
118 }
119 }
120 }
121 }
122
123
124
125
126
127
128 public int size() {
129 return categories.size();
130 }
131 }