1 19 20 package org.netbeans.modules.j2ee.ddloaders.multiview; 21 22 import org.netbeans.api.java.project.JavaProjectConstants; 23 import org.netbeans.api.project.FileOwnerQuery; 24 import org.netbeans.api.project.Project; 25 import org.netbeans.api.project.ProjectUtils; 26 import org.netbeans.api.project.SourceGroup; 27 import org.netbeans.api.project.Sources; 28 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 29 import org.netbeans.core.spi.multiview.MultiViewElement; 30 import org.netbeans.modules.j2ee.common.Util; 31 import org.netbeans.modules.j2ee.dd.api.common.RootInterface; 32 import org.netbeans.modules.j2ee.dd.api.ejb.*; 33 import org.netbeans.modules.j2ee.dd.impl.common.DDUtils; 34 import org.netbeans.modules.j2ee.dd.impl.ejb.EjbJarProxy; 35 import org.netbeans.modules.j2ee.ddloaders.ejb.DDChangeEvent; 36 import org.netbeans.modules.j2ee.ddloaders.ejb.DDChangeListener; 37 import org.netbeans.modules.j2ee.ddloaders.ejb.EjbJarDataLoader; 38 import org.netbeans.modules.j2ee.common.DDEditorNavigator; 39 import org.netbeans.modules.xml.multiview.DesignMultiViewDesc; 40 import org.netbeans.modules.xml.multiview.SectionNode; 41 import org.netbeans.modules.xml.multiview.ui.SectionNodeInnerPanel; 42 import org.netbeans.modules.xml.multiview.ui.SectionNodeView; 43 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement; 44 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 45 import org.netbeans.spi.xml.cookies.ValidateXMLSupport; 46 import org.openide.ErrorManager; 47 import org.openide.cookies.ViewCookie; 48 import org.openide.filesystems.*; 49 import org.openide.loaders.DataFolder; 50 import org.openide.loaders.DataObject; 51 import org.openide.loaders.DataObjectExistsException; 52 import org.openide.nodes.CookieSet; 53 import org.openide.nodes.Node; 54 import org.openide.util.HelpCtx; 55 import org.openide.util.NbBundle; 56 import org.openide.util.RequestProcessor; 57 import org.openide.util.Utilities; 58 import org.xml.sax.InputSource ; 59 60 import javax.swing.event.ChangeListener ; 61 import java.awt.*; 62 import java.beans.PropertyChangeEvent ; 63 import java.beans.PropertyChangeListener ; 64 import java.beans.PropertyVetoException ; 65 import java.io.IOException ; 66 import java.util.ArrayList ; 67 import java.util.Arrays ; 68 import java.util.HashMap ; 69 import java.util.List ; 70 import java.util.Map ; 71 import org.openide.awt.HtmlBrowser; 72 import org.openide.loaders.MultiDataObject; 73 74 79 public class EjbJarMultiViewDataObject extends DDMultiViewDataObject 80 implements DDChangeListener, DDEditorNavigator, FileChangeListener, ChangeListener { 81 private EjbJarProxy ejbJar; 82 private FileObject srcRoots[]; 83 private PropertyChangeListener ejbJarChangeListener; 84 private Map entityHelperMap = new HashMap (); 85 private Map sessionHelperMap = new HashMap (); 86 87 private static final long serialVersionUID = 8857563089355069362L; 88 89 92 public static final String PROP_DOCUMENT_DTD = "documentDTD"; 94 private static final int HOME = 10; 95 private static final int REMOTE = 20; 96 private static final int LOCAL_HOME = 30; 97 private static final int LOCAL = 40; 98 private static final String OVERVIEW = "Overview"; private static final String CMP_RELATIONSHIPS = "CmpRelationships"; 101 public EjbJarMultiViewDataObject(FileObject pf, EjbJarDataLoader loader) throws DataObjectExistsException { 102 super(pf, loader); 103 104 InputSource in = DataObjectAdapters.inputSource(this); 106 ValidateXMLCookie validateCookie = new ValidateXMLSupport(in); 107 CookieSet set = getCookieSet(); 108 set.add(validateCookie); 109 CookieSet.Factory viewCookieFactory = new ViewCookieFactory(); 110 set.add(ViewCookie.class, viewCookieFactory); 111 112 113 Project project = getProject(); 114 if (project != null) { 115 Sources sources = ProjectUtils.getSources(project); 116 sources.addChangeListener(this); 117 } 118 refreshSourceFolders(); 119 } 120 121 122 private void refreshSourceFolders() { 123 ArrayList srcRootList = new ArrayList (); 124 125 SourceGroup[] groups; 126 Project project = getProject(); 127 if (project != null) { 128 Sources sources = ProjectUtils.getSources(project); 129 groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 130 } else { 131 groups = null; 132 } 133 if (groups != null) { 134 for (int i = 0; i < groups.length; i++) { 135 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(groups[i].getRootFolder()); 136 if ((ejbModule != null) && (ejbModule.getDeploymentDescriptor() != null)) { 137 try { 138 FileObject fo = groups[i].getRootFolder(); 139 srcRootList.add(groups[i].getRootFolder()); 140 FileSystem fs = fo.getFileSystem(); 141 fs.removeFileChangeListener(this); fs.addFileChangeListener(this); 143 } catch (FileStateInvalidException ex) { 144 ErrorManager.getDefault().notify(ex); 145 } 146 } 147 } 148 } 149 srcRoots = (FileObject[]) srcRootList.toArray(new FileObject[srcRootList.size()]); 150 } 151 152 153 private Project getProject() { 154 return FileOwnerQuery.getOwner(getPrimaryFile()); 155 } 156 157 public FileObject getProjectDirectory() { 158 Project project = getProject(); 159 return project == null ? null : project.getProjectDirectory(); 160 } 161 162 public SourceGroup[] getSourceGroups() { 163 Project project = getProject(); 164 if (project != null) { 165 return ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 166 } else { 167 return null; 168 } 169 } 170 171 private String getPackageName(FileObject clazz) { 172 for (int i = 0; i < srcRoots.length; i++) { 173 String rp = FileUtil.getRelativePath(srcRoots[i], clazz); 174 if (rp != null) { 175 if (clazz.getExt().length() > 0) { 176 rp = rp.substring(0, rp.length() - clazz.getExt().length() - 1); 177 } 178 return rp.replace('/', '.'); 179 } 180 } 181 return null; 182 } 183 184 public EjbJar getEjbJar() { 185 if (ejbJar == null) { 186 try { 187 parseDocument(); 188 } catch (IOException e) { 189 Utils.notifyError(e); 190 } 191 } 192 return ejbJar; 193 } 194 195 protected Node createNodeDelegate() { 196 return new EjbJarMultiViewDataNode(this); 197 } 198 199 206 protected String getIconBaseForValidDocument() { 207 return Utils.ICON_BASE_DD_VALID; 208 } 209 210 217 protected String getIconBaseForInvalidDocument() { 218 return Utils.ICON_BASE_DD_INVALID; } 220 221 protected DataObject handleCopy(DataFolder f) throws IOException { 222 DataObject dataObject = super.handleCopy(f); 223 try { 224 dataObject.setValid(false); 225 } catch (PropertyVetoException e) { 226 } 228 return dataObject; 229 } 230 231 236 public void deploymentChange(DDChangeEvent evt) { 237 } 238 239 240 public HelpCtx getHelpCtx() { 241 return HelpCtx.DEFAULT_HELP; 242 } 243 244 private RequestProcessor.Task elementTask; 245 private List deletedEjbNames; 246 private List newFileNames; 247 248 private void elementCreated(final String elementName) { 249 synchronized (this) { 250 if (newFileNames == null) { 251 newFileNames = new ArrayList (); 252 } 253 newFileNames.add(elementName); 254 } 255 256 if (elementTask == null) { 257 elementTask = RequestProcessor.getDefault().post(new Runnable () { 258 public void run() { 259 if (deletedEjbNames != null) { 260 for (int i = 0; i < deletedEjbNames.size(); i++) { 261 String deletedServletName = (String ) deletedEjbNames.get(i); 262 String deletedName = deletedServletName; 263 int index = deletedServletName.lastIndexOf("."); if (index > 0) { 265 deletedName = deletedServletName.substring(index + 1); 266 } 267 boolean found = false; 268 for (int j = 0; j < newFileNames.size(); j++) { 269 String newFileName = (String ) newFileNames.get(j); 270 String newName = newFileName; 271 int ind = newFileName.lastIndexOf("."); if (ind > 0) { 273 newName = newFileName.substring(ind + 1); 274 } 275 if (deletedName.equals(newName)) { found = true; 277 DDChangeEvent ddEvent = 278 new DDChangeEvent(EjbJarMultiViewDataObject.this, 279 EjbJarMultiViewDataObject.this, deletedServletName, newFileName, 280 DDChangeEvent.EJB_CHANGED); 281 deploymentChange(ddEvent); 282 synchronized (EjbJarMultiViewDataObject.this) { 283 newFileNames.remove(newFileName); 284 } 285 break; 286 } 287 } 288 if (!found) { 289 DDChangeEvent ddEvent = 290 new DDChangeEvent(EjbJarMultiViewDataObject.this, 291 EjbJarMultiViewDataObject.this, null, deletedServletName, 292 DDChangeEvent.EJB_DELETED); 293 deploymentChange(ddEvent); 294 } 295 } synchronized (EjbJarMultiViewDataObject.this) { 297 deletedEjbNames = null; 298 } 299 } 301 synchronized (EjbJarMultiViewDataObject.this) { 302 newFileNames = null; 303 } 304 305 } 307 }, 1500, Thread.MIN_PRIORITY); 308 } else { 309 elementTask.schedule(1500); 310 } 311 } 312 313 public void fileRenamed(FileRenameEvent fileRenameEvent) { 314 FileObject fo = fileRenameEvent.getFile(); 315 String resourceName = getPackageName(fo); 316 if (resourceName != null) { 317 int index = resourceName.lastIndexOf("."); String oldName = fileRenameEvent.getName(); 319 String oldResourceName = (index >= 0 ? resourceName.substring(0, index + 1) : "") + oldName; 320 EjbJar ejbJar = getEjbJar(); 321 if (ejbJar.getStatus() == EjbJar.STATE_VALID) { 322 fireEvent(oldResourceName, resourceName, DDChangeEvent.EJB_CHANGED); 323 } 324 } 325 } 326 327 public void fileFolderCreated(FileEvent fileEvent) { 328 } 329 330 public void fileDeleted(FileEvent fileEvent) { 331 FileObject fo = fileEvent.getFile(); 332 String resourceName = getPackageName(fo); 333 if (resourceName != null) { 334 if (newFileNames == null) { 335 fireEvent(null, resourceName, DDChangeEvent.EJB_DELETED); 336 } else { 337 Ejb[] ejbs = getEjbJar().getEnterpriseBeans().getEjbs(); 338 for (int i = 0; i < ejbs.length; i++) { 339 if (resourceName.equals(ejbs[i].getEjbClass())) { 340 synchronized (this) { 341 if (deletedEjbNames == null) { 342 deletedEjbNames = new ArrayList (); 343 } 344 deletedEjbNames.add(resourceName); 345 } 346 break; 347 } 348 } 349 } 350 } 351 } 352 353 public void fileDataCreated(FileEvent fileEvent) { 354 FileObject fo = fileEvent.getFile(); 355 String resourceName = getPackageName(fo); 356 if (resourceName != null) { 357 elementCreated(resourceName); 358 } 359 } 360 361 public void fileChanged(FileEvent fileEvent) { 362 } 363 364 public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) { 365 } 366 367 public void stateChanged(javax.swing.event.ChangeEvent e) { 368 refreshSourceFolders(); 369 } 370 371 protected void parseDocument() throws IOException { 372 DDProvider ddProvider = DDProvider.getDefault(); 373 if (ejbJar == null || ejbJar.getOriginal() == null) { 374 try { 375 setEjbJar((EjbJarProxy) ddProvider.getDDRoot(getPrimaryFile())); 376 } catch (IOException e) { 377 if (ejbJar == null) { 378 setEjbJar(new EjbJarProxy(null, null)); 379 } 380 } 381 } else { 382 DDUtils.merge(ejbJar, createReader()); 383 } 384 setSaxError(ejbJar.getError()); 385 } 386 387 protected void validateDocument() throws IOException { 388 setSaxError(DDUtils.createEjbJarProxy(createReader()).getError()); 389 } 390 391 private void setEjbJar(EjbJarProxy newEjbJar) { 392 if(ejbJar != null) { 393 ejbJar.removePropertyChangeListener(ejbJarChangeListener); 394 } 395 ejbJar = newEjbJar; 396 if (ejbJarChangeListener == null) { 397 ejbJarChangeListener = new EjbJarPropertyChangeListener(); 398 } 399 ejbJar.addPropertyChangeListener(ejbJarChangeListener); 400 } 401 402 protected RootInterface getDDModel() { 403 if (ejbJar == null) { 404 try { 405 parseDocument(); 406 } catch (IOException ex) { 407 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 408 } 409 } 410 return ejbJar; 411 } 412 413 public boolean isDocumentParseable() { 414 return EjbJar.STATE_INVALID_UNPARSABLE != getEjbJar().getStatus(); 415 } 416 417 protected String getPrefixMark() { 418 return "<ejb-jar"; 419 } 420 421 424 protected DesignMultiViewDesc[] getMultiViewDesc() { 425 if (Util.isJavaEE5orHigher(getProject())){ 426 return new DesignMultiViewDesc[0]; 428 } 429 return new DesignMultiViewDesc[]{ 430 new DDView(this, OVERVIEW), 431 new DDView(this, CMP_RELATIONSHIPS), 432 }; 433 } 434 435 438 protected boolean isModelCreated() { 439 return (ejbJar!=null && ejbJar.getOriginal()!=null); 440 } 441 442 public void showElement(final Object element) { 443 if (element instanceof Relationships || element instanceof EjbRelation) { 444 openView(1); 445 } else { 446 openView(0); 447 } 448 Utils.runInAwtDispatchThread(new Runnable () { 449 public void run() { 450 final SectionNodeView sectionView = 451 (SectionNodeView) EjbJarMultiViewDataObject.this .getActiveMVElement().getSectionView(); 452 final Node root = sectionView.getRoot(); 453 final SectionNode node = ((SectionNode) root.getChildren().getNodes()[0]).getNodeForElement(element); 454 if (node != null) { 455 sectionView.openPanel(node); 456 ((SectionNodeInnerPanel) node.getSectionNodePanel().getInnerPanel()).focusData(element); 457 } 458 } 459 }); 460 } 461 462 463 466 private class ViewCookieFactory implements CookieSet.Factory { 467 468 public Node.Cookie createCookie(Class klass) { 469 if (klass == ViewCookie.class) { 470 return new ViewSupport(EjbJarMultiViewDataObject.this.getPrimaryEntry()); 471 } else { 472 return null; 473 } 474 } 475 } 476 477 481 private static final class ViewSupport implements ViewCookie { 482 483 private MultiDataObject.Entry primary; 484 485 public ViewSupport(MultiDataObject.Entry primary) { 486 this.primary = primary; 487 } 488 489 public void view() { 490 try { 491 HtmlBrowser.URLDisplayer.getDefault().showURL(primary.getFile().getURL()); 492 } catch (FileStateInvalidException e) { 493 } 494 } 495 } 496 497 498 private static class DDView extends DesignMultiViewDesc implements java.io.Serializable { 499 500 private String name; 501 static final long serialVersionUID = -8759598009819101630L; 502 503 DDView(EjbJarMultiViewDataObject dataObject, String name) { 504 super(dataObject, name); 505 this.name=name; 506 } 507 508 public MultiViewElement createElement() { 509 EjbJarMultiViewDataObject dataObject = (EjbJarMultiViewDataObject) getDataObject(); 510 if (name.equals(OVERVIEW)) { 511 return new EjbMultiViewElement(dataObject); 512 } else if (name.equals(CMP_RELATIONSHIPS)) { 513 return new CmpRelationshipsMultiViewElement(dataObject); 514 } else { 515 return null; 517 } 518 } 519 520 public HelpCtx getHelpCtx() { 521 final EjbJarMultiViewDataObject dataObject = ((EjbJarMultiViewDataObject)getDataObject()); 522 return new HelpCtx(dataObject.getActiveMVElement().getSectionView().getClass()); 523 } 524 525 public Image getIcon() { 526 return Utilities.loadImage(Utils.ICON_BASE_DD_VALID + ".gif"); } 528 529 public String preferredID() { 530 return "dd_multiview_" + name; } 532 533 public String getDisplayName() { 534 return NbBundle.getMessage(EjbJarMultiViewDataObject.class,"LBL_"+name); } 536 537 } 538 539 541 public ToolBarMultiViewElement getActiveMVElement() { 542 return (ToolBarMultiViewElement)super.getActiveMultiViewElement(); 543 } 544 545 private Ejb getEjbFromEjbClass(String ejbClassName) { 546 EnterpriseBeans enterpriseBeans = getEjbJar().getEnterpriseBeans(); 547 if(enterpriseBeans != null) { 548 Ejb[] ejbs = enterpriseBeans.getEjbs(); 549 for (int i = 0; i < ejbs.length; i++) { 550 if (ejbs[i].getEjbClass() != null && ejbs[i].getEjbClass().equals(ejbClassName)) { 551 return ejbs[i]; 552 } 553 } 554 } 555 return null; 556 } 557 558 private int getBeanInterfaceType(String interfaceName) { 559 int interfaceType = -1; 560 EjbJar ejbJar = getEjbJar(); 561 if (ejbJar == null) { 562 return interfaceType; 563 } 564 EnterpriseBeans eb = ejbJar.getEnterpriseBeans(); 565 if (eb == null) { 566 return interfaceType; 567 } 568 EntityAndSession[] beans = eb.getSession(); 569 for (int i = 0; i < beans.length; i++) { 570 if (beans[i].getHome() != null && 571 beans[i].getHome().equals(interfaceName)) { 572 interfaceType = HOME; 573 break; 574 } 575 if (beans[i].getRemote() != null && 576 beans[i].getRemote().equals(interfaceName)) { 577 interfaceType = REMOTE; 578 break; 579 } 580 if (beans[i].getLocalHome() != null && 581 beans[i].getLocalHome().equals(interfaceName)) { 582 interfaceType = LOCAL_HOME; 583 break; 584 } 585 if (beans[i].getLocal() != null && 586 beans[i].getLocal().equals(interfaceName)) { 587 interfaceType = LOCAL; 588 break; 589 } 590 } 591 return interfaceType; 592 } 593 594 private int getSpecificEvent(int eventType, int interfaceType) { 595 if (eventType == DDChangeEvent.EJB_CHANGED) { 596 switch (interfaceType) { 597 case HOME: 598 { 599 return DDChangeEvent.EJB_HOME_CHANGED; 600 } 601 case REMOTE: 602 { 603 return DDChangeEvent.EJB_REMOTE_CHANGED; 604 } 605 case LOCAL_HOME: 606 { 607 return DDChangeEvent.EJB_LOCAL_HOME_CHANGED; 608 } 609 case LOCAL: 610 { 611 return DDChangeEvent.EJB_LOCAL_CHANGED; 612 } 613 } 614 } 615 if (eventType == DDChangeEvent.EJB_DELETED) { 616 switch (interfaceType) { 617 case HOME: 618 { 619 return DDChangeEvent.EJB_HOME_DELETED; 620 } 621 case REMOTE: 622 { 623 return DDChangeEvent.EJB_REMOTE_DELETED; 624 } 625 case LOCAL_HOME: 626 { 627 return DDChangeEvent.EJB_LOCAL_HOME_DELETED; 628 } 629 case LOCAL: 630 { 631 return DDChangeEvent.EJB_LOCAL_DELETED; 632 } 633 } 634 } 635 return -1; 636 } 637 638 private boolean fireEvent(String oldResourceName, String resourceName, int eventType) { 639 boolean elementFound = false; 640 String resource; 641 int specificEventType = -1; 642 if (eventType == DDChangeEvent.EJB_CHANGED) { 643 resource = oldResourceName; 644 } else { 645 resource = resourceName; 646 } 647 Ejb ejb = getEjbFromEjbClass(resource); 648 649 if (ejb != null) { 650 if (eventType == DDChangeEvent.EJB_CHANGED) { 651 specificEventType = DDChangeEvent.EJB_CLASS_CHANGED; 652 } else { 653 specificEventType = DDChangeEvent.EJB_CLASS_DELETED; 654 } 655 elementFound = true; 656 } 657 658 if (!elementFound) { 659 int interfaceType = getBeanInterfaceType(resource); 660 661 if (interfaceType > 0) { 662 specificEventType = 663 getSpecificEvent(eventType, interfaceType); 664 elementFound = true; 665 } 666 } 667 if (elementFound) { 668 assert(specificEventType > 0); 669 DDChangeEvent ddEvent = 670 new DDChangeEvent(this, this, oldResourceName, 671 resourceName, specificEventType); 672 deploymentChange(ddEvent); 673 } 674 return elementFound; 675 } 676 677 public EntityHelper getEntityHelper(Entity entity) { 678 EntityHelper entityHelper = (EntityHelper) entityHelperMap.get(entity); 679 if (entityHelper == null) { 680 entityHelper = new EntityHelper(this, entity); 681 entityHelperMap.put(entity, entityHelper); 682 } 683 return entityHelper; 684 } 685 686 public SessionHelper getSessionHelper(Session session) { 687 SessionHelper sessionHelper = (SessionHelper) sessionHelperMap.get(session); 688 if (sessionHelper == null) { 689 sessionHelper = new SessionHelper(this, session); 690 sessionHelperMap.put(session, sessionHelper); 691 } 692 return sessionHelper; 693 } 694 695 private class EjbJarPropertyChangeListener implements PropertyChangeListener { 696 697 public void propertyChange(PropertyChangeEvent evt) { 698 if (EjbJar.PROPERTY_STATUS.equals(evt.getPropertyName())) { 699 return; 700 } 701 Object source = evt.getSource(); 702 if (source instanceof EnterpriseBeans) { 703 Object oldValue = evt.getOldValue(); 704 Object newValue = evt.getNewValue(); 705 if ((oldValue instanceof Entity || newValue instanceof Entity)) { 706 entityHelperMap.keySet().retainAll(Arrays.asList(((EnterpriseBeans) source).getEntity())); 707 } else if ((oldValue instanceof Session || newValue instanceof Session)) { 708 sessionHelperMap.keySet().retainAll(Arrays.asList(((EnterpriseBeans) source).getSession())); 709 } 710 } 711 } 712 } 713 714 715 } 716 | Popular Tags |