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