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.Map;
29 import java.util.concurrent.ConcurrentMap;
30
31 import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry;
32
33
34
35
36 public class PluginCache {
37 private final transient PluginRegistry<PluginEntry> pluginCategories = new PluginRegistry<PluginEntry>();
38
39
40
41
42
43
44
45 public ConcurrentMap<String, PluginEntry> getCategory(final String category) {
46 return pluginCategories.getCategory(category);
47 }
48
49
50
51
52
53
54
55 public void writeCache(final OutputStream os) throws IOException {
56 final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os));
57 try {
58 out.writeInt(pluginCategories.getCategoryCount());
59 for (final Map.Entry<String, ConcurrentMap<String, PluginEntry>> category : pluginCategories.getCategories()) {
60 out.writeUTF(category.getKey());
61 final Map<String, PluginEntry> m = category.getValue();
62 out.writeInt(m.size());
63 for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) {
64 final PluginEntry plugin = entry.getValue();
65 out.writeUTF(plugin.getKey());
66 out.writeUTF(plugin.getClassName());
67 out.writeUTF(plugin.getName());
68 out.writeBoolean(plugin.isPrintable());
69 out.writeBoolean(plugin.isDefer());
70 }
71 }
72 } finally {
73 out.close();
74 }
75 }
76
77
78
79
80
81
82
83 public void loadCacheFiles(final Enumeration<URL> resources) throws IOException {
84 pluginCategories.clear();
85 while (resources.hasMoreElements()) {
86 final URL url = resources.nextElement();
87 final DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()));
88 try {
89 final int count = in.readInt();
90 for (int i = 0; i < count; i++) {
91 final String category = in.readUTF();
92 final ConcurrentMap<String, PluginEntry> m = pluginCategories.getCategory(category);
93 final int entries = in.readInt();
94 for (int j = 0; j < entries; j++) {
95 final PluginEntry entry = new PluginEntry();
96 entry.setKey(in.readUTF());
97 entry.setClassName(in.readUTF());
98 entry.setName(in.readUTF());
99 entry.setPrintable(in.readBoolean());
100 entry.setDefer(in.readBoolean());
101 entry.setCategory(category);
102 m.putIfAbsent(entry.getKey(), entry);
103 }
104 }
105 } finally {
106 in.close();
107 }
108 }
109 }
110
111
112
113
114
115
116 public int size() {
117 return pluginCategories.getCategoryCount();
118 }
119 }