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 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     * Default Bootstrap class.
027     * 
028     * This is a default implementation of the Bootstrap,  it is used by the 
029     * Maven JBI plugin to provide a standard implementation of a Bootstrap
030     * when a component does not provide one
031     * 
032     * @deprecated Due to JBI classloader mechanism, component should not
033     *    use this class directly, but copy it, or rely on the maven-jbi-plugin
034     *    to provide a default implementation.
035     */
036    public class DefaultBootstrap implements Bootstrap
037    {
038    
039        protected InstallationContext context;
040        protected ObjectName mbeanName;
041        
042        public DefaultBootstrap() {
043        }
044        
045        public ObjectName getExtensionMBeanName() {
046            return mbeanName;
047        }
048    
049        protected Object getExtensionMBean() throws Exception {
050            return null;
051        }
052        
053        protected ObjectName createExtensionMBeanName() throws Exception {
054            return this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
055        }
056    
057        /* (non-Javadoc)
058         * @see javax.jbi.component.Bootstrap#init(javax.jbi.component.InstallationContext)
059         */
060        public void init(InstallationContext installContext) throws JBIException {
061            try {
062                this.context = installContext;
063                doInit();
064            } catch (JBIException e) {
065                throw e;
066            } catch (Exception e) {
067                throw new JBIException("Error calling init", e);
068            }
069        }
070    
071        protected void doInit() throws Exception {
072            Object mbean = getExtensionMBean();
073            if (mbean != null) {
074                this.mbeanName = createExtensionMBeanName();
075                MBeanServer server = this.context.getContext().getMBeanServer();
076                if (server == null) {
077                    throw new JBIException("null mBeanServer");
078                }
079                if (server.isRegistered(this.mbeanName)) {
080                    server.unregisterMBean(this.mbeanName);
081                }
082                server.registerMBean(mbean, this.mbeanName);
083            }
084        }
085        
086        /* (non-Javadoc)
087         * @see javax.jbi.component.Bootstrap#cleanUp()
088         */
089        public void cleanUp() throws JBIException {
090            try {
091                doCleanUp();
092            } catch (JBIException e) {
093                throw e;
094            } catch (Exception e) {
095                throw new JBIException("Error calling cleanUp", e);
096            }
097        }
098    
099        protected void doCleanUp() throws Exception {
100            if (this.mbeanName != null) {
101                MBeanServer server = this.context.getContext().getMBeanServer();
102                if (server == null) {
103                    throw new JBIException("null mBeanServer");
104                }
105                if (server.isRegistered(this.mbeanName)) {
106                    server.unregisterMBean(this.mbeanName);
107                }
108            }
109        }
110    
111        /* (non-Javadoc)
112         * @see javax.jbi.component.Bootstrap#onInstall()
113         */
114        public void onInstall() throws JBIException {
115            try {
116                doOnInstall();
117            } catch (JBIException e) {
118                throw e;
119            } catch (Exception e) {
120                throw new JBIException("Error calling onInstall", e);
121            }
122        }
123    
124        protected void doOnInstall() throws Exception {
125        }
126        
127        /* (non-Javadoc)
128         * @see javax.jbi.component.Bootstrap#onUninstall()
129         */
130        public void onUninstall() throws JBIException {
131            try {
132                doOnUninstall();
133            } catch (JBIException e) {
134                throw e;
135            } catch (Exception e) {
136                throw new JBIException("Error calling onUninstall", e);
137            }
138        }
139    
140        protected void doOnUninstall() throws Exception {
141        }
142        
143    }