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