KickJava   Java API By Example, From Geeks To Geeks.

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


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.tools.admingui.handlers;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.EventObject JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import com.iplanet.jato.view.View;
31 import com.iplanet.jato.RequestContext;
32 import com.iplanet.jato.RequestManager;
33 import com.iplanet.jato.model.Model;
34
35 import javax.management.AttributeList JavaDoc;
36 import javax.management.Attribute JavaDoc;
37
38 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
39 import com.sun.enterprise.tools.admingui.util.Util;
40
41 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
42 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
43 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
44 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
45 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
46
47
48 public class SecurityMapHandlers {
49
50     public void createSecurityMap(RequestContext ctx, HandlerContext handlerCtx) {
51     View view = handlerCtx.getView();
52         if (!(view instanceof DescriptorContainerView)) {
53             View parent = view.getParent();
54             if (!(parent instanceof DescriptorContainerView)) {
55                 throw new FrameworkException(
56             "view is not DescriptorContainerView", null, view);
57             } else {
58                 view = parent;
59             }
60         }
61         if (view instanceof DescriptorCCPageTitle) {
62             view = view.getParent();
63         }
64         DescriptorContainerView descView = (DescriptorContainerView)view;
65         ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
66         ArrayList JavaDoc modelNames = (ArrayList JavaDoc)handlerCtx.getInputValue(MODEL_NAMES);
67         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
68         String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue(METHOD_NAME);
69         String JavaDoc username = (String JavaDoc)handlerCtx.getInputValue(DISPLAY_USERNAME);
70         String JavaDoc password = (String JavaDoc)handlerCtx.getInputValue(DISPLAY_PASSWORD);
71
72         if (displayNames == null)
73             throw new FrameworkException("DisplayNames not specified", descView.getViewDescriptor(), view);
74
75         if (modelNames == null)
76             throw new FrameworkException("ModelNames not specified", descView.getViewDescriptor(), view);
77
78         if (objectName == null)
79             throw new FrameworkException("objectName not specified", descView.getViewDescriptor(), view);
80
81         if (methodName == null)
82             throw new FrameworkException("methodName not specified", descView.getViewDescriptor(), view);
83
84         if (username == null)
85             throw new FrameworkException("username not specified", descView.getViewDescriptor(), view);
86
87         if (password == null)
88             throw new FrameworkException("password not specified", descView.getViewDescriptor(), view);
89
90         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
91         Attribute JavaDoc attr;
92         for (int i = 0; i < displayNames.size(); i++) {
93             String JavaDoc value = (String JavaDoc)descView.getDisplayFieldValue(((String JavaDoc)(displayNames.get(i))));
94             String JavaDoc[] str = null;
95             String JavaDoc modelName = (String JavaDoc)modelNames.get(i);
96             if (modelName.equals(MODEL_PRINCIPAL) || modelName.equals(MODEL_GROUP)) {
97                 if (Util.isEmpty(value)) //only put in either user group or principal (anissa)
98
continue;
99                 if (value != null)
100                     value = value.replaceAll(" ", "");
101                 if (value != null && value.indexOf(",") != -1) {
102                     str = getStringArray(value, ",");
103                 } else {
104                     str = new String JavaDoc[1];
105                     str[0] = value;
106                 }
107             }
108             if (str != null) {
109                 attr = new Attribute JavaDoc(modelName, str);
110             } else {
111                 attr = new Attribute JavaDoc(modelName, value);
112             }
113             attrList.add(attr);
114         }
115         Object JavaDoc params[] = {attrList, username, password, null};
116         String JavaDoc types[] = {
117             "javax.management.AttributeList",
118             "java.lang.String",
119             "java.lang.String",
120             "java.lang.String"
121         };
122
123         Object JavaDoc returnValue = null;
124         try {
125             returnValue = MBeanUtil.invoke(objectName, methodName, params, types);
126         } catch (Exception JavaDoc ex) {
127         throw new FrameworkException(
128         ex, descView.getViewDescriptor(), view);
129         }
130     }
131
132     public void populateSMDisplayFields(RequestContext ctx, HandlerContext handlerCtx) {
133     View view = handlerCtx.getView();
134         if (!(view instanceof DescriptorContainerView)) {
135             View parent = view.getParent();
136             if (!(parent instanceof DescriptorContainerView)) {
137                 throw new FrameworkException(
138             "view is not DescriptorContainerView", null, view);
139             } else {
140                 view = parent;
141             }
142         }
143         if (view instanceof DescriptorCCPageTitle) {
144             view = view.getParent();
145         }
146         DescriptorContainerView descView = (DescriptorContainerView)view;
147         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
148         String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue(METHOD_NAME);
149         String JavaDoc mapName = (String JavaDoc)handlerCtx.getInputValue(MAP_NAME);
150         String JavaDoc poolName = (String JavaDoc)handlerCtx.getInputValue(POOL_NAME);
151         if (objectName == null) {
152             throw new FrameworkException("Object Name not specified", descView.getViewDescriptor(), view);
153     }
154         if (methodName == null) {
155             throw new FrameworkException("Method Name not specified", descView.getViewDescriptor(), view);
156     }
157         if (mapName == null) {
158             throw new FrameworkException("Map Name not specified", descView.getViewDescriptor(), view);
159     }
160         if (poolName == null) {
161             throw new FrameworkException("poolName not specified", descView.getViewDescriptor(), view);
162     }
163
164         String JavaDoc principalNames = null;
165         String JavaDoc userNames = null;
166         Object JavaDoc params[] = {mapName, poolName};
167         String JavaDoc types[] = {
168             "java.lang.String",
169             "java.lang.String"
170         };
171
172         String JavaDoc poolname = null;
173         String JavaDoc mapname = null;
174         String JavaDoc userName = null;
175         String JavaDoc password = null;
176         String JavaDoc principals[] = null;
177         String JavaDoc usergroups[] = null;
178
179         try {
180             Object JavaDoc obj = MBeanUtil.invoke(objectName, GET_ATTRIBUTES_METHOD, params, types);
181             AttributeList JavaDoc attrList = (AttributeList JavaDoc)obj;
182             if(attrList != null){
183                 int s = attrList.size();
184
185                 for(int i=0;i<s;i++){
186                     Attribute JavaDoc attribute =(Attribute JavaDoc)attrList.get(i);
187                     if(attribute.getName().equals(MODEL_NAME)){
188                         mapname =(String JavaDoc)attribute.getValue();
189                         continue;
190                     }
191                     if ((attribute.getName().equals(MODEL_PRINCIPAL)))
192                         principals = (String JavaDoc[])attribute.getValue();
193                     if ((attribute.getName().equals(MODEL_GROUP)))
194                         usergroups =(String JavaDoc[])attribute.getValue();
195                     if((attribute.getName()).equals(MODEL_USERNAME))
196                         userName = (String JavaDoc)attribute.getValue();
197                     if((attribute.getName()).equals(MODEL_PASSWORD))
198                         password = (String JavaDoc)attribute.getValue();
199
200                 }
201
202                 //ctx.getRequest().getSession().setAttribute(SESSION_GROUPS, usergroups);
203
//ctx.getRequest().getSession().setAttribute(SESSION_PRINCIPALS, principals);
204

205                 principalNames = getString(principals);
206                 userNames = getString(usergroups);
207
208                 descView.setDisplayFieldValue(DIPSLAY_NAME, mapname);
209                 descView.setDisplayFieldValue(DISPLAY_PRINCIPAL, principalNames);
210                 descView.setDisplayFieldValue(DISPLAY_PRINCIPAL_ORIG, principalNames);
211                 descView.setDisplayFieldValue(DISPLAY_GROUP, userNames);
212                 descView.setDisplayFieldValue(DISPLAY_GROUP_ORIG, userNames);
213                 descView.setDisplayFieldValue(DISPLAY_USERNAME, userName);
214                 descView.setDisplayFieldValue(DISPLAY_PASSWORD, password);
215             }
216         } catch (Exception JavaDoc ex) {
217             throw new FrameworkException(
218         "failed while getting attribute names and values", ex,
219         descView.getViewDescriptor(), view);
220         }
221     }
222
223     public void updateSecurityMap(RequestContext ctx, HandlerContext handlerCtx) {
224     View view = handlerCtx.getView();
225         if (!(view instanceof DescriptorContainerView)) {
226             View parent = view.getParent();
227             if (!(parent instanceof DescriptorContainerView)) {
228                 throw new FrameworkException(
229             "view is not DescriptorContainerView", null, view);
230             } else {
231                 view = parent;
232             }
233         }
234         if (view instanceof DescriptorCCPageTitle) {
235             view = view.getParent();
236         }
237
238         DescriptorContainerView descView = (DescriptorContainerView)view;
239
240         ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
241         ArrayList JavaDoc modelNames = (ArrayList JavaDoc)handlerCtx.getInputValue(MODEL_NAMES);
242         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
243         String JavaDoc poolName = (String JavaDoc)handlerCtx.getInputValue(POOL_NAME);
244         
245         //String groupListOrig[] = (String[])ctx.getRequest().getSession().getAttribute(SESSION_GROUPS);
246
//String principalListOrig[] = (String[])ctx.getRequest().getSession().getAttribute(SESSION_PRINCIPALS);
247

248         String JavaDoc mapName = (String JavaDoc)descView.getDisplayFieldValue(DIPSLAY_NAME);
249         String JavaDoc pswd = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_PASSWORD);
250         String JavaDoc username = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_USERNAME);
251         String JavaDoc groupList = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_GROUP);
252         String JavaDoc groupListHidden = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_GROUP_ORIG);
253         String JavaDoc principalList = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_PRINCIPAL);
254         String JavaDoc principalListHidden = (String JavaDoc)descView.getDisplayFieldValue(DISPLAY_PRINCIPAL_ORIG);
255
256         if (objectName == null) {
257             throw new FrameworkException("Object Name not specified",
258         descView.getViewDescriptor(), view);
259     }
260         if (poolName == null) {
261             throw new FrameworkException("Pool Name not specified",
262         descView.getViewDescriptor(), view);
263     }
264         if (displayNames == null) {
265             throw new FrameworkException("Display names not specified",
266         descView.getViewDescriptor(), view);
267     }
268         if (modelNames == null) {
269             throw new FrameworkException("Model names not specified",
270         descView.getViewDescriptor(), view);
271     }
272
273         String JavaDoc[] groups = getStringArray(groupList, ",");
274         String JavaDoc[] principals = getStringArray(principalList, ",");
275         String JavaDoc groupListOrig[] = getStringArray(groupListHidden, ",");
276         String JavaDoc principalListOrig[] = getStringArray(principalListHidden, ",");
277
278         String JavaDoc groupRemoves = getRemoves(groupListOrig, groups);
279         String JavaDoc groupAdds = getAdds(groups, groupListOrig);
280         String JavaDoc principalRemoves = getRemoves(principalListOrig, principals);
281         String JavaDoc principalAdds = getAdds(principals, principalListOrig);
282
283         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
284         Attribute JavaDoc attr;
285         attr = new Attribute JavaDoc(MODEL_POOLNAME, poolName);
286         attrList.add(attr);
287         attr = new Attribute JavaDoc(MODEL_NAME, mapName);
288         attrList.add(attr);
289         attr = new Attribute JavaDoc(MODEL_USERNAME, username);
290         attrList.add(attr);
291         attr = new Attribute JavaDoc(MODEL_PASSWORD, pswd);
292         attrList.add(attr);
293         attr = new Attribute JavaDoc(MODEL_ADD_PRINCIPALS, principalAdds);
294         attrList.add(attr);
295         attr = new Attribute JavaDoc(MODEL_ADD_GROUPS, groupAdds);
296         attrList.add(attr);
297         attr = new Attribute JavaDoc(MODEL_REMOVE_PRINCIPALS, principalRemoves);
298         attrList.add(attr);
299         attr = new Attribute JavaDoc(MODEL_REMOVE_GROUPS, groupRemoves);
300         attrList.add(attr);
301         Object JavaDoc params[] = {attrList, null};
302         String JavaDoc types[] = {
303             "javax.management.AttributeList",
304             "java.lang.String"
305         };
306
307         try {
308             Object JavaDoc object = MBeanUtil.invoke(objectName, UPDATE_ATTRIBUTES_METHOD, params, types);
309         } catch (Exception JavaDoc ex) {
310             throw new FrameworkException(ex, descView.getViewDescriptor(), view);
311         }
312     }
313
314     public String JavaDoc getRemoves(String JavaDoc[] orig, String JavaDoc[] updates) {
315         String JavaDoc removes = null;
316         if (orig != null && updates != null) {
317             for (int i=0; i < orig.length ; i++) {
318                 boolean match = false;
319                 for (int j=0; j < updates.length; j++) {
320                     if(updates[j].equals(orig[i])){
321                         match = true;
322                     }
323                 }
324                 if (!match) {
325                     if (removes == null) {
326                         removes = orig[i];
327                     } else {
328                         removes += ","+orig[i];
329                     }
330                 }
331             }
332         }
333
334         return removes;
335     }
336
337     public String JavaDoc getAdds(String JavaDoc[] updates, String JavaDoc[] orig) {
338         String JavaDoc adds = null;
339         if(orig != null && updates != null){
340             for(int i=0; i < updates.length ; i++) {
341                 boolean match = false;
342                 for (int j=0; j < orig.length; j++) {
343                     if(orig[j].equals(updates[i])){
344                         match = true;
345                     }
346
347                 }
348                 if(!match) {
349                     if(adds == null) {
350                         adds = updates[i];
351                     } else {
352                         adds += ","+updates[i];
353                     }
354                 }
355
356             }
357         }
358         return adds;
359     }
360
361     private String JavaDoc[] getStringArray(String JavaDoc str, String JavaDoc delimiter) {
362         if(str == null) {
363             return null;
364         }
365         str = str.replaceAll(" ", "");
366         StringTokenizer JavaDoc strToken = new StringTokenizer JavaDoc(str, delimiter);
367         String JavaDoc[] strArray = new String JavaDoc[strToken.countTokens()];
368         int i = 0;
369
370         while(strToken.hasMoreTokens()) {
371             strArray[i++] = strToken.nextToken();
372         }
373
374         return strArray;
375     }
376
377
378     private String JavaDoc getString(String JavaDoc[] values) {
379         if (values == null || values.length < 1 )
380             return "";
381         String JavaDoc str = values[0];
382         for (int j = 1; j < values.length; j++) {
383             str += ","+values[j];
384         }
385
386         return str;
387     }
388
389
390     public static final String JavaDoc DISPLAY_NAMES = "displayNames";
391     public static final String JavaDoc MODEL_NAMES = "attributeNames";
392     public static final String JavaDoc OBJECT_NAME = "objectName";
393     public static final String JavaDoc METHOD_NAME = "methodName";
394     public static final String JavaDoc POOL_NAME = "poolName";
395     public static final String JavaDoc MAP_NAME = "mapName";
396
397     public static final String JavaDoc GET_ATTRIBUTES_METHOD = "getAttributes";
398     public static final String JavaDoc UPDATE_ATTRIBUTES_METHOD = "updateSecurityMap";
399     
400     public static final String JavaDoc MODEL_NAME = "name";
401     public static final String JavaDoc MODEL_PRINCIPAL = "principal";
402     public static final String JavaDoc MODEL_GROUP = "user_group";
403     public static final String JavaDoc MODEL_USERNAME = "user_name";
404     public static final String JavaDoc MODEL_PASSWORD = "password";
405     public static final String JavaDoc MODEL_POOLNAME = "pool_name";
406     public static final String JavaDoc MODEL_ADD_PRINCIPALS = "add_principals";
407     public static final String JavaDoc MODEL_REMOVE_PRINCIPALS = "remove_principals";
408     public static final String JavaDoc MODEL_ADD_GROUPS = "add_user_groups";
409     public static final String JavaDoc MODEL_REMOVE_GROUPS = "remove_user_groups";
410
411     public static final String JavaDoc DIPSLAY_NAME = "name";
412     public static final String JavaDoc DISPLAY_PRINCIPAL = "principals";
413     public static final String JavaDoc DISPLAY_PRINCIPAL_ORIG = "principalsOrig";
414     public static final String JavaDoc DISPLAY_GROUP = "userGroups";
415     public static final String JavaDoc DISPLAY_GROUP_ORIG = "userGroupsOrig";
416     public static final String JavaDoc DISPLAY_USERNAME = "username";
417     public static final String JavaDoc DISPLAY_PASSWORD = "password";
418
419
420     public static final String JavaDoc SESSION_GROUPS = "groups";
421     public static final String JavaDoc SESSION_PRINCIPALS = "principals";
422 }
423
Popular Tags