KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > offline > AuthRealmMBeanX


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 package com.sun.enterprise.management.offline;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import java.io.IOException JavaDoc;
30
31 import com.sun.appserv.management.util.misc.CollectionUtil;
32
33 import com.sun.enterprise.admin.config.MBeanConfigException;
34
35 import com.sun.enterprise.security.auth.realm.file.FileRealm;
36 import com.sun.enterprise.security.auth.realm.BadRealmException;
37 import com.sun.enterprise.security.auth.realm.NoSuchUserException;
38
39
40 /**
41     <b>CAUTION: this code was COPIED from
42        com.sun.enterprise.admin.mbeans.AuthRealmMBean. This
43        class initially was a subclass of AuthRealmMBean, but
44        it is impossible to instantiate the superclass, due to its
45        hooks into MBeanRegistry, which throws an Exception when
46        running "offline". </b>
47     @see com.sun.enterprise.admin.mbeans.AuthRealmMBean
48  */

49 final class AuthRealmMBeanX
50 {
51     private final String JavaDoc mFile;
52     
53         public
54     AuthRealmMBeanX( final String JavaDoc file )
55     {
56         mFile = file;
57     }
58
59         private FileRealm
60     getRealmKeyFile()
61         throws MBeanConfigException
62     {
63         try
64         {
65             return new FileRealm( mFile );
66         }
67         catch(Exception JavaDoc e)
68         {
69             throw new MBeanConfigException( e.getMessage() );
70         }
71     }
72
73         private static String JavaDoc[]
74     toStringArray(final Enumeration JavaDoc e)
75     {
76         final List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
77         
78         while( e.hasMoreElements() )
79         {
80             list.add( "" + e.nextElement() );
81          }
82         return CollectionUtil.toStringArray( list );
83     }
84     
85         public String JavaDoc[]
86     getUserNames()
87         throws MBeanConfigException
88     {
89         final FileRealm realm = getRealmKeyFile();
90         try
91         {
92             return toStringArray(realm.getUserNames());
93         }
94         catch(BadRealmException bre)
95         {
96             throw new MBeanConfigException(bre.getMessage());
97         }
98     }
99
100     /**
101      * Returns names of all the groups from the instance realm keyfile
102      */

103     public String JavaDoc[] getGroupNames() throws MBeanConfigException
104     {
105         final FileRealm realm = getRealmKeyFile();
106         try
107         {
108             return toStringArray(realm.getGroupNames());
109         }
110         catch(BadRealmException bre)
111         {
112             throw new MBeanConfigException(bre.getMessage());
113         }
114     }
115
116     /**
117      * Returns the name of all the groups that this user belongs to from the instance realm keyfile
118      */

119     public String JavaDoc[] getUserGroupNames(String JavaDoc userName) throws MBeanConfigException
120     {
121         if( userName == null )
122         {
123             throw new IllegalArgumentException JavaDoc( "" + null );
124         }
125
126         final FileRealm realm = getRealmKeyFile();
127         try
128         {
129             return toStringArray(realm.getGroupNames(userName));
130         }
131         catch(NoSuchUserException nse)
132         {
133             throw new MBeanConfigException(nse.getMessage());
134         }
135     }
136
137     /**
138      * Adds new user to file realm. User cannot exist already.
139      */

140         public void
141     addUser(
142         final String JavaDoc userName,
143         final String JavaDoc password,
144         final String JavaDoc[] groupList)
145         throws MBeanConfigException
146     {
147         final FileRealm realm = getRealmKeyFile();
148         try
149         {
150             realm.addUser(userName, password, groupList);
151             saveInstanceRealmKeyFile(realm);
152         }
153         catch(Exception JavaDoc e)
154         {
155             throw new MBeanConfigException( e.getMessage() );
156         }
157     }
158
159     /**
160      * Remove user from file realm. User must exist.
161      */

162     public void removeUser( final String JavaDoc userName)
163         throws MBeanConfigException
164     {
165         final FileRealm realm = getRealmKeyFile();
166         try
167         {
168             realm.removeUser(userName);
169             saveInstanceRealmKeyFile(realm);
170         }
171         catch(NoSuchUserException nse)
172         {
173             throw new MBeanConfigException(nse.getMessage());
174         }
175     }
176
177     /**
178      * Update data for an existing user. User must exist.
179      This is equivalent to calling removeUser() followed by addUser().
180      */

181     public void updateUser(
182         final String JavaDoc userName,
183         final String JavaDoc password,
184         final String JavaDoc[] groupList)
185         throws MBeanConfigException
186     {
187         final FileRealm realm = getRealmKeyFile();
188         try
189         {
190             realm.updateUser(userName, userName, password, groupList);
191             saveInstanceRealmKeyFile( realm );
192         }
193         catch( Exception JavaDoc e )
194         {
195             throw new MBeanConfigException( e .getMessage() );
196         }
197     }
198
199     // ****************************************************************************
200
private void
201     saveInstanceRealmKeyFile(final FileRealm realm)
202         throws MBeanConfigException
203     {
204         try
205         {
206             realm.writeKeyFile( mFile );
207         }
208         catch(IOException JavaDoc e)
209         {
210             throw new MBeanConfigException( e.getMessage() );
211         }
212  }
213
214
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Popular Tags