KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > app > EarDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.app;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.*;
25 import java.io.*;
26 import java.util.*;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29
30 import org.openide.DialogDescriptor;
31 import org.openide.filesystems.*;
32 import org.openide.loaders.*;
33 import org.openide.nodes.*;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.RequestProcessor;
36
37 import org.xml.sax.*;
38 import org.openide.ErrorManager;
39 import org.openide.util.NbBundle;
40
41 import org.netbeans.modules.j2ee.ddloaders.common.xmlutils.SAXParseError;
42 import org.netbeans.modules.j2ee.dd.api.application.*;
43 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
44 import org.netbeans.spi.xml.cookies.*;
45 import org.openide.DialogDisplayer;
46 import org.netbeans.modules.j2ee.dd.impl.application.ApplicationProxy;
47
48 //////////import org.netbeans.api.java.classpath.ClassPath;
49
import org.netbeans.api.java.project.JavaProjectConstants;
50 import org.netbeans.api.project.FileOwnerQuery;
51 import org.netbeans.api.project.Project;
52 import org.netbeans.api.project.ProjectUtils;
53 import org.netbeans.api.project.ProjectInformation;
54
55 /////////////ludo import org.netbeans.modules.web.api.webmodule.WebModule;
56
import org.netbeans.api.project.SourceGroup;
57 import org.netbeans.api.project.Sources;
58 import org.netbeans.modules.j2ee.ddloaders.common.DD2beansDataObject;
59
60
61 /** Represents a DD object in the Repository.
62  *
63  * @author mkuchtiak, Ludovic Champenois
64  */

65 public class EarDataObject extends DD2beansDataObject
66     implements DDChangeListener, ApplicationProxy.OutputProvider, FileChangeListener, ChangeListener JavaDoc {
67     private Application ejbJar;
68     private FileObject srcRoots[];
69     protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); // NOI18N
70

71     private static final long serialVersionUID = 8857563089355069362L;
72     
73     /** Property name for documentDTD property */
74     public static final String JavaDoc PROP_DOCUMENT_DTD = "documentDTD"; // NOI18N
75

76
77     private boolean unparsable=true;
78     
79     /** List of updates to ejbs that should be processed */
80     private Vector updates;
81     
82     private RequestProcessor.Task updateTask;
83     private FileObjectObserver fileListener;
84
85     private static final int HOME = 10;
86     private static final int REMOTE = 20;
87     private static final int LOCAL_HOME = 30;
88     private static final int LOCAL = 40;
89
90     public EarDataObject (FileObject pf, EarDataLoader loader) throws DataObjectExistsException {
91         super (pf, loader);
92         init (pf,loader);
93     }
94     public boolean isRenameAllowed(){
95         return false;
96     }
97     public boolean isUnparsable(){
98         return unparsable;
99     }
100     
101     private void init (FileObject fo,EarDataLoader loader) {
102         // added ValidateXMLCookie
103
InputSource in = DataObjectAdapters.inputSource(this);
104         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
105         getCookieSet().add(validateCookie);
106         
107
108
109         fileListener = new FileObjectObserver(fo);
110         
111         Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
112         if (project != null) {
113             Sources sources = ProjectUtils.getSources(project);
114             sources.addChangeListener (this);
115         }
116         refreshSourceFolders ();
117     }
118
119     private void refreshSourceFolders () {
120         ArrayList srcRootList = new ArrayList ();
121         
122         Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
123         if (project != null) {
124             Sources sources = ProjectUtils.getSources(project);
125             sources.addChangeListener (this);
126             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
127             for (int i = 0; i < groups.length; i++) {
128                 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(groups[i].getRootFolder());
129                 if ((ejbModule != null) && (ejbModule.getDeploymentDescriptor() != null)) {
130                     try {
131                         FileObject fo = groups [i].getRootFolder ();
132                         srcRootList.add (groups [i].getRootFolder ());
133                         FileSystem fs = fo.getFileSystem ();
134                         fs.removeFileChangeListener(this); //avoid being added multiple times
135
fs.addFileChangeListener (this);
136                     } catch (FileStateInvalidException ex) {
137                         ErrorManager.getDefault ().notify (ex);
138                     }
139                 }
140             }
141         }
142         srcRoots = (FileObject []) srcRootList.toArray (new FileObject [srcRootList.size ()]);
143     }
144     
145     private String JavaDoc getPackageName (FileObject clazz) {
146         for (int i = 0; i < srcRoots.length; i++) {
147             String JavaDoc rp = FileUtil.getRelativePath (srcRoots [i], clazz);
148             if (rp != null) {
149                 if (clazz.getExt ().length () > 0) {
150                     rp = rp.substring (0, rp.length () - clazz.getExt ().length () - 1);
151                 }
152                 return rp.replace ('/', '.');
153             }
154         }
155         return null;
156     }
157     
158     public Application getApplication(){
159         if (ejbJar==null){
160             parsingDocument();
161         }
162         return ejbJar;
163     }
164     
165     public Application getOriginalApplication() throws IOException {
166         return DDProvider.getDefault().getDDRoot(getPrimaryFile());
167     }
168     
169     protected org.openide.nodes.Node createNodeDelegate () {
170         return new EarDataNode(this);
171     }
172
173
174     /** gets the Icon Base for node delegate when parser accepts the xml document as valid
175      *
176      * PENDING: move into node
177      * @return Icon Base for node delegate
178      */

