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.http;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.Bootstrap;
021    import javax.jbi.component.InstallationContext;
022    import javax.management.MBeanServer;
023    import javax.management.ObjectName;
024    
025    /**
026     * Base class for components bootstrap.
027     * 
028     * @author Guillaume Nodet
029     * @version $Revision: 533074 $
030     * @since 3.0
031     */
032    public class HttpBootstrap implements Bootstrap {
033    
034        protected InstallationContext context;
035        protected ObjectName mbeanName;
036        protected HttpConfiguration configuration;
037    
038        public HttpBootstrap() {
039        }
040    
041        public ObjectName getExtensionMBeanName() {
042            return mbeanName;
043        }
044    
045        /*
046         * (non-Javadoc)
047         * 
048         * @see javax.jbi.component.Bootstrap#init(javax.jbi.component.InstallationContext)
049         */
050        public void init(InstallationContext installContext) throws JBIException {
051            try {
052                this.context = installContext;
053                doInit();
054            } catch (JBIException e) {
055                throw e;
056            } catch (Exception e) {
057                throw new JBIException("Error calling init", e);
058            }
059        }
060    
061        protected void doInit() throws Exception {
062            configuration = new HttpConfiguration();
063            configuration.setRootDir(this.context.getInstallRoot());
064            configuration.setComponentName(this.context.getComponentName());
065            configuration.load();
066            mbeanName = this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
067            MBeanServer server = this.context.getContext().getMBeanServer();
068            if (server == null) {
069                throw new JBIException("null mBeanServer");
070            }
071            if (server.isRegistered(this.mbeanName)) {
072                server.unregisterMBean(this.mbeanName);
073            }
074            server.registerMBean(configuration, this.mbeanName);
075        }
076    
077        /*
078         * (non-Javadoc)
079         * 
080         * @see javax.jbi.component.Bootstrap#cleanUp()
081         */
082        public void cleanUp() throws JBIException {
083            try {
084                doCleanUp();
085            } catch (JBIException e) {
086                throw e;
087            } catch (Exception e) {
088                throw new JBIException("Error calling cleanUp", e);
089            }
090        }
091    
092        protected void doCleanUp() throws Exception {
093            if (this.mbeanName != null) {
094                MBeanServer server = this.context.getContext().getMBeanServer();
095                if (server == null) {
096                    throw new JBIException("null mBeanServer");
097                }
098                if (server.isRegistered(this.mbeanName)) {
099                    server.unregisterMBean(this.mbeanName);
100                }
101            }
102        }
103    
104        /*
105         * (non-Javadoc)
106         * 
107         * @see javax.jbi.component.Bootstrap#onInstall()
108         */
109        public void onInstall() throws JBIException {
110        }
111    
112        /*
113         * (non-Javadoc)
114         * 
115         * @see javax.jbi.component.Bootstrap#onUninstall()
116         */
117        public void onUninstall() throws JBIException {
118        }
119    
120    }