1 19 20 40 41 42 package org.netbeans.modules.j2ee.deployment.config; 43 44 import javax.enterprise.deploy.spi.*; 45 import javax.enterprise.deploy.spi.exceptions.*; 46 import javax.enterprise.deploy.model.*; 47 import javax.enterprise.deploy.shared.*; 48 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 49 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 50 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 51 import org.netbeans.modules.j2ee.deployment.impl.Server; 52 import org.netbeans.modules.schema2beans.*; 53 import org.openide.nodes.*; 54 import org.openide.*; 55 import org.openide.util.WeakListeners; 56 57 import java.util.*; 58 59 import java.beans.*; 60 61 public class ModuleDeploymentSupport implements PropertyChangeListener { 62 63 public static final String SEPARATOR = "/"; public static final String WEBSERVICES_XML = "webservices.xml"; private static Map filenameToPathMap = null; 66 67 private final DeployableObjectImpl obj; 68 private final Map rootMap = Collections.synchronizedMap(new HashMap(5)); private final Map beanMap = Collections.synchronizedMap(new IdentityHashMap()); private final Map leafMap = Collections.synchronizedMap(new IdentityHashMap()); private final Set xpathListeners = Collections.synchronizedSet(new HashSet()); 72 private final Map<DDRoot, PropertyChangeListener> propertyChangeListeners = Collections.synchronizedMap(new WeakHashMap()); 73 private final J2eeModuleProvider provider; 74 private final boolean sendEvent; 75 76 private static Map moduleDDlocationMap = new HashMap(10); 77 static { 78 moduleDDlocationMap.put(J2eeModule.EAR, new String [] { 79 J2eeModule.APP_XML}); 80 moduleDDlocationMap.put(J2eeModule.WAR, new String [] { 81 J2eeModule.WEB_XML, 82 J2eeModule.WEBSERVICES_XML}); 83 moduleDDlocationMap.put(J2eeModule.EJB, new String [] { 84 J2eeModule.EJBJAR_XML, 85 J2eeModule.EJBSERVICES_XML}); 86 moduleDDlocationMap.put(J2eeModule.CONN, new String [] { 87 J2eeModule.CONNECTOR_XML}); 88 moduleDDlocationMap.put(J2eeModule.CLIENT, new String [] { 89 J2eeModule.CLIENT_XML}); 90 } 91 92 public static String [] getDDPaths(Object type) { 93 return (String []) moduleDDlocationMap.get(type); 94 } 95 96 public ModuleDeploymentSupport(J2eeModuleProvider provider, boolean sendEvent) { 97 this.provider = provider; 98 this.sendEvent = sendEvent; 99 obj = new DeployableObjectImpl(this); 100 String [] ddLocs = getDDPaths(provider.getJ2eeModule().getModuleType()); 101 for(int i = 0; i < ddLocs.length; i++) { 102 createRoot(ddLocs[i]); 103 } 104 } 105 106 private ModuleType getModuleType() { 107 return (ModuleType) provider.getJ2eeModule().getModuleType(); 108 } 109 110 private BaseBean getDeploymentDescriptor(String ddLoc) { 111 return provider.getJ2eeModule().getDeploymentDescriptor(ddLoc); 112 } 113 114 private DDRoot createRoot(String ddLoc) { 115 BaseBean bean = getDeploymentDescriptor(ddLoc); 116 if (bean == null) return null; 118 while(!bean.isRoot()) bean = bean.parent(); 119 DDRoot root = new DDRoot(new DDNodeBean(null,bean,this)); 120 rootMap.put(ddLoc,root); 121 122 beanMap.put(bean,root); 123 PropertyChangeListener weakListener = WeakListeners.propertyChange(this, root.proxy.bean); 124 propertyChangeListeners.put(root, weakListener); 125 root.proxy.bean.addPropertyChangeListener(weakListener); 126 return root; 127 } 128 129 public DeployableObjectImpl getDeployableObject() { 130 return obj; 131 } 132 133 public DDRoot getDDBeanRoot() { 135 String loc = ((String []) moduleDDlocationMap.get(getModuleType()))[0]; 136 return getDDBeanRoot(loc); 137 } 138 139 public DDRoot getDDBeanRoot(String loc) { 140 DDRoot root = (DDRoot) rootMap.get(loc); 141 142 if (root == null && ! isPrimaryDD(loc, getType())) { 145 root = createRoot(loc); 146 DDRoot proot = getPrimaryDD(); 147 } 148 return root; 149 } 150 151 public ModuleType getType() { 152 return getModuleType(); 153 } 154 155 public String getVersion() { 156 return provider.getJ2eeModule().getModuleVersion(); 157 } 158 159 160 public Class getClassFromScope(String cls) { 161 return null; } 163 164 static public boolean isPrimaryDD(String ddLocation, Object type) { 165 String [] ddLocs = (String []) moduleDDlocationMap.get(type); 166 if (ddLocs.length < 1) 167 return false; 168 return ddLocs[0].equals(ddLocation); 169 } 170 171 static private Map filenameToPathMap() { 172 if (filenameToPathMap == null) { 173 filenameToPathMap = new HashMap(); 174 175 filenameToPathMap.put(filename(J2eeModule.APP_XML), J2eeModule.APP_XML); 176 filenameToPathMap.put(filename(J2eeModule.WEB_XML), J2eeModule.WEB_XML); 177 filenameToPathMap.put(filename(J2eeModule.EJBJAR_XML), J2eeModule.EJBJAR_XML); 178 filenameToPathMap.put(filename(J2eeModule.CONNECTOR_XML), J2eeModule.CONNECTOR_XML); 179 filenameToPathMap.put(filename(J2eeModule.CLIENT_XML), J2eeModule.CLIENT_XML); 180 181 filenameToPathMap.put(J2eeModule.APP_XML, J2eeModule.APP_XML); 182 filenameToPathMap.put(J2eeModule.WEB_XML, J2eeModule.WEB_XML); 183 filenameToPathMap.put(J2eeModule.EJBJAR_XML, J2eeModule.EJBJAR_XML); 184 filenameToPathMap.put(J2eeModule.CONNECTOR_XML, J2eeModule.CONNECTOR_XML); 185 filenameToPathMap.put(J2eeModule.CLIENT_XML, J2eeModule.CLIENT_XML); 186 } 187 return filenameToPathMap; 188 } 189 190 static private String filename(String path) { 191 int i = path.lastIndexOf(SEPARATOR); 192 return path.substring(i+1); 193 } 194 195 static public String filenameToPath(String filename, Object type) { 196 if (filename.endsWith(WEBSERVICES_XML)) { 197 if (J2eeModule.EJB.equals(type)) { 198 return J2eeModule.EJBSERVICES_XML; 199 } else { 200 return J2eeModule.WEBSERVICES_XML; 201 } 202 } 203 String name = (String ) filenameToPathMap().get(filename); 204 if (name == null) 205 name = filename; 206 return name; 207 } 208 209 public DDRoot getPrimaryDD() { 210 ModuleType type = this.getType(); 211 String [] ddLocs = (String []) moduleDDlocationMap.get(type); 212 if (ddLocs.length < 1) 213 return null; 214 return (DDRoot) rootMap.get(ddLocs[0]); 215 } 216 217 219 public void cleanup() { 220 DDRoot roots[]; 222 synchronized (rootMap) { 223 roots = (DDRoot[]) rootMap.values().toArray(new DDRoot[rootMap.size()]); 224 } 225 for (DDRoot root: roots) { 226 PropertyChangeListener listener = propertyChangeListeners.get(root); 227 if (listener != null) { 228 root.proxy.bean.removePropertyChangeListener(listener); 229 } 230 } 231 } 232 233 248 StandardDDImpl getBean(BaseBean bean) { 249 if (bean == null) return null; 253 StandardDDImpl ret = (StandardDDImpl) beanMap.get(bean); 254 255 if (ret == null) { 256 280 if (bean.parent() != null) { 281 ret = new StandardDDImpl(new DDNodeBean(bean,this)); 282 beanMap.put(bean,ret); 283 } 284 } 285 return ret; 286 } 287 288 StandardDDImpl getBean(BaseProperty prop,int index) { 290 291 if(index < 0) return getBean(prop); 292 293 if (!leafMap.containsKey(prop)) { 294 leafMap.put(prop, new StandardDDImpl[index + 1]); 295 } else if (((StandardDDImpl[])leafMap.get(prop)).length <= index) { 296 StandardDDImpl[] a = (StandardDDImpl[])leafMap.get(prop); 297 StandardDDImpl[] b = new StandardDDImpl[index + 1]; 298 299 leafMap.put(prop, b); 300 for (int i = 0; i < a.length; i++) { 301 b[i] = a[i]; 302 } 303 } 304 305 StandardDDImpl[] arr = (StandardDDImpl[])leafMap.get(prop); 306 StandardDDImpl elem = arr[index]; 307 308 if (elem == null) { 309 elem = new StandardDDImpl(new DDLeafBean(prop, index, this)); 310 arr[index] = elem; 311 } 312 313 return elem; 314 } 315 316 StandardDDImpl getBean(BaseProperty prop) { 318 StandardDDImpl elem = (StandardDDImpl) leafMap.get(prop); 319 if(elem == null) { 320 elem = new StandardDDImpl(new DDLeafBean(prop, this)); 321 leafMap.put(prop,elem); 322 } 323 return elem; 324 } 325 326 StandardDDImpl getBean(String name) { 327 return getBean(name,getDDBeanRoot().proxy.bean); 328 } 329 330 StandardDDImpl getBean(String name,BaseBean rootBean) { 331 Bean parent = GraphManager.getPropertyParent(rootBean, name); 332 if (parent == null) { 333 return getDDBeanRoot(); 334 } 335 String shortName = GraphManager.getPropertyName(name); 336 int index = GraphManager.getPropertyIndex(rootBean, name); 337 340 BaseProperty prop = parent.getProperty(shortName); 341 342 if(index < 0 && prop.isIndexed()) index = 0; 343 344 StandardDDImpl ret; 345 if(prop.isBean()) { 346 if(prop.isIndexed()) 347 ret = getBean((BaseBean) parent.getValue(shortName,index)); 348 else 349 ret = getBean((BaseBean) parent.getValue(shortName)); 350 } 351 else { 352 if(prop.isIndexed()) 353 ret = getBean(prop,index); 354 else 355 ret = getBean(prop); 356 } 357 return ret; 360 } 361 362 void addXpathListener(DDCommon bean, String xpath, XpathListener listen) { 363 if (sendEvent) { 364 xpathListeners.add(new XpathListenerStorage(bean,xpath,listen)); 365 } 366 } 367 368 void removeXpathListener(DDCommon bean, String xpath, XpathListener listen) { 369 if (sendEvent) { 370 xpathListeners.remove(new XpathListenerStorage(bean,xpath,listen)); 371 } 372 } 373 374 439 440 public void propertyChange(PropertyChangeEvent event) { 441 442 Object oldValue = event.getOldValue(); 443 Object newValue = event.getNewValue(); 445 String name = event.getPropertyName(); 447 448 452 try { 453 StandardDDImpl eventBean = null; 454 if(newValue == null && oldValue instanceof BaseBean) { 455 eventBean = getBean((BaseBean) oldValue); 456 } 457 else { 458 Object eventObj = oldValue != null ? oldValue : newValue; 459 if (!(eventObj instanceof BaseBean)) { 460 eventObj = event.getSource(); 461 } 462 if (eventObj instanceof BaseBean) { 463 BaseBean root = (BaseBean) eventObj; 464 while (! root.isRoot()) { 465 root = root.parent(); 466 } 467 boolean rootInCache = false; 469 for (Iterator ddRoots=rootMap.values().iterator(); ddRoots.hasNext();) { 470 DDRoot ddroot = (DDRoot) ddRoots.next(); 471 if (ddroot.proxy != null && ddroot.proxy.bean == root) { 472 rootInCache = true; 473 break; 474 } 475 } 476 if (rootInCache) { 477 eventBean = getBean(name, root); 478 } 479 } 480 if (eventBean == null) { 481 eventBean = getBean(name); 482 } 483 } 484 485 if (eventBean == null && oldValue instanceof Object []) { 490 List newElements = new ArrayList(); 493 if(newValue != null) { 494 Object [] newValues = (Object [])newValue; 495 for (int i=0; i<newValues.length; i++) { 496 if (newValues[i] == null) { 497 continue; 498 } 499 newElements.add(newValues[i]); 500 } 501 } 502 Object [] values = (Object []) oldValue; 503 for(int i = 0; i < values.length; i++) { 504 Object value = values[i]; 505 if(!(value instanceof BaseBean)) break; 508 if(newElements.contains(value)) { 510 newElements.remove(value); 511 continue; 512 } 513 StandardDDImpl valueBean = getBean((BaseBean)value); 514 if(valueBean == null) continue; 516 processEvent(value,null,valueBean.proxy,event); 518 } 519 524 } 525 526 if (eventBean == null) return; 528 529 if(oldValue == null && eventBean.proxy.isProxy()) { 530 eventBean.setProxy(new DDNodeBean((DDProxy)eventBean.proxy)); 531 return; } 533 534 processEvent(oldValue,newValue,eventBean.proxy,event); 535 536 } catch (Exception e) { 537 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 538 } 539 } 540 541 void processEvent(Object oldValue, Object newValue, DDCommon eventBean, PropertyChangeEvent event) { 542 543 545 546 557 559 String eventDtdPath = eventBean.getXpath(); 560 561 Object type = XpathEvent.BEAN_CHANGED; 562 if(oldValue == null) type = XpathEvent.BEAN_ADDED; 563 if(newValue == null) type = XpathEvent.BEAN_REMOVED; 564 XpathEvent xe = new XpathEvent(eventBean.container,type); 565 xe.setChangeEvent(event); 566 567 568 Object xpathListenerArray[] = xpathListeners.toArray(); 569 for (int i = 0; i < xpathListenerArray.length; i++) { 570 XpathListenerStorage x = (XpathListenerStorage) xpathListenerArray[i]; 571 if (x.bean == null) { 572 continue; 573 } 574 String xp = x.getNormalizedPath(); 575 576 578 DDCommon leftBean,rightBean; 581 if(eventDtdPath.startsWith(xp)) { 582 leftBean = x.bean; 584 rightBean = eventBean; 585 } else if(xp.startsWith(eventDtdPath)) { 586 leftBean = eventBean; 588 rightBean = x.bean; 589 } else continue; 590 while (leftBean != rightBean && rightBean != null) 591 rightBean = rightBean.parent; 592 if(leftBean == rightBean) { 593 x.listen.fireXpathEvent(xe); 594 } 595 } 596 603 } 604 605 private class XpathListenerStorage { 606 private DDCommon bean = null; 607 private String xpath; 608 private boolean xpathRelative; 609 private XpathListener listen; 610 private String normal = null; 611 612 XpathListenerStorage(DDCommon bean,String xpath,XpathListener listen) { 613 this.bean = bean; 614 this.xpath = xpath; 615 this.listen = listen; 616 xpathRelative = ! xpath.startsWith(SEPARATOR); 617 } 618 619 public String getNormalizedPath() { 620 if(normal == null) { 621 String base = xpath; 622 if (xpathRelative) { 623 base = bean.getXpath() + SEPARATOR + base; 624 } 625 normal = normalizePath(base); 626 } 627 return normal; 628 } 629 630 public String toString() { 631 return bean + " " + xpath + " " + listen; 632 } 633 634 public int hashCode() { return listen.hashCode(); } 635 636 public boolean equals(Object o) { 637 if(o instanceof XpathListenerStorage) { 638 XpathListenerStorage x = (XpathListenerStorage) o; 639 return (x.bean == bean) && (x.xpath == xpath) && (x.listen == listen); 640 } 641 return false; 642 } 643 644 } 645 646 static String normalizePath(String path) { 647 boolean absolute = path.startsWith(SEPARATOR); 648 StringTokenizer tokens = new StringTokenizer(path, SEPARATOR, false); 649 650 LinkedList l = new LinkedList(); 651 652 while(tokens.hasMoreElements()) 653 l.addLast(tokens.nextElement()); 654 655 for(int i = 0 ; i < l.size(); ) { 656 String tok = (String ) l.get(i); 657 if(tok.equals(".")) l.remove(i); 658 else if(tok.equals("..") && i > 0 && !l.get(i-1).equals("..")) { 659 l.remove(i); 660 l.remove(i-1); 661 i--; 662 } else i++; 663 } 664 665 StringBuffer ret = new StringBuffer (); 666 667 for(int i = 0; i < l.size(); i++) { 668 if(absolute || i > 0) ret.append(SEPARATOR); 669 ret.append(l.get(i)); 670 } 671 672 return ret.toString(); 673 674 } 675 676 public J2eeModuleProvider getProvider() { 677 return provider; 678 } 679 } 680 | Popular Tags |