001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: RemoteDeployer.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployer;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030:
031: import org.ow2.easybeans.api.EZBServer;
032: import org.ow2.easybeans.deployable.DeployerFactory;
033: import org.ow2.util.ee.deploy.api.archive.IArchive;
034: import org.ow2.util.ee.deploy.api.deployable.IDeployable;
035: import org.ow2.util.ee.deploy.api.deployer.DeployerException;
036: import org.ow2.util.ee.deploy.api.deployer.IDeployer;
037: import org.ow2.util.ee.deploy.impl.archive.ArchiveManager;
038: import org.ow2.util.ee.deploy.impl.helper.DeployableHelper;
039: import org.ow2.util.ee.deploy.impl.helper.DeployableHelperException;
040: import org.ow2.util.log.Log;
041: import org.ow2.util.log.LogFactory;
042:
043: /**
044: * Deployer allowing deploy/undeploy functions on this container.
045: * @author Florent Benoit
046: */
047: public final class RemoteDeployer implements IRemoteDeployer {
048:
049: /**
050: * Folder to create in tmp folder.
051: */
052: private static final String DEFAULT_FOLDER = "EasyBeans-"
053: + RemoteDeployer.class.getSimpleName();
054:
055: /**
056: * Logger.
057: */
058: private static Log logger = LogFactory.getLog(RemoteDeployer.class);
059:
060: /**
061: * Link to a local deployer.
062: */
063: private IDeployer deployer = null;
064:
065: /**
066: * Build a Deployer for the given Embedded instance.
067: * @param embedded the Server instance.
068: * @throws DeployerException if the local deployer can't be accessed.
069: */
070: public RemoteDeployer(final EZBServer embedded)
071: throws DeployerException {
072: // Get local deployer
073: this .deployer = DeployerFactory.getDeployer(embedded);
074: }
075:
076: /**
077: * Dump the given bytes to a local file and then return the path to this
078: * file.
079: * @param fileName the name of the file to deploy
080: * @param fileContent the content of the given file
081: * @return the path of the deployed file
082: */
083: public String dumpFile(final String fileName,
084: final byte[] fileContent) {
085: logger
086: .info(
087: "Dump file to the local filesystem with the name = ''{0}''.",
088: fileName);
089:
090: // Dump the file on a temporary file
091: File rootFolder = new File(System.getProperty("java.io.tmpdir")
092: + File.separator + DEFAULT_FOLDER);
093: rootFolder.mkdirs();
094:
095: // Create file in this folder
096: File file = new File(rootFolder, fileName);
097:
098: // Dump the content on this file
099: FileOutputStream out = null;
100: try {
101: out = new FileOutputStream(file);
102: } catch (FileNotFoundException e) {
103: throw new IllegalStateException(
104: "Cannot build an outputstream on file '" + file
105: + "'.", e);
106: }
107:
108: // Write the array of bytes
109: try {
110: out.write(fileContent);
111: } catch (IOException e) {
112: throw new IllegalStateException(
113: "Cannot write byte in outputstream", e);
114: }
115:
116: // Close resource
117: try {
118: out.close();
119: } catch (IOException e) {
120: throw new IllegalStateException("Cannot close outpustream",
121: e);
122: }
123:
124: // return the local dump of the file
125: return file.getPath();
126: }
127:
128: /**
129: * Dump the given bytes to a local file and then deploy this file to a local deployer.
130: * @param fileName the name of the file to deploy
131: * @param fileContent the content of the given file
132: */
133: public void deployFile(final String fileName,
134: final byte[] fileContent) {
135:
136: // Deploy this local file
137: deploy(dumpFile(fileName, fileContent));
138: }
139:
140: /**
141: * Deploy a file to a local deployer.
142: * @param fileName the name of the file to deploy
143: */
144: public void deploy(final String fileName) {
145:
146: logger.info("Deploying ''{0}''", fileName);
147:
148: IDeployable deployable = getDeployable(fileName);
149:
150: try {
151: deployer.deploy(deployable);
152: } catch (DeployerException e) {
153: logger.error("Cannot deploy the deployable ''{0}''",
154: deployable, e);
155: throw new RuntimeException("Cannot deploy the deployable '"
156: + deployable + "' : " + e.getMessage());
157: }
158: }
159:
160: /**
161: * Gets a deployable for a given file.
162: * @param fileName the name of the file
163: * @return a deployable for the given filename
164: */
165: protected IDeployable getDeployable(final String fileName) {
166: // Get File
167: File file = new File(fileName);
168:
169: // check file
170: if (!file.exists()) {
171: throw new RuntimeException("The file '" + fileName
172: + "' is not present on the filesystem.");
173: }
174:
175: // Else, get the deployable
176: IArchive archive = ArchiveManager.getInstance()
177: .getArchive(file);
178: if (archive == null) {
179: logger.error(
180: "No archive found for the invalid file ''{0}''",
181: file);
182: throw new RuntimeException(
183: "No archive found for the invalid file '" + file
184: + "'.");
185: }
186: IDeployable deployable;
187: try {
188: deployable = DeployableHelper.getDeployable(archive);
189: } catch (DeployableHelperException e) {
190: logger.error(
191: "Cannot get a deployable for the archive ''{0}''",
192: archive, e);
193: throw new RuntimeException(
194: "Cannot get a deployable for the archive '"
195: + archive + "' : " + e.getMessage());
196: }
197:
198: return deployable;
199: }
200:
201: /**
202: * Undeploy a file by using a local deployer.
203: * @param fileName the name of the file to undeploy
204: */
205: public void undeploy(final String fileName) {
206: logger.info("Undeploying ''{0}''", fileName);
207:
208: IDeployable deployable = getDeployable(fileName);
209:
210: try {
211: deployer.undeploy(deployable);
212: } catch (DeployerException e) {
213: logger.error("Cannot undeploy the deployable ''{0}''",
214: deployable, e);
215: throw new RuntimeException(
216: "Cannot undeploy the deployable '" + deployable
217: + "' : " + e.getMessage());
218: }
219: }
220:
221: }
|