Coverage Report - org.seasar.uruma.util.S2ContainerListToHtmlUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
S2ContainerListToHtmlUtil
0%
0/183
0%
0/81
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 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  
  * S2ContainerにレジストされているオブジェクトをHTMLに出力するユーティリティです。<br />
 39  
 
 40  
  * @author y.sugigami
 41  
  */
 42  0
 public class S2ContainerListToHtmlUtil {
 43  
 
 44  
     /**
 45  
      * 初期化パラメータの設定パスのキーです。
 46  
      */
 47  
     public static final String CONFIG_PATH_KEY = "configPath";
 48  
 
 49  
     /**a
 50  
      * 初期化パラメータのデバッグのキーです。
 51  
      */
 52  
     public static final String DEBUG_KEY = "debug";
 53  
 
 54  
     /**
 55  
      * queryStringのコマンドのキーです。
 56  
      */
 57  
     public static final String COMMAND = "command";
 58  
 
 59  
     /**
 60  
      * queryStringの再起動のキーです。
 61  
      */
 62  
     public static final String RESTART = "restart";
 63  
 
 64  
     /**
 65  
      * queryStringの一覧のキーです。
 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  
      * {@link S2Container}の中身({@link ComponentDef})を表示します。
 80  
      * 
 81  
      * @param request
 82  
      * @param response
 83  
      * @throws IOException
 84  
      */
 85  
     public static String list(final S2Container container, String path) {
 86  0
             StringBuffer sb = new StringBuffer();
 87  0
         if (container == null) {
 88  0
                 sb.append("S2Container[" + escape(path) + "] is not found.");
 89  0
             return sb.toString();
 90  
         }
 91  0
         sb.append("<html><head><title>Seasar2 Component List</title></head><body>");
 92  
         try {
 93  0
                 sb.append("<h1>S2Container</h1>");
 94  0
                 sb.append(printSmartDeploy(container));
 95  0
             sb.append("<ul>");
 96  
             try {
 97  0
                     sb.append("<li>path : <code>" + escape(container.getPath()) + "</code></li>");
 98  0
                 final String nameSpace = container.getNamespace();
 99  0
                 if (!StringUtil.isEmpty(nameSpace)) {
 100  0
                         sb.append("<li>namespace : <code>" + escape(nameSpace) + "</code></li>");
 101  
                 }
 102  0
                 final String envValue = Env.getValue();
 103  0
                 if (!StringUtil.isEmpty(envValue)) {
 104  0
                         sb.append("<li>env : <code>" + escape(envValue) + "</code></li>");
 105  
                 }
 106  0
             } finally {
 107  0
                     sb.append("</ul>");
 108  0
             }
 109  0
             sb.append(listComponent(container));
 110  
             
 111  0
             sb.append(listInclude(container, path));
 112  
             
 113  0
         } finally {
 114  0
                 sb.append("</body></html>");
 115  0
         }
 116  0
         return sb.toString();
 117  
     }
 118  
     
 119  
     public static String list(final String path) {
 120  0
         final S2Container container = getContainer(path);
 121  0
         return list(container, path);
 122  
     }
 123  
 
 124  
     /**
 125  
      * {@link S2Container}を返します。
 126  
      * 
 127  
      * @param path
 128  
      * @return {@link S2Container}
 129  
      */
 130  
     protected static S2Container getContainer(final String path) {
 131  0
         final S2Container root = SingletonS2ContainerFactory.getContainer();
 132  
         try {
 133  0
             return StringUtil.isEmpty(path) ? root : root.getDescendant(path);
 134  0
         } catch (final ContainerNotRegisteredRuntimeException e) {
 135  0
             return null;
 136  
         }
 137  
     }
 138  
 
 139  
     private static StringBuffer printSmartDeploy(final S2Container container) {
 140  0
             StringBuffer sb = new StringBuffer();
 141  0
             sb.append("<p>S2Container is working under ");
 142  
         try {
 143  0
             if (SmartDeployUtil.isHotdeployMode(container)) {
 144  0
                     sb.append(MODE_BEGIN + "HOT deploy" + MODE_END);
 145  0
             } else if (SmartDeployUtil.isWarmdeployMode(container)) {
 146  0
                 sb.append(MODE_BEGIN + "WARM deploy" + MODE_END);
 147  0
             } else if (SmartDeployUtil.isCooldeployMode(container)) {
 148  0
                 sb.append(MODE_BEGIN + "COOL deploy" + MODE_END);
 149  
             } else {
 150  0
                 sb.append("normal");
 151  
             }
 152  0
         } finally {
 153  0
             sb.append(" mode.</p>");
 154  0
         }
 155  0
         return sb;
 156  
     }
 157  
 
 158  
     private static StringBuffer listInclude(final S2Container container, final String path) {
 159  0
             StringBuffer sb = new StringBuffer();
 160  0
         if (container.getChildSize() == 0) {
 161  0
             return sb;
 162  
         }
 163  0
         sb.append("<h2>Includes</h2>");
 164  0
         sb.append("<p><ul>");
 165  
         try {
 166  0
             for (int i = 0; i < container.getChildSize(); ++i) {
 167  0
                 final S2Container child = container.getChild(i);
 168  0
                 final String childPath = child.getPath();
 169  0
                 sb.append("<li><h2 style='list-style-type: circle'><code>" + childPath + "</code></h2></li>");
 170  
                 
 171  0
                 sb.append(listComponent(child));
 172  
             }
 173  0
         } finally {
 174  0
             sb.append("</ul></p>");
 175  0
         }
 176  0
         return sb;
 177  
     }
 178  
 
 179  
     private static StringBuffer listComponent(final S2Container container) {
 180  0
             StringBuffer sb = new StringBuffer();
 181  0
         if (container.getComponentDefSize() == 0) {
 182  0
             return sb;
 183  
         }
 184  0
         sb.append("<h2>Components</h2>");
 185  0
         sb.append("<p><ul>");
 186  
         try {
 187  0
             for (int i = 0; i < container.getComponentDefSize(); ++i) {
 188  0
                 final ComponentDef cd = container.getComponentDef(i);
 189  0
                 sb.append(printComponent(cd));
 190  
             }
 191  0
         } finally {
 192  0
             sb.append("</ul></p>");
 193  0
         }
 194  0
         return sb;
 195  
     }
 196  
 
 197  
     private static StringBuffer printComponent(final ComponentDef cd) {
 198  0
             StringBuffer sb = new StringBuffer();
 199  0
         final String name = cd.getComponentName();
 200  0
         final Class<?> clazz = cd.getComponentClass();
 201  0
         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  0
         sb.append("<ul>");
 206  0
         sb.append("<li style='list-style-type: circle'>instance : <code>"
 207  
                 + escape(cd.getInstanceDef().getName()) + "</code></li>");
 208  0
         sb.append("<li style='list-style-type: circle'>autoBinding : <code>"
 209  
                 + escape(cd.getAutoBindingDef().getName()) + "</code></li>");
 210  
 
 211  0
         Expression expression = cd.getExpression();
 212  0
         final String expr = (expression != null) ? expression.toString() : "";
 213  0
         if (!StringUtil.isEmpty(expr)) {
 214  0
             sb.append("<li style='list-style-type: circle'>ognl : <code>"
 215  
                     + escape(expr) + "</code></li>");
 216  
         }
 217  
 
 218  0
         sb.append(printArg(cd));
 219  0
         sb.append(printAspect(cd));
 220  0
         sb.append(printProperty(cd));
 221  0
         sb.append(printInitMethod(cd));
 222  0
         sb.append(printDestroyMethod(cd));
 223  
 
 224  
         try {
 225  0
             final Object component = cd.getComponent();
 226  0
             sb.append("<li style='list-style-type: circle'>toString : <pre style='border-style: solid; border-width: 1'>"
 227  
                             + escape(component.toString()) + "</pre></li>");
 228  0
         } catch (final Exception ignore) {
 229  0
         }
 230  0
         sb.append("</ul>");
 231  0
         return sb;
 232  
     }
 233  
 
 234  
     private static StringBuffer printArg(final ArgDefAware cd) {
 235  0
             StringBuffer sb = new StringBuffer();
 236  0
         for (int i = 0; i < cd.getArgDefSize(); ++i) {
 237  0
             sb.append("<li style='list-style-type: circle'>arg<ul>");
 238  0
             final ArgDef ad = cd.getArgDef(i);
 239  
 
 240  0
             Expression expression = ad.getExpression();
 241  0
             final String expr = (expression != null) ? expression.toString() : "";
 242  0
             if (!StringUtil.isEmpty(expr)) {
 243  0
                 sb.append("<li style='list-style-type: circle'>ognl : <code>"
 244  
                         + escape(expr) + "</code></li>");
 245  
             }
 246  
 
 247  0
             final ComponentDef child = getChildComponentDef(ad);
 248  0
             if (child != null) {
 249  0
                     sb.append(printComponent(child));
 250  
             }
 251  
 
 252  0
             sb.append("</ul></li>");
 253  
         }
 254  0
         return sb;
 255  
     }
 256  
 
 257  
     private static StringBuffer printAspect(final ComponentDef cd) {
 258  0
             StringBuffer sb = new StringBuffer();
 259  0
         for (int i = 0; i < cd.getAspectDefSize(); ++i) {
 260  0
             sb.append("<li style='list-style-type: circle'>aspect<ul>");
 261  0
             final AspectDef ad = cd.getAspectDef(i);
 262  0
             final PointcutImpl pc = (PointcutImpl) ad.getPointcut();
 263  0
             if (pc != null) {
 264  0
                 final String[] pointCuts = pc.getMethodNames();
 265  0
                 if (pointCuts != null && pointCuts.length > 0) {
 266  0
                         sb.append("<li style='list-style-type: circle'>pointcut<ul>");
 267  0
                     for (int j = 0; j < pointCuts.length; ++j) {
 268  0
                         sb.append("<li style='list-style-type: circle'><code>"
 269  
                                 + escape(pointCuts[j]) + "</code></li>");
 270  
                     }
 271  0
                     sb.append("</ul></li>");
 272  
                 }
 273  
             }
 274  
 
 275  0
             Expression expression = ad.getExpression();
 276  0
             final String expr = (expression != null) ? expression.toString() : "";
 277  0
             if (!StringUtil.isEmpty(expr)) {
 278  0
                 sb.append("<li style='list-style-type: circle'>ognl : <code>"
 279  
                         + escape(expr) + "</code></li>");
 280  
             }
 281  
 
 282  0
             final ComponentDef child = getChildComponentDef(ad);
 283  0
             if (child != null) {
 284  0
                     sb.append(printComponent(child));
 285  
             }
 286  
 
 287  0
             sb.append("</ul></li>");
 288  
         }
 289  0
         return sb;
 290  
     }
 291  
 
 292  
     private static StringBuffer printProperty(final ComponentDef cd) {
 293  0
             StringBuffer sb = new StringBuffer();
 294  0
         for (int i = 0; i < cd.getPropertyDefSize(); ++i) {
 295  0
             sb.append("<li style='list-style-type: circle'>property<ul>");
 296  0
             final PropertyDef pd = cd.getPropertyDef(i);
 297  0
             sb.append("<li style='list-style-type: circle'>name : <code>"
 298  
                     + escape(pd.getPropertyName()) + "</code></li>");
 299  
 
 300  0
             Expression expression = pd.getExpression();
 301  0
             final String expr = (expression != null) ? expression.toString() : "";
 302  0
             if (!StringUtil.isEmpty(expr)) {
 303  0
                 sb.append("<li style='list-style-type: circle'>ognl : <code>"
 304  
                         + escape(expr) + "</code></li>");
 305  
             }
 306  
 
 307  0
             final ComponentDef child = getChildComponentDef(pd);
 308  0
             if (child != null) {
 309  0
                     sb.append(printComponent(child));
 310  
             }
 311  
 
 312  0
             sb.append("</ul></li>");
 313  
         }
 314  0
         return sb;
 315  
     }
 316  
 
 317  
     private static StringBuffer printInitMethod(final ComponentDef cd) {
 318  0
             StringBuffer sb = new StringBuffer();
 319  0
         for (int i = 0; i < cd.getInitMethodDefSize(); ++i) {
 320  0
             sb.append("<li style='list-style-type: circle'>initMethod<ul>");
 321  0
             sb.append(printMethod(cd.getInitMethodDef(i)));
 322  0
             sb.append("</ul></li>");
 323  
         }
 324  0
         return sb;
 325  
     }
 326  
 
 327  
     private static StringBuffer printDestroyMethod(final ComponentDef cd) {
 328  0
             StringBuffer sb = new StringBuffer();
 329  0
         for (int i = 0; i < cd.getDestroyMethodDefSize(); ++i) {
 330  0
             sb.append("<li style='list-style-type: circle'>destroyMethod<ul>");
 331  0
             sb.append(printMethod(cd.getDestroyMethodDef(i)));
 332  0
             sb.append("</ul></li>");
 333  
         }
 334  0
         return sb;
 335  
     }
 336  
 
 337  
     private static StringBuffer printMethod(final MethodDef md) {
 338  0
             StringBuffer sb = new StringBuffer();
 339  0
         sb.append("<li style='list-style-type: circle'>name : <code>"
 340  
                 + escape(md.getMethodName()) + "</code></li>");
 341  
 
 342  0
         Expression expression = md.getExpression();
 343  0
         final String expr = (expression != null) ? expression.toString() : "";
 344  0
         if (!StringUtil.isEmpty(expr)) {
 345  0
             sb.append("<li style='list-style-type: circle'>ognl : <code>"
 346  
                     + escape(expr) + "</code></li>");
 347  
         }
 348  
 
 349  0
         final ComponentDef child = getChildComponentDef(md);
 350  0
         if (child != null) {
 351  0
                 sb.append(printComponent(child));
 352  
         }
 353  0
         return sb;
 354  
     }
 355  
 
 356  
     private static ComponentDef getChildComponentDef(final Object o) {
 357  
         try {
 358  0
             final Field f = ArgDefImpl.class
 359  
                     .getDeclaredField("childComponentDef");
 360  0
             f.setAccessible(true);
 361  0
             return (ComponentDef) f.get(o);
 362  0
         } catch (final Exception e) {
 363  0
             return null;
 364  
         }
 365  
     }
 366  
 
 367  
     private static String escape(final String text) {
 368  0
         if (text == null) {
 369  0
             return "null";
 370  
         }
 371  0
         final StringBuffer buf = new StringBuffer(text.length() * 4);
 372  0
         for (int i = 0; i < text.length(); ++i) {
 373  0
             final char ch = text.charAt(i);
 374  0
             switch (ch) {
 375  
             case '<':
 376  0
                 buf.append("&lt;");
 377  0
                 break;
 378  
             case '>':
 379  0
                 buf.append("&gt;");
 380  0
                 break;
 381  
             case '&':
 382  0
                 buf.append("&amp;");
 383  0
                 break;
 384  
             case '"':
 385  0
                 buf.append("&quot;");
 386  0
                 break;
 387  
             default:
 388  0
                 buf.append(ch);
 389  
                 break;
 390  
             }
 391  
         }
 392  0
         return new String(buf);
 393  
     }
 394  
 }