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 org.apache.servicemix.jbi.management.ManagementContextMBean;
020    
021    import javax.jbi.management.LifeCycleMBean;
022    import javax.management.ObjectName;
023    import javax.portlet.ActionRequest;
024    import javax.portlet.ActionResponse;
025    import javax.portlet.RenderRequest;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    
031    public class JBIContainerPortlet extends ServiceMixPortlet {
032    
033        public static class ServiceInfo {
034            private String name;
035            private String description;
036            private String state;
037            
038            public String getDescription() {
039                return description;
040            }
041            public void setDescription(String description) {
042                this.description = description;
043            }
044            public String getName() {
045                return name;
046            }
047            public void setName(String name) {
048                this.name = name;
049            }
050            public String getState() {
051                return state;
052            }
053            public void setState(String state) {
054                this.state = state;
055            }
056        }
057        
058        protected void fillViewRequest(RenderRequest request) throws Exception {
059            LifeCycleMBean container = getJBIContainer();
060            ManagementContextMBean management = getManagementContext();
061            request.setAttribute("state", container.getCurrentState());
062            request.setAttribute("info", management.getSystemInfo());
063            ObjectName[] services = management.getSystemServices();
064            List infos = new ArrayList();
065            for (int i = 0; i < services.length; i++) {
066                ServiceInfo info = new ServiceInfo();
067                info.name =  getAttribute(services[i], "name");
068                info.description =  getAttribute(services[i], "description");
069                info.state =  getAttribute(services[i], "currentState");
070                infos.add(info);
071            }
072            request.setAttribute("services", infos);
073        }
074        
075        protected String getAttribute(ObjectName name, String attribute) {
076            try {
077                return (String) getServerConnection().getAttribute(name, attribute);
078            } catch (Exception e) {
079                log.error("Could not retrieve attribute '" + attribute + "' for mbean '" + name + "'");
080                return null;
081            }
082        }
083    
084        protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
085            String action = actionRequest.getParameter("action");
086            String name   = actionRequest.getParameter("name");
087            System.err.println("doProcessAction: " + action + " for " + name);
088            ManagementContextMBean management = getManagementContext();
089            ObjectName service = management.getSystemService(getContainerName() + "." + name);
090            getServerConnection().invoke(service, action, new Object[0], new String[0]);
091        }
092    }