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.bpmscript.jbi.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: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: * Bootstrap class.
030: * This class is usefull to perform tasks at installation / uninstallation time
031: */
032: public class BpmScriptBootstrap implements Bootstrap {
033:
034: protected final transient Log logger = LogFactory
035: .getLog(getClass());
036:
037: protected InstallationContext context;
038: protected ObjectName mbeanName;
039:
040: public ObjectName getExtensionMBeanName() {
041: return mbeanName;
042: }
043:
044: protected Object getExtensionMBean() throws Exception {
045: return null;
046: }
047:
048: protected ObjectName createExtensionMBeanName() throws Exception {
049: return this .context.getContext().getMBeanNames()
050: .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)
057: 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()
079: .getMBeanServer();
080: if (server == null) {
081: throw new JBIException("null mBeanServer");
082: }
083: if (server.isRegistered(this .mbeanName)) {
084: server.unregisterMBean(this .mbeanName);
085: }
086: server.registerMBean(mbean, this .mbeanName);
087: }
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.jbi.component.Bootstrap#cleanUp()
092: */
093: public void cleanUp() throws JBIException {
094: try {
095: if (logger.isDebugEnabled()) {
096: logger.debug("Cleaning up bootstrap");
097: }
098: doCleanUp();
099: if (logger.isDebugEnabled()) {
100: logger.debug("Bootstrap cleaned up");
101: }
102: } catch (JBIException e) {
103: throw e;
104: } catch (Exception e) {
105: throw new JBIException("Error calling cleanUp", e);
106: }
107: }
108:
109: protected void doCleanUp() throws Exception {
110: if (this .mbeanName != null) {
111: MBeanServer server = this .context.getContext()
112: .getMBeanServer();
113: if (server == null) {
114: throw new JBIException("null mBeanServer");
115: }
116: if (server.isRegistered(this .mbeanName)) {
117: server.unregisterMBean(this .mbeanName);
118: }
119: }
120: }
121:
122: /* (non-Javadoc)
123: * @see javax.jbi.component.Bootstrap#onInstall()
124: */
125: public void onInstall() throws JBIException {
126: try {
127: if (logger.isDebugEnabled()) {
128: logger.debug("Bootstrap onInstall");
129: }
130: doOnInstall();
131: if (logger.isDebugEnabled()) {
132: logger.debug("Bootstrap onInstall done");
133: }
134: } catch (JBIException e) {
135: throw e;
136: } catch (Exception e) {
137: throw new JBIException("Error calling onInstall", e);
138: }
139: }
140:
141: protected void doOnInstall() throws Exception {
142: }
143:
144: /* (non-Javadoc)
145: * @see javax.jbi.component.Bootstrap#onUninstall()
146: */
147: public void onUninstall() throws JBIException {
148: try {
149: if (logger.isDebugEnabled()) {
150: logger.debug("Bootstrap onUninstall");
151: }
152: doOnUninstall();
153: if (logger.isDebugEnabled()) {
154: logger.debug("Bootstrap onUninstall done");
155: }
156: } catch (JBIException e) {
157: throw e;
158: } catch (Exception e) {
159: throw new JBIException("Error calling onUninstall", e);
160: }
161: }
162:
163: protected void doOnUninstall() throws Exception {
164: }
165:
166: }
|