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:
018: /*
019: * ChainBuilder ESB
020: * Visual Enterprise Integration
021: *
022: * Copyright (C) 2006 Bostech Corporation
023: *
024: * This program is free software; you can redistribute it and/or modify it
025: * under the terms of the GNU General Public License as published by the
026: * Free Software Foundation; either version 2 of the License, or (at your option)
027: * any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
031: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
032: * for more details.
033: *
034: * You should have received a copy of the GNU General Public License along with
035: * this program; if not, write to the Free Software Foundation, Inc.,
036: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
037: *
038: * $Id: CbServiceUnitManager.java 11995 2008-02-18 08:39:52Z lzheng $
039: */
040: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
041:
042: import javax.jbi.component.ServiceUnitManager;
043: import javax.jbi.management.DeploymentException;
044: import javax.jbi.management.LifeCycleMBean;
045:
046: import org.apache.commons.logging.Log;
047:
048: import com.bostechcorp.cbesb.common.util.ErrorUtil;
049: import com.bostechcorp.cbesb.runtime.ccsl.lib.CcslUtil;
050: import com.bostechcorp.cbesb.runtime.ccsl.lib.UpocInstance;
051:
052: /**
053: * A simple service unit manager.
054: * This service unit manager uses {@link IDeployer} objects
055: * to handle different type of service units.
056: *
057: * @author Guillaume Nodet
058: * @version $Revision: 426415 $
059: * @since 3.0
060: */
061: public class CbServiceUnitManager implements ServiceUnitManager {
062:
063: protected final transient Log logger;
064:
065: protected CbComponent component;
066:
067: protected IDeployer[] deployers;
068:
069: protected boolean persistent;
070:
071: public CbServiceUnitManager(CbComponent component,
072: IDeployer[] deployers) {
073: this (component, deployers, false);
074: }
075:
076: public CbServiceUnitManager(CbComponent component,
077: IDeployer[] deployers, boolean persistent) {
078: this .component = component;
079: this .logger = component.logger;
080: this .deployers = deployers;
081: this .persistent = persistent;
082: }
083:
084: /* (non-Javadoc)
085: * @see javax.jbi.component.ServiceUnitManager#deploy(java.lang.String, java.lang.String)
086: */
087: public synchronized String deploy(String serviceUnitName,
088: String serviceUnitRootPath) throws DeploymentException {
089: try {
090: if (logger.isDebugEnabled()) {
091: logger.debug("Deploying service unit");
092: }
093: if (serviceUnitName == null
094: || serviceUnitName.length() == 0) {
095: throw new IllegalArgumentException(
096: "serviceUnitName should be non null and non empty");
097: }
098: if (getServiceUnit(serviceUnitName) != null) {
099: throw failure("deploy", "Service Unit '"
100: + serviceUnitName + "' is already deployed",
101: null);
102: }
103: CbServiceUnit su = doDeploy(serviceUnitName,
104: serviceUnitRootPath);
105: if (su == null) {
106: throw failure("deploy",
107: "Unable to find suitable deployer for Service Unit '"
108: + serviceUnitName + "'", null);
109: }
110: component.getRegistry().registerServiceUnit(su);
111: if (logger.isDebugEnabled()) {
112: logger.debug("Service unit deployed");
113: }
114: return createSuccessMessage("deploy");
115: } catch (DeploymentException e) {
116: throw e;
117: } catch (Exception e) {
118: throw failure("deploy", "Unable to deploy service unit", e);
119: }
120: }
121:
122: protected CbServiceUnit doDeploy(String serviceUnitName,
123: String serviceUnitRootPath) throws Exception {
124: for (int i = 0; i < deployers.length; i++) {
125: if (deployers[i].canDeploy(serviceUnitName,
126: serviceUnitRootPath)) {
127: return deployers[i].deploy(serviceUnitName,
128: serviceUnitRootPath);
129: }
130: }
131: return null;
132: }
133:
134: /* (non-Javadoc)
135: * @see javax.jbi.component.ServiceUnitManager#init(java.lang.String, java.lang.String)
136: */
137: public synchronized void init(String serviceUnitName,
138: String serviceUnitRootPath) throws DeploymentException {
139: try {
140: if (logger.isDebugEnabled()) {
141: logger.debug("Initializing service unit");
142: }
143: if (serviceUnitName == null
144: || serviceUnitName.length() == 0) {
145: throw new IllegalArgumentException(
146: "serviceUnitName should be non null and non empty");
147: }
148: if (getServiceUnit(serviceUnitName) == null) {
149: if (!persistent) {
150: CbServiceUnit su = doDeploy(serviceUnitName,
151: serviceUnitRootPath);
152: if (su == null) {
153: throw failure("deploy",
154: "Unable to find suitable deployer for Service Unit '"
155: + serviceUnitName + "'", null);
156: }
157: component.getRegistry().registerServiceUnit(su);
158: } else {
159: throw failure("init", "Service Unit '"
160: + serviceUnitName + "' is not deployed",
161: null);
162: }
163: }
164: doInit(serviceUnitName, serviceUnitRootPath);
165: if (logger.isDebugEnabled()) {
166: logger.debug("Service unit initialized");
167: }
168: } catch (DeploymentException e) {
169: throw e;
170: } catch (Exception e) {
171: throw failure("init", "Unable to init service unit", e);
172: }
173: }
174:
175: protected void doInit(String serviceUnitName,
176: String serviceUnitRootPath) throws Exception {
177: }
178:
179: /* (non-Javadoc)
180: * @see javax.jbi.component.ServiceUnitManager#start(java.lang.String)
181: */
182: public synchronized void start(String serviceUnitName)
183: throws DeploymentException {
184: try {
185: if (logger.isDebugEnabled()) {
186: logger.debug("Starting service unit");
187: }
188: if (serviceUnitName == null
189: || serviceUnitName.length() == 0) {
190: throw new IllegalArgumentException(
191: "serviceUnitName should be non null and non empty");
192: }
193: CbServiceUnit su = (CbServiceUnit) getServiceUnit(serviceUnitName);
194: if (su == null) {
195: throw failure("start", "Service Unit '"
196: + serviceUnitName + "' is not deployed", null);
197: }
198: if (!LifeCycleMBean.STOPPED.equals(su.getCurrentState())
199: && !LifeCycleMBean.SHUTDOWN.equals(su
200: .getCurrentState())) {
201: throw failure(
202: "start",
203: "ServiceUnit should be in a SHUTDOWN or STOPPED state",
204: null);
205: }
206: su.start();
207: if (logger.isDebugEnabled()) {
208: logger.debug("Service unit started");
209: }
210: } catch (DeploymentException e) {
211: throw e;
212: } catch (Exception e) {
213: throw failure("start", "Unable to start service unit", e);
214: }
215: }
216:
217: /* (non-Javadoc)
218: * @see javax.jbi.component.ServiceUnitManager#stop(java.lang.String)
219: */
220: public synchronized void stop(String serviceUnitName)
221: throws DeploymentException {
222: try {
223: if (logger.isDebugEnabled()) {
224: logger.debug("Stopping service unit");
225: }
226: if (serviceUnitName == null
227: || serviceUnitName.length() == 0) {
228: throw new IllegalArgumentException(
229: "serviceUnitName should be non null and non empty");
230: }
231: CbServiceUnit su = (CbServiceUnit) getServiceUnit(serviceUnitName);
232: if (su == null) {
233: throw failure("stop", "Service Unit '"
234: + serviceUnitName + "' is not deployed", null);
235: }
236: if (!LifeCycleMBean.STARTED.equals(su.getCurrentState())) {
237: throw failure("stop",
238: "ServiceUnit should be in a SHUTDOWN state",
239: null);
240: }
241: su.stop();
242: if (logger.isDebugEnabled()) {
243: logger.debug("Service unit stopped");
244: }
245: } catch (DeploymentException e) {
246: throw e;
247: } catch (Exception e) {
248: throw failure("stop", "Unable to stop service unit", e);
249: }
250: }
251:
252: /* (non-Javadoc)
253: * @see javax.jbi.component.ServiceUnitManager#shutDown(java.lang.String)
254: */
255: public synchronized void shutDown(String serviceUnitName)
256: throws DeploymentException {
257: try {
258: if (logger.isDebugEnabled()) {
259: logger.debug("Shutting down service unit");
260: }
261: if (serviceUnitName == null
262: || serviceUnitName.length() == 0) {
263: throw new IllegalArgumentException(
264: "serviceUnitName should be non null and non empty");
265: }
266: CbServiceUnit su = (CbServiceUnit) getServiceUnit(serviceUnitName);
267: if (su == null) {
268: throw failure("shutDown", "Service Unit '"
269: + serviceUnitName + "' is not deployed", null);
270: }
271: su.shutDown();
272: String suRootDir = su.getRootPath();
273: if (suRootDir != null) {
274: try {
275: UpocInstance.removeInstances(CcslUtil
276: .getRootScriptPath(suRootDir));
277: } catch (Exception e) {
278: ErrorUtil.printWarn(
279: "remove upoc instance eexception", e);
280: }
281: }
282:
283: if (logger.isDebugEnabled()) {
284: logger.debug("Service unit shut down");
285: }
286: } catch (DeploymentException e) {
287: throw e;
288: } catch (Exception e) {
289: throw failure("shutDown",
290: "Unable to shutdown service unit", e);
291: }
292: }
293:
294: /* (non-Javadoc)
295: * @see javax.jbi.component.ServiceUnitManager#undeploy(java.lang.String, java.lang.String)
296: */
297: public synchronized String undeploy(String serviceUnitName,
298: String serviceUnitRootPath) throws DeploymentException {
299: try {
300: if (logger.isDebugEnabled()) {
301: logger.debug("Undeploying service unit");
302: }
303: if (logger.isDebugEnabled()) {
304: logger.debug("Shutting down service unit");
305: }
306: if (serviceUnitName == null
307: || serviceUnitName.length() == 0) {
308: throw new IllegalArgumentException(
309: "serviceUnitName should be non null and non empty");
310: }
311: CbServiceUnit su = (CbServiceUnit) getServiceUnit(serviceUnitName);
312: if (su == null) {
313: throw failure("undeploy", "Service Unit '"
314: + serviceUnitName + "' is not deployed", null);
315: }
316: if (!LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) {
317: throw failure("undeploy",
318: "ServiceUnit should be in a SHUTDOWN state",
319: null);
320: }
321: doUndeploy(su);
322: component.getRegistry().unregisterServiceUnit(su);
323: if (logger.isDebugEnabled()) {
324: logger.debug("Service unit undeployed");
325: }
326: return createSuccessMessage("undeploy");
327: } catch (DeploymentException e) {
328: throw e;
329: } catch (Exception e) {
330: throw failure("undeploy",
331: "Unable to undeploy service unit", e);
332: }
333: }
334:
335: protected void doUndeploy(CbServiceUnit su) throws Exception {
336: for (int i = 0; i < deployers.length; i++) {
337: if (deployers[i].canDeploy(su.getName(), su.getRootPath())) {
338: deployers[i].undeploy(su);
339: return;
340: }
341: }
342: throw failure("undeploy",
343: "Unable to find suitable deployer for Service Unit '"
344: + su.getName() + "'", null);
345: }
346:
347: protected DeploymentException failure(String task, String info,
348: Exception e) throws DeploymentException {
349: TaskMessageUtil.Message msg = new TaskMessageUtil.Message();
350: msg.setComponent(component.getComponentName());
351: msg.setTask(task);
352: msg.setResult("FAILED");
353: msg.setType("ERROR");
354: msg.setException(e);
355: msg.setMessage(info);
356: return new DeploymentException(TaskMessageUtil
357: .createComponentMessage(msg));
358: }
359:
360: protected String createSuccessMessage(String task) {
361: TaskMessageUtil.Message msg = new TaskMessageUtil.Message();
362: msg.setComponent(component.getComponentName());
363: msg.setTask(task);
364: msg.setResult("SUCCESS");
365: return TaskMessageUtil.createComponentMessage(msg);
366: }
367:
368: protected CbServiceUnit getServiceUnit(String name) {
369: return component.getRegistry().getServiceUnit(name);
370: }
371:
372: }
|