KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 java.io.BufferedInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.util.LinkedHashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33
34 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
35 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
36 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
37 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
38
39 import org.apache.geronimo.common.DeploymentException;
40 import org.apache.geronimo.deployment.plugin.factories.AuthenticationFailedException;
41 import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryImpl;
42 import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager;
43 import org.apache.geronimo.deployment.plugin.jmx.LocalDeploymentManager;
44 import org.apache.geronimo.util.SimpleEncryption;
45 import org.apache.geronimo.kernel.Kernel;
46 import org.apache.geronimo.kernel.config.ConfigurationManager;
47 import org.apache.geronimo.kernel.config.ConfigurationUtil;
48 import org.apache.geronimo.system.main.LocalServer;
49
50 /**
51  * Supports online connections to the server, via JSR-88, valid only
52  * when the server is online.
53  *
54  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
55  */

56 public class ServerConnection {
57     private final static Map JavaDoc OPTION_HELP = new LinkedHashMap JavaDoc(9);
58
59     static {
60         OPTION_HELP.put("--uri", "A URI to contact the server. If not specified, the deployer defaults to " +
61                 "operating on a Geronimo server running on the standard port on localhost.\n" +
62                 "A URI to connect to Geronimo (including optional host and port parameters) has the form: " +
63                 "deployer:geronimo:jmx[://host[:port]] (though you could also just use --host and --port instead).");
64         OPTION_HELP.put("--host", "The host name of a Geronimo server to deploy to. This option is " +
65                 "not compatible with --uri, but is often used with --port.");
66         OPTION_HELP.put("--port", "The RMI listen port of a Geronimo server to deploy to. This option is " +
67                 "not compatible with --uri, but is often used with --host. The default port is 1099.");
68         OPTION_HELP.put("--driver", "If you want to use this tool with a server other than Geronimo, " +
69                 "then you must provide the path to its driver JAR. Currently, manifest " +
70                 "Class-Path entries in that JAR are ignored.");
71         OPTION_HELP.put("--user", "If the deployment operation requires authentication, then you can " +
72                 "specify the username to use to connect. If no password is specified, the " +
73                 "deployer will attempt to connect to the server with no password, and if " +
74                 "that fails, will prompt you for a password.");
75         OPTION_HELP.put("--password", "Specifies a password to use to authenticate to the server.");
76         OPTION_HELP.put("--syserr", "Enables error logging to syserr. Disabled by default.");
77         OPTION_HELP.put("--verbose", "Enables verbose execution mode. Disabled by default.");
78         OPTION_HELP.put("--offline", "Deploy offline to a local server, using whatever deployers are available in the local server");
79     }
80
81     public static Map JavaDoc getOptionHelp() {
82         return OPTION_HELP;
83     }
84
85     /**
86      * Checks whether the stated command-line argument is a general argument (which
87      * may be the general argument itself, or a required parameter after the general
88      * argument). For example, if the arguments were "--user bob foo" then
89      * this should return true for "--user" and "bob" and false for "foo".
90      *
91      * @param args The previous arguments on the command line
92      * @param option The argument we're checking at the moment
93      * @return True if the argument we're checking is part of a general argument
94      */

95     public static boolean isGeneralOption(List JavaDoc args, String JavaDoc option) {
96         if (OPTION_HELP.containsKey(option) || option.equals("--url")) {
97             return true;
98         }
99         if (args.size() == 0) {
100             return false;
101         }
102         String JavaDoc last = (String JavaDoc) args.get(args.size() - 1);
103         return last.equals("--uri") || last.equals("--url") || last.equals("--driver") || last.equals("--user") ||
104                 last.equals("--password") || last.equals("--host") || last.equals("--port");
105     }
106
107     private final static String JavaDoc DEFAULT_URI = "deployer:geronimo:jmx";
108
109     private DeploymentManager JavaDoc manager;
110     private PrintWriter JavaDoc out;
111     private InputStream JavaDoc in;
112     private SavedAuthentication auth;
113     private boolean logToSysErr;
114     private boolean verboseMessages;
115
116     public ServerConnection(String JavaDoc[] args, PrintWriter JavaDoc out, InputStream JavaDoc in) throws DeploymentException {
117         String JavaDoc uri = null, driver = null, user = null, password = null, host = null;
118         Integer JavaDoc port = null;
119         this.out = out;
120         this.in = in;
121         boolean offline = false;
122         for (int i = 0; i < args.length; i++) {
123             String JavaDoc arg = args[i];
124             if (arg.equals("--uri") || arg.equals("--url")) {
125                 if (uri != null) {
126                     throw new DeploymentSyntaxException("Cannot specify more than one URI");
127                 } else if (i >= args.length - 1) {
128                     throw new DeploymentSyntaxException("Must specify a URI (e.g. --uri deployer:...)");
129                 }
130                 if (host != null || port != null) {
131                     throw new DeploymentSyntaxException("Cannot specify a URI as well as a host/port");
132                 }
133                 uri = args[++i];
134             } else if (arg.equals("--host")) {
135                 if (host != null) {
136                     throw new DeploymentSyntaxException("Cannot specify more than one host");
137                 } else if (i >= args.length - 1) {
138                     throw new DeploymentSyntaxException("Must specify a hostname (e.g. --host localhost)");
139                 }
140                 if (uri != null) {
141                     throw new DeploymentSyntaxException("Cannot specify a URI as well as a host/port");
142                 }
143                 host = args[++i];
144             } else if (arg.equals("--port")) {
145                 if (port != null) {
146                     throw new DeploymentSyntaxException("Cannot specify more than one port");
147                 } else if (i >= args.length - 1) {
148                     throw new DeploymentSyntaxException("Must specify a port (e.g. --port 1099)");
149                 }
150                 if (uri != null) {
151                     throw new DeploymentSyntaxException("Cannot specify a URI as well as a host/port");
152                 }
153                 try {
154                     port = new Integer JavaDoc(args[++i]);
155                 } catch (NumberFormatException JavaDoc e) {
156                     throw new DeploymentSyntaxException("Port must be a number (" + e.getMessage() + ")");
157                 }
158             } else if (arg.equals("--driver")) {
159                 if (driver != null) {
160                     throw new DeploymentSyntaxException("Cannot specify more than one driver");
161                 } else if (i >= args.length - 1) {
162                     throw new DeploymentSyntaxException("Must specify a driver JAR (--driver jarfile)");
163                 }
164                 driver = args[++i];
165             } else if (arg.equals("--offline")) {
166                 //throw new DeploymentSyntaxException("This tool no longer handles offline deployment");
167
offline = true;
168             } else if (arg.equals("--user")) {
169                 if (user != null) {
170                     throw new DeploymentSyntaxException("Cannot specify more than one user name");
171                 } else if (i >= args.length - 1) {
172                     throw new DeploymentSyntaxException("Must specify a username (--user username)");
173                 }
174                 user = args[++i];
175             } else if (arg.equals("--password")) {
176                 if (password != null) {
177                     throw new DeploymentSyntaxException("Cannot specify more than one password");
178                 } else if (i >= args.length - 1) {
179                     throw new DeploymentSyntaxException("Must specify a password (--password password)");
180                 }
181                 password = args[++i];
182             } else if (arg.equals("--verbose")) {
183                 verboseMessages = true;
184             } else if (arg.equals("--syserr")) {
185                 logToSysErr = true;
186             } else {
187                 throw new DeploymentException("Invalid option " + arg);
188             }
189         }
190         if ((driver != null) && uri == null) {
191             throw new DeploymentSyntaxException("A custom driver requires a custom URI");
192         }
193         if (host != null || port != null) {
194             uri = DEFAULT_URI + "://" + (host == null ? "" : host) + (port == null ? "" : ":" + port);
195         }
196         if (offline) {
197             LocalServer localServer;
198             try {
199                 localServer = new LocalServer("org.apache.geronimo.configs/j2ee-system//car", "var/config/offline-deployer-list");
200             } catch (Exception JavaDoc e) {
201                 throw new DeploymentException("Could not start local server", e);
202             }
203             Kernel kernel = localServer.getKernel();
204             ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
205             configurationManager.setOnline(false);
206
207             manager = new LocalDeploymentManager(localServer.getKernel());
208         } else {
209             tryToConnect(uri, driver, user, password, true);
210         }
211         if (manager == null) {
212             throw new DeploymentException("Unexpected error; connection failed.");
213         }
214     }
215
216     public void close() throws DeploymentException {
217         if (manager != null) {
218             manager.release();
219         }
220     }
221
222     Serializable JavaDoc getAuthentication() {
223         return auth;
224     }
225
226     String JavaDoc getServerURI() {
227         return auth.uri;
228     }
229
230     private void tryToConnect(String JavaDoc argURI, String JavaDoc driver, String JavaDoc user, String JavaDoc password, boolean authPrompt) throws DeploymentException {
231         DeploymentFactoryManager JavaDoc mgr = DeploymentFactoryManager.getInstance();
232         if (driver != null) {
233             loadDriver(driver, mgr);
234         } else {
235             mgr.registerDeploymentFactory(new DeploymentFactoryImpl());
236         }
237         String JavaDoc useURI = argURI == null ? DEFAULT_URI : argURI;
238
239         if (authPrompt && user == null && password == null) {
240             InputStream JavaDoc in;
241             // First check for .geronimo-deployer on class path (e.g. packaged in deployer.jar)
242
in = ServerConnection.class.getResourceAsStream("/.geronimo-deployer");
243             // If not there, check in home directory
244
if (in == null) {
245                 File JavaDoc authFile = new File JavaDoc(System.getProperty("user.home"), ".geronimo-deployer");
246                 if (authFile.exists() && authFile.canRead()) {
247                     try {
248                         in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(authFile));
249                     } catch (FileNotFoundException JavaDoc e) {
250                         // ignore
251
}
252                 }
253             }
254             if (in != null) {
255                 try {
256                     Properties JavaDoc props = new Properties JavaDoc();
257                     props.load(in);
258                     String JavaDoc encryped = props.getProperty("login." + useURI);
259                     if (encryped != null) {
260                         if (encryped.startsWith("{Standard}")) {
261                             SavedAuthentication auth = (SavedAuthentication) SimpleEncryption.decrypt(encryped.substring(10));
262                             if (auth.uri.equals(useURI)) {
263                                 user = auth.user;
264                                 password = new String JavaDoc(auth.password);
265                             }
266                         } else if (encryped.startsWith("{Plain}")) {
267                             int pos = encryped.indexOf("/");
268                             user = encryped.substring(7, pos);
269                             password = encryped.substring(pos + 1);
270                         } else {
271                             System.out.print(DeployUtils.reformat("Unknown encryption used in saved login file", 4, 72));
272                         }
273                     }
274                 } catch (IOException JavaDoc e) {
275                     System.out.print(DeployUtils.reformat("Unable to read authentication from saved login file: " + e.getMessage(), 4, 72));
276                 } finally {
277                     try {
278                         in.close();
279                     } catch (IOException JavaDoc e) {
280                         // ingore
281
}
282                 }
283             }
284         }
285
286         if (authPrompt && !useURI.equals(DEFAULT_URI) && user == null && password == null) {
287             // Non-standard URI, but no authentication information
288
doAuthPromptAndRetry(useURI, user, password);
289             return;
290         } else { // Standard URI with no auth, Non-standard URI with auth, or else this is the 2nd try already
291
try {
292                 manager = mgr.getDeploymentManager(useURI, user, password);
293                 auth = new SavedAuthentication(useURI, user, password == null ? null : password.toCharArray());
294             } catch (AuthenticationFailedException e) { // server's there, you just can't talk to it
295
if (authPrompt) {
296                     doAuthPromptAndRetry(useURI, user, password);
297                     return;
298                 } else {
299                     throw new DeploymentException("Login Failed");
300                 }
301             } catch (DeploymentManagerCreationException JavaDoc e) {
302                 throw new DeploymentException("Unable to connect to server at " + useURI + " -- " + e.getMessage());
303             }
304         }
305
306         if (manager instanceof JMXDeploymentManager) {
307             JMXDeploymentManager deploymentManager = (JMXDeploymentManager) manager;
308             deploymentManager.setLogConfiguration(logToSysErr, verboseMessages);
309         }
310     }
311
312     private void loadDriver(String JavaDoc driver, DeploymentFactoryManager JavaDoc mgr) throws DeploymentException {
313         File JavaDoc file = new File JavaDoc(driver);
314         if (!file.exists() || !file.canRead() || !DeployUtils.isJarFile(file)) {
315             throw new DeploymentSyntaxException("Driver '" + file.getAbsolutePath() + "' is not a readable JAR file");
316         }
317         String JavaDoc className = null;
318         try {
319             JarFile JavaDoc jar = new JarFile JavaDoc(file);
320             className = jar.getManifest().getMainAttributes().getValue("J2EE-DeploymentFactory-Implementation-Class");
321             if (className == null) {
322                 throw new DeploymentException("The driver JAR " + file.getAbsolutePath() + " does not specify a J2EE-DeploymentFactory-Implementation-Class; cannot load driver.");
323             }
324             jar.close();
325             DeploymentFactory JavaDoc factory = (DeploymentFactory JavaDoc) Class.forName(className).newInstance();
326             mgr.registerDeploymentFactory(factory);
327         } catch (DeploymentException e) {
328             throw e;
329         } catch (Exception JavaDoc e) {
330             throw new DeploymentSyntaxException("Unable to load driver class " + className + " from JAR " + file.getAbsolutePath(), e);
331         }
332     }
333
334     private void doAuthPromptAndRetry(String JavaDoc uri, String JavaDoc user, String JavaDoc password) throws DeploymentException {
335         try {
336             InputPrompt prompt = new InputPrompt(in, out);
337             if (user == null) {
338                 user = prompt.getInput("Username: ");
339             }
340             if (password == null) {
341                 password = prompt.getPassword("Password: ");
342             }
343         } catch (IOException JavaDoc e) {
344             throw new DeploymentException("Unable to prompt for login", e);
345         }
346         tryToConnect(uri, null, user, password, false);
347     }
348
349     public DeploymentManager JavaDoc getDeploymentManager() {
350         return manager;
351     }
352
353     public boolean isGeronimo() {
354         return manager.getClass().getName().startsWith("org.apache.geronimo.");
355     }
356
357     private final static class SavedAuthentication implements Serializable JavaDoc {
358         private String JavaDoc uri;
359         private String JavaDoc user;
360         private char[] password;
361
362         public SavedAuthentication(String JavaDoc uri, String JavaDoc user, char[] password) {
363             this.uri = uri;
364             this.user = user;
365             this.password = password;
366         }
367     }
368 }
369
Popular Tags