1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.console.dependencyview; 18 19 import java.util.ArrayList; 20 import java.util.HashMap; 21 import java.util.Iterator; 22 import java.util.List; 23 import java.util.Map; 24 import java.util.Set; 25 import java.util.SortedSet; 26 27 import javax.servlet.http.HttpSession; 28 29 import org.apache.geronimo.console.util.Tree; 30 import org.apache.geronimo.console.util.TreeEntry; 31 import org.apache.geronimo.gbean.AbstractName; 32 import org.apache.geronimo.gbean.AbstractNameQuery; 33 import org.apache.geronimo.kernel.config.Configuration; 34 import org.apache.geronimo.kernel.config.ConfigurationInfo; 35 import org.apache.geronimo.kernel.config.ConfigurationManager; 36 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 37 import org.apache.geronimo.kernel.config.ConfigurationUtil; 38 import org.apache.geronimo.kernel.repository.Artifact; 39 import org.apache.geronimo.kernel.repository.ListableRepository; 40 import org.apache.geronimo.management.geronimo.J2EEServer; 41 import org.directwebremoting.annotations.RemoteMethod; 42 import org.directwebremoting.annotations.RemoteProxy; 43 import org.slf4j.Logger; 44 import org.slf4j.LoggerFactory; 45 46 @RemoteProxy 47 public class DependencyViewHelper { 48 49 private static final String NO_CHILD = "none"; 50 51 private static final String NOT_LEAF_TYPE = "not_leaf"; 52 53 private static final String NORMAL_TYPE = "normal"; 54 55 private static final Logger logger = LoggerFactory.getLogger(DependencyViewHelper.class); 56 57 public static void addDependencies(TreeEntry curr, Configuration conf) { 58 if (curr == null || conf == null) 59 return; 60 TreeEntry dep = new TreeEntry("dependencies", NOT_LEAF_TYPE); 61 curr.addChild(dep); 62 for (Iterator iterator = conf.getDependencies().iterator(); iterator.hasNext();) { 63 dep.addChild(new TreeEntry(iterator.next().toString(), NORMAL_TYPE)); 64 } 65 for (Iterator iterator = conf.getServiceParents().iterator(); iterator.hasNext();) { 66 Configuration config = (Configuration) iterator.next(); 67 dep.addChild(new TreeEntry(config.getId().toString(), NORMAL_TYPE)); 68 } 69 } 70 71 @RemoteMethod 72 public static Tree getTrees(HttpSession session) { 73 Tree dependencyTree = new Tree(null, "name"); 74 75 TreeEntry treeEAR = new TreeEntry("Enterprise Applications", NOT_LEAF_TYPE); 76 dependencyTree.addItem(treeEAR); 77 78 TreeEntry treeEJB = new TreeEntry("EJBModule", NOT_LEAF_TYPE); 79 dependencyTree.addItem(treeEJB); 80 81 TreeEntry treeWeb = new TreeEntry("WebModule", NOT_LEAF_TYPE); 82 dependencyTree.addItem(treeWeb); 83 84 TreeEntry treeRAR = new TreeEntry("ResourceAdapterModule", NOT_LEAF_TYPE); 85 dependencyTree.addItem(treeRAR); 86 87 TreeEntry treeCLI = new TreeEntry("AppClientModule", NOT_LEAF_TYPE); 88 dependencyTree.addItem(treeCLI); 89 90 TreeEntry treeSys = new TreeEntry("System Module", NOT_LEAF_TYPE); 91 dependencyTree.addItem(treeSys); 92 93 org.apache.geronimo.kernel.Kernel kernel = org.apache.geronimo.kernel.KernelRegistry.getSingleKernel(); 94 95 ConfigurationManager configManager = ConfigurationUtil.getConfigurationManager(kernel); 96 97 List infos = configManager.listConfigurations(); 98 for (Iterator infoIterator = infos.iterator(); infoIterator.hasNext();) { 99 ConfigurationInfo info = (ConfigurationInfo) infoIterator.next(); 100 Configuration conf = configManager.getConfiguration(info.getConfigID()); 101 if (conf != null) { 102 TreeEntry curr = new TreeEntry(info.getConfigID().toString(), NORMAL_TYPE); 103 switch (info.getType().getValue()) { 104 case 0:// EAR 105 { 106 treeEAR.addChild(curr); 107 break; 108 } 109 case 1:// EJB 110 { 111 treeEJB.addChild(curr); 112 break; 113 } 114 case 2:// CAR 115 { 116 treeCLI.addChild(curr); 117 break; 118 } 119 120 case 3:// RAR 121 { 122 treeRAR.addChild(curr); 123 break; 124 } 125 case 4:// WAR 126 { 127 treeWeb.addChild(curr); 128 break; 129 } 130 case 5:// SERVICE 131 { 132 treeSys.addChild(curr); 133 break; 134 } 135 } 136 137 addDependencies(curr, conf); 138 139 if (info.getType().getValue() == ConfigurationModuleType.EAR.getValue()) { 140 TreeEntry nodeEJB = new TreeEntry("EJBModule", NOT_LEAF_TYPE); 141 curr.addChild(nodeEJB); 142 143 TreeEntry nodeWeb = new TreeEntry("WebModule", NOT_LEAF_TYPE); 144 curr.addChild(nodeWeb); 145 146 TreeEntry nodeRAR = new TreeEntry("ResourceAdapterModule", NOT_LEAF_TYPE); 147 curr.addChild(nodeRAR); 148 149 TreeEntry nodeCLI = new TreeEntry("AppClientModule", NOT_LEAF_TYPE); 150 curr.addChild(nodeCLI); 151 152 Map<String, String> query = new HashMap<String, String>(); 153 query.put("j2eeType", "EJBModule"); 154 query.put("J2EEApplication", info.getConfigID().toString()); 155 Set<AbstractName> setEnt = kernel.listGBeans(new AbstractNameQuery(null, query)); 156 for (AbstractName gb : setEnt) { 157 TreeEntry subCurr = new TreeEntry(info.getConfigID().getGroupId() + "/" 158 + info.getConfigID().getArtifactId() + "_" + gb.getNameProperty("name") + "/" 159 + info.getConfigID().getVersion() + "/" + info.getConfigID().getType(), NORMAL_TYPE); 160 nodeEJB.addChild(subCurr); 161 addDependencies(subCurr, configManager.getConfiguration(gb.getArtifact())); 162 } 163 164 Map<String, String> query1 = new HashMap<String, String>(); 165 query1.put("j2eeType", "ResourceAdapterModule"); 166 query1.put("J2EEApplication", info.getConfigID().toString()); 167 Set<AbstractName> setEnt1 = kernel.listGBeans(new AbstractNameQuery(null, query1)); 168 169 for (AbstractName gb : setEnt1) { 170 TreeEntry subCurr = new TreeEntry(info.getConfigID().getGroupId() + "/" 171 + info.getConfigID().getArtifactId() + "_" + gb.getNameProperty("name") + "/" 172 + info.getConfigID().getVersion() + "/" + info.getConfigID().getType(), NORMAL_TYPE); 173 nodeRAR.addChild(subCurr); 174 addDependencies(subCurr, configManager.getConfiguration(gb.getArtifact())); 175 } 176 177 for (Configuration config : conf.getChildren()) { 178 TreeEntry subCurr = new TreeEntry(config.getAbstractName().toString(), NORMAL_TYPE); 179 nodeWeb.addChild(subCurr); 180 addDependencies(subCurr, config); 181 } 182 183 for (Artifact name : conf.getOwnedConfigurations()) { 184 TreeEntry subCurr = new TreeEntry(name.toString(), NORMAL_TYPE); 185 nodeCLI.addChild(subCurr); 186 addDependencies(subCurr, configManager.getConfiguration(name)); 187 } 188 189 } 190 191 } 192 193 } 194 195 TreeEntry treeRepo = new TreeEntry("Repository", NORMAL_TYPE); 196 dependencyTree.addItem(treeRepo); 197 J2EEServer server = (J2EEServer) session.getAttribute(DependencyViewPortlet.Server_Key); 198 if (null == server) { 199 logger.error("can not find expected J2EEServer object"); 200 treeRepo.addChild(new TreeEntry("Not found the content of repository", NORMAL_TYPE));// Ignore the error at client 201 return dependencyTree; 202 } 203 session.removeAttribute(DependencyViewPortlet.Server_Key); 204 ListableRepository[] repos = server.getRepositories(); 205 for (int i = 0; i < repos.length; i++) { 206 ListableRepository repo = repos[i]; 207 final SortedSet artifacts = repo.list(); 208 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 209 String fileName = iterator.next().toString(); 210 treeRepo.addChild(new TreeEntry(fileName, NORMAL_TYPE)); 211 } 212 } 213 check_no_child(dependencyTree.getItems()); 214 return dependencyTree; 215 } 216 217 private static void check_no_child(List<TreeEntry> list) { 218 List<TreeEntry> children; 219 for (TreeEntry entry : list) { 220 children = entry.getChildren(); 221 if (children.size() > 0) 222 check_no_child(children); 223 else if (entry.getType().equals(NOT_LEAF_TYPE)) 224 children.add(new TreeEntry(NO_CHILD, NORMAL_TYPE)); 225 } 226 } 227 }