1 23 24 28 29 package com.sun.enterprise.tools.admingui.handlers; 30 31 32 import java.util.EventObject ; 33 import java.util.StringTokenizer ; 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 ; 44 import javax.management.ObjectName ; 45 import javax.management.AttributeList ; 46 import javax.management.Attribute ; 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 classname = (String )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 realm = null;; 79 try { 80 realm = Class.forName(classname); 81 } catch (ClassNotFoundException ex) { 82 if (Util.isLoggableFINER()) { 83 Util.logFINER("The FileRealm classname, "+classname+" was not found."); 84 } 85 } 86 Class baseClass = null;; 87 try { 88 baseClass = Class.forName("com.sun.enterprise.security.auth.realm.IASRealm"); 89 } catch (ClassNotFoundException 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 return false; 111 } 112 113 private boolean hasPropsForFileRealm(DescriptorContainerView parent) { 114 Object objectName = parent.getViewDescriptor().getParameter("objectName"); 115 if (objectName == null) 116 return false; 117 118 AttributeList props = (AttributeList )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 prop = (Attribute )props.get(i); 128 String 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 ex) { 155 throw new FrameworkException(ex, desc, view); 156 } 157 } 158 159 private void load(CCActionTableModelInterface model, 160 RequestContext ctx, HandlerContext handlerCtx) { 161 Object [] userNames = (Object [])handlerCtx.getInputValue("userNames"); 162 String objectName = (String )handlerCtx.getInputValue("objectName"); 163 if (userNames == null) { 164 return; } 166 for (int rowNo = 0; rowNo < userNames.length; rowNo++) { 167 model.appendRow(); 168 model.setValue(USER_ID, userNames[rowNo]); 170 String groupNames = getGroupNames((String )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 user = (String )descView.getDisplayFieldValue(USER_ID); 192 String passwd = (String )descView.getDisplayFieldValue(PASSWORD); 193 String groupList = (String )descView.getDisplayFieldValue(GROUP_LIST); 194 195 String objectName = (String )handlerCtx.getInputValue("objectName"); 196 String methodName = (String )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 [] groups = getStringArray(groupList, ","); 205 Object [] params = new Object []{user, passwd, groups}; 206 String [] types = new String []{"java.lang.String", 207 "java.lang.String", 208 groups.getClass().getName()}; 209 if (Util.isLoggableFINER()) { 210 StringBuffer buf = new StringBuffer (); 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 object = MBeanUtil.invoke(objectName, methodName, params, types); 222 } catch (Exception ex) { 223 throw new FrameworkException(ex, descView.getViewDescriptor(), view); 224 } 225 } 226 227 private String [] getStringArray(String str, String delimiter) { 229 if(str == null) { 230 return null; 231 } 232 StringTokenizer strToken = new StringTokenizer (str, delimiter); 233 String [] strArray = new String [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 objectName = (String )handlerCtx.getInputValue("objectName"); 265 String userName = (String )handlerCtx.getInputValue("userName"); 266 String groupNames = getGroupNames(userName, objectName); 267 268 descView.setDisplayFieldValue(USER_ID, userName); 269 descView.setDisplayFieldValue(GROUP_LIST, groupNames); 270 } 271 272 private String getGroupNames(String userName, String objectName) { 273 String [] groupList = null; 274 String groupNames = null; 275 try { 276 Object obj = MBeanUtil.invoke(objectName, GETUSER_GROUPNAME, 277 new Object []{userName}, new String []{"java.lang.String"}); 278 groupList = (String [])obj; 279 } catch (Exception 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 childName = (String )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 deleteKey = (String )handlerCtx.getInputValue("deleteKey"); 314 String objectName = (String )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 while (model.next()) { 328 if (model.isRowSelected()) { 329 Object [] params; 330 ObjectName retObject = null; 331 String [] types; 332 if (Util.isLoggableFINEST()) { 333 Util.logFINEST("USERNAME TO DELETE ="+model.getValue(deleteKey)); 334 } 335 params = new Object []{model.getValue(deleteKey)}; 336 types = new String []{"java.lang.String"}; 337 retObject = (ObjectName )MBeanUtil.invoke(objectName, DELETE_USER, 338 params, types); 339 model.setRowSelected(false); 340 } 341 } 342 } catch (Exception 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 private static final String GETUSER_GROUPNAME = "getUserGroupNames"; 353 private static final String GETUSER_NAME = "getUserNames"; 354 private static final String DELETE_USER = "removeUser"; 355 356 private static final String USER_ID = "userId"; 358 private static final String GROUP_LIST = "groupList"; 359 private static final String PASSWORD = "password"; 360 } 361 | Popular Tags |