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.servicemix.console;
018
019 import java.util.ArrayList;
020 import java.util.List;
021 import java.util.SortedMap;
022 import java.util.TreeMap;
023
024 import javax.management.ObjectName;
025 import javax.portlet.ActionRequest;
026 import javax.portlet.ActionResponse;
027 import javax.portlet.PortletConfig;
028 import javax.portlet.PortletContext;
029 import javax.portlet.PortletException;
030 import javax.portlet.PortletRequestDispatcher;
031 import javax.portlet.RenderRequest;
032 import javax.portlet.RenderResponse;
033
034 import org.apache.servicemix.jbi.management.ManagementContextMBean;
035
036 public class JBIComponentsPortlet extends ServiceMixPortlet {
037
038 private static final String MODE_KEY = "mode";
039 private static final String LIST_MODE = "list";
040 private static final String COMP_MODE = "comp";
041
042 protected PortletRequestDispatcher compView;
043
044 public static class ComponentInfo {
045 private String name;
046 private String type;
047 private String state;
048
049 public String getType() {
050 return type;
051 }
052 public void setType(String type) {
053 this.type = type;
054 }
055 public String getName() {
056 return name;
057 }
058 public void setName(String name) {
059 this.name = name;
060 }
061 public String getState() {
062 return state;
063 }
064 public void setState(String state) {
065 this.state = state;
066 }
067 }
068
069 public void init(PortletConfig portletConfig) throws PortletException {
070 super.init(portletConfig);
071 PortletContext pc = portletConfig.getPortletContext();
072 compView = pc.getRequestDispatcher("/WEB-INF/view/" + getPortletName() + "/comp.jsp");
073 }
074
075 protected void renderView(RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
076 String mode = renderRequest.getParameter(MODE_KEY);
077 System.err.println("Mode: " + mode);
078 if (COMP_MODE.equals(mode)) {
079 renderCompRequest(renderRequest, renderResponse);
080 } else {
081 // Render list
082 renderListRequest(renderRequest, renderResponse);
083 }
084 }
085
086 protected void renderCompRequest(RenderRequest request, RenderResponse response) throws Exception {
087 compView.include(request, response);
088 }
089
090 protected void renderListRequest(RenderRequest request, RenderResponse response) throws Exception {
091 ManagementContextMBean management = getManagementContext();
092 SortedMap components = new TreeMap();
093 ObjectName[] bcs = management.getBindingComponents();
094 for (int i = 0; i < bcs.length; i++) {
095 ComponentInfo info = new ComponentInfo();
096 info.name = getAttribute(bcs[i], "name");
097 info.type = "Binding Component";
098 info.state = getAttribute(bcs[i], "currentState");
099 components.put(info.name, info);
100 }
101 ObjectName[] 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 infos = new ArrayList(components.values());
114 request.setAttribute("components", infos);
115 normalView.include(request, response);
116 }
117
118 protected String getAttribute(ObjectName name, String attribute) {
119 try {
120 return (String) getServerConnection().getAttribute(name, attribute);
121 } catch (Exception 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 {
128 String action = actionRequest.getParameter("action");
129 String 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 }