179     protected String JavaDoc getIconBaseForValidDocument() {
180         return "org/netbeans/modules/j2ee/ddloaders/ejb/DDDataIcon.gif"; // NOI18N
181
}
182     
183     /** gets the Icon Base for node delegate when parser finds error(s) in xml document
184      * @return Icon Base for node delegate
185      *
186      * PENDING: move into node
187      */

188     protected String JavaDoc getIconBaseForInvalidDocument() {
189         return "org/netbeans/modules/j2ee/ddloaders/ejb/DDDataIcon1.gif"; // NOI18N
190
}
191     
192     /** gets the String for node delegate when parser accepts the xml document as valid
193      * @return String for valid xml document
194     */

195     public String JavaDoc getStringForValidDocument() {
196         return NbBundle.getMessage (EarDataObject.class, "LAB_deploymentDescriptor");
197     }
198     
199     /** gets the String for node delegate when parser finds error(s) in xml document
200      * @param error Error description
201      * @return String for node delegate
202     */

203     public String JavaDoc getStringForInvalidDocument(SAXParseError error) {
204         return NbBundle.getMessage (EarDataObject.class, "TXT_errorOnLine", new Integer JavaDoc(error.getErrorLine()));
205     }
206                     
207     /** Create document from the Node. This method is called after Node (Node properties)is changed.
208      * The document is generated from data modul (isDocumentGenerable=true)
209     */

210     protected String JavaDoc generateDocument() {
211         //System.out.println("Generating document - generate....");
212
ByteArrayOutputStream out = new ByteArrayOutputStream();
213         try {
214             ejbJar.write(out);
215             out.close();
216             return out.toString("UTF8"); //NOI18N
217
}
218         catch (IOException e) {
219             ErrorManager.getDefault ().notify(org.openide.ErrorManager.INFORMATIONAL, e);
220         }
221         catch (IllegalStateException JavaDoc e){
222             ErrorManager.getDefault ().notify(org.openide.ErrorManager.INFORMATIONAL, e);
223     }
224     return out.toString ();
225     }
226         
227     /** Update document in text editor. This method is called after Node (Node properties)is changed.
228      * The document is updated programaticaly - not from data modul (isDocumentGenerable=false)
229      * @param doc document which should be updated
230     */

231     protected String JavaDoc updateDocument(javax.swing.text.Document JavaDoc doc) {
232         return null;
233     }
234     /** Method from EjbJarProfy.OutputProvider
235     */

236     public void write(Application ejbJarProxy) throws IOException {
237         Application app = getApplication();
238         if (app!=null) {
239             app.merge(ejbJarProxy,Application.MERGE_UNION);
240         }
241         setNodeDirty(true);
242     }
243     /** Method from EjbJarProfy.OutputProvider
244     */

245     public FileObject getTarget() {
246         return getPrimaryFile();
247     }
248     
249     protected SAXParseError updateNode(InputSource is) throws IOException {
250         //System.out.println("updateNode() ");
251
if (ejbJar==null) {
252             try {
253                 ejbJar = DDProvider.getDefault().getDDRoot(getPrimaryFile());
254                 firePropertyChange(Node.PROP_PROPERTY_SETS,null,null);
255                 if (unparsable) {
256                     unparsable=false;
257                 }
258                 return null;
259             } catch (RuntimeException JavaDoc e) {
260                 if (!unparsable) {
261                     unparsable=true;
262                 }
263                 return new SAXParseError(new SAXParseException(e.getMessage(),new org.xml.sax.helpers.LocatorImpl JavaDoc()));
264             }
265         }
266         Application app=null;
267         try {
268             app = EarDDUtils.createApplication(is);
269             if (!ejbJar.getVersion().equals(app.getVersion())) {
270                 ((ApplicationProxy)ejbJar).setOriginal(app);
271             }
272             unparsable=false;
273             if (app.getError()!= null) {
274                 // semantic error we can badge or tooltip here
275
System.out.println(app.getError().getMessage());
276             }
277         } catch (SAXParseException e) {
278             if (!unparsable) {
279                 unparsable=true;
280             }
281             return new SAXParseError(e);
282         } catch (SAXException e) {
283             if (!unparsable) {
284                 unparsable=true;
285             }
286             throw new IOException();
287         }
288         if (app!=null){
289             // set new graph or merge it with old one
290
ejbJar.merge(app,org.netbeans.modules.schema2beans.BaseBean.MERGE_UPDATE);
291
292         }
293         return null;
294     }
295     
296     /**
297      * Adds Ejb
298      *
299      * One ejb element element. The ejb-name is
300      * set to Ejb_&lt clazz&gt by default.
301      *
302      * @param clazz class name of ejb
303      * @param pathName path to ejb class (pkg/foo/Bar)
304      */

