KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > CommandPackage


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.deployment.cli;
19
20 import org.apache.geronimo.common.DeploymentException;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Arrays JavaDoc;
28
29 /**
30  * The CLI deployer logic to create a configuration package. Can only be run
31  * in ofline mode (not via JSR-88).
32  *
33  * @version $Rev: 53762 $ $Date: 2004-10-04 18:54:53 -0400 (Mon, 04 Oct 2004) $
34  */

35 public class CommandPackage extends AbstractCommand {
36     public CommandPackage() {
37         super("package", "3. Use if you know what you're doing", "[--classPath path] [--mainClass class] [--install] [module] [plan] fileName",
38                 "Creates a configuration JAR rather than installing into the server " +
39                 "environment. The fileName argument specifies the JAR to create. The " +
40                 "optional classPath argument specifies a Class-Path to include in the JAR " +
41                 "manifest. The optional mainClass argument specifies the Main-Class to include in " +
42                 "the JAR manifest. The install option specifies that the " +
43                 "configuration should be build into a JAR and also installed into " +
44                 "the server configuration (otherwise it is packaged but not installed).\n" +
45                 "The standard arguments may not be used with this command -- it " +
46                 "never connects to a remote server.");
47     }
48
49     public boolean isLocalOnly() {
50         return true;
51     }
52
53     public void execute(PrintWriter JavaDoc out, ServerConnection connection, String JavaDoc[] argArray) throws DeploymentException {
54         if(connection.isOnline()) {
55             throw new DeploymentException("This command cannot be run when the server is running. Make sure the server is shut down first.");
56         }
57
58         String JavaDoc classPath = null;
59         String JavaDoc mainClass = null;
60         String JavaDoc endorsedDirs = null;
61         boolean install = false;
62
63         // Read off the optional arguments (clasPath, mainClass, endorsedDirs, and install)
64
LinkedList JavaDoc args = new LinkedList JavaDoc(Arrays.asList(argArray));
65         for (Iterator JavaDoc iterator = args.iterator(); iterator.hasNext();) {
66             String JavaDoc arg = (String JavaDoc) iterator.next();
67             if(arg.equals("--classPath")) {
68                 iterator.remove();
69                 classPath = (String JavaDoc) iterator.next();
70                 iterator.remove();
71             } else if(arg.equals("--mainClass")) {
72                 iterator.remove();
73                 mainClass = (String JavaDoc) iterator.next();
74                 iterator.remove();
75             } else if(arg.equals("--endorsedDirs")) {
76                 iterator.remove();
77                 endorsedDirs = (String JavaDoc) iterator.next();
78                 iterator.remove();
79             } else if(arg.equals("--install")) {
80                 iterator.remove();
81                 install = true;
82             } else if(arg.startsWith("--")) {
83                 throw new DeploymentSyntaxException("Invalid option '" + arg + "'");
84             } else {
85                 break;
86             }
87         }
88
89         // if we have any other options on the comman line they are invalid
90
for (Iterator JavaDoc iterator = args.iterator(); iterator.hasNext();) {
91             String JavaDoc arg = (String JavaDoc) iterator.next();
92             if(arg.startsWith("--")) {
93                 throw new DeploymentSyntaxException("All command line options must appear before module, plan or packageFile: " + arg);
94             }
95         }
96
97         if(args.isEmpty()) {
98             throw new DeploymentSyntaxException("No fileName specified for package command");
99         }
100
101         // Read off packageFile which is always the last argument
102
File JavaDoc packageFile;
103         packageFile = new File JavaDoc((String JavaDoc) args.removeLast());
104         File JavaDoc parent = packageFile.getAbsoluteFile().getParentFile();
105         if(!parent.exists() || !parent.canWrite()) {
106             throw new DeploymentSyntaxException("Cannot write to output file "+packageFile.getAbsolutePath());
107         }
108
109         // Read off the plan and module
110
File JavaDoc module = null;
111         File JavaDoc plan = null;
112         if(!args.isEmpty()) {
113             // if the arg is a directory or jar file, it must be the module; otherwise it is the plan
114
File JavaDoc test = new File JavaDoc((String JavaDoc) args.removeLast()).getAbsoluteFile();
115             if(DeployUtils.isJarFile(test) || test.isDirectory()) {
116                 module = test;
117             } else {
118                 plan = test;
119             }
120         }
121         if(!args.isEmpty()) {
122             File JavaDoc test = new File JavaDoc((String JavaDoc) args.removeLast()).getAbsoluteFile();
123             if(DeployUtils.isJarFile(test) || test.isDirectory()) {
124                 if(module != null) {
125                     throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
126                 }
127                 module = test;
128             } else {
129                 if(plan != null) {
130                     throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
131                 }
132                 plan = test;
133             }
134         }
135
136         // are there extra left over args on the command prompt
137
if(!args.isEmpty()) {
138             throw new DeploymentSyntaxException("Too many arguments for package command");
139         }
140
141         // invoke the deployer
142
List JavaDoc list = (List JavaDoc) connection.invokeOfflineDeployer(
143                 new Object JavaDoc[]{
144                     plan,
145                     module,
146                     packageFile,
147                     install ? Boolean.TRUE : Boolean.FALSE,
148                     mainClass,
149                     classPath,
150                     endorsedDirs},
151                 new String JavaDoc[]{
152                     File JavaDoc.class.getName(),
153                     File JavaDoc.class.getName(),
154                     File JavaDoc.class.getName(),
155                     boolean.class.getName(),
156                     String JavaDoc.class.getName(),
157                     String JavaDoc.class.getName(),
158                     String JavaDoc.class.getName()});
159
160         // print the configurations created
161
for (int j = 0; j < list.size(); j++) {
162             out.println("Packaged configuration "+list.get(j)+" to "+packageFile);
163         }
164     }
165 }
166
Popular Tags