1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.river.tool.envcheck.plugins;
19
20 import org.apache.river.start.ServiceDescriptor;
21 import org.apache.river.start.SharedActivatableServiceDescriptor;
22 import org.apache.river.start.SharedActivationGroupDescriptor;
23 import org.apache.river.tool.envcheck.AbstractPlugin;
24 import org.apache.river.tool.envcheck.EnvCheck;
25 import org.apache.river.tool.envcheck.Reporter;
26 import org.apache.river.tool.envcheck.Reporter.Message;
27 import org.apache.river.tool.envcheck.SubVMTask;
28 import org.apache.river.tool.envcheck.Util;
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.ResourceBundle;
33 import java.util.Set;
34 import net.jini.config.Configuration;
35 import net.jini.config.ConfigurationException;
36 import net.jini.config.ConfigurationFile;
37 import net.jini.config.ConfigurationProvider;
38 import org.apache.river.api.security.CombinerSecurityManager;
39
40
41
42
43
44
45
46
47
48
49 public class CheckPersistence extends AbstractPlugin {
50
51 EnvCheck envCheck;
52
53
54
55
56
57
58
59
60 public void run(EnvCheck envCheck) {
61 this.envCheck = envCheck;
62 ServiceDescriptor[] d = envCheck.getDescriptors();
63 for (int i = 0; i < d.length; i++) {
64 if (d[i] instanceof SharedActivatableServiceDescriptor) {
65 SharedActivatableServiceDescriptor sd =
66 (SharedActivatableServiceDescriptor) d[i];
67 checkDirectory(sd);
68 }
69 }
70 }
71
72
73
74
75
76
77
78
79
80 private void checkDirectory(SharedActivatableServiceDescriptor d) {
81 SharedActivationGroupDescriptor gd = envCheck.getGroupDescriptor();
82 String source = getString("descfor", d.getImplClassName());
83 Object o = envCheck.launch(d, gd, taskName("GetEntriesTask"));
84 if (o instanceof String[]) {
85 checkEntries((String[]) o, d, source);
86 } else if (o instanceof String) {
87 Message message = new Message(Reporter.WARNING,
88 (String) o,
89 getString("dirExp"));
90 Reporter.print(message, source);
91 } else {
92 handleUnexpectedSubtaskReturn(o, source);
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106 private void checkEntries(String[] entries,
107 SharedActivatableServiceDescriptor d,
108 String source)
109 {
110 if (entries.length == 0) {
111 Message message = new Message(Reporter.WARNING,
112 getString("noentry"),
113 getString("dirExp"));
114 Reporter.print(message, source);
115 }
116 for (int i = 0; i < entries.length; i += 2) {
117 String name = entries[i];
118 String dir = entries[i + 1];
119 String loopSource = source + ": " + name + "=" + dir;
120 Object lobj = checkDir(dir, d);
121 Message message;
122 if (lobj == null) {
123 message = new Message(Reporter.INFO,
124 getString("dirOK"),
125 getString("dirExp"));
126 Reporter.print(message, loopSource);
127 } else if (lobj instanceof String) {
128 message = new Message(Reporter.ERROR,
129 (String) lobj,
130 getString("dirExp"));
131 Reporter.print(message, loopSource);
132 } else {
133 handleUnexpectedSubtaskReturn(lobj, loopSource);
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147 private Object checkDir(String dir, SharedActivatableServiceDescriptor d) {
148 if (dir == null) {
149 return getString("nulldir");
150 }
151 String taskName = taskName("CheckDirTask");
152 String[] args = new String[]{dir};
153 SharedActivationGroupDescriptor g = envCheck.getGroupDescriptor();
154 return envCheck.launch(d, g, taskName, args);
155 }
156
157
158
159
160 public static class CheckDirTask implements SubVMTask {
161
162 private ResourceBundle bundle =
163 Util.getResourceBundle(CheckPersistence.class);
164
165 public Object run(String[] args) {
166 if (System.getSecurityManager() == null) {
167 System.setSecurityManager(new CombinerSecurityManager());
168 }
169 String dir = args[0];
170 File dirFile = new File(dir);
171 if (!dirFile.exists()) {
172 return null;
173 }
174 if (!dirFile.isDirectory()) {
175 return Util.getString("notadir", bundle, dir);
176 }
177 File[] contents = dirFile.listFiles();
178 if (contents == null) {
179 return Util.getString("emptylist", bundle, dir);
180 }
181 if (contents.length > 0) {
182 return Util.getString("dirnotempty", bundle, dir);
183 }
184 return null;
185 }
186 }
187
188
189
190
191
192
193
194 public static class GetEntriesTask implements SubVMTask {
195
196 private ResourceBundle bundle =
197 Util.getResourceBundle(CheckPersistence.class);
198
199 public Object run(String[] args) {
200 try {
201 Configuration config =
202 ConfigurationProvider.getInstance(args);
203 return getEntries(config);
204 } catch (ConfigurationException e) {
205 return Util.getString("configproblem", bundle, e.getMessage());
206 } catch (Exception e) {
207 return e;
208 }
209 }
210
211
212
213
214
215
216
217
218
219 private Object getEntries(Configuration conf) {
220 ConfigurationFile cf = (ConfigurationFile) conf;
221 ArrayList<String> list = new ArrayList<String>();
222 Set<String> names = cf.getEntryNames();
223 Iterator<String> it = names.iterator();
224 String s = "";
225 while (it.hasNext()) {
226 String name = it.next();
227 s += name + "\n";
228 int lastDot = name.lastIndexOf(".persistenceDirectory");
229 if (lastDot > 0) {
230 String component = name.substring(0, lastDot);
231 try {
232 String dir = conf.getEntry(component,
233 "persistenceDirectory",
234 String.class,
235 null);
236 list.add(name);
237 list.add(dir);
238 } catch (ConfigurationException e) {
239 return e;
240 }
241 }
242 }
243 return list.toArray(new String[list.size()]);
244 }
245 }
246 }