1 23 24 package com.sun.enterprise.tools.admingui.handlers; 25 26 import java.util.ArrayList ; 27 import java.util.EventObject ; 28 import java.util.StringTokenizer ; 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 ; 36 import javax.management.Attribute ; 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 displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 66 ArrayList modelNames = (ArrayList )handlerCtx.getInputValue(MODEL_NAMES); 67 String objectName = (String )handlerCtx.getInputValue(OBJECT_NAME); 68 String methodName = (String )handlerCtx.getInputValue(METHOD_NAME); 69 String username = (String )handlerCtx.getInputValue(DISPLAY_USERNAME); 70 String password = (String )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 attrList = new AttributeList (); 91 Attribute attr; 92 for (int i = 0; i < displayNames.size(); i++) { 93 String value = (String )descView.getDisplayFieldValue(((String )(displayNames.get(i)))); 94 String [] str = null; 95 String modelName = (String )modelNames.get(i); 96 if (modelName.equals(MODEL_PRINCIPAL) || modelName.equals(MODEL_GROUP)) { 97 if (Util.isEmpty(value)) 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 [1]; 105 str[0] = value; 106 } 107 } 108 if (str != null) { 109 attr = new Attribute (modelName, str); 110 } else { 111 attr = new Attribute (modelName, value); 112 } 113 attrList.add(attr); 114 } 115 Object params[] = {attrList, username, password, null}; 116 String types[] = { 117 "javax.management.AttributeList", 118 "java.lang.String", 119 "java.lang.String", 120 "java.lang.String" 121 }; 122 123 Object returnValue = null; 124 try { 125 returnValue = MBeanUtil.invoke(objectName, methodName, params, types); 126 } catch (Exception 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 objectName = (String )handlerCtx.getInputValue(OBJECT_NAME); 148 String methodName = (String )handlerCtx.getInputValue(METHOD_NAME); 149 String mapName = (String )handlerCtx.getInputValue(MAP_NAME); 150 String poolName = (String )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 principalNames = null; 165 String userNames = null; 166 Object params[] = {mapName, poolName}; 167 String types[] = { 168 "java.lang.String", 169 "java.lang.String" 170 }; 171 172 String poolname = null; 173 String mapname = null; 174 String userName = null; 175 String password = null; 176 String principals[] = null; 177 String usergroups[] = null; 178 179 try { 180 Object obj = MBeanUtil.invoke(objectName, GET_ATTRIBUTES_METHOD, params, types); 181 AttributeList attrList = (AttributeList )obj; 182 if(attrList != null){ 183 int s = attrList.size(); 184 185 for(int i=0;i<s;i++){ 186 Attribute attribute =(Attribute )attrList.get(i); 187 if(attribute.getName().equals(MODEL_NAME)){ 188 mapname =(String )attribute.getValue(); 189 continue; 190 } 191 if ((attribute.getName().equals(MODEL_PRINCIPAL))) 192 principals = (String [])attribute.getValue(); 193 if ((attribute.getName().equals(MODEL_GROUP))) 194 usergroups =(String [])attribute.getValue(); 195 if((attribute.getName()).equals(MODEL_USERNAME)) 196 userName = (String )attribute.getValue(); 197 if((attribute.getName()).equals(MODEL_PASSWORD)) 198 password = (String )attribute.getValue(); 199 200 } 201 202 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 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 displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 241 ArrayList modelNames = (ArrayList )handlerCtx.getInputValue(MODEL_NAMES); 242 String objectName = (String )handlerCtx.getInputValue(OBJECT_NAME); 243 String poolName = (String )handlerCtx.getInputValue(POOL_NAME); 244 245 248 String mapName = (String )descView.getDisplayFieldValue(DIPSLAY_NAME); 249 String pswd = (String )descView.getDisplayFieldValue(DISPLAY_PASSWORD); 250 String username = (String )descView.getDisplayFieldValue(DISPLAY_USERNAME); 251 String groupList = (String )descView.getDisplayFieldValue(DISPLAY_GROUP); 252 String groupListHidden = (String )descView.getDisplayFieldValue(DISPLAY_GROUP_ORIG); 253 String principalList = (String )descView.getDisplayFieldValue(DISPLAY_PRINCIPAL); 254 String principalListHidden = (String )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 [] groups = getStringArray(groupList, ","); 274 String [] principals = getStringArray(principalList, ","); 275 String groupListOrig[] = getStringArray(groupListHidden, ","); 276 String principalListOrig[] = getStringArray(principalListHidden, ","); 277 278 String groupRemoves = getRemoves(groupListOrig, groups); 279 String groupAdds = getAdds(groups, groupListOrig); 280 String principalRemoves = getRemoves(principalListOrig, principals); 281 String principalAdds = getAdds(principals, principalListOrig); 282 283 AttributeList attrList = new AttributeList (); 284 Attribute attr; 285 attr = new Attribute (MODEL_POOLNAME, poolName); 286 attrList.add(attr); 287 attr = new Attribute (MODEL_NAME, mapName); 288 attrList.add(attr); 289 attr = new Attribute (MODEL_USERNAME, username); 290 attrList.add(attr); 291 attr = new Attribute (MODEL_PASSWORD, pswd); 292 attrList.add(attr); 293 attr = new Attribute (MODEL_ADD_PRINCIPALS, principalAdds); 294 attrList.add(attr); 295 attr = new Attribute (MODEL_ADD_GROUPS, groupAdds); 296 attrList.add(attr); 297 attr = new Attribute (MODEL_REMOVE_PRINCIPALS, principalRemoves); 298 attrList.add(attr); 299 attr = new Attribute (MODEL_REMOVE_GROUPS, groupRemoves); 300 attrList.add(attr); 301 Object params[] = {attrList, null}; 302 String types[] = { 303 "javax.management.AttributeList", 304 "java.lang.String" 305 }; 306 307 try { 308 Object object = MBeanUtil.invoke(objectName, UPDATE_ATTRIBUTES_METHOD, params, types); 309 } catch (Exception ex) { 310 throw new FrameworkException(ex, descView.getViewDescriptor(), view); 311 } 312 } 313 314 public String getRemoves(String [] orig, String [] updates) { 315 String 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 getAdds(String [] updates, String [] orig) { 338 String 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 [] getStringArray(String str, String delimiter) { 362 if(str == null) { 363 return null; 364 } 365 str = str.replaceAll(" ", ""); 366 StringTokenizer strToken = new StringTokenizer (str, delimiter); 367 String [] strArray = new String [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 getString(String [] values) { 379 if (values == null || values.length < 1 ) 380 return ""; 381 String 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 DISPLAY_NAMES = "displayNames"; 391 public static final String MODEL_NAMES = "attributeNames"; 392 public static final String OBJECT_NAME = "objectName"; 393 public static final String METHOD_NAME = "methodName"; 394 public static final String POOL_NAME = "poolName"; 395 public static final String MAP_NAME = "mapName"; 396 397 public static final String GET_ATTRIBUTES_METHOD = "getAttributes"; 398 public static final String UPDATE_ATTRIBUTES_METHOD = "updateSecurityMap"; 399 400 public static final String MODEL_NAME = "name"; 401 public static final String MODEL_PRINCIPAL = "principal"; 402 public static final String MODEL_GROUP = "user_group"; 403 public static final String MODEL_USERNAME = "user_name"; 404 public static final String MODEL_PASSWORD = "password"; 405 public static final String MODEL_POOLNAME = "pool_name"; 406 public static final String MODEL_ADD_PRINCIPALS = "add_principals"; 407 public static final String MODEL_REMOVE_PRINCIPALS = "remove_principals"; 408 public static final String MODEL_ADD_GROUPS = "add_user_groups"; 409 public static final String MODEL_REMOVE_GROUPS = "remove_user_groups"; 410 411 public static final String DIPSLAY_NAME = "name"; 412 public static final String DISPLAY_PRINCIPAL = "principals"; 413 public static final String DISPLAY_PRINCIPAL_ORIG = "principalsOrig"; 414 public static final String DISPLAY_GROUP = "userGroups"; 415 public static final String DISPLAY_GROUP_ORIG = "userGroupsOrig"; 416 public static final String DISPLAY_USERNAME = "username"; 417 public static final String DISPLAY_PASSWORD = "password"; 418 419 420 public static final String SESSION_GROUPS = "groups"; 421 public static final String SESSION_PRINCIPALS = "principals"; 422 } 423 | Popular Tags |