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