305 // public void createDefaultEJBConfiguration (String clazz, String urlPattern) {
306
// // PENDING: should be synchronized
307
// EnterpriseBeans a = getEjbJar ().getEnterpriseBeans();
308
// try {
309
// Session newEjb = a.newSession();//Ludo todo add more ejb type cmp, mdb.
310
// newEjb.setEjbClass (clazz);
311
// String name = "Ludo was there Name123";//DDUtils.findFreeName (a.getServlet (), "EjbName" , "Ejb_"+clazz); // NOI18N
312
// newEjb.setEjbName (name);
313
// newEjb.setDescription (NbBundle.getMessage (EarDataObject.class, "TXT_newEjbElementDescription"));
314
// newEjb.setDisplayName ("Session "+clazz); // NOI18N
315
// a.addSession (newEjb);
316
//
317
// setNodeDirty (true);
318
// } catch (Exception ex) {}
319
// }
320

321     protected DataObject handleCopy(DataFolder f) throws IOException {
322         DataObject dObj = super.handleCopy(f);
323         try { dObj.setValid(false); }catch(java.beans.PropertyVetoException JavaDoc e){}
324         return dObj;
325     }
326
327     protected void dispose () {
328         // no more changes in DD
329
synchronized (this) {
330             updates = null;
331             if (updateTask != null) {
332                 updateTask.cancel();
333             }
334         }
335         super.dispose ();
336     }
337     
338     /** Getter for property documentDTD.
339      * @return Value of property documentDTD or <CODE>null</CODE> if documentDTD cannot be obtained.
340      */

341   /*** public String getDocumentDTD () {
342         if (documentDTD == null) {
343             EjbJar wa = getEjbJar ();
344         }
345         return documentDTD;
346     }***/

347
348     /** This methods gets called when servlet is changed
349      * @param evt - object that describes the change.
350      */

351     public void deploymentChange (DDChangeEvent evt) {
352         // fix of #28542, don't add ejb, if it's already defined in DD
353
// if ( evt.getType() == DDChangeEvent.EJB_ADDED && EjbDefined( evt.getNewValue() ) )
354
// return;
355

356         synchronized (this) {
357             if (updates == null) {
358                 updates = new Vector ();
359             }
360             updates.addElement (evt);
361         }
362         
363         // schedule processDDChangeEvent
364
if (updateTask == null) {
365             updateTask = RequestProcessor.getDefault().post (new Runnable JavaDoc () {
366                 public void run () {
367                     java.util.List JavaDoc changes = null;
368                     synchronized (EarDataObject.this) {
369                         if (!EarDataObject.this.isValid())
370                             return;
371                         if (updates != null) {
372                             changes = updates;
373                             updates = null;
374                         }
375                     }
376                     if (changes != null)
377                         showDDChangesDialog (changes);
378                 }
379             }, 2000, Thread.MIN_PRIORITY);
380         }
381         else {
382             updateTask.schedule (2000);
383         }
384     }
385     
386 // private boolean EjbDefined(String classname) {
387
// EjbJar ejbJar = getEjbJar();
388
// if (ejbJar==null) return true;
389
// Ejb[] ejbs = ejbJar.getEnterpriseBeans(). getEjbs();
390
// for ( int i = 0; i < ejbs.length; i++ ) {
391
// //Ludo TODO need to check the other class names: remote, home....depending on the ejb type
392
// if ( ejbs[i].getEjbClass() != null && ejbs[i].getEjbClass().equals( classname ) )
393
// return true;
394
// }
395
// return false;
396
// }
397

