1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.util;
17
18 import java.io.IOException;
19 import java.lang.reflect.Field;
20
21 import org.seasar.framework.aop.impl.PointcutImpl;
22 import org.seasar.framework.container.ArgDef;
23 import org.seasar.framework.container.ArgDefAware;
24 import org.seasar.framework.container.AspectDef;
25 import org.seasar.framework.container.ComponentDef;
26 import org.seasar.framework.container.ContainerNotRegisteredRuntimeException;
27 import org.seasar.framework.container.Expression;
28 import org.seasar.framework.container.MethodDef;
29 import org.seasar.framework.container.PropertyDef;
30 import org.seasar.framework.container.S2Container;
31 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
32 import org.seasar.framework.container.impl.ArgDefImpl;
33 import org.seasar.framework.container.util.SmartDeployUtil;
34 import org.seasar.framework.env.Env;
35 import org.seasar.framework.util.StringUtil;
36
37
38
39
40
41
42 public class S2ContainerListToHtmlUtil {
43
44
45
46
47 public static final String CONFIG_PATH_KEY = "configPath";
48
49
50
51
52 public static final String DEBUG_KEY = "debug";
53
54
55
56
57 public static final String COMMAND = "command";
58
59
60
61
62 public static final String RESTART = "restart";
63
64
65
66
67 public static final String LIST = "list";
68
69
70
71
72 public static final String PATH = "path";
73
74 private static final String MODE_BEGIN = "<strong><font color='#DC143C'>";
75
76 private static final String MODE_END = "</font></strong>";
77
78
79
80
81
82
83
84
85 public static String list(final S2Container container, String path) {
86 StringBuffer sb = new StringBuffer();
87 if (container == null) {
88 sb.append("S2Container[" + escape(path) + "] is not found.");
89 return sb.toString();
90 }
91 sb.append("<html><head><title>Seasar2 Component List</title></head><body>");
92 try {
93 sb.append("<h1>S2Container</h1>");
94 sb.append(printSmartDeploy(container));
95 sb.append("<ul>");
96 try {
97 sb.append("<li>path : <code>" + escape(container.getPath()) + "</code></li>");
98 final String nameSpace = container.getNamespace();
99 if (!StringUtil.isEmpty(nameSpace)) {
100 sb.append("<li>namespace : <code>" + escape(nameSpace) + "</code></li>");
101 }
102 final String envValue = Env.getValue();
103 if (!StringUtil.isEmpty(envValue)) {
104 sb.append("<li>env : <code>" + escape(envValue) + "</code></li>");
105 }
106 } finally {
107 sb.append("</ul>");
108 }
109 sb.append(listComponent(container));
110
111 sb.append(listInclude(container, path));
112
113 } finally {
114 sb.append("</body></html>");
115 }
116 return sb.toString();
117 }
118
119 public static String list(final String path) {
120 final S2Container container = getContainer(path);
121 return list(container, path);
122 }
123
124
125
126
127
128
129
130 protected static S2Container getContainer(final String path) {
131 final S2Container root = SingletonS2ContainerFactory.getContainer();
132 try {
133 return StringUtil.isEmpty(path) ? root : root.getDescendant(path);
134 } catch (final ContainerNotRegisteredRuntimeException e) {
135 return null;
136 }
137 }
138
139 private static StringBuffer printSmartDeploy(final S2Container container) {
140 StringBuffer sb = new StringBuffer();
141 sb.append("<p>S2Container is working under ");
142 try {
143 if (SmartDeployUtil.isHotdeployMode(container)) {
144 sb.append(MODE_BEGIN + "HOT deploy" + MODE_END);
145 } else if (SmartDeployUtil.isWarmdeployMode(container)) {
146 sb.append(MODE_BEGIN + "WARM deploy" + MODE_END);
147 } else if (SmartDeployUtil.isCooldeployMode(container)) {
148 sb.append(MODE_BEGIN + "COOL deploy" + MODE_END);
149 } else {
150 sb.append("normal");
151 }
152 } finally {
153 sb.append(" mode.</p>");
154 }
155 return sb;
156 }
157
158 private static StringBuffer listInclude(final S2Container container, final String path) {
159 StringBuffer sb = new StringBuffer();
160 if (container.getChildSize() == 0) {
161 return sb;
162 }
163 sb.append("<h2>Includes</h2>");
164 sb.append("<p><ul>");
165 try {
166 for (int i = 0; i < container.getChildSize(); ++i) {
167 final S2Container child = container.getChild(i);
168 final String childPath = child.getPath();
169 sb.append("<li><h2 style='list-style-type: circle'><code>" + childPath + "</code></h2></li>");
170
171 sb.append(listComponent(child));
172 }
173 } finally {
174 sb.append("</ul></p>");
175 }
176 return sb;
177 }
178
179 private static StringBuffer listComponent(final S2Container container) {
180 StringBuffer sb = new StringBuffer();
181 if (container.getComponentDefSize() == 0) {
182 return sb;
183 }
184 sb.append("<h2>Components</h2>");
185 sb.append("<p><ul>");
186 try {
187 for (int i = 0; i < container.getComponentDefSize(); ++i) {
188 final ComponentDef cd = container.getComponentDef(i);
189 sb.append(printComponent(cd));
190 }
191 } finally {
192 sb.append("</ul></p>");
193 }
194 return sb;
195 }
196
197 private static StringBuffer printComponent(final ComponentDef cd) {
198 StringBuffer sb = new StringBuffer();
199 final String name = cd.getComponentName();
200 final Class<?> clazz = cd.getComponentClass();
201 sb.append("<li style='list-style-type: square'><code><strong>"
202 + (name != null ? escape(name) : "-") + " ["
203 + (clazz != null ? escape(clazz.getName()) : "-")
204 + "]</strong></code>");
205 sb.append("<ul>");
206 sb.append("<li style='list-style-type: circle'>instance : <code>"
207 + escape(cd.getInstanceDef().getName()) + "</code></li>");
208 sb.append("<li style='list-style-type: circle'>autoBinding : <code>"
209 + escape(cd.getAutoBindingDef().getName()) + "</code></li>");
210
211 Expression expression = cd.getExpression();
212 final String expr = (expression != null) ? expression.toString() : "";
213 if (!StringUtil.isEmpty(expr)) {
214 sb.append("<li style='list-style-type: circle'>ognl : <code>"
215 + escape(expr) + "</code></li>");
216 }
217
218 sb.append(printArg(cd));
219 sb.append(printAspect(cd));
220 sb.append(printProperty(cd));
221 sb.append(printInitMethod(cd));
222 sb.append(printDestroyMethod(cd));
223
224 try {
225 final Object component = cd.getComponent();
226 sb.append("<li style='list-style-type: circle'>toString : <pre style='border-style: solid; border-width: 1'>"
227 + escape(component.toString()) + "</pre></li>");
228 } catch (final Exception ignore) {
229 }
230 sb.append("</ul>");
231 return sb;
232 }
233
234 private static StringBuffer printArg(final ArgDefAware cd) {
235 StringBuffer sb = new StringBuffer();
236 for (int i = 0; i < cd.getArgDefSize(); ++i) {
237 sb.append("<li style='list-style-type: circle'>arg<ul>");
238 final ArgDef ad = cd.getArgDef(i);
239
240 Expression expression = ad.getExpression();
241 final String expr = (expression != null) ? expression.toString() : "";
242 if (!StringUtil.isEmpty(expr)) {
243 sb.append("<li style='list-style-type: circle'>ognl : <code>"
244 + escape(expr) + "</code></li>");
245 }
246
247 final ComponentDef child = getChildComponentDef(ad);
248 if (child != null) {
249 sb.append(printComponent(child));
250 }
251
252 sb.append("</ul></li>");
253 }
254 return sb;
255 }
256
257 private static StringBuffer printAspect(final ComponentDef cd) {
258 StringBuffer sb = new StringBuffer();
259 for (int i = 0; i < cd.getAspectDefSize(); ++i) {
260 sb.append("<li style='list-style-type: circle'>aspect<ul>");
261 final AspectDef ad = cd.getAspectDef(i);
262 final PointcutImpl pc = (PointcutImpl) ad.getPointcut();
263 if (pc != null) {
264 final String[] pointCuts = pc.getMethodNames();
265 if (pointCuts != null && pointCuts.length > 0) {
266 sb.append("<li style='list-style-type: circle'>pointcut<ul>");
267 for (int j = 0; j < pointCuts.length; ++j) {
268 sb.append("<li style='list-style-type: circle'><code>"
269 + escape(pointCuts[j]) + "</code></li>");
270 }
271 sb.append("</ul></li>");
272 }
273 }
274
275 Expression expression = ad.getExpression();
276 final String expr = (expression != null) ? expression.toString() : "";
277 if (!StringUtil.isEmpty(expr)) {
278 sb.append("<li style='list-style-type: circle'>ognl : <code>"
279 + escape(expr) + "</code></li>");
280 }
281
282 final ComponentDef child = getChildComponentDef(ad);
283 if (child != null) {
284 sb.append(printComponent(child));
285 }
286
287 sb.append("</ul></li>");
288 }
289 return sb;
290 }
291
292 private static StringBuffer printProperty(final ComponentDef cd) {
293 StringBuffer sb = new StringBuffer();
294 for (int i = 0; i < cd.getPropertyDefSize(); ++i) {
295 sb.append("<li style='list-style-type: circle'>property<ul>");
296 final PropertyDef pd = cd.getPropertyDef(i);
297 sb.append("<li style='list-style-type: circle'>name : <code>"
298 + escape(pd.getPropertyName()) + "</code></li>");
299
300 Expression expression = pd.getExpression();
301 final String expr = (expression != null) ? expression.toString() : "";
302 if (!StringUtil.isEmpty(expr)) {
303 sb.append("<li style='list-style-type: circle'>ognl : <code>"
304 + escape(expr) + "</code></li>");
305 }
306
307 final ComponentDef child = getChildComponentDef(pd);
308 if (child != null) {
309 sb.append(printComponent(child));
310 }
311
312 sb.append("</ul></li>");
313 }
314 return sb;
315 }
316
317 private static StringBuffer printInitMethod(final ComponentDef cd) {
318 StringBuffer sb = new StringBuffer();
319 for (int i = 0; i < cd.getInitMethodDefSize(); ++i) {
320 sb.append("<li style='list-style-type: circle'>initMethod<ul>");
321 sb.append(printMethod(cd.getInitMethodDef(i)));
322 sb.append("</ul></li>");
323 }
324 return sb;
325 }
326
327 private static StringBuffer printDestroyMethod(final ComponentDef cd) {
328 StringBuffer sb = new StringBuffer();
329 for (int i = 0; i < cd.getDestroyMethodDefSize(); ++i) {
330 sb.append("<li style='list-style-type: circle'>destroyMethod<ul>");
331 sb.append(printMethod(cd.getDestroyMethodDef(i)));
332 sb.append("</ul></li>");
333 }
334 return sb;
335 }
336
337 private static StringBuffer printMethod(final MethodDef md) {
338 StringBuffer sb = new StringBuffer();
339 sb.append("<li style='list-style-type: circle'>name : <code>"
340 + escape(md.getMethodName()) + "</code></li>");
341
342 Expression expression = md.getExpression();
343 final String expr = (expression != null) ? expression.toString() : "";
344 if (!StringUtil.isEmpty(expr)) {
345 sb.append("<li style='list-style-type: circle'>ognl : <code>"
346 + escape(expr) + "</code></li>");
347 }
348
349 final ComponentDef child = getChildComponentDef(md);
350 if (child != null) {
351 sb.append(printComponent(child));
352 }
353 return sb;
354 }
355
356 private static ComponentDef getChildComponentDef(final Object o) {
357 try {
358 final Field f = ArgDefImpl.class
359 .getDeclaredField("childComponentDef");
360 f.setAccessible(true);
361 return (ComponentDef) f.get(o);
362 } catch (final Exception e) {
363 return null;
364 }
365 }
366
367 private static String escape(final String text) {
368 if (text == null) {
369 return "null";
370 }
371 final StringBuffer buf = new StringBuffer(text.length() * 4);
372 for (int i = 0; i < text.length(); ++i) {
373 final char ch = text.charAt(i);
374 switch (ch) {
375 case '<':
376 buf.append("<");
377 break;
378 case '>':
379 buf.append(">");
380 break;
381 case '&':
382 buf.append("&");
383 break;
384 case '"':
385 buf.append(""");
386 break;
387 default:
388 buf.append(ch);
389 break;
390 }
391 }
392 return new String(buf);
393 }
394 }