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.dependencyview;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025: import java.util.SortedSet;
026:
027: import javax.portlet.ActionRequest;
028: import javax.portlet.ActionResponse;
029: import javax.portlet.PortletConfig;
030: import javax.portlet.PortletException;
031: import javax.portlet.PortletRequestDispatcher;
032: import javax.portlet.RenderRequest;
033: import javax.portlet.RenderResponse;
034: import javax.portlet.WindowState;
035:
036: import org.apache.geronimo.console.BasePortlet;
037: import org.apache.geronimo.console.util.PortletManager;
038: import org.apache.geronimo.console.util.StringTree;
039: import org.apache.geronimo.gbean.AbstractName;
040: import org.apache.geronimo.gbean.AbstractNameQuery;
041: import org.apache.geronimo.kernel.config.Configuration;
042: import org.apache.geronimo.kernel.config.ConfigurationInfo;
043: import org.apache.geronimo.kernel.config.ConfigurationManager;
044: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
045: import org.apache.geronimo.kernel.config.ConfigurationUtil;
046: import org.apache.geronimo.kernel.repository.Artifact;
047: import org.apache.geronimo.kernel.repository.ListableRepository;
048:
049: public class DependencyViewPortlet extends BasePortlet {
050:
051: private static final String NORMALVIEW_JSP = "/WEB-INF/view/dependencyview/view.jsp";
052:
053: private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/dependencyview/view.jsp";
054:
055: private static final String HELPVIEW_JSP = "/WEB-INF/view/dependencyview/help.jsp";
056:
057: private PortletRequestDispatcher normalView;
058:
059: private PortletRequestDispatcher maximizedView;
060:
061: private PortletRequestDispatcher helpView;
062:
063: public void processAction(ActionRequest actionRequest,
064: ActionResponse actionResponse) throws PortletException,
065: IOException {
066: }
067:
068: protected void doView(RenderRequest renderRequest,
069: RenderResponse renderResponse) throws IOException,
070: PortletException {
071: if (WindowState.MINIMIZED
072: .equals(renderRequest.getWindowState())) {
073: return;
074: }
075:
076: renderRequest.getPortletSession().setAttribute(
077: "dependencyTree", getJSONTrees(renderRequest));
078:
079: if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
080: normalView.include(renderRequest, renderResponse);
081: } else {
082: maximizedView.include(renderRequest, renderResponse);
083: }
084: }
085:
086: protected void doHelp(RenderRequest renderRequest,
087: RenderResponse renderResponse) throws PortletException,
088: IOException {
089: helpView.include(renderRequest, renderResponse);
090: }
091:
092: public void init(PortletConfig portletConfig)
093: throws PortletException {
094: super .init(portletConfig);
095: normalView = portletConfig.getPortletContext()
096: .getRequestDispatcher(NORMALVIEW_JSP);
097: maximizedView = portletConfig.getPortletContext()
098: .getRequestDispatcher(MAXIMIZEDVIEW_JSP);
099: helpView = portletConfig.getPortletContext()
100: .getRequestDispatcher(HELPVIEW_JSP);
101:
102: }
103:
104: public void destroy() {
105: normalView = null;
106: maximizedView = null;
107: helpView = null;
108: super .destroy();
109: }
110:
111: private static ArrayList parentNodes = new ArrayList();
112:
113: public static String getJSONTrees(RenderRequest renderRequest) {
114: List list = getTrees(renderRequest);
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: public static void addDependencies(StringTree curr,
131: Configuration conf) {
132: if (curr == null || conf == null)
133: return;
134: StringTree dep = new StringTree("dependencies");
135: curr.addChild(dep);
136: for (Iterator iterator = conf.getDependencies().iterator(); iterator
137: .hasNext();) {
138: dep.addChild(iterator.next().toString());
139: }
140: for (Iterator iterator = conf.getServiceParents().iterator(); iterator
141: .hasNext();) {
142: Configuration config = (Configuration) iterator.next();
143: dep.addChild(config.getId().toString());
144: }
145: }
146:
147: public static ArrayList getTrees(RenderRequest request) {
148:
149: ArrayList arryList = new ArrayList();
150: StringTree treeEAR = new StringTree("Enterprise Applications");
151: arryList.add(treeEAR);
152:
153: StringTree treeEJB = new StringTree("EJBModule");
154: arryList.add(treeEJB);
155:
156: StringTree treeWeb = new StringTree("WebModule");
157: arryList.add(treeWeb);
158:
159: StringTree treeRAR = new StringTree("ResourceAdapterModule");
160: arryList.add(treeRAR);
161:
162: StringTree treeCLI = new StringTree("AppClientModule");
163: arryList.add(treeCLI);
164:
165: StringTree treeSys = new StringTree("System Module");
166: arryList.add(treeSys);
167:
168: org.apache.geronimo.kernel.Kernel kernel = org.apache.geronimo.kernel.KernelRegistry
169: .getSingleKernel();
170:
171: ConfigurationManager configManager = ConfigurationUtil
172: .getConfigurationManager(kernel);
173:
174: List infos = configManager.listConfigurations();
175: for (Iterator infoIterator = infos.iterator(); infoIterator
176: .hasNext();) {
177: ConfigurationInfo info = (ConfigurationInfo) infoIterator
178: .next();
179: Configuration conf = configManager.getConfiguration(info
180: .getConfigID());
181: if (conf != null) {
182: StringTree curr = new StringTree(info.getConfigID()
183: .toString());
184: ;
185: switch (info.getType().getValue()) {
186: case 0:// EAR
187: {
188: treeEAR.addChild(curr);
189: break;
190: }
191: case 1:// EJB
192: {
193: treeEJB.addChild(curr);
194: break;
195: }
196: case 2:// CAR
197: {
198: treeCLI.addChild(curr);
199: break;
200: }
201:
202: case 3:// RAR
203: {
204: treeRAR.addChild(curr);
205: break;
206: }
207: case 4:// WAR
208: {
209: treeWeb.addChild(curr);
210: break;
211: }
212: case 5:// SERVICE
213: {
214: treeSys.addChild(curr);
215: break;
216: }
217: }
218:
219: addDependencies(curr, conf);
220:
221: if (info.getType().getValue() == ConfigurationModuleType.EAR
222: .getValue()) {
223: StringTree nodeEJB = new StringTree("EJBModule");
224: curr.addChild(nodeEJB);
225:
226: StringTree nodeWeb = new StringTree("WebModule");
227: curr.addChild(nodeWeb);
228:
229: StringTree nodeRAR = new StringTree(
230: "ResourceAdapterModule");
231: curr.addChild(nodeRAR);
232:
233: StringTree nodeCLI = new StringTree(
234: "AppClientModule");
235: curr.addChild(nodeCLI);
236:
237: Map<String, String> query = new HashMap<String, String>();
238: query.put("j2eeType", "EJBModule");
239: query.put("J2EEApplication", info.getConfigID()
240: .toString());
241: Set<AbstractName> setEnt = kernel
242: .listGBeans(new AbstractNameQuery(null,
243: query));
244: for (AbstractName gb : setEnt) {
245: StringTree subCurr = new StringTree(info
246: .getConfigID().getGroupId()
247: + "/"
248: + info.getConfigID().getArtifactId()
249: + "_"
250: + gb.getNameProperty("name")
251: + "/"
252: + info.getConfigID().getVersion()
253: + "/" + info.getConfigID().getType());
254: nodeEJB.addChild(subCurr);
255: addDependencies(subCurr, configManager
256: .getConfiguration(gb.getArtifact()));
257: }
258:
259: Map<String, String> query1 = new HashMap<String, String>();
260: query1.put("j2eeType", "ResourceAdapterModule");
261: query1.put("J2EEApplication", info.getConfigID()
262: .toString());
263: Set<AbstractName> setEnt1 = kernel
264: .listGBeans(new AbstractNameQuery(null,
265: query1));
266:
267: for (AbstractName gb : setEnt1) {
268: StringTree subCurr = new StringTree(info
269: .getConfigID().getGroupId()
270: + "/"
271: + info.getConfigID().getArtifactId()
272: + "_"
273: + gb.getNameProperty("name")
274: + "/"
275: + info.getConfigID().getVersion()
276: + "/" + info.getConfigID().getType());
277: nodeRAR.addChild(subCurr);
278: addDependencies(subCurr, configManager
279: .getConfiguration(gb.getArtifact()));
280: }
281:
282: for (Configuration config : conf.getChildren()) {
283: StringTree subCurr = new StringTree(config
284: .getAbstractName().toString());
285: nodeWeb.addChild(subCurr);
286: addDependencies(subCurr, config);
287: }
288:
289: for (Artifact name : conf.getOwnedConfigurations()) {
290: StringTree subCurr = new StringTree(name
291: .toString());
292: nodeCLI.addChild(subCurr);
293: addDependencies(subCurr, configManager
294: .getConfiguration(name));
295: }
296:
297: }
298:
299: }
300:
301: }
302:
303: StringTree treeRepo = new StringTree("Repository");
304: arryList.add(treeRepo);
305:
306: ListableRepository[] repos = PortletManager.getCurrentServer(
307: request).getRepositories();
308: for (int i = 0; i < repos.length; i++) {
309: ListableRepository repo = repos[i];
310: final SortedSet artifacts = repo.list();
311: for (Iterator iterator = artifacts.iterator(); iterator
312: .hasNext();) {
313: String fileName = iterator.next().toString();
314: treeRepo.addChild(fileName);
315: }
316:
317: }
318:
319: return arryList;
320:
321: }
322:
323: }
|