398     private void showDDChangesDialog (List changes) {
399         final JButton JavaDoc processButton;
400         final JButton JavaDoc processAllButton;
401         final JButton JavaDoc closeButton;
402         final DDChangesPanel connectionPanel;
403         final DialogDescriptor confirmChangesDescriptor;
404         final Dialog JavaDoc confirmChangesDialog[] = { null };
405         
406         processButton = new JButton JavaDoc (NbBundle.getMessage (EarDataObject.class, "LAB_processButton"));
407         processButton.setMnemonic (NbBundle.getMessage (EarDataObject.class, "LAB_processButton_Mnemonic").charAt (0));
408         processButton.setToolTipText (NbBundle.getMessage (EarDataObject.class, "ACS_processButtonA11yDesc"));
409         processAllButton = new JButton JavaDoc (NbBundle.getMessage (EarDataObject.class, "LAB_processAllButton"));
410         processAllButton.setMnemonic (NbBundle.getMessage (EarDataObject.class, "LAB_processAllButton_Mnemonic").charAt (0));
411         processAllButton.setToolTipText (NbBundle.getMessage (EarDataObject.class, "ACS_processAllButtonA11yDesc"));
412         closeButton = new JButton JavaDoc (NbBundle.getMessage (EarDataObject.class, "LAB_closeButton"));
413         closeButton.setMnemonic (NbBundle.getMessage (EarDataObject.class, "LAB_closeButton_Mnemonic").charAt (0));
414         closeButton.setToolTipText (NbBundle.getMessage (EarDataObject.class, "ACS_closeButtonA11yDesc"));
415         final Object JavaDoc [] options = new Object JavaDoc [] {
416             processButton,
417             processAllButton
418         };
419         final Object JavaDoc [] additionalOptions = new Object JavaDoc [] {
420             closeButton
421         };
422     
423         String JavaDoc fsname = ""; //NOI18N
424
Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
425         if (project != null) {
426             String JavaDoc projectName = null;
427             ProjectInformation projectInfo = ProjectUtils.getInformation(project);
428             if(projectInfo != null){
429                 fsname = projectInfo.getName();
430             }
431         }
432
433     ///LUDO WebModule wm = WebModule.getWebModule(getPrimaryFile ());
434
/// if (wm!=null) {
435
/// fsname=wm.getContextPath();
436
/// }
437
String JavaDoc caption = NbBundle.getMessage (EarDataObject.class, "MSG_SynchronizeCaption", fsname);
438         connectionPanel = new DDChangesPanel (caption, processButton);
439         confirmChangesDescriptor = new DialogDescriptor (
440             connectionPanel,
441             NbBundle.getMessage (EarDataObject.class, "LAB_ConfirmDialog"),
442             true,
443             options,
444             processButton,
445             DialogDescriptor.RIGHT_ALIGN,
446             HelpCtx.DEFAULT_HELP,
447             new ActionListener () {
448                 public void actionPerformed (ActionEvent e) {
449                     if (e.getSource () instanceof Component JavaDoc) {
450                         Component JavaDoc root;
451
452                         // hack to avoid multiple calls for disposed dialogs:
453
root = javax.swing.SwingUtilities.getRoot ((Component JavaDoc)e.getSource ());
454                         if (!root.isDisplayable ()) {
455                             return;
456                         }
457                     }
458                     if (options[0].equals (e.getSource ())) {
459                         int min = connectionPanel.changesList.getMinSelectionIndex ();
460                         int max = connectionPanel.changesList.getMaxSelectionIndex ();
461                         for (int i = max; i >= min; i--) {
462                             if (connectionPanel.changesList.isSelectedIndex (i)) {
463                                 final DDChangeEvent ev = (DDChangeEvent)connectionPanel.listModel.getElementAt (i);
464                                 processDDChangeEvent (ev);
465                                 connectionPanel.listModel.removeElementAt (i);
466                             }
467                         }
468                         if (connectionPanel.listModel.isEmpty ()) {
469                             confirmChangesDialog[0].setVisible (false);
470                         }
471                         else {
472                             processButton.setEnabled (false);
473                         }
474                     }
475                     else if (options[1].equals (e.getSource ())) {
476                         Enumeration en = connectionPanel.listModel.elements ();
477                         while (en.hasMoreElements ()) {
478                             processDDChangeEvent ((DDChangeEvent)en.nextElement ());
479                         }
480                         confirmChangesDialog[0].setVisible (false);
481                         connectionPanel.setChanges (null);
482                     }
483                     else if (additionalOptions[0].equals (e.getSource ())) {
484                         confirmChangesDialog[0].setVisible (false);
485                         connectionPanel.setChanges (null);
486                     }
487                 }
488             }
489         );
490         confirmChangesDescriptor.setAdditionalOptions (additionalOptions);
491         
492         processButton.setEnabled (false);
493         processAllButton.requestFocus ();
494         connectionPanel.setChanges (changes);
495         
496         try {
497             confirmChangesDialog[0] = DialogDisplayer.getDefault ().createDialog (confirmChangesDescriptor);
498             confirmChangesDialog[0].setVisible(true);
499         } finally {
500             confirmChangesDialog[0].dispose ();
501         }
502     }
503     
504     private void processDDChangeEvent (DDChangeEvent evt) {
505         if (!isValid())
506             return;
507        
508         if (evt.getType () == DDChangeEvent.EJB_ADDED) {
509             String JavaDoc clz = evt.getNewValue ();
510
511             // new from template or copy of another servlet
512
String JavaDoc urimapping = "/servlet/"+clz; // NOI18N
513
//createDefaultEJBConfiguration (clz, urimapping);
514
} else {
515             updateDD(evt.getOldValue(), (String JavaDoc)evt.getNewValue (), evt.getType());
516         }
517     }
518
519     public HelpCtx getHelpCtx() {
520         return new HelpCtx(EarDataObject.class);
521     }
522
523     private RequestProcessor.Task elementTask;
524     private List deletedEjbNames;
525     private List newFileNames;
526     
527     private void elementCreated(final String JavaDoc elementName) {
528         synchronized (this) {
529             if (newFileNames==null) {
530                 newFileNames=new ArrayList();
531             }
532             newFileNames.add(elementName);
533         }
534         
535         if (elementTask == null) {
536             elementTask = RequestProcessor.getDefault().post (new Runnable JavaDoc () {
537                 public void run () {
538                     if (deletedEjbNames!=null) {
539                         for (int i=0;i<deletedEjbNames.size();i++) {
540                             String JavaDoc deletedServletName = (String JavaDoc)deletedEjbNames.get(i);
541                             String JavaDoc deletedName=deletedServletName;
542                             int index = deletedServletName.lastIndexOf("."); //NOI18N
543
if (index>0) deletedName = deletedServletName.substring(index+1);
544                             boolean found = false;
545                             for (int j=0;j<newFileNames.size();j++) {
546                                 String JavaDoc newFileName = (String JavaDoc)newFileNames.get(j);
547                                 String JavaDoc newName = newFileName;
548                                 int ind = newFileName.lastIndexOf("."); //NOI18N
549
if (ind>0) newName = newFileName.substring(ind+1);
550                                 if (deletedName.equals(newName)) { // servlet was removed
551
found=true;
552                                     DDChangeEvent ddEvent =
553                                         new DDChangeEvent(EarDataObject.this,EarDataObject.this,deletedServletName,newFileName,DDChangeEvent.EJB_CHANGED);
554                                     deploymentChange (ddEvent);
555                                     synchronized (EarDataObject.this) {
556                                         newFileNames.remove(newFileName);
557                                     }
558                                     break;
559                                 }
560                             }
561                             if (!found) {
562                                 DDChangeEvent ddEvent =
563                                     new DDChangeEvent(EarDataObject.this,EarDataObject.this,null,deletedServletName,DDChangeEvent.EJB_DELETED);
564                                 deploymentChange (ddEvent);
565                             }
566                         } //end for
567
synchronized (EarDataObject.this) {
568                             deletedEjbNames=null;
569                         }
570                     } // servlets
571

572                     synchronized (EarDataObject.this) {
573                         newFileNames=null;
574                     }
575                     
576                 }///end run
577

578             }, 1500, Thread.MIN_PRIORITY);
579         }
580         else {
581             elementTask.schedule (1500);
582         }
583     }
584
585     public void fileRenamed(org.openide.filesystems.FileRenameEvent fileRenameEvent) {
586 // System.out.println("fileRenamed"); //NOI18N
587
// System.out.println("fileRenameEvent : " + fileRenameEvent); //NOI18N
588

589         FileObject fo = fileRenameEvent.getFile();
590         String JavaDoc resourceName = getPackageName (fo);
591         if (resourceName != null) {
592             int index = resourceName.lastIndexOf("."); //NOI18N
593
String JavaDoc oldName = fileRenameEvent.getName();
594             String JavaDoc oldResourceName = (index>=0?resourceName.substring(0,index+1):"")+oldName;
595             Application ejbJar = getApplication();
596             if (ejbJar.getStatus()==Application.STATE_VALID) {
597                 fireEvent(oldResourceName, resourceName, DDChangeEvent.EJB_CHANGED);
598                 ///Ejb ejb = (Ejb)ejbJar.findBeanByName("Session","EjbClass",oldResourceName); //NOI18N
599
/****
600                 Listener listener = (Listener)ejbJar.findBeanByName("Listener","ListenerClass",oldResourceName); //NOI18N
601                 if (listener!=null) {
602                     DDChangeEvent ddEvent =
603                         new DDChangeEvent(this,this,oldResourceName,resourceName,DDChangeEvent.LISTENER_CHANGED);
604                     deploymentChange (ddEvent);
605                 }
606                 Filter filter = (Filter)ejbJar.findBeanByName("Filter","FilterClass",oldResourceName); //NOI18N
607                 if (filter!=null) {
608                     DDChangeEvent ddEvent =
609                         new DDChangeEvent(this,this,oldResourceName,resourceName,DDChangeEvent.FILTER_CHANGED);
610                     deploymentChange (ddEvent);
611                 }***********/

612             }
613         }
614     }
615     
616     public void fileFolderCreated(org.openide.filesystems.FileEvent fileEvent) {
617     }
618     
619     public void fileDeleted(org.openide.filesystems.FileEvent fileEvent) {
620 // System.out.println("fileDeleted"); //NOI18N
621
// System.out.println("fileEvent : " + fileEvent); //NOI18N
622

623         FileObject fo = fileEvent.getFile();
624         String JavaDoc resourceName = getPackageName (fo);
625         if (resourceName != null) {
626             boolean foundElement=false;
627             if (newFileNames==null) {
628                 foundElement =
629                     fireEvent(null, resourceName, DDChangeEvent.EJB_DELETED);
630                 
631                 /*if (foundElement) return;
632                 Filter[] filters = getEjbJar().getFilter();
633                 for (int i=0;i<filters.length;i++) {
634                     if (resourceName.equals(filters[i].getFilterClass())) {
635                         DDChangeEvent ddEvent = new DDChangeEvent(this,this,null,resourceName,DDChangeEvent.FILTER_DELETED);
636                         deploymentChange (ddEvent);
637                         foundElement=true;
638                         break;
639                     }
640                 }
641                 if (foundElement) return;
642                 Listener[] listeners = getEjbJar().getListener();
643                 for (int i=0;i<listeners.length;i++) {
644                     if (resourceName.equals(listeners[i].getListenerClass())) {
645                         DDChangeEvent ddEvent = new DDChangeEvent(this,this,null,resourceName,DDChangeEvent.LISTENER_DELETED);
646                         deploymentChange (ddEvent);
647                         break; // listener with that class should be only one
648                     }
649                 }*/

650             } else {
651 // Ejb[] ejbs = getEjbJar().getEnterpriseBeans(). getEjbs();
652
// for (int i=0;i<ejbs.length;i++) {
653
// if (resourceName.equals(ejbs[i].getEjbClass())) {
654
// synchronized (this) {
655
// if (deletedEjbNames==null) {
656
// deletedEjbNames=new ArrayList();
657
// }
658
// deletedEjbNames.add(resourceName);
659
// }
660
// foundElement=true;
661
// break;
662
// }
663
// }
664
if (foundElement) return;
665
666                /* Filter[] filters = getEjbJar().getFilter();
667                 for (int i=0;i<filters.length;i++) {
668                     if (resourceName.equals(filters[i].getFilterClass())) {
669                         synchronized (this) {
670                             if (deletedFilterNames==null) {
671                                 deletedFilterNames=new ArrayList();
672                             }
673                             deletedFilterNames.add(resourceName);
674                         }
675                         foundElement=true;
676                         break;
677                     }
678                 }
679                 if (foundElement) return;
680                 Listener[] listeners = getEjbJar().getListener();
681                 for (int i=0;i<listeners.length;i++) {
682                     if (resourceName.equals(listeners[i].getListenerClass())) {
683                         synchronized (this) {
684                             if (deletedListenerNames==null) {
685                                 deletedListenerNames=new ArrayList();
686                             }
687                             deletedListenerNames.add(resourceName);
688                         }
689                         break;
690                     }
691                 }*/

692             }
693         }
694     }
695     
696     public void fileDataCreated(org.openide.filesystems.FileEvent fileEvent) {
697         FileObject fo = fileEvent.getFile();
698         String JavaDoc resourceName = getPackageName (fo);
699         if (resourceName != null) {
700             elementCreated(resourceName);
701         }
702     }
703     
704     public void fileChanged(org.openide.filesystems.FileEvent fileEvent) {
705     }
706     
707     public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fileAttributeEvent) {
708     }
709     
710     public void stateChanged (javax.swing.event.ChangeEvent JavaDoc e) {
711         refreshSourceFolders ();
712     }
713     
714     /** WeakListener for accepting external changes to web.xml
715     */

