001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.console.jndiview;
018    
019    import org.apache.geronimo.kernel.Kernel;
020    import org.apache.geronimo.naming.java.RootContext;
021    import org.apache.geronimo.gbean.AbstractName;
022    import javax.naming.Context;
023    import javax.naming.InitialContext;
024    import javax.naming.NameClassPair;
025    import javax.naming.NamingEnumeration;
026    
027    import java.io.IOException;
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.Hashtable;
031    import java.util.Map;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Set;
035    import java.util.Collections;
036    
037    import org.apache.geronimo.console.BasePortlet;
038    import org.apache.geronimo.console.util.StringTree;
039    import org.apache.commons.logging.Log;
040    import org.apache.commons.logging.LogFactory;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.PortletException;
046    import javax.portlet.PortletRequestDispatcher;
047    import javax.portlet.RenderRequest;
048    import javax.portlet.RenderResponse;
049    import javax.portlet.WindowState;
050    
051    public class JNDIViewPortlet extends BasePortlet {
052    
053        private static final Log log = LogFactory.getLog(JNDIViewPortlet.class);
054            
055        private static final String NORMALVIEW_JSP = "/WEB-INF/view/jndiview/view.jsp";
056    
057        private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/jndiview/view.jsp";
058    
059        private static final String HELPVIEW_JSP = "/WEB-INF/view/jndiview/help.jsp";
060    
061        private PortletRequestDispatcher normalView;
062    
063        private PortletRequestDispatcher maximizedView;
064    
065        private PortletRequestDispatcher helpView;
066      
067        public void processAction(ActionRequest actionRequest,
068                ActionResponse actionResponse) throws PortletException, IOException {
069        }
070    
071        protected void doView(RenderRequest renderRequest,
072                RenderResponse renderResponse) throws IOException, PortletException {
073            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
074                return;
075            }
076    
077            try {
078                renderRequest.getPortletSession().setAttribute("jndiTree",
079                        getJSONTrees());
080            } catch (Exception ex) {
081                throw new PortletException(ex);
082            }
083            if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
084                normalView.include(renderRequest, renderResponse);
085            } else {
086                maximizedView.include(renderRequest, renderResponse);
087            }
088        }
089    
090        protected void doHelp(RenderRequest renderRequest,
091                RenderResponse renderResponse) throws PortletException, IOException {
092            helpView.include(renderRequest, renderResponse);
093        }
094    
095        public void init(PortletConfig portletConfig) throws PortletException {
096            super.init(portletConfig);
097            normalView = portletConfig.getPortletContext().getRequestDispatcher(
098                    NORMALVIEW_JSP);
099            maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
100                    MAXIMIZEDVIEW_JSP);
101            helpView = portletConfig.getPortletContext().getRequestDispatcher(
102                    HELPVIEW_JSP);
103    
104        }
105    
106        public void destroy() {
107            normalView = null;
108            maximizedView = null;
109            helpView = null;
110            super.destroy();
111        }
112    
113        public String getJSONTrees() throws Exception {
114            List list = getContextTree();
115            if (list == null)
116                return "[]";
117    
118            StringBuffer stb = new StringBuffer();
119            stb.append("[");
120            for (int i = 0; i < list.size(); i++) {
121                StringTree node = (StringTree) list.get(i);
122                if (i != 0)
123                    stb.append(",");
124                stb.append(node.toJSONObject("" + i));
125            }
126            stb.append("]");
127            return stb.toString();
128        }
129    
130        private void buildEJBModule(Kernel kernel, List arryList, Hashtable entApp)
131                throws Exception {
132            Map query = new HashMap();
133            query.put("j2eeType", "EJBModule");
134            Set setEnt = kernel.listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(null, query));
135            Iterator iterator = setEnt.iterator();
136    
137            while (iterator.hasNext()) {
138                AbstractName gb = (AbstractName) iterator.next();
139                StringTree ejbModule = new StringTree(gb.getNameProperty("name"));
140    
141                if (gb.getNameProperty("J2EEApplication") == null
142                        || gb.getNameProperty("J2EEApplication").equals("null")) {
143                    StringTree treeEnt = (StringTree) entApp.get("EJBModule");
144                    treeEnt.addChild(ejbModule);
145                } else {
146                    StringTree treeEnt = (StringTree) entApp.get(gb
147                            .getNameProperty("J2EEApplication"));
148                    treeEnt = treeEnt.findNode("EJBModule");
149                    treeEnt.addChild(ejbModule);
150                }
151                Map queryEnt = new HashMap();
152                StringTree entityBean = new StringTree("EntityBeans");
153                ejbModule.addChild(entityBean);
154                queryEnt.put("j2eeType", "EntityBean");
155                queryEnt.put("EJBModule", gb.getNameProperty("name"));
156                queryEnt.put("J2EEApplication", gb
157                        .getNameProperty("J2EEApplication"));
158                Set setEntBean = kernel
159                        .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
160                                null, queryEnt));
161    
162                Iterator iterEntBean = setEntBean.iterator();
163    
164                while (iterEntBean.hasNext()) {
165                    AbstractName gbEntBean = (AbstractName) iterEntBean.next();
166                    StringTree beanNode = new StringTree(gbEntBean
167                            .getNameProperty("name"));
168                    entityBean.addChild(beanNode);
169                    Context jndi = (Context) kernel.getAttribute(gbEntBean,
170                            "componentContext");
171                    buildContext(beanNode, jndi, "java:comp");
172                }
173    
174                queryEnt = new HashMap();
175                StringTree sessionBean = new StringTree("SessionBeans");
176                ejbModule.addChild(sessionBean);
177                queryEnt.put("j2eeType", "StatelessSessionBean");
178                queryEnt.put("EJBModule", gb.getNameProperty("name"));
179                queryEnt.put("J2EEApplication", gb
180                        .getNameProperty("J2EEApplication"));
181                Set setSessionBean = kernel
182                        .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
183                                null, queryEnt));
184    
185                Iterator iterSessionBean = setSessionBean.iterator();
186    
187                while (iterSessionBean.hasNext()) {
188                    AbstractName gbSessionBean = (AbstractName) iterSessionBean
189                            .next();
190                    StringTree beanNode = new StringTree(gbSessionBean
191                            .getNameProperty("name"));
192                    sessionBean.addChild(beanNode);
193                    Context jndi = (Context) kernel.getAttribute(gbSessionBean,
194                            "componentContext");
195                    buildContext(beanNode, jndi, "java:comp");
196                }
197    
198                queryEnt = new HashMap();
199                queryEnt.put("j2eeType", "StatefullSessionBean");
200                queryEnt.put("EJBModule", gb.getNameProperty("name"));
201                queryEnt.put("J2EEApplication", gb
202                        .getNameProperty("J2EEApplication"));
203                setSessionBean = kernel
204                        .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
205                                null, queryEnt));
206    
207                iterSessionBean = setSessionBean.iterator();
208    
209                while (iterSessionBean.hasNext()) {
210                    AbstractName gbSessionBean = (AbstractName) iterSessionBean
211                            .next();
212                    StringTree beanNode = new StringTree(gbSessionBean
213                            .getNameProperty("name"));
214                    sessionBean.addChild(beanNode);
215                    Context jndi = (Context) kernel.getAttribute(gbSessionBean,
216                            "componentContext");
217                    buildContext(beanNode, jndi, "java:comp");
218                }
219            }
220        }
221    
222        private void buildWebModule(Kernel kernel, List arryList, Hashtable entApp)
223                throws Exception {
224            Map query = new HashMap();
225            query.put("j2eeType", "WebModule");
226            Set setEnt = kernel
227                    .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
228                            null, query));
229            Iterator iterator = setEnt.iterator();
230    
231            while (iterator.hasNext()) {
232    
233                AbstractName gb = (AbstractName) iterator.next();
234                StringTree webModule = new StringTree(gb.getNameProperty("name"));
235    
236                if (gb.getNameProperty("J2EEApplication") == null
237                        || gb.getNameProperty("J2EEApplication").equals("null")) {
238                    StringTree treeEnt = (StringTree) entApp.get("WebModule");
239                    treeEnt.addChild(webModule);
240                } else {
241                    StringTree treeEnt = (StringTree) entApp.get(gb
242                            .getNameProperty("J2EEApplication"));
243                    treeEnt = treeEnt.findNode("WebModule");
244                    treeEnt.addChild(webModule);
245                }
246    
247                Map map = (Map) kernel.getAttribute(gb, "componentContext");
248                String[] servlets = (String[]) kernel.getAttribute(gb, "servlets");
249    
250                StringTree servletsNode = null;
251                StringTree jspNode = null;
252    
253                for (int i = 0; i < servlets.length; i++) {
254                    String servlet = servlets[i];
255                    servlet = servlet.substring(servlet.indexOf("name=") + 5);
256                    if (servlet.indexOf(",") != -1)
257                        servlet = servlet.substring(0, servlet.indexOf(","));
258                    if (!servlet.equals("jsp") && servlet.startsWith("jsp.")) {
259                        if (servletsNode == null) {
260                            servletsNode = new StringTree("Servlets");
261                            webModule.addChild(servletsNode);
262                        }
263                        if (jspNode == null) {
264                            jspNode = new StringTree("JSP");
265                            servletsNode.addChild(jspNode);
266                        }
267                        jspNode.addChild(new StringTree(servlet.substring(4)));
268                    } else if (!servlet.equals("jsp")) {
269                        if (servletsNode == null) {
270                            servletsNode = new StringTree("Servlets");
271                            webModule.addChild(servletsNode);
272                        }
273                        servletsNode.addChild(new StringTree(servlet));
274                    }
275                }
276                Iterator contexts = map.keySet().iterator();
277                while (contexts.hasNext())
278                    webModule.addChild(new StringTree("java:comp/" + contexts.next()));
279            }
280        }
281    
282        private void buildResourceModule(Kernel kernel, List arryList,
283                Hashtable entApp) throws Exception {
284            Map query = new HashMap();
285            query.put("j2eeType", "ResourceAdapterModule");
286            Set setEnt = kernel
287                    .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
288                            null, query));
289            Iterator iterator = setEnt.iterator();
290    
291            while (iterator.hasNext()) {
292                AbstractName gb = (AbstractName) iterator.next();
293                String resourceModule = gb.getNameProperty("name");
294                if (gb.getNameProperty("J2EEApplication") == null
295                        || gb.getNameProperty("J2EEApplication").equals("null")) {
296                    StringTree treeEnt = (StringTree) entApp
297                            .get("ResourceAdapterModule");
298                    treeEnt.addChild(new StringTree(resourceModule));
299                } else {
300                    StringTree treeEnt = (StringTree) entApp.get(gb
301                            .getNameProperty("J2EEApplication"));
302                    treeEnt = treeEnt.findNode("ResourceAdapterModule");
303                    treeEnt.addChild(new StringTree(resourceModule));
304                }
305            }
306        }
307    
308        private void buildAppClientModule(Kernel kernel, List arryList,
309                Hashtable entApp) throws Exception {
310            Map query = new HashMap();
311            query.put("j2eeType", "AppClientModule");
312            Set setEnt = kernel
313                    .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
314                            null, query));
315            Iterator iterator = setEnt.iterator();
316    
317            while (iterator.hasNext()) {
318                AbstractName gb = (AbstractName) iterator.next();
319                String appClienteModule = gb.getNameProperty("name");
320                if (gb.getNameProperty("J2EEApplication") == null
321                        || gb.getNameProperty("J2EEApplication").equals("null")) {
322                    StringTree treeEnt = (StringTree) entApp.get("AppClientModule");
323                    treeEnt.addChild(new StringTree(appClienteModule));
324                } else {
325                    StringTree treeEnt = (StringTree) entApp.get(gb
326                            .getNameProperty("J2EEApplication"));
327                    treeEnt = treeEnt.findNode("AppClientModule");
328                    treeEnt.addChild(new StringTree(appClienteModule));
329                }
330            }
331        }
332    
333        public void buildContext(StringTree node, Context compCtx, String nodeCurr) {
334            Context oldCtx = RootContext.getComponentContext();
335            RootContext.setComponentContext(compCtx);
336            try {
337                InitialContext ctx = new InitialContext();
338                buildContextSub(node, (Context)ctx.lookup("java:comp"), nodeCurr);
339            } catch (Exception e) {
340                log.warn("Error looking up java:comp context", e);
341            } finally {        
342                RootContext.setComponentContext(oldCtx);
343            }
344        }
345        
346        private void buildContextSub(StringTree node, Context ctx, String nodeCurr) {
347            try {
348                NamingEnumeration enumName = ctx.list("");
349                while (enumName.hasMoreElements()) {
350                    NameClassPair pair = (NameClassPair) enumName.next();
351                    Object obj = ctx.lookup(pair.getName());
352                    if (obj instanceof Context) {
353                        buildContextSub(node, (Context) obj, nodeCurr + "/"
354                                + pair.getName());
355                    } else {
356                        node.addChild(new StringTree(nodeCurr + "/" + pair.getName()));
357                    }
358                }
359            } catch (Exception e) {
360                log.warn("Error listing context", e);
361            }
362        }
363    
364        private void buildGlobal(StringTree tree, Context context, String parent)
365                throws Exception {
366            if (parent == null)
367                parent = "";
368            if (!parent.equals(""))
369                parent = parent + "/";
370            javax.naming.NamingEnumeration enum1 = context.list("");
371            while (enum1.hasMoreElements()) {
372                javax.naming.NameClassPair pair = (javax.naming.NameClassPair) enum1
373                        .next();
374                Object obj = context.lookup(pair.getName());
375                if (obj instanceof Context) {
376                    buildGlobal(tree, (Context) obj, parent + pair.getName());
377                } else {
378                    tree.addChild(new StringTree(parent + pair.getName()));
379                }
380            }
381        }
382    
383        public List getContextTree() throws Exception {
384            List arryList = new ArrayList();
385            Hashtable entApp = new Hashtable();
386    
387            StringTree treeGlobal = new StringTree("Global Context");
388            arryList.add(treeGlobal);
389            buildGlobal(treeGlobal,
390                    org.apache.xbean.naming.global.GlobalContextManager
391                            .getGlobalContext(), "");
392    
393            StringTree tree = new StringTree("Enterprise Applications");
394            arryList.add(tree);
395    
396            StringTree treeMod = new StringTree("EJBModule");
397            entApp.put("EJBModule", treeMod);
398            arryList.add(treeMod);
399    
400            treeMod = new StringTree("WebModule");
401            entApp.put("WebModule", treeMod);
402            arryList.add(treeMod);
403    
404            treeMod = new StringTree("ResourceAdapterModule");
405            entApp.put("ResourceAdapterModule", treeMod);
406            arryList.add(treeMod);
407    
408            treeMod = new StringTree("AppClientModule");
409            entApp.put("AppClientModule", treeMod);
410            arryList.add(treeMod);
411    
412            org.apache.geronimo.kernel.Kernel kernel = org.apache.geronimo.kernel.KernelRegistry
413                    .getSingleKernel();
414    
415            Set setEnt = kernel
416                    .listGBeans(new org.apache.geronimo.gbean.AbstractNameQuery(
417                            null,
418                            Collections.EMPTY_MAP,
419                            org.apache.geronimo.j2ee.management.impl.J2EEApplicationImpl.class
420                                    .getName()));
421            Iterator iterator = setEnt.iterator();
422            while (iterator.hasNext()) {
423                org.apache.geronimo.gbean.AbstractName gb = (org.apache.geronimo.gbean.AbstractName) iterator
424                        .next();
425                StringTree curr = new StringTree(gb.getNameProperty("name"));
426                tree.addChild(curr);
427                entApp.put(gb.getNameProperty("name"), curr);
428    
429                StringTree temp = new StringTree("EJBModule");
430                curr.addChild(temp);
431    
432                temp = new StringTree("WebModule");
433                curr.addChild(temp);
434    
435                temp = new StringTree("ResourceAdapterModule");
436                curr.addChild(temp);
437    
438                temp = new StringTree("AppClientModule");
439                curr.addChild(temp);
440            }
441    
442            buildEJBModule(kernel, arryList, entApp);
443            buildWebModule(kernel, arryList, entApp);
444            buildResourceModule(kernel, arryList, entApp);
445            buildAppClientModule(kernel, arryList, entApp);
446            return arryList;
447        }
448    }