KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > MasterPasswordFileManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * RepositoryManager.java
26  *
27  * Created on August 19, 2003, 2:29 PM
28  */

29
30 package com.sun.enterprise.admin.servermgmt;
31
32 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
33 import com.sun.enterprise.security.store.PasswordAdapter;
34 import com.sun.enterprise.security.util.SSHA;
35 import com.sun.enterprise.util.i18n.StringManager;
36 import com.sun.enterprise.util.SystemPropertyConstants;
37
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.BufferedInputStream JavaDoc;
40 import java.io.BufferedOutputStream JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43 import java.io.FileOutputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45
46
47 /**
48  * The RepositoryManager serves as a common base class for the following
49  * PEDomainsManager, PEInstancesManager, AgentManager (the SE Node Agent).
50  * Its purpose is to abstract out any shared functionality related to
51  * lifecycle management of domains, instances and node agents. This includes
52  * creation, deletion, listing, and starting and stopping.
53  *
54  * @author kebbs
55  */

56 public class MasterPasswordFileManager extends KeystoreManager {
57
58     private static final String JavaDoc MASTER_PASSWORD_ALIAS="master-password";
59     private static final String JavaDoc ENCODED_CHARSET = "UTF-8";
60     private static final int SALT_SIZE = 8;
61     
62     private static final StringManager _strMgr =
63         StringManager.getManager(MasterPasswordFileManager.class);
64
65     /** Creates a new instance of RepositoryManager */
66     public MasterPasswordFileManager() {
67         super();
68     }
69
70     /**
71      *
72      * @return The password protecting the master password keywtore
73      */

74     private char[] getMasterPasswordPassword(RepositoryConfig config)
75             throws RepositoryException
76     {
77         //XXX fixed String
78
return MASTER_PASSWORD_ALIAS.toCharArray();
79     }
80     
81     protected void deleteMasterPasswordFile(RepositoryConfig config)
82     {
83         final PEFileLayout layout = getFileLayout(config);
84         final File JavaDoc pwdFile = layout.getMasterPasswordFile();
85         pwdFile.delete();
86     }
87     
88     /**
89      * Create the master password keystore. This routine can also modify the master password
90      * if the keystore already exists
91      * @param config
92      * @param masterPassword
93      * @throws RepositoryException
94      */

95     protected void createMasterPasswordFile(
96         RepositoryConfig config, String JavaDoc masterPassword)
97         throws RepositoryException
98     {
99         final PEFileLayout layout = getFileLayout(config);
100         final File JavaDoc pwdFile = layout.getMasterPasswordFile();
101         try {
102             PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(),
103                 getMasterPasswordPassword(config));
104             p.setPasswordForAlias(MASTER_PASSWORD_ALIAS, masterPassword.getBytes());
105             chmod("600", pwdFile);
106         } catch (Exception JavaDoc ex) {
107             throw new RepositoryException(_strMgr.getString("masterPasswordFileNotCreated", pwdFile),
108                 ex);
109         }
110     }
111     
112     /**
113      * Return the master password stored in the master password keystore.
114      * @param config
115      * @throws RepositoryException
116      * @return
117      */

118     public String JavaDoc readMasterPasswordFile(
119         RepositoryConfig config) throws RepositoryException
120     {
121         final PEFileLayout layout = getFileLayout(config);
122         final File JavaDoc pwdFile = layout.getMasterPasswordFile();
123         if (pwdFile.exists()) {
124             try {
125                 PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(),
126                     getMasterPasswordPassword(config));
127                 return p.getPasswordForAlias(MASTER_PASSWORD_ALIAS);
128             } catch (Exception JavaDoc ex) {
129                 throw new RepositoryException(_strMgr.getString("masterPasswordFileNotRead", pwdFile),
130                     ex);
131             }
132         } else {
133             //Return null if the password file does not exist.
134
return null;
135         }
136     }
137     /**
138      * Changes the master password in the master password file
139      * @param saveMasterPassword
140      * @param config
141      * @param newPassword
142      * @throws RepositoryException
143      */

144     protected void changeMasterPasswordInMasterPasswordFile(RepositoryConfig config, String JavaDoc newPassword,
145         boolean saveMasterPassword) throws RepositoryException
146     {
147         deleteMasterPasswordFile(config);
148         if (saveMasterPassword) {
149             createMasterPasswordFile(config, newPassword);
150         }
151     }
152 }
153
Popular Tags