KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > FileUserHandler


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  * FileUserHandler.java
26  *
27  */

28
29 package com.sun.enterprise.tools.admingui.handlers;
30
31
32 import java.util.EventObject JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import com.iplanet.jato.view.View;
36 import com.iplanet.jato.view.ViewBase;
37 import com.iplanet.jato.view.ViewBean;
38 import com.iplanet.jato.model.DefaultModel;
39 import com.iplanet.jato.view.ContainerViewBase;
40 import com.iplanet.jato.RequestContext;
41 import com.iplanet.jato.RequestManager;
42
43 import javax.management.MBeanException JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45 import javax.management.AttributeList JavaDoc;
46 import javax.management.Attribute JavaDoc;
47
48 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
49 import com.sun.enterprise.tools.guiframework.model.ModelManager;
50 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
51 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
52 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
53 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
54 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
55 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
56
57 import com.sun.web.ui.model.CCActionTableModelInterface;
58
59 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
60 import com.sun.enterprise.tools.admingui.util.Util;
61 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode;
62 import com.sun.enterprise.tools.admingui.tree.IndexTreeModelImpl;
63 import com.sun.enterprise.tools.admingui.tree.IndexTreeModel;
64
65 public class FileUserHandler {
66
67     public boolean manageUsers(RequestContext ctx, HandlerContext handlerCtx) {
68         DescriptorContainerView parent = (DescriptorContainerView) handlerCtx.getView().getParent();
69
70     String JavaDoc classname = (String JavaDoc)handlerCtx.getInputValue("classname") ;
71         if (classname == null) {
72             if (Util.isLoggableFINER()) {
73                 Util.logFINER("The manageUsers 'classname' input variable was not specified. ("+
74                     classname + ")");
75             }
76             return false;
77         }
78         Class JavaDoc realm = null;;
79         try {
80             realm = Class.forName(classname);
81         } catch (ClassNotFoundException JavaDoc ex) {
82             if (Util.isLoggableFINER()) {
83                 Util.logFINER("The FileRealm classname, "+classname+" was not found.");
84             }
85         }
86         Class JavaDoc baseClass = null;;
87         try {
88             baseClass = Class.forName("com.sun.enterprise.security.auth.realm.IASRealm");
89         } catch (ClassNotFoundException JavaDoc ex) {
90             if (Util.isLoggableFINER()) {
91                 Util.logFINER("The class: " +
92                 "\"com.sun.enterprise.security.auth.realm.IASRealm\"" +
93                 " was not found.");
94             }
95         }
96         if (realm == null || baseClass == null)
97             return false;
98         
99         if (baseClass.isAssignableFrom(realm))
100             return hasPropsForFileRealm(parent);
101         
102         if (Util.isLoggableFINER()) {
103             Util.logFINER("The FileRealm classname, \"" + classname +
104                 "\" does not extend \"com.sun.enterprise.security.auth.realm.IASRealm\".");
105         }
106         
107     //if (classname != null && classname.equals("com.sun.enterprise.security.auth.realm.file.FileRealm")) {
108
// return true;
109
//}
110
return false;
111     }
112     
113     private boolean hasPropsForFileRealm(DescriptorContainerView parent) {
114         Object JavaDoc objectName = parent.getViewDescriptor().getParameter("objectName");
115         if (objectName == null)
116             return false;
117         
118         AttributeList JavaDoc props = (AttributeList JavaDoc)MBeanUtil.invoke(objectName.toString(),
119             "getProperties", null, null);
120         if (props == null)
121             return false;
122         
123         boolean jassContextFound = false;
124         boolean fileRealmFound = false;
125
126         for (int i = 0; i < props.size(); i++) {
127             Attribute JavaDoc prop = (Attribute JavaDoc)props.get(i);
128             String JavaDoc name = prop.getName();
129             if (name.equals("jaas-context") && prop.getValue() != null)
130                 jassContextFound = true;
131             if (name.equals("file") && prop.getValue() != null)
132                 fileRealmFound = true;
133         }
134         if (jassContextFound && fileRealmFound)
135             return true;
136         return false;
137     }
138
139     public void populateFileUsersModel(RequestContext ctx, HandlerContext handlerCtx) {
140     View view = handlerCtx.getView();
141     if (!(handlerCtx.getEvent() instanceof BeforeCreateEvent)) {
142         ViewDescriptor desc = (view instanceof DescriptorContainerView) ?
143         ((DescriptorContainerView)view).getViewDescriptor() :
144         (ViewDescriptor)null;
145         throw new FrameworkException("This handler is for 'beforeCreate'" +
146         " handlers only!", desc, view);
147     }
148     ViewDescriptor desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
149     try {
150             if (desc instanceof CCActionTableDescriptor) {
151         CCActionTableDescriptor ccDesc = (CCActionTableDescriptor)desc;
152                 load(ccDesc.getModel(), ctx, handlerCtx);
153         }
154         } catch (Exception JavaDoc ex) {
155             throw new FrameworkException(ex, desc, view);
156         }
157     }
158
159     private void load(CCActionTableModelInterface model,
160                 RequestContext ctx, HandlerContext handlerCtx) {
161         Object JavaDoc[] userNames = (Object JavaDoc[])handlerCtx.getInputValue("userNames");
162     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
163         if (userNames == null) {
164             return; //nothing to load..
165
}
166         for (int rowNo = 0; rowNo < userNames.length; rowNo++) {
167             model.appendRow();
168         //userId, and groupList fieldNames are defined in the xml file.
169
model.setValue(USER_ID, userNames[rowNo]);
170         String JavaDoc groupNames = getGroupNames((String JavaDoc)userNames[rowNo], objectName);
171         model.setValue(GROUP_LIST, groupNames);
172         }
173     }
174
175     public void addOrUpdateFileUser(RequestContext ctx, HandlerContext handlerCtx) {
176     View view = handlerCtx.getView();
177         if (!(view instanceof DescriptorContainerView)) {
178             View parent = view.getParent();
179             if (!(parent instanceof DescriptorContainerView)) {
180         throw new FrameworkException("view is not DescriptorContainerView.", null, view);
181             }
182             else {
183                 view = parent;
184             }
185         }
186         if (view instanceof DescriptorCCPageTitle) {
187             view = view.getParent();
188         }
189         DescriptorContainerView descView = (DescriptorContainerView)view;
190
191     String JavaDoc user = (String JavaDoc)descView.getDisplayFieldValue(USER_ID);
192     String JavaDoc passwd = (String JavaDoc)descView.getDisplayFieldValue(PASSWORD);
193     String JavaDoc groupList = (String JavaDoc)descView.getDisplayFieldValue(GROUP_LIST);
194         
195     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
196     String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue("methodName");
197         if (methodName == null || (methodName.equals("addUser") == false &&
198             methodName.equals("updateUser") == false)) {
199             throw new FrameworkException(
200                 "Illegal method name, must be \"addUser\" or \"updateUser\": " +
201                 methodName, descView.getViewDescriptor(), view);
202         }
203         groupList = groupList.replaceAll(" ", "");
204     String JavaDoc[] groups = getStringArray(groupList, ",");
205         Object JavaDoc[] params = new Object JavaDoc[]{user, passwd, groups};
206     String JavaDoc[] types = new String JavaDoc[]{"java.lang.String",
207                                       "java.lang.String",
208                                       groups.getClass().getName()};
209     if (Util.isLoggableFINER()) {
210             // Log some trace info
211
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
212             buf.append("USER NAME = "+user);
213             buf.append(", PASSWORD = "+passwd);
214             buf.append(", GROUP = "+groupList);
215             buf.append(", OBJECTNAME = "+objectName);
216             buf.append(", METHODNAME = "+methodName);
217         Util.logFINER(buf.toString());
218     }
219
220         try {
221             Object JavaDoc object = MBeanUtil.invoke(objectName, methodName, params, types);
222         } catch (Exception JavaDoc ex) {
223             throw new FrameworkException(ex, descView.getViewDescriptor(), view);
224         }
225     }
226
227     //can be an utility method
228
private String JavaDoc[] getStringArray(String JavaDoc str, String JavaDoc delimiter) {
229     if(str == null) {
230         return null;
231     }
232     StringTokenizer JavaDoc strToken = new StringTokenizer JavaDoc(str, delimiter);
233     String JavaDoc[] strArray = new String JavaDoc[strToken.countTokens()];
234     int i = 0;
235
236     while(strToken.hasMoreTokens()) {
237         strArray[i++] = strToken.nextToken();
238     }
239
240     return strArray;
241     }
242     
243     public void getUser(RequestContext ctx, HandlerContext handlerCtx) {
244         handlerCtx.setOutputValue("user", ctx.getRequest().getRemoteUser());
245     }
246
247     public void populateFileUsersDisplayFields(RequestContext ctx, HandlerContext handlerCtx) {
248     View view = handlerCtx.getView();
249         if (!(view instanceof DescriptorContainerView)) {
250             View parent = view.getParent();
251             if (!(parent instanceof DescriptorContainerView)) {
252         throw new FrameworkException(
253             "view is not DescriptorContainerView", null, view);
254             } else {
255                 view = parent;
256             }
257         }
258         if (view instanceof DescriptorCCPageTitle) {
259             view = view.getParent();
260         }
261
262         DescriptorContainerView descView = (DescriptorContainerView)view;
263         
264     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
265     String JavaDoc userName = (String JavaDoc)handlerCtx.getInputValue("userName");
266     String JavaDoc groupNames = getGroupNames(userName, objectName);
267
268         descView.setDisplayFieldValue(USER_ID, userName);
269     descView.setDisplayFieldValue(GROUP_LIST, groupNames);
270     }
271
272     private String JavaDoc getGroupNames(String JavaDoc userName, String JavaDoc objectName) {
273     String JavaDoc[] groupList = null;
274     String JavaDoc groupNames = null;
275     try {
276         Object JavaDoc obj = MBeanUtil.invoke(objectName, GETUSER_GROUPNAME,
277                 new Object JavaDoc[]{userName}, new String JavaDoc[]{"java.lang.String"});
278             groupList = (String JavaDoc[])obj;
279         } catch (Exception JavaDoc ex) {
280         throw new FrameworkException(ex);
281         }
282     if(groupList != null && groupList.length > 0) {
283         groupNames = groupList[0];
284         for (int i = 1; i < groupList.length; i++) {
285         groupNames += ","+groupList[i];
286         }
287     }
288     return groupNames;
289     }
290
291
292     public void deleteFileUser(RequestContext ctx, HandlerContext handlerCtx) {
293     View view = handlerCtx.getView();
294     DescriptorContainerView descView = (DescriptorContainerView)
295         (((ViewBase)view).getParentViewBean());
296     ViewDescriptor vd = descView.getViewDescriptor();
297     String JavaDoc childName = (String JavaDoc)vd.getParameter("tableChildName");
298
299     if (childName == null) {
300         throw new FrameworkException("tableChildName not specified", vd, view);
301     }
302     ViewDescriptor tableDesc = vd.getChildDescriptor(childName);
303     if (tableDesc == null) {
304         throw new FrameworkException("tableDescriptor is null", vd, view);
305     }
306     if (!(tableDesc instanceof CCActionTableDescriptor)) {
307         throw new FrameworkException("tableDescriptor is of wrong type",
308         tableDesc, view);
309     }
310         CCActionTableModelInterface model =
311             ((CCActionTableDescriptor)tableDesc).getModel();
312
313         String JavaDoc deleteKey = (String JavaDoc)handlerCtx.getInputValue("deleteKey");
314     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
315
316     if (deleteKey == null) {
317         throw new FrameworkException("No delete key specified", tableDesc, null);
318     }
319     if (objectName == null) {
320         throw new FrameworkException("No ObjectName specified", tableDesc, null);
321     }
322
323     model.setRowSelectionType("multiple");
324     try {
325         model.beforeFirst();
326         // from the model, get the child that has the needed value...
327
while (model.next()) {
328         if (model.isRowSelected()) {
329             Object JavaDoc[] params;
330             ObjectName JavaDoc retObject = null;
331             String JavaDoc[] types;
332             if (Util.isLoggableFINEST()) {
333             Util.logFINEST("USERNAME TO DELETE ="+model.getValue(deleteKey));
334             }
335             params = new Object JavaDoc[]{model.getValue(deleteKey)};
336             types = new String JavaDoc[]{"java.lang.String"};
337             retObject = (ObjectName JavaDoc)MBeanUtil.invoke(objectName, DELETE_USER,
338                             params, types);
339             model.setRowSelected(false);
340         }
341         }
342     } catch (Exception JavaDoc ex) {
343         throw new FrameworkException(
344         "Exception while attempting to delete user!", ex, tableDesc, null);
345         }
346         ((ContainerViewBase)descView).removeChild(childName);
347     ((DefaultModel)model).clear();
348     }
349
350
351     //mbean method names
352
private static final String JavaDoc GETUSER_GROUPNAME = "getUserGroupNames";
353     private static final String JavaDoc GETUSER_NAME = "getUserNames";
354     private static final String JavaDoc DELETE_USER = "removeUser";
355
356     //DisplayField names
357
private static final String JavaDoc USER_ID = "userId";
358     private static final String JavaDoc GROUP_LIST = "groupList";
359     private static final String JavaDoc PASSWORD = "password";
360 }
361
Popular Tags