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: */package org.apache.openejb.config;
017:
018: import static org.apache.openejb.util.JarExtractor.delete;
019:
020: import java.util.Properties;
021: import java.io.File;
022: import java.io.IOException;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026:
027: import org.apache.commons.cli.CommandLine;
028: import org.apache.commons.cli.CommandLineParser;
029: import org.apache.commons.cli.HelpFormatter;
030: import org.apache.commons.cli.Option;
031: import org.apache.commons.cli.OptionBuilder;
032: import org.apache.commons.cli.Options;
033: import org.apache.commons.cli.ParseException;
034: import org.apache.commons.cli.PosixParser;
035: import org.apache.openejb.NoSuchApplicationException;
036: import org.apache.openejb.UndeployException;
037: import org.apache.openejb.assembler.Deployer;
038: import org.apache.openejb.cli.SystemExitException;
039: import org.apache.openejb.util.Messages;
040: import org.apache.openejb.util.OpenEjbVersion;
041:
042: /**
043: * UnDeploy EJB beans
044: */
045: public class Undeploy {
046:
047: private static Messages messages = new Messages(Undeploy.class);
048:
049: private static final String defaultServerUrl = "ejbd://localhost:4201";
050:
051: public static void main(String[] args) throws SystemExitException {
052:
053: CommandLineParser parser = new PosixParser();
054:
055: // create the Options
056: Options options = new Options();
057: options.addOption(Undeploy.option("v", "version",
058: "cmd.deploy.opt.version"));
059: options.addOption(Undeploy.option("h", "help",
060: "cmd.undeploy.opt.help")); // TODO this message doesn't exist
061: options.addOption(Undeploy.option("s", "server-url", "url",
062: "cmd.deploy.opt.server"));
063:
064: CommandLine line = null;
065: try {
066: // parse the command line arguments
067: line = parser.parse(options, args);
068: } catch (ParseException exp) {
069: Undeploy.help(options);
070: throw new SystemExitException(-1);
071: }
072:
073: if (line.hasOption("help")) {
074: Undeploy.help(options);
075: return;
076: } else if (line.hasOption("version")) {
077: OpenEjbVersion.get().print(System.out);
078: return;
079: }
080:
081: if (line.getArgList().size() == 0) {
082: System.out.println("Must specify an module id.");
083: help(options);
084: return;
085: }
086:
087: Properties p = new Properties();
088: p
089: .put(Context.INITIAL_CONTEXT_FACTORY,
090: "org.apache.openejb.client.RemoteInitialContextFactory");
091:
092: String serverUrl = Undeploy.defaultServerUrl;
093: if (line.hasOption(serverUrl)) {
094: serverUrl = line.getOptionValue("serverUrl");
095: }
096: p.put(Context.PROVIDER_URL, serverUrl);
097:
098: Deployer deployer = null;
099: try {
100: InitialContext ctx = new InitialContext(p);
101: deployer = (Deployer) ctx
102: .lookup("openejb/DeployerBusinessRemote");
103: } catch (javax.naming.ServiceUnavailableException e) {
104: System.out.println(e.getCause().getMessage());
105: System.out.println(Undeploy.messages
106: .format("cmd.deploy.serverOffline"));
107: throw new SystemExitException(-1);
108: } catch (javax.naming.NamingException e) {
109: System.out
110: .println("DeployerEjb does not exist in server '"
111: + serverUrl
112: + "', check the server logs to ensure it exists and has not been removed.");
113: throw new SystemExitException(-2);
114: }
115:
116: int exitCode = 0;
117: for (Object obj : line.getArgList()) {
118: String moduleId = (String) obj;
119:
120: try {
121: undeploy(moduleId, deployer);
122: } catch (DeploymentTerminatedException e) {
123: System.out.println(e.getMessage());
124: exitCode++;
125: } catch (UndeployException e) {
126: System.out.println(messages.format(
127: "cmd.undeploy.failed", moduleId));
128: e.printStackTrace(System.out);
129: exitCode++;
130: } catch (NoSuchApplicationException e) {
131: // TODO make this message
132: System.out.println(messages.format(
133: "cmd.undeploy.noSuchModule", moduleId));
134: exitCode++;
135: }
136: }
137:
138: if (exitCode != 0) {
139: throw new SystemExitException(exitCode);
140: }
141: }
142:
143: public static void undeploy(String moduleId, Deployer deployer)
144: throws UndeployException, NoSuchApplicationException,
145: DeploymentTerminatedException {
146: // Treat moduleId as a file path, and see if there is a matching app to undeploy
147: undeploy(moduleId, new File(moduleId), deployer);
148: }
149:
150: public static void undeploy(String moduleId, File file,
151: Deployer deployer) throws UndeployException,
152: NoSuchApplicationException, DeploymentTerminatedException {
153: try {
154: file = file.getCanonicalFile();
155: } catch (IOException e) {
156: }
157:
158: boolean undeployed = false;
159: if (file != null) {
160: String path = file.getAbsolutePath();
161: try {
162: deployer.undeploy(path);
163: undeployed = true;
164: moduleId = path;
165: if (!delete(file)) {
166: throw new DeploymentTerminatedException(messages
167: .format("cmd.undeploy.cantDelete", file
168: .getAbsolutePath()));
169: }
170: } catch (NoSuchApplicationException e) {
171: }
172: }
173:
174: // If that didn't work, undeploy using just the moduleId
175: if (!undeployed) {
176: deployer.undeploy(moduleId);
177: System.out.println(messages.format(
178: "cmd.undeploy.nothingToDelete", moduleId));
179: }
180:
181: // TODO make this message
182: System.out.println(messages.format("cmd.undeploy.successful",
183: moduleId));
184: }
185:
186: private static void help(Options options) {
187: HelpFormatter formatter = new HelpFormatter();
188: formatter.printHelp("undeploy [options] <file> [<file>...]",
189: "\n" + Undeploy.i18n("cmd.undeploy.description"),
190: options, "\n");
191: }
192:
193: private static Option option(String shortOpt, String longOpt,
194: String description) {
195: return OptionBuilder.withLongOpt(longOpt).withDescription(
196: Undeploy.i18n(description)).create(shortOpt);
197: }
198:
199: private static Option option(String shortOpt, String longOpt,
200: String argName, String description) {
201: return OptionBuilder.withLongOpt(longOpt).withArgName(argName)
202: .hasArg().withDescription(Undeploy.i18n(description))
203: .create(shortOpt);
204: }
205:
206: private static String i18n(String key) {
207: return Undeploy.messages.format(key);
208: }
209:
210: public static class DeploymentTerminatedException extends
211: Deploy.DeploymentTerminatedException {
212:
213: private static final long serialVersionUID = 1L;
214:
215: public DeploymentTerminatedException(String message) {
216: super (message);
217: }
218:
219: public DeploymentTerminatedException(String message,
220: Throwable cause) {
221: super(message, cause);
222: }
223: }
224: }
|