716     private class FileObjectObserver implements FileChangeListener {
717         FileObjectObserver (FileObject fo) {
718             fo.addFileChangeListener((FileChangeListener)org.openide.util.WeakListeners.create(
719                                         FileChangeListener.class, this, fo));
720         }
721         
722         public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) {
723         }
724         
725         public void fileChanged(FileEvent fileEvent) {
726             ApplicationProxy ejbJar = (ApplicationProxy) EarDataObject.this.getApplication();
727             boolean needRewriting = true;
728             if (ejbJar!= null && ejbJar.isWriting()) { // change from outside
729
ejbJar.setWriting(false);
730                 needRewriting=false;
731             }
732             if (isSavingDocument()) {// document is being saved
733
setSavingDocument(false);
734                 needRewriting=false;
735             }
736             if (needRewriting) getEditorSupport().restartTimer();
737         }
738         
739         public void fileDataCreated(FileEvent fileEvent) {
740         }
741         
742         public void fileDeleted(FileEvent fileEvent) {
743         }
744         
745         public void fileFolderCreated(FileEvent fileEvent) {
746         }
747         
748         public void fileRenamed(FileRenameEvent fileRenameEvent) {
749         }
750         
751     }
752
753 // private Application getEjbFromEjbClass(String ejbClassName){
754
// Ejb returnValue = null;
755
// Ejb[] ejbs = getEjbJar().getEnterpriseBeans(). getEjbs();
756
// for ( int i = 0; i < ejbs.length; i++ ) {
757
// if ( ejbs[i].getEjbClass() != null &&
758
// ejbs[i].getEjbClass().equals( ejbClassName ) ){
759
// returnValue = ejbs[i];
760
// break;
761
// }
762
// }
763
// return returnValue;
764
// }
765
//
766
// private int getBeanInterfaceType(String interfaceName){
767
// int interfaceType = -1;
768
// EntityAndSession[] beans = ejbJar.getEnterpriseBeans().getSession();
769
// for ( int i = 0; i < beans.length; i++ ) {
770
// if ( beans[i].getHome() != null &&
771
// beans[i].getHome().equals( interfaceName ) ){
772
// interfaceType = HOME;
773
// break;
774
// }
775
// if ( beans[i].getRemote() != null &&
776
// beans[i].getRemote().equals( interfaceName ) ){
777
// interfaceType = REMOTE;
778
// break;
779
// }
780
// if ( beans[i].getLocalHome() != null &&
781
// beans[i].getLocalHome().equals( interfaceName ) ){
782
// interfaceType = LOCAL_HOME;
783
// break;
784
// }
785
// if ( beans[i].getLocal() != null &&
786
// beans[i].getLocal().equals( interfaceName ) ){
787
// interfaceType = LOCAL;
788
// break;
789
// }
790
// }
791
// return interfaceType;
792
// }
793
//
794
// private EntityAndSession getBeanFromInterface(String interfaceName){
795
// EntityAndSession returnValue = null;
796
// EntityAndSession[] beans = ejbJar.getEnterpriseBeans().getSession();
797
// for ( int i = 0; i < beans.length; i++ ) {
798
// if ( beans[i].getHome() != null &&
799
// beans[i].getHome().equals( interfaceName ) ){
800
// returnValue = beans[i];
801
// break;
802
// }
803
// if ( beans[i].getRemote() != null &&
804
// beans[i].getRemote().equals( interfaceName ) ){
805
// returnValue = beans[i];
806
// break;
807
// }
808
// if ( beans[i].getLocalHome() != null &&
809
// beans[i].getLocalHome().equals( interfaceName ) ){
810
// returnValue = beans[i];
811
// break;
812
// }
813
// if ( beans[i].getLocal() != null &&
814
// beans[i].getLocal().equals( interfaceName ) ){
815
// returnValue = beans[i];
816
// break;
817
// }
818
// }
819
// return returnValue;
820
// }
821

