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