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.lwcontainer;
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 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027
028 /**
029 * Base class for components bootstrap.
030 *
031 * @author Guillaume Nodet
032 * @version $Revision: 34165 $
033 * @since 3.0
034 */
035 public class LwContainerBootstrap implements Bootstrap {
036
037 protected final transient Log logger = LogFactory.getLog(getClass());
038
039 protected InstallationContext context;
040 protected ObjectName mbeanName;
041
042 public LwContainerBootstrap() {
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 if (logger.isDebugEnabled()) {
063 logger.debug("Initializing bootstrap");
064 }
065 this.context = installContext;
066 doInit();
067 if (logger.isDebugEnabled()) {
068 logger.debug("Bootstrap initialized");
069 }
070 } catch (JBIException e) {
071 throw e;
072 } catch (Exception e) {
073 throw new JBIException("Error calling init", e);
074 }
075 }
076
077 protected void doInit() throws Exception {
078 Object mbean = getExtensionMBean();
079 if (mbean != null) {
080 this.mbeanName = createExtensionMBeanName();
081 MBeanServer server = this.context.getContext().getMBeanServer();
082 if (server == null) {
083 throw new JBIException("null mBeanServer");
084 }
085 if (server.isRegistered(this.mbeanName)) {
086 server.unregisterMBean(this.mbeanName);
087 }
088 server.registerMBean(mbean, this.mbeanName);
089 }
090 }
091
092 /* (non-Javadoc)
093 * @see javax.jbi.component.Bootstrap#cleanUp()
094 */
095 public void cleanUp() throws JBIException {
096 try {
097 if (logger.isDebugEnabled()) {
098 logger.debug("Cleaning up bootstrap");
099 }
100 doCleanUp();
101 if (logger.isDebugEnabled()) {
102 logger.debug("Bootstrap cleaned up");
103 }
104 } catch (JBIException e) {
105 throw e;
106 } catch (Exception e) {
107 throw new JBIException("Error calling cleanUp", e);
108 }
109 }
110
111 protected void doCleanUp() throws Exception {
112 if (this.mbeanName != null) {
113 MBeanServer server = this.context.getContext().getMBeanServer();
114 if (server == null) {
115 throw new JBIException("null mBeanServer");
116 }
117 if (server.isRegistered(this.mbeanName)) {
118 server.unregisterMBean(this.mbeanName);
119 }
120 }
121 }
122
123 /* (non-Javadoc)
124 * @see javax.jbi.component.Bootstrap#onInstall()
125 */
126 public void onInstall() throws JBIException {
127 try {
128 if (logger.isDebugEnabled()) {
129 logger.debug("Bootstrap onInstall");
130 }
131 doOnInstall();
132 if (logger.isDebugEnabled()) {
133 logger.debug("Bootstrap onInstall done");
134 }
135 } catch (JBIException e) {
136 throw e;
137 } catch (Exception e) {
138 throw new JBIException("Error calling onInstall", e);
139 }
140 }
141
142 protected void doOnInstall() throws Exception {
143 }
144
145 /* (non-Javadoc)
146 * @see javax.jbi.component.Bootstrap#onUninstall()
147 */
148 public void onUninstall() throws JBIException {
149 try {
150 if (logger.isDebugEnabled()) {
151 logger.debug("Bootstrap onUninstall");
152 }
153 doOnUninstall();
154 if (logger.isDebugEnabled()) {
155 logger.debug("Bootstrap onUninstall done");
156 }
157 } catch (JBIException e) {
158 throw e;
159 } catch (Exception e) {
160 throw new JBIException("Error calling onUninstall", e);
161 }
162 }
163
164 protected void doOnUninstall() throws Exception {
165 }
166
167 }