1 17 package org.apache.servicemix.console; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.SortedMap ; 22 import java.util.TreeMap ; 23 24 import javax.management.ObjectName ; 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 MODE_KEY = "mode"; 39 private static final String LIST_MODE = "list"; 40 private static final String COMP_MODE = "comp"; 41 42 protected PortletRequestDispatcher compView; 43 44 public static class ComponentInfo { 45 private String name; 46 private String type; 47 private String state; 48 49 public String getType() { 50 return type; 51 } 52 public void setType(String type) { 53 this.type = type; 54 } 55 public String getName() { 56 return name; 57 } 58 public void setName(String name) { 59 this.name = name; 60 } 61 public String getState() { 62 return state; 63 } 64 public void setState(String 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 { 76 String mode = renderRequest.getParameter(MODE_KEY); 77 System.err.println("Mode: " + mode); 78 if (COMP_MODE.equals(mode)) { 79 renderCompRequest(renderRequest, renderResponse); 80 } else { 81 renderListRequest(renderRequest, renderResponse); 83 } 84 } 85 86 protected void renderCompRequest(RenderRequest request, RenderResponse response) throws Exception { 87 compView.include(request, response); 88 } 89 90 protected void renderListRequest(RenderRequest request, RenderResponse response) throws Exception { 91 ManagementContextMBean management = getManagementContext(); 92 SortedMap components = new TreeMap (); 93 ObjectName [] 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 [] 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 } 141 | Popular Tags |