KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.geronimo.common.DeploymentException;
21 import org.apache.geronimo.util.SimpleEncryption;
22
23 import java.io.PrintWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * The CLI deployer logic to start.
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class CommandLogin extends AbstractCommand {
40     public CommandLogin() {
41         super("login", "1. Common Commands", "",
42                 "Saves the username and password for this connection to the "+
43                 "file .geronimo-deployer in the current user's home directory. " +
44                 "Future connections to the same server will try to use this "+
45                 "saved authentication information instead of prompting where " +
46                 "possible. This information is saved separately per connection " +
47                 "URL, so you can specify --url or --host and/or --port on the command " +
48                 "line to save a login to a different server.\n" +
49                 "WARNING: while the login information is not saved in " +
50                 "clear text, it is not secure either. If you want to " +
51                 "save the authentication securely, you should change the " +
52                 ".geronimo-deployer file in your home directory so that nobody " +
53                 "else can read or write it.");
54     }
55
56     public CommandLogin(String JavaDoc command, String JavaDoc group, String JavaDoc helpArgumentList, String JavaDoc helpText) {
57         super(command, group, helpArgumentList, helpText);
58     }
59
60     public void execute(PrintWriter JavaDoc out, ServerConnection connection, String JavaDoc[] args) throws DeploymentException {
61         try {
62             File JavaDoc authFile = new File JavaDoc(System.getProperty("user.home"), ".geronimo-deployer");
63             if(!authFile.exists()) {
64                 if(!authFile.createNewFile()) {
65                     throw new DeploymentException("Unable to create "+authFile.getAbsolutePath()+" to hold saved logins");
66                 }
67             }
68             if(!authFile.canRead() || !authFile.canWrite()) {
69                 throw new DeploymentException("Saved login file "+authFile.getAbsolutePath()+" is not readable or not writable");
70             }
71             Properties JavaDoc props = new Properties JavaDoc();
72             InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(authFile));
73             props.load(in);
74             in.close();
75             props.setProperty("login."+connection.getServerURI(), "{Standard}"+SimpleEncryption.encrypt(connection.getAuthentication()));
76             OutputStream JavaDoc save = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(authFile));
77             props.store(save, "Saved authentication information to connect to Geronimo servers");
78             save.flush();
79             save.close();
80             System.out.print(DeployUtils.reformat("Saved login for: "+connection.getServerURI(), 4, 72));
81         } catch (IOException JavaDoc e) {
82             throw new DeploymentException("Unable to save authentication to login file", e);
83         }
84     }
85 }
86
Popular Tags