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