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