1 19 20 40 package org.netbeans.modules.j2ee.sun.share.config; 41 42 import java.util.*; 43 import java.beans.*; 44 45 import javax.enterprise.deploy.spi.*; 46 import javax.enterprise.deploy.spi.exceptions.*; 47 import javax.enterprise.deploy.model.*; 48 import javax.enterprise.deploy.shared.*; 49 50 import org.openide.*; 51 import org.openide.nodes.*; 52 import org.openide.util.WeakListeners; 53 54 import org.netbeans.modules.schema2beans.*; 55 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 56 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 57 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 58 59 60 62 public class ModuleDDSupport implements PropertyChangeListener { 63 64 public static final String SEPARATOR = "/"; public static final String WEBSERVICES_XML = "webservices.xml"; private static Map filenameToPathMap = null; 67 68 private Map rootMap = new HashMap(5); private Map configMap = new IdentityHashMap(5); private Map beanMap = Collections.synchronizedMap(new IdentityHashMap()); private Map leafMap = Collections.synchronizedMap(new IdentityHashMap()); private Map listenerMap = Collections.synchronizedMap(new IdentityHashMap(5)); private Set xpathListeners = new HashSet(); 74 private J2eeModuleProvider provider; 75 private DeploymentConfiguration config; 76 77 private static Map moduleDDlocationMap = new HashMap(10); 78 79 static { 80 moduleDDlocationMap.put(J2eeModule.EAR, 81 new String [] {J2eeModule.APP_XML}); 82 moduleDDlocationMap.put(J2eeModule.WAR, 83 new String [] {J2eeModule.WEB_XML,J2eeModule.WEBSERVICES_XML}); 84 moduleDDlocationMap.put(J2eeModule.EJB, 85 new String [] {J2eeModule.EJBJAR_XML,J2eeModule.EJBSERVICES_XML}); 86 moduleDDlocationMap.put(J2eeModule.CONN, 87 new String [] { J2eeModule.CONNECTOR_XML}); 88 moduleDDlocationMap.put(J2eeModule.CLIENT, 89 new String [] { J2eeModule.CLIENT_XML}); 90 } 91 92 public static String [] getDDPaths(Object type) { 93 return (String []) moduleDDlocationMap.get(type); 94 } 95 96 public ModuleDDSupport(J2eeModuleProvider provider, DeploymentConfiguration config) { 97 this.provider = provider; 98 this.config = config; 99 String [] ddLocs = getDDPaths(provider.getJ2eeModule().getModuleType()); 100 for(int i = 0; i < ddLocs.length; i++) { 101 createRoot(ddLocs[i]); 102 } 103 } 104 105 private ModuleType getModuleType() { 106 return (ModuleType) provider.getJ2eeModule().getModuleType(); 107 } 108 109 private BaseBean getDeploymentDescriptor(String ddLoc) { 110 return provider.getJ2eeModule().getDeploymentDescriptor(ddLoc); 111 } 112 113 private DDRoot createRoot(String ddLoc) { 114 BaseBean bean = getDeploymentDescriptor(ddLoc); 115 if (bean == null) { return null; 117 } 118 while(!bean.isRoot()) { 119 bean = bean.parent(); 120 } 121 DDRoot root = new DDRoot(new DDNodeBean(null,bean,this)); 122 rootMap.put(ddLoc,root); 123 beanMap.put(bean,root); 124 125 PropertyChangeListener weakListener = WeakListeners.propertyChange(this,root.proxy.bean); 126 listenerMap.put(bean, weakListener); 127 bean.addPropertyChangeListener(weakListener); 128 129 return root; 130 } 131 132 public DeployableObject getDeployableObject() { 133 return config.getDeployableObject(); 134 } 135 136 public DDRoot getDDBeanRoot() { 138 String loc = ((String []) moduleDDlocationMap.get(getModuleType()))[0]; 139 return getDDBeanRoot(loc); 140 } 141 142 public DDRoot getDDBeanRoot(String loc) { 143 DDRoot root = (DDRoot) rootMap.get(loc); 144 145 if (root == null && ! isPrimaryDD(loc, getType())) { 148 root = createRoot(loc); 149 DDRoot proot = getPrimaryDD(); 150 ConfigBeanStorage configRoot = (ConfigBeanStorage) configMap.get(proot); 151 152 if (root != null && configRoot != null) { 153 DConfigBeanRoot cbroot = (DConfigBeanRoot) configRoot.getConfigBean(); 154 DConfigBean cb = cbroot.getDConfigBean(root); 155 if (cb != null) { 156 try { 157 ConfigBeanStorage cbs = new ConfigBeanStorage(cb, null, configRoot.getStorage()); 158 configMap.put(root, cbs); 159 } catch (Exception ex) { 160 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 161 } 162 } 163 } 164 } 165 return root; 166 } 167 168 public ModuleType getType() { 169 return getModuleType(); 170 } 171 172 public String getVersion() { 173 return provider.getJ2eeModule().getModuleVersion(); 174 } 175 176 177 public Class getClassFromScope(String cls) { 178 return null; } 180 181 public Node[] getNodes() { 182 String [] ddLocs = (String []) moduleDDlocationMap.get(getModuleType()); 183 List ret = new ArrayList(); 184 for(int i = 0; i < ddLocs.length; i++) { 185 Object dd = rootMap.get(ddLocs[i]); 186 if(dd == null) { 187 continue; 188 } 189 ConfigBeanStorage cbs = (ConfigBeanStorage) configMap.get(dd); 190 if(cbs != null) { 191 Node n = cbs.getNode(); 192 if (n != null) { 193 ret.add(n); 194 } else { 195 throw new RuntimeException ("CBS.getNode returned null"); } 197 } 198 } 199 return (Node[]) ret.toArray(new Node[ret.size()]); 200 } 201 202 public void resetConfigCache() { 203 configMap = new IdentityHashMap(5); xpathListeners = new HashSet(); 205 } 206 207 public void createConfigs(ConfigurationStorage storage) throws ConfigurationException { 208 String [] ddLocs = (String []) moduleDDlocationMap.get(getModuleType()); 209 DDRoot root = (DDRoot) rootMap.get(ddLocs[0]); 210 DConfigBeanRoot cbroot = config.getDConfigBeanRoot(root); 211 ConfigBeanStorage cbs = new ConfigBeanStorage(cbroot, null, storage); 212 configMap.put(root,cbs); 213 214 for(Iterator it = rootMap.keySet().iterator(); it.hasNext() ;) { 215 String ddLoc = (String ) it.next(); 216 if (isPrimaryDD(ddLoc, getModuleType())) { 217 continue; 218 } 219 root = (DDRoot) rootMap.get(ddLoc); 220 DConfigBean cb = cbroot.getDConfigBean(root); 221 if(cb == null) { 222 continue; 223 } 224 ConfigBeanStorage cbStorage = new ConfigBeanStorage(cb, null, storage); 225 configMap.put(root,cbStorage); 226 } 227 } 228 229 static public boolean isPrimaryDD(String ddLocation, Object type) { 230 String [] ddLocs = (String []) moduleDDlocationMap.get(type); 231 if (ddLocs.length < 1) { 232 return false; 233 } 234 return ddLocs[0].equals(ddLocation); 235 } 236 237 static private Map filenameToPathMap() { 238 if (filenameToPathMap == null) { 239 filenameToPathMap = new HashMap(); 240 241 filenameToPathMap.put(filename(J2eeModule.APP_XML), J2eeModule.APP_XML); 242 filenameToPathMap.put(filename(J2eeModule.WEB_XML), J2eeModule.WEB_XML); 243 filenameToPathMap.put(filename(J2eeModule.EJBJAR_XML), J2eeModule.EJBJAR_XML); 244 filenameToPathMap.put(filename(J2eeModule.CONNECTOR_XML), J2eeModule.CONNECTOR_XML); 245 filenameToPathMap.put(filename(J2eeModule.CLIENT_XML), J2eeModule.CLIENT_XML); 246 247 filenameToPathMap.put(J2eeModule.APP_XML, J2eeModule.APP_XML); 248 filenameToPathMap.put(J2eeModule.WEB_XML, J2eeModule.WEB_XML); 249 filenameToPathMap.put(J2eeModule.EJBJAR_XML, J2eeModule.EJBJAR_XML); 250 filenameToPathMap.put(J2eeModule.CONNECTOR_XML, J2eeModule.CONNECTOR_XML); 251 filenameToPathMap.put(J2eeModule.CLIENT_XML, J2eeModule.CLIENT_XML); 252 } 253 return filenameToPathMap; 254 } 255 256 static private String filename(String path) { 257 int i = path.lastIndexOf(SEPARATOR); 258 return path.substring(i+1); 259 } 260 261 static public String filenameToPath(String filename, Object type) { 262 if (filename.endsWith(WEBSERVICES_XML)) { 263 if (J2eeModule.EJB.equals(type)) { 264 return J2eeModule.EJBSERVICES_XML; 265 } else { 266 return J2eeModule.WEBSERVICES_XML; 267 } 268 } 269 String name = (String ) filenameToPathMap().get(filename); 270 if (name == null) { 271 name = filename; 272 } 273 return name; 274 } 275 276 public DDRoot getPrimaryDD() { 277 ModuleType type = this.getType(); 278 String [] ddLocs = (String []) moduleDDlocationMap.get(type); 279 if (ddLocs.length < 1) { 280 return null; 281 } 282 return (DDRoot) rootMap.get(ddLocs[0]); 283 } 284 285 287 public void cleanup() { 288 for (Iterator i = rootMap.values().iterator(); i.hasNext();) { 290 DDRoot root = (DDRoot)i.next(); 291 292 PropertyChangeListener weakListener = (PropertyChangeListener) listenerMap.get(root.proxy.bean); 293 root.proxy.bean.removePropertyChangeListener(weakListener); 294 295 ConfigBeanStorage cbs = (ConfigBeanStorage) configMap.get(root); 297 try { 298 if(config != null && cbs != null && cbs.bean != null) { 299 config.removeDConfigBean((DConfigBeanRoot)cbs.bean); 300 } 301 } catch (BeanNotFoundException bnfe) { 302 ErrorManager.getDefault().log("BeanNotFoundException caught by ModuleDDSupport: " + bnfe.getMessage()); 304 } 305 } 306 307 listenerMap = null; 308 rootMap = null; 309 configMap = null; 310 beanMap = null; 311 xpathListeners = null; 312 leafMap = null; 313 provider = null; 314 } 315 316 317 333 StandardDDImpl getBean(BaseBean bean) { 334 if (bean == null) { 338 return null; 339 } 340 341 if (beanMap == null) { 342 return null; 343 } 344 345 StandardDDImpl ret = (StandardDDImpl) beanMap.get(bean); 346 347 if (ret == null) { 348 372 if (bean.parent() != null) { 373 ret = new StandardDDImpl(new DDNodeBean(bean,this)); 374 beanMap.put(bean,ret); 375 } 376 } 377 return ret; 378 } 379 380 StandardDDImpl getBean(BaseProperty prop,int index) { 382 383 if(index < 0) { 384 return getBean(prop); 385 } 386 387 if (!leafMap.containsKey(prop)) { 388 leafMap.put(prop, new StandardDDImpl[index + 1]); 389 } else if (((StandardDDImpl[])leafMap.get(prop)).length <= index) { 390 StandardDDImpl[] a = (StandardDDImpl[])leafMap.get(prop); 391 StandardDDImpl[] b = new StandardDDImpl[index + 1]; 392 393 leafMap.put(prop, b); 394 for (int i = 0; i < a.length; i++) { 395 b[i] = a[i]; 396 } 397 } 398 399 StandardDDImpl[] arr = (StandardDDImpl[])leafMap.get(prop); 400 StandardDDImpl elem = arr[index]; 401 402 if (elem == null) { 403 elem = new StandardDDImpl(new DDLeafBean(prop, index, this)); 404 arr[index] = elem; 405 } 406 407 return elem; 408 } 409 410 StandardDDImpl getBean(BaseProperty prop) { 412 StandardDDImpl elem = (StandardDDImpl) leafMap.get(prop); 413 if(elem == null) { 414 elem = new StandardDDImpl(new DDLeafBean(prop, this)); 415 leafMap.put(prop,elem); 416 } 417 return elem; 418 } 419 420 StandardDDImpl getBean(String name) { 421 return getBean(name,getDDBeanRoot().proxy.bean); 422 } 423 424 StandardDDImpl getBean(String name,BaseBean rootBean) { 425 Bean parent = GraphManager.getPropertyParent(rootBean, name); 426 if (parent == null) { 427 return getDDBeanRoot(); 428 } 429 String shortName = GraphManager.getPropertyName(name); 430 int index = GraphManager.getPropertyIndex(rootBean, name); 431 434 BaseProperty prop = parent.getProperty(shortName); 435 436 if(index < 0 && prop.isIndexed()) { 437 index = 0; 438 } 439 440 StandardDDImpl ret; 441 if(prop.isBean()) { 442 if(prop.isIndexed()) { 443 ret = getBean((BaseBean) parent.getValue(shortName,index)); 444 } else { 445 ret = getBean((BaseBean) parent.getValue(shortName)); 446 } 447 } 448 else { 449 if(prop.isIndexed()) { 450 ret = getBean(prop,index); 451 } else { 452 ret = getBean(prop); 453 } 454 } 455 return ret; 458 } 459 460 void addXpathListener(DDCommon bean, String xpath, XpathListener listen) { 461 xpathListeners.add(new XpathListenerStorage(bean,xpath,listen)); 462 } 463 464 void removeXpathListener(DDCommon bean, String xpath, XpathListener listen) { 465 xpathListeners.remove(new XpathListenerStorage(bean,xpath,listen)); 466 } 467 468 533 534 public void propertyChange(PropertyChangeEvent event) { 535 536 Object oldValue = event.getOldValue(); 537 Object newValue = event.getNewValue(); 539 String name = event.getPropertyName(); 541 542 if(rootMap == null) { 546 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception ( 547 "ModuleDDSupport: Unexpected change event (NAME=" + name + ", old=" + oldValue + 548 ", new=" + newValue + ") received on previously removed DDBean->DConfigBean graph. See IZ 81332.")); 549 return; 551 } 552 553 try { 554 StandardDDImpl eventBean = null; 555 if(newValue == null && oldValue instanceof BaseBean) { 556 eventBean = getBean((BaseBean) oldValue); 557 } else { 558 Object eventObj = oldValue != null ? oldValue : newValue; 559 if (!(eventObj instanceof BaseBean)) { 560 eventObj = event.getSource(); 561 } 562 if (eventObj instanceof BaseBean) { 563 BaseBean root = (BaseBean) eventObj; 564 while (! root.isRoot()) { 565 root = root.parent(); 566 } 567 boolean rootInCache = false; 569 for (Iterator ddRoots=rootMap.values().iterator(); ddRoots.hasNext();) { 570 DDRoot ddroot = (DDRoot) ddRoots.next(); 571 if (ddroot.proxy != null && ddroot.proxy.bean == root) { 572 rootInCache = true; 573 break; 574 } 575 } 576 if (rootInCache) { 577 eventBean = getBean(name, root); 578 } 579 } 580 if (eventBean == null) { 581 eventBean = getBean(name); 582 } 583 } 584 585 if (eventBean == null && oldValue instanceof Object []) { 590 List newElements = new ArrayList(); 593 if(newValue != null) { 594 Object [] newValues = (Object [])newValue; 595 for (int i=0; i<newValues.length; i++) { 596 if (newValues[i] == null) { 597 continue; 598 } 599 newElements.add(newValues[i]); 600 } 601 } 602 Object [] values = (Object []) oldValue; 603 for(int i = 0; i < values.length; i++) { 604 Object value = values[i]; 605 if(!(value instanceof BaseBean)) { 608 break; 609 } 610 if(newElements.contains(value)) { 612 newElements.remove(value); 613 continue; 614 } 615 StandardDDImpl valueBean = getBean((BaseBean)value); 616 if(valueBean == null) { 618 continue; 619 } 620 processEvent(value,null,valueBean.proxy,event); 622 } 623 628 } 629 630 if (eventBean == null) { 632 return; 633 } 634 635 if(oldValue == null && eventBean.proxy.isProxy()) { 636 eventBean.setProxy(new DDNodeBean((DDProxy)eventBean.proxy)); 637 return; } 639 640 processEvent(oldValue,newValue,eventBean.proxy,event); 641 642 } catch (Exception e) { 643 ErrorManager.getDefault().notify(e); 644 } 645 } 646 647 void processEvent(Object oldValue, Object newValue, DDCommon eventBean, PropertyChangeEvent event) { 648 649 651 652 663 665 String eventDtdPath = eventBean.getXpath(); 666 667 Object type = XpathEvent.BEAN_CHANGED; 668 if(oldValue == null) { 669 type = XpathEvent.BEAN_ADDED; 670 } 671 if(newValue == null) { 672 type = XpathEvent.BEAN_REMOVED; 673 } 674 XpathEvent xe = new XpathEvent(eventBean.container,type); 675 xe.setChangeEvent(event); 676 677 678 Object xpathListenerArray[] = xpathListeners.toArray(); 679 for (int i = 0; i < xpathListenerArray.length; i++) { 680 XpathListenerStorage x = (XpathListenerStorage) xpathListenerArray[i]; 681 if (x.bean == null) { 682 continue; 683 } 684 String xp = x.getNormalizedPath(); 685 686 688 DDCommon leftBean,rightBean; 691 if(eventDtdPath.startsWith(xp)) { 692 leftBean = x.bean; 694 rightBean = eventBean; 695 } else if(xp.startsWith(eventDtdPath)) { 696 leftBean = eventBean; 698 rightBean = x.bean; 699 } else { 700 continue; 701 } 702 while (leftBean != rightBean && rightBean != null) { 703 rightBean = rightBean.parent; 704 } 705 if(leftBean == rightBean) { 706 x.listen.fireXpathEvent(xe); 707 } 708 } 709 eventBean.fireEvent(xe); 714 716 } 717 718 private class XpathListenerStorage { 719 private DDCommon bean = null; 720 private String xpath; 721 private boolean xpathRelative; 722 private XpathListener listen; 723 private String normal = null; 724 725 XpathListenerStorage(DDCommon bean,String xpath,XpathListener listen) { 726 this.bean = bean; 727 this.xpath = xpath; 728 this.listen = listen; 729 xpathRelative = ! xpath.startsWith(SEPARATOR); 730 } 731 732 public String getNormalizedPath() { 733 if(normal == null) { 734 String base = xpath; 735 if (xpathRelative) { 736 base = bean.getXpath() + SEPARATOR + base; 737 } 738 normal = normalizePath(base); 739 } 740 return normal; 741 } 742 743 public String toString() { 744 return bean + " " + xpath + " " + listen; 745 } 746 747 public int hashCode() { return listen.hashCode(); } 748 749 public boolean equals(Object o) { 750 if(o instanceof XpathListenerStorage) { 751 XpathListenerStorage x = (XpathListenerStorage) o; 752 return (x.bean == bean) && (x.xpath == xpath) && (x.listen == listen); 753 } 754 return false; 755 } 756 757 } 758 759 static String normalizePath(String path) { 760 boolean absolute = path.startsWith(SEPARATOR); 761 StringTokenizer tokens = new StringTokenizer(path, SEPARATOR, false); 762 763 LinkedList l = new LinkedList(); 764 765 while(tokens.hasMoreElements()) { 766 l.addLast(tokens.nextElement()); 767 } 768 769 for(int i = 0 ; i < l.size(); ) { 770 String tok = (String ) l.get(i); 771 if(tok.equals(".")) { 772 l.remove(i); 773 } else if(tok.equals("..") && i > 0 && !l.get(i-1).equals("..")) { 774 l.remove(i); 775 l.remove(i-1); 776 i--; 777 } else { 778 i++; 779 } 780 } 781 782 StringBuffer ret = new StringBuffer (); 783 784 for(int i = 0; i < l.size(); i++) { 785 if(absolute || i > 0) { 786 ret.append(SEPARATOR); 787 } 788 ret.append(l.get(i)); 789 } 790 791 return ret.toString(); 792 793 } 794 795 public J2eeModuleProvider getProvider() { 796 return provider; 797 } 798 } 799 | Popular Tags |