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