KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > console > JBIComponentsPortlet


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.servicemix.console;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.SortedMap JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import javax.management.ObjectName JavaDoc;
25 import javax.portlet.ActionRequest;
26 import javax.portlet.ActionResponse;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletContext;
29 import javax.portlet.PortletException;
30 import javax.portlet.PortletRequestDispatcher;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33
34 import org.apache.servicemix.jbi.management.ManagementContextMBean;
35
36 public class JBIComponentsPortlet extends ServiceMixPortlet {
37
38     private static final String JavaDoc MODE_KEY = "mode";
39     private static final String JavaDoc LIST_MODE = "list";
40     private static final String JavaDoc COMP_MODE = "comp";
41     
42     protected PortletRequestDispatcher compView;
43     
44        public static class ComponentInfo {
45             private String JavaDoc name;
46             private String JavaDoc type;
47             private String JavaDoc state;
48             
49             public String JavaDoc getType() {
50                 return type;
51             }
52             public void setType(String JavaDoc type) {
53                 this.type = type;
54             }
55             public String JavaDoc getName() {
56                 return name;
57             }
58             public void setName(String JavaDoc name) {
59                 this.name = name;
60             }
61             public String JavaDoc getState() {
62                 return state;
63             }
64             public void setState(String JavaDoc state) {
65                 this.state = state;
66             }
67         }
68
69         public void init(PortletConfig portletConfig) throws PortletException {
70             super.init(portletConfig);
71             PortletContext pc = portletConfig.getPortletContext();
72             compView = pc.getRequestDispatcher("/WEB-INF/view/" + getPortletName() + "/comp.jsp");
73         }
74        
75         protected void renderView(RenderRequest renderRequest, RenderResponse renderResponse) throws Exception JavaDoc {
76             String JavaDoc mode = renderRequest.getParameter(MODE_KEY);
77             System.err.println("Mode: " + mode);
78             if (COMP_MODE.equals(mode)) {
79                 renderCompRequest(renderRequest, renderResponse);
80             } else {
81                 // Render list
82
renderListRequest(renderRequest, renderResponse);
83             }
84         }
85
86         protected void renderCompRequest(RenderRequest request, RenderResponse response) throws Exception JavaDoc {
87             compView.include(request, response);
88         }
89         
90         protected void renderListRequest(RenderRequest request, RenderResponse response) throws Exception JavaDoc {
91             ManagementContextMBean management = getManagementContext();
92             SortedMap JavaDoc components = new TreeMap JavaDoc();
93             ObjectName JavaDoc[] bcs = management.getBindingComponents();
94             for (int i = 0; i < bcs.length; i++) {
95                 ComponentInfo info = new ComponentInfo();
96                 info.name = getAttribute(bcs[i], "name");
97                 info.type = "Binding Component";
98                 info.state = getAttribute(bcs[i], "currentState");
99                 components.put(info.name, info);
100             }
101             ObjectName JavaDoc[] ses = management.getEngineComponents();
102             for (int i = 0; i < ses.length; i++) {
103                 ComponentInfo info = new ComponentInfo();
104                 info.name = getAttribute(ses[i], "name");
105                 info.type = "Service Engine";
106                 info.state = getAttribute(ses[i], "currentState");
107                 if (components.containsKey(info.name)) {
108                     ((ComponentInfo) components.get(info.name)).type = "Unknown";
109                 } else {
110                     components.put(info.name, info);
111                 }
112             }
113             List JavaDoc infos = new ArrayList JavaDoc(components.values());
114             request.setAttribute("components", infos);
115             normalView.include(request, response);
116         }
117         
118         protected String JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) {
119             try {
120                 return (String JavaDoc) getServerConnection().getAttribute(name, attribute);
121             } catch (Exception JavaDoc e) {
122                 log.error("Could not retrieve attribute '" + attribute + "' for mbean '" + name + "'");
123                 return null;
124             }
125         }
126
127         protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception JavaDoc {
128             String JavaDoc action = actionRequest.getParameter("action");
129             String JavaDoc name = actionRequest.getParameter("name");
130             System.err.println("doProcessAction: " + action + " for " + name);
131             ManagementContextMBean management = getManagementContext();
132             if ("stop".equals(action)) {
133                 management.stopComponent(name);
134             } else if ("start".equals(action)) {
135                 management.startComponent(name);
136             } else if ("shutdown".equals(action)) {
137                 management.shutDownComponent(name);
138             }
139         }
140 }
141
Popular Tags