Home » geronimo-2.2-source-release » org.apache.geronimo.console.jndiview » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.console.jndiview;
   18   
   19   import java.util.ArrayList;
   20   import java.util.Collections;
   21   import java.util.HashMap;
   22   import java.util.Hashtable;
   23   import java.util.Iterator;
   24   import java.util.List;
   25   import java.util.Map;
   26   import java.util.Set;
   27   
   28   import javax.naming.Context;
   29   import javax.naming.InitialContext;
   30   import javax.naming.NameClassPair;
   31   import javax.naming.NamingEnumeration;
   32   import javax.naming.NamingException;
   33   
   34   import org.apache.geronimo.console.util.Tree;
   35   import org.apache.geronimo.console.util.TreeEntry;
   36   import org.apache.geronimo.gbean.AbstractName;
   37   import org.apache.geronimo.kernel.Kernel;
   38   import org.apache.geronimo.naming.java.RootContext;
   39   import org.directwebremoting.annotations.RemoteMethod;
   40   import org.directwebremoting.annotations.RemoteProxy;
   41   import org.slf4j.Logger;
   42   import org.slf4j.LoggerFactory;
   43   
   44   @RemoteProxy
   45   public class JNDIViewHelper {
   46   
   47       private static final Logger log = LoggerFactory.getLogger(JNDIViewHelper.class);
   48       
   49       private static final String NO_CHILD = "none";
   50       
   51       private static final String NOT_LEAF_TYPE = "not_leaf";
   52       
   53       private static final String NORMAL_TYPE = "normal";
   54   
   55       private void buildEJBModule(Kernel kernel, List arryList, Hashtable entApp) throws Exception {
   56           Map query = new HashMap();
   57           query.put("j2eeType", "EJBModule");
   58           Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, query));
   59           Iterator iterator = setEnt.iterator();
   60   
   61           while (iterator.hasNext()) {
   62               AbstractName gb = (AbstractName) iterator.next();
   63               TreeEntry ejbModule = new TreeEntry(gb.getNameProperty("name"), NORMAL_TYPE);
   64   
   65               if (gb.getNameProperty("J2EEApplication") == null || gb.getNameProperty("J2EEApplication").equals("null")) {
   66                   TreeEntry treeEnt = (TreeEntry) entApp.get("EJBModule");
   67                   treeEnt.addChild(ejbModule);
   68               } else {
   69                   TreeEntry treeEnt = (TreeEntry) entApp.get(gb.getNameProperty("J2EEApplication"));
   70                   treeEnt = treeEnt.findEntry("EJBModule");
   71                   treeEnt.addChild(ejbModule);
   72               }
   73               Map queryEnt = new HashMap();
   74               TreeEntry entityBean = new TreeEntry("EntityBeans", NOT_LEAF_TYPE);
   75               ejbModule.addChild(entityBean);
   76               queryEnt.put("j2eeType", "EntityBean");
   77               queryEnt.put("EJBModule", gb.getNameProperty("name"));
   78               queryEnt.put("J2EEApplication", gb.getNameProperty("J2EEApplication"));
   79               Set setEntBean = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, queryEnt));
   80   
   81               Iterator iterEntBean = setEntBean.iterator();
   82   
   83               while (iterEntBean.hasNext()) {
   84                   AbstractName gbEntBean = (AbstractName) iterEntBean.next();
   85                   TreeEntry beanNode = new TreeEntry(gbEntBean.getNameProperty("name"), NORMAL_TYPE);
   86                   entityBean.addChild(beanNode);
   87                   Context jndi = (Context) kernel.getAttribute(gbEntBean, "componentContext");
   88                   buildContext(beanNode, jndi, "java:comp");
   89               }
   90   
   91               queryEnt = new HashMap();
   92               TreeEntry sessionBean = new TreeEntry("SessionBeans", NOT_LEAF_TYPE);
   93               ejbModule.addChild(sessionBean);
   94               queryEnt.put("j2eeType", "StatelessSessionBean");
   95               queryEnt.put("EJBModule", gb.getNameProperty("name"));
   96               queryEnt.put("J2EEApplication", gb.getNameProperty("J2EEApplication"));
   97               Set setSessionBean = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, queryEnt));
   98   
   99               Iterator iterSessionBean = setSessionBean.iterator();
  100   
  101               while (iterSessionBean.hasNext()) {
  102                   AbstractName gbSessionBean = (AbstractName) iterSessionBean.next();
  103                   TreeEntry beanNode = new TreeEntry(gbSessionBean.getNameProperty("name"), NORMAL_TYPE);
  104                   sessionBean.addChild(beanNode);
  105                   Context jndi = (Context) kernel.getAttribute(gbSessionBean, "componentContext");
  106                   buildContext(beanNode, jndi, "java:comp");
  107               }
  108   
  109               queryEnt = new HashMap();
  110               queryEnt.put("j2eeType", "StatefullSessionBean");
  111               queryEnt.put("EJBModule", gb.getNameProperty("name"));
  112               queryEnt.put("J2EEApplication", gb.getNameProperty("J2EEApplication"));
  113               setSessionBean = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, queryEnt));
  114   
  115               iterSessionBean = setSessionBean.iterator();
  116   
  117               while (iterSessionBean.hasNext()) {
  118                   AbstractName gbSessionBean = (AbstractName) iterSessionBean.next();
  119                   TreeEntry beanNode = new TreeEntry(gbSessionBean.getNameProperty("name"),NORMAL_TYPE);
  120                   sessionBean.addChild(beanNode);
  121                   Context jndi = (Context) kernel.getAttribute(gbSessionBean, "componentContext");
  122                   buildContext(beanNode, jndi, "java:comp");
  123               }
  124           }
  125       }
  126   
  127       private void buildWebModule(Kernel kernel, List arryList, Hashtable entApp) throws Exception {
  128           Map query = new HashMap();
  129           query.put("j2eeType", "WebModule");
  130           Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, query));
  131           Iterator iterator = setEnt.iterator();
  132   
  133           while (iterator.hasNext()) {
  134   
  135               AbstractName gb = (AbstractName) iterator.next();
  136               TreeEntry webModule = new TreeEntry(gb.getNameProperty("name"), NORMAL_TYPE);
  137   
  138               if (gb.getNameProperty("J2EEApplication") == null || gb.getNameProperty("J2EEApplication").equals("null")) {
  139                   TreeEntry treeEnt = (TreeEntry) entApp.get("WebModule");
  140                   treeEnt.addChild(webModule);
  141               } else {
  142                   TreeEntry treeEnt = (TreeEntry) entApp.get(gb.getNameProperty("J2EEApplication"));
  143                   treeEnt = treeEnt.findEntry("WebModule");
  144                   treeEnt.addChild(webModule);
  145               }
  146   
  147               Map map = (Map) kernel.getAttribute(gb, "componentContext");
  148               String[] servlets = (String[]) kernel.getAttribute(gb, "servlets");
  149   
  150               TreeEntry servletsNode = null;
  151               TreeEntry jspNode = null;
  152   
  153               for (int i = 0; i < servlets.length; i++) {
  154                   String servlet = servlets[i];
  155                   servlet = servlet.substring(servlet.indexOf("name=") + 5);
  156                   if (servlet.indexOf(",") != -1)
  157                       servlet = servlet.substring(0, servlet.indexOf(","));
  158                   if (!servlet.equals("jsp") && servlet.startsWith("jsp.")) {
  159                       if (servletsNode == null) {
  160                           servletsNode = new TreeEntry("Servlets", NOT_LEAF_TYPE);
  161                           webModule.addChild(servletsNode);
  162                       }
  163                       if (jspNode == null) {
  164                           jspNode = new TreeEntry("JSP", NOT_LEAF_TYPE);
  165                           servletsNode.addChild(jspNode);
  166                       }
  167                       jspNode.addChild(new TreeEntry(servlet.substring(4), NORMAL_TYPE));
  168                   } else if (!servlet.equals("jsp")) {
  169                       if (servletsNode == null) {
  170                           servletsNode = new TreeEntry("Servlets", NOT_LEAF_TYPE);
  171                           webModule.addChild(servletsNode);
  172                       }
  173                       servletsNode.addChild(new TreeEntry(servlet, NORMAL_TYPE));
  174                   }
  175               }
  176               Iterator contexts = map.keySet().iterator();
  177               while (contexts.hasNext())
  178                   webModule.addChild(new TreeEntry("java:comp/" + contexts.next(), NORMAL_TYPE));
  179           }
  180       }
  181   
  182       private void buildResourceModule(Kernel kernel, List arryList, Hashtable entApp) throws Exception {
  183           Map query = new HashMap();
  184           query.put("j2eeType", "ResourceAdapterModule");
  185           Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, query));
  186           Iterator iterator = setEnt.iterator();
  187   
  188           while (iterator.hasNext()) {
  189               AbstractName gb = (AbstractName) iterator.next();
  190               String resourceModule = gb.getNameProperty("name");
  191               if (gb.getNameProperty("J2EEApplication") == null || gb.getNameProperty("J2EEApplication").equals("null")) {
  192                   TreeEntry treeEnt = (TreeEntry) entApp.get("ResourceAdapterModule");
  193                   treeEnt.addChild(new TreeEntry(resourceModule, NORMAL_TYPE));
  194               } else {
  195                   TreeEntry treeEnt = (TreeEntry) entApp.get(gb.getNameProperty("J2EEApplication"));
  196                   treeEnt = treeEnt.findEntry("ResourceAdapterModule");
  197                   treeEnt.addChild(new TreeEntry(resourceModule, NORMAL_TYPE));
  198               }
  199           }
  200       }
  201   
  202       private void buildAppClientModule(Kernel kernel, List arryList, Hashtable entApp) throws Exception {
  203           Map query = new HashMap();
  204           query.put("j2eeType", "AppClientModule");
  205           Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, query));
  206           Iterator iterator = setEnt.iterator();
  207   
  208           while (iterator.hasNext()) {
  209               AbstractName gb = (AbstractName) iterator.next();
  210               String appClienteModule = gb.getNameProperty("name");
  211               if (gb.getNameProperty("J2EEApplication") == null || gb.getNameProperty("J2EEApplication").equals("null")) {
  212                   TreeEntry treeEnt = (TreeEntry) entApp.get("AppClientModule");
  213                   treeEnt.addChild(new TreeEntry(appClienteModule, NORMAL_TYPE));
  214               } else {
  215                   TreeEntry treeEnt = (TreeEntry) entApp.get(gb.getNameProperty("J2EEApplication"));
  216                   treeEnt = treeEnt.findEntry("AppClientModule");
  217                   treeEnt.addChild(new TreeEntry(appClienteModule, NORMAL_TYPE));
  218               }
  219           }
  220       }
  221   
  222       public void buildContext(TreeEntry node, Context compCtx, String nodeCurr) {
  223           Context oldCtx = RootContext.getComponentContext();
  224           RootContext.setComponentContext(compCtx);
  225           try {
  226               InitialContext ctx = new InitialContext();
  227               buildContextSub(node, (Context) ctx.lookup("java:comp"), nodeCurr);
  228           } catch (Exception e) {
  229               log.warn("Error looking up java:comp context", e);
  230           } finally {
  231               RootContext.setComponentContext(oldCtx);
  232           }
  233       }
  234   
  235       private void buildContextSub(TreeEntry node, Context ctx, String nodeCurr) {
  236           try {
  237               NamingEnumeration enumName = ctx.list("");
  238               while (enumName.hasMoreElements()) {
  239                   NameClassPair pair = (NameClassPair) enumName.next();
  240                   Object obj = null;
  241                   try {
  242                       obj = ctx.lookup(pair.getName());
  243                   } catch (NamingException e) {
  244                       // ignore.... not a context
  245                   }
  246                   if (obj instanceof Context) {
  247                       buildContextSub(node, (Context) obj, nodeCurr + "/" + pair.getName());
  248                   } else {
  249                       node.addChild(new TreeEntry(nodeCurr + "/" + pair.getName(), NORMAL_TYPE));
  250                   }
  251               }
  252           } catch (Exception e) {
  253               log.warn("Error listing context", e);
  254           }
  255       }
  256   
  257       private void buildGlobal(TreeEntry tree, Context context, String parent) throws Exception {
  258           if (parent == null)
  259               parent = "";
  260           if (!parent.equals(""))
  261               parent = parent + "/";
  262           javax.naming.NamingEnumeration enum1 = context.list("");
  263           while (enum1.hasMoreElements()) {
  264               javax.naming.NameClassPair pair = (javax.naming.NameClassPair) enum1.next();
  265               Object obj = null;
  266               try {
  267                   obj = context.lookup(pair.getName());
  268               } catch (NamingException e) {
  269                   // ignore.... it wasn't a context
  270               }
  271               if (obj instanceof Context) {
  272                   buildGlobal(tree, (Context) obj, parent + pair.getName());
  273               } else {
  274                   tree.addChild(new TreeEntry(parent + pair.getName(), NORMAL_TYPE));
  275               }
  276           }
  277       }
  278   
  279       @RemoteMethod
  280       public Tree getTrees() throws Exception {
  281           Tree jndiTree = new Tree(null, "name");
  282   
  283           List arryList = new ArrayList();
  284           Hashtable entApp = new Hashtable();
  285   
  286           TreeEntry treeGlobal = new TreeEntry("Global Context", NOT_LEAF_TYPE);
  287           jndiTree.addItem(treeGlobal);
  288           buildGlobal(treeGlobal, org.apache.xbean.naming.global.GlobalContextManager.getGlobalContext(), "");
  289   
  290           TreeEntry tree = new TreeEntry("Enterprise Applications", NOT_LEAF_TYPE);
  291           jndiTree.addItem(tree);
  292   
  293           TreeEntry treeMod = new TreeEntry("EJBModule", NOT_LEAF_TYPE);
  294           entApp.put("EJBModule", treeMod);
  295           jndiTree.addItem(treeMod);
  296   
  297           treeMod = new TreeEntry("WebModule", NOT_LEAF_TYPE);
  298           entApp.put("WebModule", treeMod);
  299           jndiTree.addItem(treeMod);
  300   
  301           treeMod = new TreeEntry("ResourceAdapterModule", NOT_LEAF_TYPE);
  302           entApp.put("ResourceAdapterModule", treeMod);
  303           jndiTree.addItem(treeMod);
  304   
  305           treeMod = new TreeEntry("AppClientModule", NOT_LEAF_TYPE);
  306           entApp.put("AppClientModule", treeMod);
  307           jndiTree.addItem(treeMod);
  308   
  309           org.apache.geronimo.kernel.Kernel kernel = org.apache.geronimo.kernel.KernelRegistry.getSingleKernel();
  310   
  311           Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, Collections.EMPTY_MAP,
  312                   org.apache.geronimo.j2ee.management.impl.J2EEApplicationImpl.class.getName()));
  313           Iterator iterator = setEnt.iterator();
  314           while (iterator.hasNext()) {
  315               org.apache.geronimo.gbean.AbstractName gb = (org.apache.geronimo.gbean.AbstractName) iterator.next();
  316               TreeEntry curr = new TreeEntry(gb.getNameProperty("name"), NORMAL_TYPE);
  317               tree.addChild(curr);
  318               entApp.put(gb.getNameProperty("name"), curr);
  319   
  320               TreeEntry temp = new TreeEntry("EJBModule", NOT_LEAF_TYPE);
  321               curr.addChild(temp);
  322   
  323               temp = new TreeEntry("WebModule", NOT_LEAF_TYPE);
  324               curr.addChild(temp);
  325   
  326               temp = new TreeEntry("ResourceAdapterModule", NOT_LEAF_TYPE);
  327               curr.addChild(temp);
  328   
  329               temp = new TreeEntry("AppClientModule", NOT_LEAF_TYPE);
  330               curr.addChild(temp);
  331           }
  332   
  333           buildEJBModule(kernel, jndiTree.getItems(), entApp);
  334           buildWebModule(kernel, jndiTree.getItems(), entApp);
  335           buildResourceModule(kernel, jndiTree.getItems(), entApp);
  336           buildAppClientModule(kernel, jndiTree.getItems(), entApp);
  337   
  338           check_no_child(jndiTree.getItems());
  339           return jndiTree;
  340       }
  341   
  342       private static void check_no_child(List<TreeEntry> list) {
  343           List<TreeEntry> children;
  344           for (TreeEntry entry : list) {
  345               children = entry.getChildren();
  346               if (children.size() > 0)
  347                   check_no_child(children);
  348               else if (entry.getType().equals(NOT_LEAF_TYPE))
  349                   children.add(new TreeEntry(NO_CHILD, NORMAL_TYPE));
  350           }
  351       }
  352   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.jndiview » [javadoc | source]