KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.management.offline;
26
27 import java.util.Set JavaDoc;
28
29 import javax.management.AttributeNotFoundException JavaDoc;
30
31 import com.sun.enterprise.config.ConfigContext;
32 import com.sun.enterprise.config.ConfigBean;
33 import com.sun.enterprise.config.ConfigException;
34
35 import com.sun.appserv.management.config.AuthRealmConfig;
36
37 import com.sun.appserv.management.util.misc.GSetUtil;
38 import com.sun.appserv.management.util.misc.StringUtil;
39
40
41 final class AuthRealmConfigBeanHelper extends StdConfigBeanHelper
42 {
43     private AuthRealmSupport mSupport;
44     
45         public
46     AuthRealmConfigBeanHelper(
47         final ConfigContext configContext,
48         final ConfigBean configBean )
49     {
50         super( configContext, configBean );
51         
52         mSupport = createSupport();
53     }
54     
55     private static final String JavaDoc TEMPLATE_PREFIX = "${";
56     
57         private AuthRealmSupport
58    createSupport()
59    {
60         AuthRealmSupport support = null;
61         
62         if ( isStdFileRealm() )
63         {
64             String JavaDoc file = null;
65             try
66             {
67                 file = getFile();
68             }
69             catch( Exception JavaDoc e )
70             {
71                 // some realms are malformed
72
}
73             
74             if ( file != null && file.indexOf( TEMPLATE_PREFIX ) < 0 )
75             {
76                 support = new AuthRealmSupport( this );
77             }
78             else if ( file != null )
79             {
80                 // can't support it
81
}
82         }
83         
84         return support;
85    }
86     
87         private boolean
88    isStdFileRealm()
89    {
90         return AuthRealmConfig.DEFAULT_REALM_CLASSNAME.equals( getClassname() );
91    }
92     
93         private void
94     checkRealmType()
95     {
96         if ( ! isStdFileRealm() )
97         {
98             throw new IllegalArgumentException JavaDoc(
99                 "AuthRealm type " + getClassname() +" not supported." );
100         }
101     }
102    
103     private static final String JavaDoc USER_NAMES_ATTR = "UserNames";
104     private static final String JavaDoc GROUP_NAMES_ATTR = "GroupNames";
105     
106         protected Set JavaDoc<String JavaDoc>
107     _getAttributeNames()
108     {
109         final Set JavaDoc<String JavaDoc> attrNames = super._getAttributeNames();
110         
111         if ( isStdFileRealm() )
112         {
113             attrNames.add( GROUP_NAMES_ATTR );
114             attrNames.add( USER_NAMES_ATTR );
115         }
116         
117         return attrNames;
118     }
119     
120         protected Class JavaDoc
121     getAttributeClass( final String JavaDoc attrName )
122     {
123         Class JavaDoc result = null;
124         
125         if ( isStdFileRealm() &&
126             ( GROUP_NAMES_ATTR.equals( attrName ) ||
127                 USER_NAMES_ATTR.equals( attrName ) ) )
128         {
129             result = String JavaDoc[].class;
130         }
131         else
132         {
133             result = super.getAttributeClass( attrName );
134         }
135         return result;
136     }
137     
138         public Object JavaDoc
139     getAttribute( final String JavaDoc attrName )
140         throws AttributeNotFoundException JavaDoc
141     {
142         Object JavaDoc result = null;
143         
144         if ( GROUP_NAMES_ATTR.equals( attrName ) )
145         {
146             checkRealmType();
147             result = mSupport.getGroupNames();
148             //sdebug( "GroupNames: " + StringUtil.toString( (String[])result ) );
149
}
150         else if ( USER_NAMES_ATTR.equals( attrName ) )
151         {
152             checkRealmType();
153             result = mSupport.getUserNames();
154             //sdebug( "UserNames: " + StringUtil.toString( (String[])result ) );
155
}
156         else
157         {
158             result = super.getAttribute( attrName );
159         }
160         return result;
161     }
162     
163     static private final Set JavaDoc<String JavaDoc> SUPPORTED_OPERATIONS =
164         GSetUtil.newUnmodifiableStringSet(
165             "getUserGroupNames", "addUser", "updateUser", "removeUser" );
166     
167         public Object JavaDoc
168     handleInvoke(
169         String JavaDoc operationName,
170         Object JavaDoc[] args,
171         String JavaDoc[] types )
172     {
173         Object JavaDoc result = null;
174         final int numArgs = args == null ? 0 : args.length;
175         
176         if ( isStdFileRealm() &&
177             SUPPORTED_OPERATIONS.contains( operationName ) && numArgs >= 1 )
178         {
179             final String JavaDoc user = (String JavaDoc)args[ 0 ];
180                 
181             if ( operationName.equals( "getUserGroupNames" ) && numArgs == 1)
182             {
183                 result = mSupport.getUserGroupNames( user );
184             }
185             else if ( operationName.equals( "addUser" ) && numArgs == 3)
186             {
187                 final String JavaDoc password = (String JavaDoc)args[ 1 ];
188                 final String JavaDoc[] groupList = (String JavaDoc[])args[ 2 ];
189                 mSupport.addUser( user, password, groupList );
190             }
191             else if ( operationName.equals( "updateUser" ) && numArgs == 3)
192             {
193                 final String JavaDoc password = (String JavaDoc)args[ 1 ];
194                 final String JavaDoc[] groupList = (String JavaDoc[])args[ 2 ];
195                 mSupport.updateUser( user, password, groupList );
196             }
197             else if ( operationName.equals( "removeUser" ) && numArgs == 1)
198             {
199                 mSupport.removeUser( user );
200             }
201             else
202             {
203                 unsupportedOperation( operationName, args, types );
204             }
205         }
206         else
207         {
208             unsupportedOperation( operationName, args, types );
209         }
210         //sdebug( "handleInvoke: " + operationName + "(): " + result );
211

212         return result;
213     }
214     
215         public String JavaDoc
216     getFile()
217     {
218         return getPropertyValue( "file" );
219     }
220     
221     
222         public String JavaDoc
223     getClassname()
224     {
225         try
226         {
227             return (String JavaDoc)getAttribute( "Classname" );
228         }
229         catch( AttributeNotFoundException JavaDoc e )
230         {
231             e.printStackTrace();
232             throw new RuntimeException JavaDoc( e );
233         }
234     }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
Popular Tags