1 23 24 package com.sun.enterprise.tools.admingui.handlers; 25 26 import java.util.List ; 27 import java.util.EventObject ; 28 import java.util.Properties ; 29 import java.util.Enumeration ; 30 31 import com.iplanet.jato.RequestContext; 32 import com.iplanet.jato.RequestManager; 33 import com.iplanet.jato.view.View; 34 import com.iplanet.jato.view.ViewBase; 35 import com.iplanet.jato.view.ViewBean; 36 import com.iplanet.jato.model.DefaultModel; 37 import com.iplanet.jato.model.ModelControlException; 38 import com.sun.enterprise.tools.admingui.util.Util; 39 40 import com.sun.web.ui.model.CCActionTableModel; 41 import javax.management.AttributeList ; 42 import javax.management.Attribute ; 43 44 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 45 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView; 46 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 47 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 48 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor; 49 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent; 50 51 import com.sun.web.ui.model.CCActionTableModelInterface; 52 53 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 54 import com.sun.enterprise.tools.admingui.util.Util; 55 56 57 public class PropertiesHandlers { 58 59 public static final String PROPERTY_NAME = "propertyName"; 60 public static final String PROPERTY_VALUE = "propertyValue"; 61 public static final String PROPERTY_WEIGHT = "weight"; 62 public static final String PROPERTY_DEFAULT_VALUE = "defaultValue"; 63 public static final String PROPERTY_ORIGINAL_OVERRIDE_VALUE = "originalOverrideValue"; 64 65 private CCActionTableModelInterface getModel(HandlerContext handlerCtx) { 66 CCActionTableModelInterface model = 67 (CCActionTableModelInterface)handlerCtx.getInputValue("propertiesModel"); 68 70 if (model == null) { 71 throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified"); 72 } 73 return model; 74 } 75 76 private void loadModel(CCActionTableModelInterface model, AttributeList attrs, Object excludes) 77 throws ModelControlException { 78 RequestContext ctx = RequestManager.getRequestContext(); 79 if (attrs == null) 80 return; 81 StringBuffer buf = new StringBuffer (); 83 if (Util.isLoggableFINER()) { 84 for(int rowNo = 0; rowNo < attrs.size(); rowNo++) { 85 Attribute attr = (Attribute )attrs.get(rowNo); 86 buf.append("properties table : name = " + attr.getName() + " , value = " + attr.getValue()+"\n"); 87 } 88 Util.logFINER(buf.toString()); 89 } 90 model.beforeFirst(); 91 for (int rowNo = 0; rowNo < attrs.size(); rowNo++) { 92 93 Attribute attr = (Attribute )attrs.get(rowNo); 94 if (! shouldExclude(attr.getName(), excludes)){ 95 model.appendRow(); 96 model.setValue(PROPERTY_NAME, attr.getName()); 97 model.setValue(PROPERTY_VALUE, attr.getValue()); 98 model.setRowSelected(false); 99 } 100 } 101 } 102 103 private void loadModel(CCActionTableModelInterface model, Properties props, Object excludes) 104 throws ModelControlException { 105 if (props == null) 106 return; 107 ((DefaultModel)model).clear(); 109 model.beforeFirst(); 110 Enumeration ee = props.propertyNames(); 111 while (ee.hasMoreElements()) { 112 String name = (String )ee.nextElement(); 113 if (!shouldExclude(name, excludes)){ 114 model.appendRow(); 115 model.setValue(PROPERTY_NAME, name); 116 model.setValue(PROPERTY_VALUE, props.getProperty(name)); 117 model.setRowSelected(false); 118 if (Util.isLoggableFINER()) { 119 Util.logFINER("properties table : " + 120 name + ":" + props.getProperty(name)); 121 } 122 } 123 } 124 } 125 126 private boolean shouldExclude(String name, Object excludes){ 127 List list = (List ) excludes; 128 if(list == null || list.isEmpty()) 129 return false; 130 for(int i=0; i< list.size(); i++){ 131 if (list.get(i).equals(name)){ 132 return true; 133 } 134 } 135 return false; 136 } 137 138 private void loadModel(CCActionTableModelInterface model, 139 Properties configProps, Properties targetProps, 140 boolean instPropsOnly) 141 throws ModelControlException { 142 if (configProps == null) 143 return; 144 model.beforeFirst(); 145 Enumeration ee = configProps.propertyNames(); 146 if (instPropsOnly == false) { 147 while (ee.hasMoreElements()) { 148 String name = (String )ee.nextElement(); 149 model.appendRow(); 150 model.setValue(PROPERTY_NAME, name); 151 model.setValue(PROPERTY_DEFAULT_VALUE, configProps.getProperty(name)); 152 if (targetProps != null) { 153 model.setValue(PROPERTY_VALUE, targetProps.getProperty(name, "")); 154 model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, targetProps.getProperty(name, "")); 155 } else { 156 model.setValue(PROPERTY_VALUE, ""); 157 model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, ""); 158 } 159 model.setRowSelected(false); 160 if (Util.isLoggableFINER()) { 161 String msg = "properties table : " + name + ":" 162 + configProps.getProperty(name); 163 if (targetProps != null) 164 msg += ":" + targetProps.getProperty(name, ""); 165 Util.logFINER( msg); 167 } 168 } 169 } else { 170 ee = targetProps.propertyNames(); 172 while (ee.hasMoreElements()) { 173 String name = (String )ee.nextElement(); 174 String value = configProps.getProperty(name, null); 175 if (value == null) { 176 model.appendRow(); 177 model.setValue(PROPERTY_NAME, name); 178 model.setValue(PROPERTY_VALUE, targetProps.getProperty(name, "")); 179 model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, targetProps.getProperty(name, "")); 180 } 181 } 182 } 183 } 184 185 186 public void loadProperties(RequestContext ctx, HandlerContext handlerCtx) { 187 try { 188 CCActionTableModelInterface model = getModel(handlerCtx); 189 ((DefaultModel)model).clear(); 190 Object obj = handlerCtx.getInputValue("properties"); 191 Object excludes = handlerCtx.getInputValue("excludes"); 192 if (obj instanceof AttributeList ) { 193 loadModel(model, (AttributeList )obj, excludes); 194 } 195 else { 196 loadModel(model, (Properties )obj, excludes); 197 } 198 } catch (Exception ex) { 199 throw new FrameworkException("loadProperties: Loading error. ", ex); 200 } 201 } 202 203 public void loadSystemProperties(RequestContext ctx, HandlerContext handlerCtx) { 204 try { 205 CCActionTableModelInterface model = getModel(handlerCtx); 206 String objectName = (String )handlerCtx.getInputValue("objectName"); 207 String methodName = (String )handlerCtx.getInputValue("methodName"); 208 String configName = (String )handlerCtx.getInputValue("configName"); 209 String targetName = (String )handlerCtx.getInputValue("targetName"); 210 Boolean inherit = (Boolean )handlerCtx.getInputValue("inherit"); 211 Boolean instPropsOnly = (Boolean )handlerCtx.getInputValue("instPropsOnly"); 212 if (objectName == null || methodName == null || configName == null) { 213 throw new FrameworkException("Missing input parameter to loadSystemProperties()"); 214 } 215 216 String [] types = new String []{"java.lang.String", "boolean"}; 217 Object [] params = new Object []{configName, inherit}; 218 Properties configProps = (Properties ) 219 MBeanUtil.invoke(objectName, methodName, params, types); 220 221 Properties targetProps = null; 222 if (targetName != null) { 223 params = new Object []{targetName, inherit}; 224 targetProps = (Properties ) 225 MBeanUtil.invoke(objectName, methodName, params, types); 226 } 227 loadModel(model, configProps, targetProps, instPropsOnly.booleanValue()); 228 } catch (Exception ex) { 229 throw new FrameworkException("loadSystemProperties: Loading error. ", ex); 230 } 231 } 232 233 public void saveProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 234 Object objectName = handlerCtx.getInputValue("objectName"); 235 if (objectName == null) { 236 throw new FrameworkException( 237 "objectName not specified in saveProperties."); 238 } 239 240 String setMethodName = (String )handlerCtx.getInputValue("setMethodName"); 241 if (setMethodName == null) { 242 setMethodName="setProperty"; 243 } 244 245 CCActionTableModelInterface model = getModel(handlerCtx); 246 model.beforeFirst(); 248 249 250 AttributeList originalProperties = (AttributeList )handlerCtx.getInputValue("originalProperties"); 251 AttributeList attrs = new AttributeList (); 252 while(model.next()) { 253 String name = (String ) model.getValue(PROPERTY_NAME); 254 String value = (String ) model.getValue(PROPERTY_VALUE); 255 if (!Util.isEmpty(name) && !Util.isEmpty(value)) { 256 attrs.add(new Attribute (name, value)); 257 } 258 } 259 260 String [] type = new String []{"javax.management.Attribute"}; 261 262 if (originalProperties != null) { 263 for (int i = 0; i < originalProperties.size(); i++) { 264 Attribute originalProperty = (Attribute )originalProperties.get(i); 265 boolean found = false; 266 for (int j = 0; j < attrs.size(); j++) { 267 Attribute attr = (Attribute )(attrs.get(j)); 268 if (attr.getName().equals(originalProperty.getName())) { 269 found = true; 270 break; 271 } 272 } 273 if (!found) { 274 Attribute propertyToClear = new Attribute (originalProperty.getName(), null); 275 Object [] params = new Object []{propertyToClear}; 276 MBeanUtil.invoke(objectName.toString(), setMethodName, params, type); 277 } 278 } 279 } 280 for (int i = 0; i < attrs.size(); i++) { 281 Attribute attr = (Attribute )attrs.get(i); 282 Object [] params = new Object []{attr}; 283 MBeanUtil.invoke(objectName.toString(), setMethodName, params, type); 284 } 285 } 286 287 288 public void saveSystemProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 289 Object objectName = handlerCtx.getInputValue("objectName"); 290 if (objectName == null) { 291 throw new FrameworkException( 292 "objectName not specified in saveSystemProperties."); 293 } 294 String target = (String )handlerCtx.getInputValue("target"); 295 if (target == null) { 296 throw new FrameworkException( 297 "target not specified in saveSystemProperties."); 298 } 299 String createMethodName = (String )handlerCtx.getInputValue("createMethodName"); 300 if (createMethodName == null) 301 createMethodName = "createSystemProperties"; 302 String deleteMethodName = (String )handlerCtx.getInputValue("deleteMethodName"); 303 if (deleteMethodName == null) 304 deleteMethodName = "deleteSystemProperty"; 305 306 CCActionTableModelInterface model = getModel(handlerCtx); 307 model.beforeFirst(); 309 310 Properties originalProperties = (Properties )handlerCtx.getInputValue("originalProperties"); 311 Properties propertiesToCreate = new Properties (); 312 while(model.next()) { 313 String name = (String ) model.getValue(PROPERTY_NAME); 314 if (name != null && (!name.trim().equals(""))) { 315 String value = (String ) model.getValue(PROPERTY_VALUE); 317 propertiesToCreate.setProperty(name, value); 318 } 320 } 321 String [] types = null; 322 if (originalProperties != null) { 323 Enumeration deleteProps = originalProperties.propertyNames(); 324 types = new String []{"java.lang.String", "java.lang.String"}; 325 while(deleteProps.hasMoreElements()) { 326 String name = (String )deleteProps.nextElement(); 327 Object [] params = new Object []{name, target}; 328 MBeanUtil.invoke(objectName.toString(), deleteMethodName, params, types); 329 } 330 } 331 types = new String []{"java.util.Properties", "java.lang.String"}; 332 Object [] params = new Object []{propertiesToCreate, target}; 333 MBeanUtil.invoke(objectName.toString(), createMethodName, params, types); 334 } 335 336 public void deleteInstOnlyProps(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 337 String deleteMethodName = (String )handlerCtx.getInputValue("deleteMethodName"); 338 if (deleteMethodName == null) 339 deleteMethodName = "deleteSystemProperty"; 340 341 String objectName = (String )handlerCtx.getInputValue("objectName"); 342 String methodName = (String )handlerCtx.getInputValue("methodName"); 343 String configName = (String )handlerCtx.getInputValue("configName"); 344 String targetName = (String )handlerCtx.getInputValue("targetName"); 345 Boolean inherit = (Boolean )handlerCtx.getInputValue("inherit"); 346 if (objectName == null || methodName == null || configName == null || targetName == null) { 347 throw new FrameworkException("Missing input parameter to deleteInstOnlyProps()"); 348 } 349 350 String [] types = new String []{"java.lang.String", "boolean"}; 351 Object [] params = new Object []{configName, inherit}; 352 Properties configProps = (Properties ) 353 MBeanUtil.invoke(objectName, methodName, params, types); 354 355 Properties targetProps = null; 356 params = new Object []{targetName, inherit}; 357 targetProps = (Properties ) 358 MBeanUtil.invoke(objectName, methodName, params, types); 359 360 types = new String []{"java.lang.String", "java.lang.String"}; 361 Enumeration ee = targetProps.propertyNames(); 362 while (ee.hasMoreElements()) { 363 String name = (String )ee.nextElement(); 364 String value = configProps.getProperty(name, null); 365 if (value == null) { 366 params = new Object []{name, targetName}; 367 MBeanUtil.invoke(objectName, deleteMethodName, params, types); 368 } 369 } 370 } 371 372 373 public void saveSystemPropertiesValues(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 374 Object objectName = handlerCtx.getInputValue("objectName"); 375 if (objectName == null) { 377 throw new FrameworkException( 378 "objectName not specified in saveSystemProperties."); 379 } 380 String target = (String )handlerCtx.getInputValue("target"); 381 if (target == null) { 382 throw new FrameworkException( 383 "target not specified in saveSystemProperties."); 384 } 385 String createMethodName = (String )handlerCtx.getInputValue("createMethodName"); 386 if (createMethodName == null) 387 createMethodName = "createSystemProperties"; 388 String deleteMethodName = (String )handlerCtx.getInputValue("deleteMethodName"); 389 if (deleteMethodName == null) 390 deleteMethodName = "deleteSystemProperty"; 391 392 CCActionTableModelInterface model = getModel(handlerCtx); 393 model.beforeFirst(); 395 String [] deleteTypes = new String []{"java.lang.String", "java.lang.String"}; 396 397 Properties propertiesToCreate = new Properties (); 398 while(model.next()) { 399 String name = (String ) model.getValue(PROPERTY_NAME); 400 String originalValue = (String ) model.getValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE); 401 String newValue = (String ) model.getValue(PROPERTY_VALUE); 402 if (name == null || (name.trim().length() == 0)) 404 continue; 405 if (originalValue.trim().equals(newValue.trim())) { 406 continue; 408 } 409 if (originalValue.trim().length() != 0 && newValue.trim().length() == 0) { 410 Object [] params = new Object []{name, target}; 412 try { 414 MBeanUtil.invoke(objectName.toString(), deleteMethodName, params, deleteTypes); 415 } catch (Exception ex) { 416 System.out.println("error while trying to delete a system property!!"); 417 } 418 } 420 if (newValue.trim().length() != 0) { 421 propertiesToCreate.setProperty(name, newValue); 423 } 425 } 426 if (propertiesToCreate.size() > 0) { 427 String [] types = new String []{"java.util.Properties", "java.lang.String"}; 428 Object [] params = new Object []{propertiesToCreate, target}; 429 try { 430 MBeanUtil.invoke(objectName.toString(), createMethodName, params, types); 431 } catch (Exception ex) { 432 } 434 } 435 } 437 438 public void deleteProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 439 CCActionTableModelInterface model = getModel(handlerCtx); 440 AttributeList attrs = new AttributeList (); 441 model.setRowSelectionType("multiple"); 442 model.beforeFirst(); 443 while(model.next()) { 444 if (!model.isRowSelected()) { 445 String name = (String ) model.getValue(PROPERTY_NAME); 446 String value = (String )model.getValue(PROPERTY_VALUE); 447 attrs.add(new Attribute (name, value)); 448 } 449 } 450 ((DefaultModel)model).clear(); 451 model.beforeFirst(); 452 for (int i = 0; i < attrs.size(); i++) { 453 model.appendRow(); 454 Attribute attr = (Attribute )attrs.get(i); 455 model.setValue(PROPERTY_NAME, attr.getName()); 456 model.setValue(PROPERTY_VALUE, attr.getValue()); 457 model.setRowSelected(false); 458 } 459 } 460 461 public void clearProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 462 CCActionTableModelInterface model = getModel(handlerCtx); 463 ((DefaultModel)model).clear(); 464 model.beforeFirst(); 465 } 466 467 public void addProperty(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 468 CCActionTableModelInterface model = getModel(handlerCtx); 469 model.setRowSelectionType("multiple"); 470 model.appendRow(); 471 model.setValue(PROPERTY_VALUE, ""); 472 model.setValue(PROPERTY_NAME, ""); 473 model.sort(); 474 model.beforeFirst(); 475 } 476 477 public void getPropertiesObjectFromTable(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 479 Boolean ignoreEmptyValues = (Boolean )handlerCtx.getInputValue("ignoreEmptyValues"); 480 String propValue = (String )handlerCtx.getInputValue("propValue"); 481 CCActionTableModelInterface model = getModel(handlerCtx); 482 model.beforeFirst(); 485 CCActionTableModel cct = (CCActionTableModel) model; 486 Properties properties = new Properties (); 487 while (model.next()) { 488 String name = (String ) model.getValue(PROPERTY_NAME); 489 String value; 490 if(propValue != null) { 491 value = (String ) model.getValue(propValue); 492 } else { 493 value = (String ) model.getValue(PROPERTY_VALUE); 494 } 495 if (!Util.isEmpty(name)) { 496 if (Util.isEmpty(value)) { 497 if (ignoreEmptyValues!=null && ignoreEmptyValues.booleanValue()==true) 498 continue; 499 } 500 properties.setProperty(name, value); 501 } 502 } 503 handlerCtx.setOutputValue("properties", properties); 504 } 505 506 507 public void sortModel(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 508 CCActionTableModelInterface model = getModel(handlerCtx); 509 if (model != null) { 510 model.sort(); 511 } 512 } 513 514 public void addTableRow(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 515 CCActionTableModelInterface model = 516 (CCActionTableModelInterface)handlerCtx.getInputValue("model"); 517 model.setRowSelectionType("multiple"); 518 model.appendRow(); 519 model.beforeFirst(); 520 } 521 } 522 | Popular Tags |