001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id:$
023: */
024: package com.bostechcorp.cbesb.runtime.component.bootstrap;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.Collection;
029: import java.util.Enumeration;
030: import java.util.Iterator;
031: import java.util.zip.ZipEntry;
032: import java.util.zip.ZipFile;
033:
034: import javax.jbi.management.DeploymentException;
035:
036: import com.bostechcorp.cbesb.common.util.ErrorUtil;
037: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
038: import com.bostechcorp.cbesb.common.util.FileUtil;
039: import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;
040: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbComponent;
041: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbServiceUnitManager;
042:
043: /**
044: * The installer service unit manager copies support files into the project workspace directory
045: * tree and adds entries into the runtime class path for the project.
046: *
047: * The installer does not handle any messages, everything happens in deploy/undeploy.
048: */
049: public class BootstrapServiceUnitManager extends CbServiceUnitManager {
050:
051: public BootstrapServiceUnitManager(CbComponent component) {
052: super (component, null, false);
053: }
054:
055: /* (non-Javadoc)
056: * @see javax.jbi.component.ServiceUnitManager#deploy(java.lang.String, java.lang.String)
057: */
058: public synchronized String deploy(String serviceUnitName,
059: String serviceUnitRootPath) throws DeploymentException {
060: try {
061: if (logger.isDebugEnabled()) {
062: logger.debug("Deploying service unit");
063: }
064: if (serviceUnitName == null
065: || serviceUnitName.length() == 0) {
066: throw new IllegalArgumentException(
067: "serviceUnitName should be non null and non empty");
068: }
069: if (getServiceUnit(serviceUnitName) != null) {
070: throw failure("deploy", "Service Unit '"
071: + serviceUnitName + "' is already deployed",
072: null);
073: }
074: // Expand zip files into the workspace directory tree
075: String ws = EsbPathHelper.getCbesbUiWorkSpace();
076: File targetDir = new File(ws);
077: File file = new File(serviceUnitRootPath);
078: if (file.exists() && file.isDirectory()) {
079: Collection<File> fileList = FileUtil.listFiles(file,
080: null, false);
081: for (Iterator it = fileList.iterator(); it.hasNext();) {
082: File f = (File) it.next();
083: if (f.getName().startsWith("ProjDef_")
084: && f.getName().endsWith(".zip")) {
085: FileUtil.unpackArchiveCheckOverwrite(f,
086: targetDir);
087: }
088: }
089: }
090: if (logger.isDebugEnabled()) {
091: logger.debug("Service unit deployed");
092: }
093: return createSuccessMessage("deploy");
094: } catch (DeploymentException e) {
095:
096: ErrorUtil.printError("Exception in deploy(): ", e);
097: throw e;
098: } catch (Exception e) {
099:
100: ErrorUtil.printError("Exception in deploy(): ", e);
101: throw failure("deploy", "Unable to deploy service unit", e);
102: }
103: }
104:
105: /* (non-Javadoc)
106: * @see javax.jbi.component.ServiceUnitManager#init(java.lang.String, java.lang.String)
107: */
108: public synchronized void init(String serviceUnitName,
109: String serviceUnitRootPath) throws DeploymentException {
110: if (logger.isDebugEnabled())
111: logger.debug("Initializing service unit");
112: // Add to runtime class path
113: try {
114: addClasspaths(serviceUnitName, serviceUnitRootPath);
115:
116: } catch (Exception ioe) {
117: throw new DeploymentException(ioe);
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see javax.jbi.component.ServiceUnitManager#start(java.lang.String)
123: */
124: public synchronized void start(String serviceUnitName)
125: throws DeploymentException {
126: if (logger.isDebugEnabled())
127: logger.debug("Starting service unit");
128:
129: }
130:
131: /* (non-Javadoc)
132: * @see javax.jbi.component.ServiceUnitManager#stop(java.lang.String)
133: */
134: public synchronized void stop(String serviceUnitName)
135: throws DeploymentException {
136: if (logger.isDebugEnabled()) {
137: logger.debug("Stopping service unit");
138: }
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.jbi.component.ServiceUnitManager#shutDown(java.lang.String)
143: */
144: public synchronized void shutDown(String serviceUnitName)
145: throws DeploymentException {
146: if (logger.isDebugEnabled()) {
147: logger.debug("Shutting down service unit");
148: }
149:
150: }
151:
152: /* (non-Javadoc)
153: * Extract the SA name from the SU name using naming convention
154: * the SA name is everything up to the first underscore
155: */
156: private String getSaNameFromSuName(String suName) {
157: String saName = null;
158: int underscorePosition = suName.indexOf("_");
159: if (underscorePosition > 1)
160: saName = suName.substring(0, underscorePosition);
161: return saName;
162: }
163:
164: /* (non-Javadoc)
165: * @see javax.jbi.component.ServiceUnitManager#undeploy(java.lang.String, java.lang.String)
166: */
167: public synchronized String undeploy(String serviceUnitName,
168: String serviceUnitRootPath) throws DeploymentException {
169: try {
170: if (logger.isDebugEnabled()) {
171: logger.debug("Undeploying service unit");
172: }
173: if (serviceUnitName == null
174: || serviceUnitName.length() == 0) {
175: throw new IllegalArgumentException(
176: "serviceUnitName should be non null and non empty");
177: }
178: // remove SA specific class path entries
179: String saName = getSaNameFromSuName(serviceUnitName);
180: if (saName != null)
181: RuntimeClassLoader.clearSaPath(saName);
182: return createSuccessMessage("undeploy");
183: } catch (Exception e) {
184: ErrorUtil
185: .printError("Unable to undeploy service unit: ", e);
186: throw failure("undeploy",
187: "Unable to undeploy service unit", e);
188: }
189: }
190:
191: /* (non-Javadoc)
192: * Add runtime class path entries
193: */
194: private void addClasspaths(String serviceUnitName,
195: String serviceUnitRootPath) throws IOException {
196: logger.debug("addClassPaths(" + serviceUnitName + ", "
197: + serviceUnitRootPath + ")");
198:
199: if (serviceUnitName != null && serviceUnitName.length() > 0
200: && serviceUnitRootPath != null
201: && serviceUnitRootPath.length() > 0) {
202:
203: String saName = getSaNameFromSuName(serviceUnitName);
204: if (saName != null) {
205: String ws = EsbPathHelper.getCbesbUiWorkSpace();
206: File baseDir = new File(ws);
207: File file = new File(serviceUnitRootPath);
208: if (file.exists() && file.isDirectory()) {
209: File sazip = null;
210: File esbzip = null;
211: Collection<File> fileList = FileUtil.listFiles(
212: file, null, false);
213: for (Iterator it = fileList.iterator(); it
214: .hasNext();) {
215: File f = (File) it.next();
216: if (f.getName().equals("ProjDef_SA.zip")) {
217: sazip = f;
218: } else if (f.getName().startsWith("ProjDef_")
219: && f.getName().endsWith(".zip")) {
220: esbzip = f;
221: }
222: }
223: if (sazip != null)
224: addToPath(sazip, baseDir, saName);
225: if (esbzip != null)
226: addToPath(esbzip, baseDir, saName);
227: }
228:
229: }
230: }
231: }
232:
233: private void addToPath(File f, File baseDir, String saName)
234: throws IOException {
235: ZipFile zipFile = new ZipFile(f);
236: for (Enumeration entries = zipFile.entries(); entries
237: .hasMoreElements();) {
238: ZipEntry entry = (ZipEntry) entries.nextElement();
239: if (entry.getName().endsWith(".jar")) {
240: String newPathEntry = baseDir.getAbsolutePath()
241: + File.separator + entry.getName();
242: RuntimeClassLoader.addToPath(saName, newPathEntry);
243: }
244: }
245:
246: }
247:
248: }
|