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.classloaderview;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Hashtable;
022: import java.util.List;
023: import java.util.Collections;
024:
025: import org.apache.geronimo.console.BasePortlet;
026: import org.apache.geronimo.console.util.StringTree;
027:
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletConfig;
031: import javax.portlet.PortletException;
032: import javax.portlet.PortletRequestDispatcher;
033: import javax.portlet.RenderRequest;
034: import javax.portlet.RenderResponse;
035: import javax.portlet.WindowState;
036:
037: import org.apache.geronimo.kernel.util.ClassLoaderRegistry;
038:
039: public class ClassLoaderViewPortlet extends BasePortlet {
040:
041: private static final String NORMALVIEW_JSP = "/WEB-INF/view/classloaderview/view.jsp";
042:
043: private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/classloaderview/view.jsp";
044:
045: private static final String HELPVIEW_JSP = "/WEB-INF/view/classloaderview/help.jsp";
046:
047: private PortletRequestDispatcher normalView;
048:
049: private PortletRequestDispatcher maximizedView;
050:
051: private PortletRequestDispatcher helpView;
052:
053: private boolean inverse;
054:
055: public void processAction(ActionRequest actionRequest,
056: ActionResponse actionResponse) throws PortletException,
057: IOException {
058: // set selectedNode
059: actionRequest.getPortletSession().setAttribute("selectedNode",
060: actionRequest.getParameter("snNode"));
061: // toggle between inverse and non-inverse each time the form is processed
062: inverse = actionRequest.getParameter("inverse")
063: .equalsIgnoreCase("true") ? true : false;
064: // toggle
065: inverse = (!inverse);
066: // store inverse variable for the jsp
067: actionRequest.getPortletSession().setAttribute("invert",
068: inverse);
069: }
070:
071: protected void doView(RenderRequest renderRequest,
072: RenderResponse renderResponse) throws IOException,
073: PortletException {
074: if (WindowState.MINIMIZED
075: .equals(renderRequest.getWindowState())) {
076: return;
077: }
078: renderRequest.getPortletSession().setAttribute(
079: "classloaderTree", this );
080: renderRequest.setAttribute("inverse", inverse);
081: renderRequest.getPortletSession().setAttribute("inverse",
082: inverse);
083:
084: if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
085: normalView.include(renderRequest, renderResponse);
086: } else {
087: maximizedView.include(renderRequest, renderResponse);
088: }
089: }
090:
091: protected void doHelp(RenderRequest renderRequest,
092: RenderResponse renderResponse) throws PortletException,
093: IOException {
094: helpView.include(renderRequest, renderResponse);
095: }
096:
097: public void init(PortletConfig portletConfig)
098: throws PortletException {
099: super .init(portletConfig);
100: normalView = portletConfig.getPortletContext()
101: .getRequestDispatcher(NORMALVIEW_JSP);
102: maximizedView = portletConfig.getPortletContext()
103: .getRequestDispatcher(MAXIMIZEDVIEW_JSP);
104: helpView = portletConfig.getPortletContext()
105: .getRequestDispatcher(HELPVIEW_JSP);
106: inverse = false;
107: }
108:
109: public void destroy() {
110: normalView = null;
111: maximizedView = null;
112: helpView = null;
113: super .destroy();
114: }
115:
116: public String getJSONTrees(boolean inverse) {
117: List list = getTrees(inverse);
118: if (list == null)
119: return "[]";
120:
121: StringBuffer stb = new StringBuffer();
122: stb.append("[");
123: Hashtable htLinks = new Hashtable();
124: // First level cannot be a link
125: for (int i = 0; i < list.size(); i++) {
126: StringTree node = (StringTree) list.get(i);
127: htLinks.put(node.name, "" + i);
128: }
129: for (int i = 0; i < list.size(); i++) {
130: StringTree node = (StringTree) list.get(i);
131: if (i != 0)
132: stb.append(",");
133: stb.append(node.toJSONObject("" + i, htLinks, true));
134: }
135: stb.append("]");
136: list = null;
137: return stb.toString();
138: }
139:
140: public ArrayList getTrees(boolean inverse) {
141: ArrayList parentNodes = new ArrayList();
142: List list = ClassLoaderRegistry.getList();
143: Iterator iter = list.iterator();
144: while (iter.hasNext()) {
145: if (!inverse)
146: updateTree((ClassLoader) iter.next(), parentNodes);
147: else
148: inverseTree((ClassLoader) iter.next(), parentNodes);
149: }
150: Collections.sort(parentNodes);
151: return parentNodes;
152: }
153:
154: public StringTree inverseTree(ClassLoader classloader,
155: ArrayList parentNodes) {
156:
157: StringTree node = new StringTree(classloader.toString());
158: int index = parentNodes.indexOf(node);
159: if (index != -1)
160: return (StringTree) parentNodes.get(index);
161:
162: node = addClasses(node, classloader);
163:
164: if (classloader instanceof org.apache.geronimo.kernel.config.MultiParentClassLoader) {
165: org.apache.geronimo.kernel.config.MultiParentClassLoader mpclassloader = (org.apache.geronimo.kernel.config.MultiParentClassLoader) classloader;
166: ClassLoader[] parents = mpclassloader.getParents();
167: if (parents != null)
168: for (int i = 0; i < parents.length; i++) {
169: StringTree parentNode = inverseTree(parents[i],
170: parentNodes);
171: node.addChild(parentNode);
172: }
173: } else if (classloader.getParent() != null) {
174: StringTree parentNode = inverseTree(
175: classloader.getParent(), parentNodes);
176: node.addChild(parentNode);
177: }
178: if (!parentNodes.contains(node))
179: parentNodes.add(node);
180:
181: return node;
182: }
183:
184: public StringTree updateTree(ClassLoader classloader,
185: ArrayList parentNodes) {
186:
187: Iterator iter = parentNodes.iterator();
188: StringTree node = null;
189: while (iter.hasNext()) {
190: StringTree currNode = (StringTree) iter.next();
191: node = currNode.findNode(classloader.toString());
192: if (node != null)
193: return node;
194: }
195:
196: if (node == null) {
197: node = new StringTree(classloader.toString());
198: node = addClasses(node, classloader);
199:
200: if (classloader instanceof org.apache.geronimo.kernel.config.MultiParentClassLoader) {
201: org.apache.geronimo.kernel.config.MultiParentClassLoader mpclassloader = (org.apache.geronimo.kernel.config.MultiParentClassLoader) classloader;
202: ClassLoader[] parents = mpclassloader.getParents();
203: if (parents == null)
204: parentNodes.add(node);
205: else if (parents.length == 0)
206: parentNodes.add(node);
207: else {
208: for (int i = 0; i < parents.length; i++) {
209: StringTree parentNode = updateTree(parents[i],
210: parentNodes);
211: parentNode.addChild(node);
212: }
213: }
214: } else if (classloader.getParent() != null) {
215: StringTree parentNode = updateTree(classloader
216: .getParent(), parentNodes);
217: parentNode.addChild(node);
218: } else
219: parentNodes.add(node);
220: }
221: return node;
222: }
223:
224: private StringTree addClasses(StringTree node, ClassLoader loader) {
225: try {
226: java.lang.reflect.Field CLASSES_VECTOR_FIELD = ClassLoader.class
227: .getDeclaredField("classes");
228:
229: if (CLASSES_VECTOR_FIELD.getType() != java.util.Vector.class) {
230: return node;
231: }
232: CLASSES_VECTOR_FIELD.setAccessible(true);
233:
234: final java.util.Vector classes = (java.util.Vector) CLASSES_VECTOR_FIELD
235: .get(loader);
236: if (classes == null)
237: return node;
238:
239: final Class[] result;
240:
241: synchronized (classes) {
242: result = new Class[classes.size()];
243: classes.toArray(result);
244: }
245:
246: CLASSES_VECTOR_FIELD.setAccessible(false);
247:
248: StringTree classNames = new StringTree("Classes");
249: StringTree interfaceNames = new StringTree("Interfaces");
250: node.addChild(classNames);
251: node.addChild(interfaceNames);
252:
253: for (int i = 0; i < result.length; i++) {
254: if (result[i].isInterface())
255: interfaceNames.addChild(result[i].toString());
256: else
257: classNames.addChild(result[i].toString());
258: }
259:
260: return node;
261: } catch (Exception e) {
262: return node;
263: }
264: }
265:
266: }
|