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