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