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 import org.apache.geronimo.util.SimpleEncryption;
22
23 import java.io.PrintWriter;
24 import java.io.File;
25 import java.io.InputStream;
26 import java.io.FileInputStream;
27 import java.io.BufferedInputStream;
28 import java.io.OutputStream;
29 import java.io.BufferedOutputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.util.Properties;
33
34 /**
35 * The CLI deployer logic to start.
36 *
37 * @version $Rev: 405624 $ $Date: 2006-05-09 21:08:49 -0700 (Tue, 09 May 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 command, String group, String helpArgumentList, String helpText) {
57 super(command, group, helpArgumentList, helpText);
58 }
59
60 public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
61 try {
62 File authFile = new File(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 props = new Properties();
72 InputStream in = new BufferedInputStream(new FileInputStream(authFile));
73 props.load(in);
74 in.close();
75 props.setProperty("login."+connection.getServerURI(), "{Standard}"+SimpleEncryption.encrypt(connection.getAuthentication()));
76 OutputStream save = new BufferedOutputStream(new FileOutputStream(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 e) {
82 throw new DeploymentException("Unable to save authentication to login file", e);
83 }
84 }
85 }