1 23 24 package com.sun.enterprise.admin.event; 25 26 import java.lang.reflect.Constructor ; 27 28 import java.util.ArrayList ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import com.sun.enterprise.admin.event.AdminEvent; 33 import com.sun.enterprise.config.ConfigContext; 34 import com.sun.enterprise.config.ConfigChange; 35 import com.sun.enterprise.config.ConfigAdd; 36 import com.sun.enterprise.config.ConfigSet; 37 import com.sun.enterprise.config.ConfigUpdate; 38 import com.sun.enterprise.config.ConfigDelete; 39 import com.sun.enterprise.config.serverbeans.ServerTags; 40 41 import com.sun.enterprise.admin.meta.MBeanRegistry; 42 import com.sun.enterprise.admin.meta.MBeanRegistryFactory; 43 import com.sun.enterprise.admin.meta.MBeanRegistryEntry; 44 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 45 import com.sun.enterprise.admin.target.Target; 46 import com.sun.enterprise.admin.target.TargetBuilder; 47 48 import com.sun.enterprise.util.i18n.StringManager; 50 51 56 public class ElementChangeHelper{ 57 58 61 static final String eventType = ElementChangeHelper.class.getName(); 62 63 private static StringManager localStrings = StringManager.getManager( ElementChangeHelper.class ); 65 private static String PROPERTY_SUBSTR = "/"+ServerTags.ELEMENT_PROPERTY+"["; 66 67 public ElementChangeHelper() 69 { 70 } 71 72 static boolean isChangesToMerge(ConfigChange change1, ConfigChange change2) 74 { 75 String xpath1, xpath2; 76 if( change1==null || change2==null || 77 (xpath1 = change1.getXPath())==null || 78 (xpath2 = change2.getXPath())==null || 79 !compareXPathes( xpath1, xpath2) ) 80 return false; 81 if(isPropertyXPath(xpath2)) return true; 83 return ( xpath1.equals(change2.getXPath()) && 84 change1.getClass().equals(change2.getClass()) ); 85 } 86 87 static boolean checkChangeListForElement(ArrayList list) 89 { 90 if(list==null || list.size()<=0) 91 return false; 92 for(int i=1; i<list.size(); i++) 93 if(!isChangesToMerge((ConfigChange)list.get(i-1), (ConfigChange)list.get(i))) 94 return false; 95 return true; 96 } 97 static boolean isPropertyChange(ConfigChange change) 99 { 100 String xpath; 101 return (change!=null && 102 (xpath = change.getXPath())!=null && 103 (xpath.indexOf(PROPERTY_SUBSTR))>=0 ); 104 } 105 static boolean isPropertyXPath(String xpath) 107 { 108 return ((xpath != null) && ((xpath.indexOf(PROPERTY_SUBSTR))>=0)); 109 } 110 static String getConfigElementPrimaryKey(String xpath) 113 { 114 if (xpath != null) { 115 xpath = xpath.trim(); 116 } 117 if((xpath!= null) && (xpath.endsWith("']"))) 118 { 119 int idx = xpath.lastIndexOf('\'', xpath.length()-3); 120 return xpath.substring(idx+1, xpath.length()-2); 121 } 122 return null; 123 } 124 125 static String getConfigElementType(String xpath) 128 { 129 if(xpath.trim().endsWith("]")) 130 xpath = xpath.substring(0, xpath.lastIndexOf('[')); 131 return xpath.substring(0, xpath.lastIndexOf('/')); 132 } 133 134 static String getConfigElementTargetName(String xpath, ConfigContext ctx) 137 { 138 String targetName = null; 139 try { 140 TargetBuilder targetBuilder = new TargetBuilder(); 142 return targetBuilder.getTargetNameForXPath(xpath, ctx, true); 143 } catch(Exception e) 144 { 145 } 147 return null; 148 } 149 static String getElementXPath(ArrayList changes) 151 { 152 return getElementXPath((ConfigChange)changes.get(0)); 153 } 154 155 static String getElementXPath(ConfigChange change) 156 { 157 String xpath = change.getXPath(); 158 if(xpath==null) 159 return xpath; 160 int iMatch = xpath.indexOf(PROPERTY_SUBSTR); 161 if(iMatch<0) 162 return xpath; 163 return xpath.substring(0, iMatch); 164 } 165 static int getActionCodeForChanges(ArrayList changes) 167 { 168 return getActionCodeForChange((ConfigChange)changes.get(0)); 169 } 170 171 static int getActionCodeForChange(ConfigChange change) 172 { 173 if(isPropertyXPath(change.getXPath())) 174 return ElementChangeEvent.ACTION_ELEMENT_UPDATE; 175 int action; 176 if(change instanceof ConfigSet) 178 action = ElementChangeEvent.ACTION_ELEMENT_CREATE; 179 else if(change instanceof ConfigAdd) 180 action = ElementChangeEvent.ACTION_ELEMENT_CREATE; 181 else if(change instanceof ConfigUpdate) 182 action = ElementChangeEvent.ACTION_ELEMENT_UPDATE; 183 else if(change instanceof ConfigDelete) 184 action = ElementChangeEvent.ACTION_ELEMENT_DELETE; 185 else 186 action = ElementChangeEvent.ACTION_ELEMENT_UNDEFINED; 187 return action; 188 } 189 190 static private boolean compareXPathes(String xpath1, String xpath2) 192 { 193 int iProp; 194 if((iProp = xpath1.indexOf(PROPERTY_SUBSTR))>0) 195 xpath1 = xpath1.substring(0, iProp); 196 if((iProp = xpath2.indexOf(PROPERTY_SUBSTR))>0) 197 xpath2 = xpath2.substring(0, iProp); 198 return xpath1.equals(xpath2); 199 } 200 201 208 public AdminEvent[] generateElementChangeEventsFromChangeList(String instanceName, ArrayList changeList, ConfigContext domainContext) 209 { 210 ArrayList merged = new ArrayList (); 211 ArrayList events = new ArrayList (); 212 ConfigChange change, lastChange = null; 213 for(int i=0; i<changeList.size(); i++) 214 { 215 change = (ConfigChange)changeList.get(i); 216 if(merged.isEmpty() || 217 isChangesToMerge(lastChange, change)) 218 { 219 merged.add(change); 221 lastChange = change; 222 } 223 else 224 { 225 if(!merged.isEmpty()) 228 { 229 ArrayList new_events = createEventsForElementChange(instanceName, merged, domainContext, changeList); 230 if(new_events!=null && new_events.size()>0) 231 events.addAll(new_events); 232 merged.clear(); 234 merged.add(change); 236 lastChange = change; 237 } 238 } 239 } 240 if(!merged.isEmpty()) 242 { 243 ArrayList new_events = createEventsForElementChange(instanceName, merged, domainContext, changeList); 244 if(new_events!=null && new_events.size()>0) 245 events.addAll(new_events); 246 } 248 return (AdminEvent[])events.toArray(new AdminEvent[events.size()]); 249 } 250 251 259 public ArrayList createEventsForElementChange(String instanceName, ArrayList changeList, ConfigContext domainContext, ArrayList globalChangeList) 260 { 261 return createEventsForElementChange(instanceName, changeList, domainContext, true, globalChangeList); 262 } 263 264 private ArrayList createEventsForElementChange(String instanceName, ArrayList changeList, ConfigContext domainContext, boolean bCheckList, ArrayList globalChangeList) 265 { 266 267 if(bCheckList && !checkChangeListForElement(changeList)) 268 { 269 String msg = localStrings.getString( "admin.event.wrong_configchange" ); 270 throw new IllegalArgumentException ( msg ); 271 } 272 273 String xpath0 = getElementXPath(changeList); 274 if(xpath0==null) 275 return null; 276 280 MBeanRegistryEntry entry = getRegistryEntry(xpath0); 281 if(entry==null) 282 return null; 283 284 String eventName = entry.getElementChangeEventName(); 285 if(eventName!=null && eventName.indexOf('.')<0) 286 eventName = EVENT_PACKAGE_PATH_PREFIX + eventName; 287 if(eventName==null) 288 return null; 290 try { 292 return ElementChangeEventsFactory.createEvents(eventName, instanceName, 293 entry.getName(), changeList, domainContext, globalChangeList); 294 } catch(Exception e) 295 { 296 e.printStackTrace(); 297 String msg = localStrings.getString( "admin.event.cannot_create_eventforchange", eventName, xpath0 ); 298 throw new IllegalArgumentException ( msg ); 299 } 300 } 301 302 303 String lastXpath = null; 304 MBeanRegistryEntry lastEntry = null; 305 static final public String EVENT_PACKAGE_PATH_PREFIX = "com.sun.enterprise.admin.event."; 306 307 private synchronized MBeanRegistryEntry getRegistryEntry(String xpath) 308 { 309 if(xpath.equals(lastXpath)) 310 return lastEntry; 311 MBeanRegistry registry = MBeanRegistryFactory.getAdminMBeanRegistry(); 312 MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByXPath(xpath); 313 if(entry!=null) 314 { 315 lastXpath = xpath; 316 lastEntry = entry; 317 } 318 return entry; 319 } 320 321 static public Set getXPathesForDynamicallyNotReconfigurableElements(ArrayList changeList) 322 { 323 HashSet xpathes = new HashSet (); 324 if(changeList==null) 325 return xpathes; 326 327 MBeanRegistry registry = MBeanRegistryFactory.getAdminMBeanRegistry(); 328 String xpath_last = null; 329 MBeanRegistryEntry entry = null; 330 boolean bLastEntryHasEvent = false; 331 String propertyName = null; 332 333 for(int i=0; i<changeList.size();i++) 334 { 335 ConfigChange change = (ConfigChange)changeList.get(i); 336 int action = getActionCodeForChange(change); 337 if(action==ElementChangeEvent.ACTION_ELEMENT_UNDEFINED) 338 continue; String xpath = change.getXPath(); 340 if ( (xpath == null) || isXPathExcludedForRestartCheck(xpath) ) { 344 continue; 345 } 346 if(!xpath.equals(xpath_last)) 347 { 348 xpath_last = xpath; 349 if(isPropertyXPath(change.getXPath())) 350 { 351 propertyName = getConfigElementPrimaryKey(xpath); 352 xpath = getElementXPath(change); 353 } 354 else 355 { 356 propertyName = null; 357 } 358 entry = registry.findMBeanRegistryEntryByXPath(xpath); 359 if(entry==null) 360 { 361 xpathes.add(xpath); 362 continue; 363 } 364 bLastEntryHasEvent = (entry.getElementChangeEventName()!=null); 365 } 366 if(bLastEntryHasEvent) 367 continue; 368 if( (action==ElementChangeEvent.ACTION_ELEMENT_CREATE && 370 entry.isElementCreationDynamicallyReconfigurable())) 371 { 372 xpathes.add(xpath); 373 continue; 374 } 375 if( (action==ElementChangeEvent.ACTION_ELEMENT_DELETE && 377 entry.isElementDeletionDynamicallyReconfigurable())) 378 { 379 xpathes.add(xpath); 380 continue; 381 } 382 if(propertyName!=null) 384 { 385 if(!entry.isPropertyDynamicallyReconfigurable(propertyName)) 386 { 387 xpathes.add(xpath); 388 continue; 389 } 390 } 391 if ( !(change instanceof ConfigUpdate)) 392 { 393 xpathes.add(xpath); 394 continue; 395 } 396 Set attrs = ((ConfigUpdate)change).getAttributeSet(); 398 Iterator iter = attrs.iterator(); 399 while(iter.hasNext()) 400 { 401 String attr = (String )iter.next(); 402 if(!entry.isAttributeDynamicallyReconfigurable(attr)) 403 { 404 xpathes.add(xpath); 405 break; 406 } 407 } 408 } 409 return xpathes; 410 } 411 412 417 public static boolean getBooleanValue(String strValue) { 418 boolean retval = false; 419 if (strValue == null) { 420 return retval; 421 } 422 if (strValue.equalsIgnoreCase("true") 423 || strValue.equalsIgnoreCase("yes") 424 || strValue.equalsIgnoreCase("on") 425 || strValue.equalsIgnoreCase("1")) { 426 retval = true; 427 } 428 return retval; 429 } 430 431 441 private static boolean isXPathExcludedForRestartCheck(String xpath) 442 { 443 if (xpath == null) { 444 return false; 445 } 446 447 for(int excludeIdx =0; excludeIdx < restartExcludeXPaths.length; 448 excludeIdx++) { 449 450 if ( xpath.startsWith(restartExcludeXPaths[excludeIdx]) ) { 451 return true; 452 } 453 } 454 return false; 455 } 456 457 461 public static Boolean findEnabledChange(ArrayList changeList) 462 { 463 if(changeList==null) 464 return null; 465 for(int i=changeList.size()-1; i>=0; i--) 466 { 467 if ( changeList.get(i) instanceof ConfigUpdate) 468 { 469 ConfigUpdate update = (ConfigUpdate)changeList.get(i); 470 String enableStr = update.getNewValue(ServerTags.ENABLED); 471 if (enableStr != null) 472 return new Boolean (ElementChangeHelper.getBooleanValue(enableStr)); 473 } 474 } 475 return null; 476 } 477 478 480 483 private static String [] restartExcludeXPaths = { "/domain/lb-configs" }; 484 } 485 | Popular Tags |