822     private int getSpecificEvent(int eventType, int interfaceType){
823         if(eventType == DDChangeEvent.EJB_CHANGED){
824             switch(interfaceType){
825                 case HOME :{
826                     return DDChangeEvent.EJB_HOME_CHANGED;
827                 }
828                 case REMOTE :{
829                     return DDChangeEvent.EJB_REMOTE_CHANGED;
830                 }
831                 case LOCAL_HOME :{
832                     return DDChangeEvent.EJB_LOCAL_HOME_CHANGED;
833                 }
834                 case LOCAL :{
835                     return DDChangeEvent.EJB_LOCAL_CHANGED;
836                 }
837             }
838         }
839         if(eventType == DDChangeEvent.EJB_DELETED){
840             switch(interfaceType){
841                 case HOME :{
842                     return DDChangeEvent.EJB_HOME_DELETED;
843                 }
844                 case REMOTE :{
845                     return DDChangeEvent.EJB_REMOTE_DELETED;
846                 }
847                 case LOCAL_HOME :{
848                     return DDChangeEvent.EJB_LOCAL_HOME_DELETED;
849                 }
850                 case LOCAL :{
851                     return DDChangeEvent.EJB_LOCAL_DELETED;
852                 }
853             }
854         }
855         ///assert(false : "control should never reach here -unsupported event type detected"); //NOI18N
856
return -1 ;
857     }
858
859     private boolean fireEvent(String JavaDoc oldResourceName, String JavaDoc resourceName,
860                 int eventType){
861 // System.out.println("fireEvent"); //NOI18N
862
// System.out.println("oldResourceName : " + oldResourceName); //NOI18N
863
// System.out.println("resourceName : " + resourceName); //NOI18N
864
// System.out.println("eventType : " + eventType); //NOI18N
865

866             boolean elementFound = false;
867             String JavaDoc resource = null;
868             int specificEventType = -1;
869             if(eventType == DDChangeEvent.EJB_CHANGED){
870                 resource = oldResourceName;
871             } else {
872                 resource = resourceName;
873             }
874 // Ejb ejb = getEjbFromEjbClass(resource);
875
//
876
// if(ejb != null){
877
// if(eventType == DDChangeEvent.EJB_CHANGED){
878
// specificEventType = DDChangeEvent.EJB_CLASS_CHANGED;
879
// } else {
880
// specificEventType = DDChangeEvent.EJB_CLASS_DELETED;
881
// }
882
// elementFound = true;
883
// }
884
//
885
// if(!elementFound){
886
// int interfaceType = getBeanInterfaceType(resource);
887
//
888
// if(interfaceType > 0 ){
889
// specificEventType =
890
// getSpecificEvent(eventType, interfaceType);
891
// elementFound = true;
892
// }
893
// }
894
if (elementFound) {
895                 assert(specificEventType > 0);
896                 DDChangeEvent ddEvent =
897                     new DDChangeEvent(this,this,oldResourceName,
898                             resourceName, specificEventType);
899                 deploymentChange (ddEvent);
900             }
901             return elementFound;
902     }
903
904     private void updateDD(String JavaDoc oldResourceName, String JavaDoc resourceName,
905                 int eventType){
906 // System.out.println("updateDD"); //NOI18N
907
// System.out.println("oldResourceName : " + oldResourceName); //NOI18N
908
// System.out.println("resourceName : " + resourceName); //NOI18N
909
// System.out.println("eventType : " + eventType); //NOI18N
910

911         boolean ddModified = false;
912
913         switch(eventType){
914             case DDChangeEvent.EJB_CLASS_CHANGED : {
915                 // update ejb-class
916
if (oldResourceName == null)
917                     return;
918
919 // Ejb ejb = getEjbFromEjbClass(oldResourceName);
920
// if(ejb != null){
921
// ejb.setEjbClass(resourceName);
922
// ddModified = true;
923
// }
924
break;
925             }
926             case DDChangeEvent.EJB_CLASS_DELETED : {
927                 // delete the whole ejb(impl file deletion)
928
if (resourceName == null){
929                     return;
930                 }
931
932 // Ejb ejb = getEjbFromEjbClass(resourceName);
933
// if(ejb != null){
934
// EjbJar ejbJar = getEjbJar ();
935
// EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans();
936
// if(enterpriseBeans != null){
937
// enterpriseBeans.removeEjb (ejb);
938
// ddModified = true;
939
// }
940
// }
941
break;
942             }
943
944             case DDChangeEvent.EJB_HOME_CHANGED : {
945                 if (oldResourceName == null)
946                     return;
947
948 // EntityAndSession bean = getBeanFromInterface(oldResourceName);
949
// if(bean != null){
950
// assert(oldResourceName.equals (bean.getHome()));
951
// bean.setHome(resourceName);
952
// ddModified = true;
953
// }
954
break;
955             }
956
957             case DDChangeEvent.EJB_REMOTE_CHANGED : {
958                 if (oldResourceName == null)
959                     return;
960
961 // EntityAndSession bean = getBeanFromInterface(oldResourceName);
962
// if(bean != null){
963
// assert(oldResourceName.equals (bean.getRemote()));
964
// bean.setRemote(resourceName);
965
// ddModified = true;
966
// }
967
break;
968             }
969
970             case DDChangeEvent.EJB_LOCAL_HOME_CHANGED : {
971                 if (oldResourceName == null)
972                     return;
973
974 // EntityAndSession bean = getBeanFromInterface(oldResourceName);
975
// if(bean != null){
976
// assert(oldResourceName.equals (bean.getLocalHome()));
977
// bean.setLocalHome(resourceName);
978
// ddModified = true;
979
// }
980
break;
981             }
982
983             case DDChangeEvent.EJB_LOCAL_CHANGED : {
984                 if (oldResourceName == null)
985                     return;
986
987 // EntityAndSession bean = getBeanFromInterface(oldResourceName);
988
// if(bean != null){
989
// assert(oldResourceName.equals (bean.getLocal()));
990
// bean.setLocal(resourceName);
991
// ddModified = true;
992
// }
993
break;
994             }
995
996             case DDChangeEvent.EJB_HOME_DELETED : {
997                 if (resourceName == null){
998                     return;
999                 }
1000
1001// EntityAndSession bean = getBeanFromInterface(resourceName);
1002
// if(bean != null){
1003
// assert(resourceName.equals (bean.getHome()));
1004
// bean.setHome(null);
1005
// ddModified = true;
1006
// }
1007
break;
1008            }
1009            
1010            case DDChangeEvent.EJB_REMOTE_DELETED : {
1011                if (resourceName == null){
1012                    return;
1013                }
1014
1015// EntityAndSession bean = getBeanFromInterface(resourceName);
1016
// if(bean != null){
1017
// assert(resourceName.equals (bean.getRemote()));
1018
// bean.setRemote(null);
1019
// ddModified = true;
1020
// }
1021
break;
1022            }
1023
1024            case DDChangeEvent.EJB_LOCAL_HOME_DELETED : {
1025                if (resourceName == null){
1026                    return;
1027                }
1028
1029// EntityAndSession bean = getBeanFromInterface(resourceName);
1030
// if(bean != null){
1031
// assert(resourceName.equals (bean.getLocalHome()));
1032
// bean.setLocalHome(null);
1033
// ddModified = true;
1034
// }
1035
break;
1036            }
1037
1038            case DDChangeEvent.EJB_LOCAL_DELETED : {
1039                if (resourceName == null){
1040                    return;
1041                }
1042
1043// EntityAndSession bean = getBeanFromInterface(resourceName);
1044
// if(bean != null){
1045
// assert(resourceName.equals (bean.getLocal()));
1046
// bean.setLocal(null);
1047
// ddModified = true;
1048
// }
1049
break;
1050            }
1051        }
1052
1053        if(ddModified){
1054            setNodeDirty (true);
1055        }
1056            /*
1057            EjbJar a = getEjbJar ();
1058            Ejb[] ejbs = a.getEnterpriseBeans(). getEjbs();
1059            java.util.Vector EJBNames = new java.util.Vector ();
1060            for (int i=0; i<ejbs.length; i++) {
1061                if (clz.equals (ejbs[i].getEjbClass ())) {
1062                    EJBNames.addElement (ejbs[i].getEjbName ());
1063                    a.getEnterpriseBeans().removeEjb (ejbs[i]);
1064                }
1065            }
1066           */

1067           //delete other elements, if any, refering to this ejb
1068
// ServletMapping [] mappings = wa.getServletMapping ();
1069
// for (int i=0; i<mappings.length; i++) {
1070
// if (EJBNames.contains (mappings[i].getServletName ())) {
1071
// wa.removeEJBMapping (mappings[i]);
1072
// }
1073
// }
1074
}
1075}
1076
Popular Tags