KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > PasswordAliasConfigMBean


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 package com.sun.enterprise.admin.mbeans;
25
26 import com.sun.enterprise.config.ConfigException;
27 import com.sun.enterprise.config.ConfigContext;
28
29 import com.sun.enterprise.util.i18n.StringManager;
30 import com.sun.enterprise.util.i18n.StringManagerBase;
31
32 import com.sun.enterprise.admin.config.BaseConfigMBean;
33 import com.sun.enterprise.admin.common.constant.AdminConstants;
34
35 import com.sun.enterprise.security.store.PasswordAdapter;
36 import com.sun.enterprise.security.store.IdentityManager;
37
38 import java.util.logging.Logger JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 import java.io.IOException JavaDoc;
43 import java.security.KeyStoreException JavaDoc;
44 import java.security.NoSuchAlgorithmException JavaDoc;
45 import java.security.UnrecoverableKeyException JavaDoc;
46 import java.security.cert.CertificateException JavaDoc;
47
48 public class PasswordAliasConfigMBean extends BaseConfigMBean
49 {
50     private static final StringManager _strMgr =
51         StringManager.getManager(PasswordAliasConfigMBean.class);
52
53     private static Logger JavaDoc _logger = null;
54         
55     private static Logger JavaDoc getLogger()
56     {
57         if (_logger == null) {
58             _logger = Logger.getLogger(AdminConstants.kLoggerName);
59         }
60         return _logger;
61     }
62
63    private static ExceptionHandler _handler = null;
64     
65     //The exception handler is used to parse and log exceptions
66
protected static ExceptionHandler getExceptionHandler()
67     {
68         if (_handler == null) {
69             _handler = new ExceptionHandler(getLogger());
70         }
71         return _handler;
72     }
73     
74     public PasswordAliasConfigMBean() {
75         super();
76     }
77                 
78     private PasswordAdapter getPasswordAdapter()
79         throws CertificateException JavaDoc, IOException JavaDoc, KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
80     {
81         //The masterPassword in the IdentityManager is available only through
82
//a running DAS, server instance, or node agent.
83
String JavaDoc masterPassword = IdentityManager.getMasterPassword();
84         return new PasswordAdapter(masterPassword.toCharArray());
85     }
86     
87     /**
88      * Add a non-existing password alias
89      * @param aliasName the name of the alias
90      * @param password the password of the alias
91      * @throws ConfigException
92      */

93     public void addPasswordAlias(String JavaDoc aliasName, String JavaDoc password) throws ConfigException
94     {
95         try {
96             PasswordAdapter p = getPasswordAdapter();
97             if (p.aliasExists(aliasName)) {
98                 throw new ConfigException(_strMgr.getString("passwordAliasExists", aliasName));
99             }
100             p.setPasswordForAlias(aliasName, password.getBytes());
101         } catch (Exception JavaDoc ex) {
102             throw getExceptionHandler().handleConfigException(
103                 ex, "addPasswordAliasException", aliasName);
104         }
105     }
106     
107     /**
108      * Remove an existing password alias
109      * @param aliasName the name of the password alias to remove
110      * @throws ConfigException
111      */

112     public void removePasswordAlias(String JavaDoc aliasName) throws ConfigException
113     {
114         try {
115             PasswordAdapter p = getPasswordAdapter();
116             if (!p.aliasExists(aliasName)) {
117                 throw new ConfigException(_strMgr.getString("passwordAliasDoesNotExist", aliasName));
118             }
119             p.removeAlias(aliasName);
120         } catch (Exception JavaDoc ex) {
121             throw getExceptionHandler().handleConfigException(
122                 ex, "removePasswordAliasException", aliasName);
123         }
124     }
125     
126     /**
127      * Update the password for an existing alias
128      * @param aliasName the name of the alias whose password is to be updated
129      * @param password the new password
130      * @throws ConfigException
131      */

132     public void updatePasswordAlias(String JavaDoc aliasName, String JavaDoc password) throws ConfigException
133     {
134         try {
135             PasswordAdapter p = getPasswordAdapter();
136             if (!p.aliasExists(aliasName)) {
137                 throw new ConfigException(_strMgr.getString("passwordAliasDoesNotExist", aliasName));
138             }
139             p.setPasswordForAlias(aliasName, password.getBytes());
140         } catch (Exception JavaDoc ex) {
141             throw getExceptionHandler().handleConfigException(
142                 ex, "updatePasswordAliasException", aliasName);
143         }
144     }
145     
146     /**
147      * Get all the password aliases
148      * @throws ConfigException
149      * @return The list of password aliases
150      */

151     public String JavaDoc[] getPasswordAliases() throws ConfigException
152     {
153         try {
154             ArrayList JavaDoc result = new ArrayList JavaDoc();
155             Enumeration JavaDoc en = getPasswordAdapter().getAliases();
156             while (en.hasMoreElements()) {
157                 result.add((String JavaDoc)en.nextElement());
158             }
159             return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
160         } catch (Exception JavaDoc ex) {
161             throw getExceptionHandler().handleConfigException(
162                 ex, "listPasswordAliasException", "");
163         }
164     }
165 }
166
Popular Tags