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.web.controller;
018    
019    import javax.servlet.http.HttpServletRequest;
020    import javax.servlet.http.HttpServletResponse;
021    
022    import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
023    import org.springframework.web.servlet.ModelAndView;
024    import org.springframework.web.servlet.mvc.Controller;
025    
026    public class ComponentLifeCycle implements Controller {
027    
028        public static final String START = "start";
029        public static final String STOP = "stop";
030        public static final String SHUTDOWN = "shutdown";
031        public static final String UNINSTALL = "uninstall";
032        
033        private AdminCommandsServiceMBean adminCommandsService;
034        private String name;
035        private String view;
036        private String action;
037        
038        public ComponentLifeCycle(AdminCommandsServiceMBean adminCommandsService, String action) {
039            if (adminCommandsService == null) {
040                throw new IllegalArgumentException("adminCommandsServiceMBean is null");
041            }
042            if (action == null) {
043                throw new IllegalArgumentException("action is null");
044            } else if (!START.equals(action) && 
045                       !STOP.equals(action) && 
046                       !SHUTDOWN.equals(action) &&
047                       !UNINSTALL.equals(action)) {
048                throw new IllegalArgumentException("action must be start, stop, shutdown or uninstall");
049            }
050            
051            this.adminCommandsService = adminCommandsService;
052            this.action = action;
053        }
054        
055        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
056            if (START.equals(action)) {
057                adminCommandsService.startComponent(name);
058            } else if (STOP.equals(action)) {
059                adminCommandsService.stopComponent(name);
060            } else if (SHUTDOWN.equals(action)) {
061                adminCommandsService.shutdownComponent(name);
062            } else if (UNINSTALL.equals(action)) {
063                adminCommandsService.uninstallComponent(name);
064            }
065            return new ModelAndView(getView());
066        }
067    
068        public String getName() {
069            return name;
070        }
071    
072        public void setName(String name) {
073            this.name = name;
074        }
075    
076        public String getView() {
077            return view;
078        }
079    
080        public void setView(String view) {
081            this.view = view;
082        }
083    
084    }