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.common;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    import javax.jbi.JBIException;
023    import javax.jbi.component.Bootstrap;
024    import javax.jbi.component.InstallationContext;
025    import javax.management.MBeanServer;
026    import javax.management.ObjectName;
027    
028    /**
029     * Base class for components bootstrap.
030     * Due to classloading mechanism in JBI, Shared Libraries are
031     * not available at bootstrap time, so this class should be
032     * copied in your own component and modified directly, instead
033     * of inheriting it.
034     * 
035     * @author Guillaume Nodet
036     * @version $Revision: 434069 $
037     * @deprecated
038     * @since 3.0
039     */
040    public class BaseBootstrap implements Bootstrap {
041    
042        protected final transient Log logger = LogFactory.getLog(getClass());
043        
044        protected InstallationContext context;
045        protected ObjectName mbeanName;
046        
047        public BaseBootstrap() {
048        }
049        
050        public ObjectName getExtensionMBeanName() {
051            return mbeanName;
052        }
053    
054        protected Object getExtensionMBean() throws Exception {
055            return null;
056        }
057        
058        protected ObjectName createExtensionMBeanName() throws Exception {
059            return this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
060        }
061    
062        /* (non-Javadoc)
063         * @see javax.jbi.component.Bootstrap#init(javax.jbi.component.InstallationContext)
064         */
065        public void init(InstallationContext installContext) throws JBIException {
066            try {
067                if (logger.isDebugEnabled()) {
068                    logger.debug("Initializing bootstrap");
069                }
070                this.context = installContext;
071                doInit();
072                if (logger.isDebugEnabled()) {
073                    logger.debug("Bootstrap initialized");
074                }
075            } catch (JBIException e) {
076                throw e;
077            } catch (Exception e) {
078                throw new JBIException("Error calling init", e);
079            }
080        }
081    
082        protected void doInit() throws Exception {
083            Object mbean = getExtensionMBean();
084            if (mbean != null) {
085                this.mbeanName = createExtensionMBeanName();
086                MBeanServer server = this.context.getContext().getMBeanServer();
087                if (server == null) {
088                    throw new JBIException("null mBeanServer");
089                }
090                if (server.isRegistered(this.mbeanName)) {
091                    server.unregisterMBean(this.mbeanName);
092                }
093                server.registerMBean(mbean, this.mbeanName);
094            }
095        }
096        
097        /* (non-Javadoc)
098         * @see javax.jbi.component.Bootstrap#cleanUp()
099         */
100        public void cleanUp() throws JBIException {
101            try {
102                if (logger.isDebugEnabled()) {
103                    logger.debug("Cleaning up bootstrap");
104                }
105                doCleanUp();
106                if (logger.isDebugEnabled()) {
107                    logger.debug("Bootstrap cleaned up");
108                }
109            } catch (JBIException e) {
110                throw e;
111            } catch (Exception e) {
112                throw new JBIException("Error calling cleanUp", e);
113            }
114        }
115    
116        protected void doCleanUp() throws Exception {
117            if (this.mbeanName != null) {
118                MBeanServer server = this.context.getContext().getMBeanServer();
119                if (server == null) {
120                    throw new JBIException("null mBeanServer");
121                }
122                if (server.isRegistered(this.mbeanName)) {
123                    server.unregisterMBean(this.mbeanName);
124                }
125            }
126        }
127    
128        /* (non-Javadoc)
129         * @see javax.jbi.component.Bootstrap#onInstall()
130         */
131        public void onInstall() throws JBIException {
132            try {
133                if (logger.isDebugEnabled()) {
134                    logger.debug("Bootstrap onInstall");
135                }
136                doOnInstall();
137                if (logger.isDebugEnabled()) {
138                    logger.debug("Bootstrap onInstall done");
139                }
140            } catch (JBIException e) {
141                throw e;
142            } catch (Exception e) {
143                throw new JBIException("Error calling onInstall", e);
144            }
145        }
146    
147        protected void doOnInstall() throws Exception {
148        }
149        
150        /* (non-Javadoc)
151         * @see javax.jbi.component.Bootstrap#onUninstall()
152         */
153        public void onUninstall() throws JBIException {
154            try {
155                if (logger.isDebugEnabled()) {
156                    logger.debug("Bootstrap onUninstall");
157                }
158                doOnUninstall();
159                if (logger.isDebugEnabled()) {
160                    logger.debug("Bootstrap onUninstall done");
161                }
162            } catch (JBIException e) {
163                throw e;
164            } catch (Exception e) {
165                throw new JBIException("Error calling onUninstall", e);
166            }
167        }
168    
169        protected void doOnUninstall() throws Exception {
170        }
171        
172    }