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: 665820 $
037     * @deprecated
038     * @since 3.0
039     */
040    @Deprecated
041    public class BaseBootstrap implements Bootstrap {
042    
043        protected final transient Log logger = LogFactory.getLog(getClass());
044        
045        protected InstallationContext context;
046        protected ObjectName mbeanName;
047        
048        public BaseBootstrap() {
049        }
050        
051        public ObjectName getExtensionMBeanName() {
052            return mbeanName;
053        }
054    
055        protected Object getExtensionMBean() throws Exception {
056            return null;
057        }
058        
059        protected ObjectName createExtensionMBeanName() throws Exception {
060            return this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
061        }
062    
063        /* (non-Javadoc)
064         * @see javax.jbi.component.Bootstrap#init(javax.jbi.component.InstallationContext)
065         */
066        public void init(InstallationContext installContext) throws JBIException {
067            try {
068                if (logger.isDebugEnabled()) {
069                    logger.debug("Initializing bootstrap");
070                }
071                this.context = installContext;
072                doInit();
073                if (logger.isDebugEnabled()) {
074                    logger.debug("Bootstrap initialized");
075                }
076            } catch (JBIException e) {
077                throw e;
078            } catch (Exception e) {
079                throw new JBIException("Error calling init", e);
080            }
081        }
082    
083        protected void doInit() throws Exception {
084            Object mbean = getExtensionMBean();
085            if (mbean != null) {
086                this.mbeanName = createExtensionMBeanName();
087                MBeanServer server = this.context.getContext().getMBeanServer();
088                if (server == null) {
089                    throw new JBIException("null mBeanServer");
090                }
091                if (server.isRegistered(this.mbeanName)) {
092                    server.unregisterMBean(this.mbeanName);
093                }
094                server.registerMBean(mbean, this.mbeanName);
095            }
096        }
097        
098        /* (non-Javadoc)
099         * @see javax.jbi.component.Bootstrap#cleanUp()
100         */
101        public void cleanUp() throws JBIException {
102            try {
103                if (logger.isDebugEnabled()) {
104                    logger.debug("Cleaning up bootstrap");
105                }
106                doCleanUp();
107                if (logger.isDebugEnabled()) {
108                    logger.debug("Bootstrap cleaned up");
109                }
110            } catch (JBIException e) {
111                throw e;
112            } catch (Exception e) {
113                throw new JBIException("Error calling cleanUp", e);
114            }
115        }
116    
117        protected void doCleanUp() throws Exception {
118            if (this.mbeanName != null) {
119                MBeanServer server = this.context.getContext().getMBeanServer();
120                if (server == null) {
121                    throw new JBIException("null mBeanServer");
122                }
123                if (server.isRegistered(this.mbeanName)) {
124                    server.unregisterMBean(this.mbeanName);
125                }
126            }
127        }
128    
129        /* (non-Javadoc)
130         * @see javax.jbi.component.Bootstrap#onInstall()
131         */
132        public void onInstall() throws JBIException {
133            try {
134                if (logger.isDebugEnabled()) {
135                    logger.debug("Bootstrap onInstall");
136                }
137                doOnInstall();
138                if (logger.isDebugEnabled()) {
139                    logger.debug("Bootstrap onInstall done");
140                }
141            } catch (JBIException e) {
142                throw e;
143            } catch (Exception e) {
144                throw new JBIException("Error calling onInstall", e);
145            }
146        }
147    
148        protected void doOnInstall() throws Exception {
149        }
150        
151        /* (non-Javadoc)
152         * @see javax.jbi.component.Bootstrap#onUninstall()
153         */
154        public void onUninstall() throws JBIException {
155            try {
156                if (logger.isDebugEnabled()) {
157                    logger.debug("Bootstrap onUninstall");
158                }
159                doOnUninstall();
160                if (logger.isDebugEnabled()) {
161                    logger.debug("Bootstrap onUninstall done");
162                }
163            } catch (JBIException e) {
164                throw e;
165            } catch (Exception e) {
166                throw new JBIException("Error calling onUninstall", e);
167            }
168        }
169    
170        protected void doOnUninstall() throws Exception {
171        }
172        
173    }