KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > sunresources > resourcesloader > SunResourceDataObject


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 package org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader;
20
21 import java.io.InputStream JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23
24
25 import org.openide.loaders.DataObjectExistsException;
26 import org.openide.loaders.XMLDataObject;
27 import org.openide.nodes.Node;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.NbBundle;
30 import org.openide.ErrorManager;
31
32 import org.netbeans.api.xml.cookies.CheckXMLCookie;
33 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
34 import org.netbeans.spi.xml.cookies.CheckXMLSupport;
35 import org.netbeans.spi.xml.cookies.DataObjectAdapters;
36 import org.netbeans.spi.xml.cookies.ValidateXMLSupport;
37
38 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider;
39 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcConnectionPool;
40 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcResource;
41 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.AdminObjectResource;
42 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorResource;
43 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorConnectionPool;
44 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.MailResource;
45 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.PersistenceManagerFactoryResource;
46 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
47 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBean;
48 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBeanDataNode;
49 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBean;
50 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBeanDataNode;
51 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBean;
52 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBeanDataNode;
53 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBean;
54 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBeanDataNode;
55 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBean;
56 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBeanDataNode;
57
58 import org.openide.filesystems.FileAttributeEvent;
59 import org.openide.filesystems.FileChangeListener;
60 import org.openide.filesystems.FileEvent;
61 import org.openide.filesystems.FileObject;
62 import org.openide.filesystems.FileRenameEvent;
63 import org.openide.util.WeakListeners;
64
65 /** Represents a SunResource object in the Repository.
66  *
67  * @author nityad
68  */

69 public class SunResourceDataObject extends XMLDataObject implements FileChangeListener { // extends MultiDataObject{
70
private static String JavaDoc JDBC_CP = "jdbc-connection-pool"; //NOI18N
71
private static String JavaDoc JDBC_DS = "jdbc-resource"; //NOI18N
72
private static String JavaDoc PMF = "persistence-manager-factory-resource"; //NOI18N
73
private static String JavaDoc MAIL = "mail-resource"; //NOI18N
74
private static String JavaDoc JMS = "jms-resource"; //NOI18N
75

76     private ValidateXMLCookie validateCookie = null;
77     private CheckXMLCookie checkCookie = null;
78     
79     ConnPoolBean cpBean = null;
80     DataSourceBean dsBean = null;
81     PersistenceManagerBean pmfBean = null;
82     JavaMailSessionBean mailBean = null;
83     JMSBean jmsBean = null;
84     
85     String JavaDoc resType;
86     
87     public SunResourceDataObject(FileObject pf, SunResourceDataLoader loader) throws DataObjectExistsException {
88         super(pf, loader);
89         pf.addFileChangeListener((FileChangeListener) WeakListeners.create(FileChangeListener.class, this, pf));
90         
91         resType = getResource(pf);
92 // init(pf);
93
}
94     
95 // private void init(FileObject pf) {
96
// CookieSet cookies = getCookieSet();
97
// Add whatever capabilities you need, e.g.:
98
/*
99         cookies.add(new ExecSupport(getPrimaryEntry()));
100         // See Editor Support template in Editor API:
101         cookies.add(new SunResourceEditorSupport(this));
102         cookies.add(new CompilerSupport.Compile(getPrimaryEntry()));
103         cookies.add(new CompilerSupport.Build(getPrimaryEntry()));
104         cookies.add(new CompilerSupport.Clean(getPrimaryEntry()));
105         cookies.add(new OpenCookie() {
106             public void open() {
107                 // do something...but usually you want to use OpenSupport instead
108             }
109         });
110          */

111 // }
112

113     public org.openide.nodes.Node.Cookie getCookie(Class JavaDoc c) {
114         Node.Cookie retValue = null;
115         if (ValidateXMLCookie.class.isAssignableFrom(c)) {
116             if (validateCookie == null) {
117                 InputSource JavaDoc in = DataObjectAdapters.inputSource(this);
118                 validateCookie = new ValidateXMLSupport(in);
119             }
120             return validateCookie;
121         } else if (CheckXMLCookie.class.isAssignableFrom(c)) {
122             if (checkCookie == null) {
123                 InputSource JavaDoc in = DataObjectAdapters.inputSource(this);
124                 checkCookie = new CheckXMLSupport(in);
125             }
126             return checkCookie;
127         }
128         
129         if (retValue == null) {
130             retValue = super.getCookie(c);
131         }
132         return retValue;
133     }
134     
135     
136     public HelpCtx getHelpCtx() {
137         return null; // HelpCtx.DEFAULT_HELP;
138
// If you add context help, change to:
139
// return new HelpCtx(SunResourceDataObject.class);
140
}
141     
142     protected Node createNodeDelegate() {
143         if(resType != null){
144             if(this.resType.equals(this.JDBC_CP)){
145                 Node node = new ConnPoolBeanDataNode(this, getPool());
146                 return node;
147             }if(this.resType.equals(this.JDBC_DS)){
148                 Node node = new DataSourceBeanDataNode(this, getDataSource());
149                 return node;
150             }if(this.resType.equals(this.PMF)){
151                 Node node = new PersistenceManagerBeanDataNode(this, getPersistenceManager());
152                 return node;
153             }if(this.resType.equals(this.MAIL)){
154                 Node node = new JavaMailSessionBeanDataNode(this, getMailSession());
155                 return node;
156             }if(this.resType.equals(this.JMS)){
157                 Node node = new JMSBeanDataNode(this, getJMS());
158                 return node;
159             }else{
160                 String JavaDoc mess = NbBundle.getMessage(SunResourceDataObject.class, "Info_notSunResource"); //NOI18N
161
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, mess);
162                 return new SunResourceDataNode(this);
163             }
164         }else{
165             return new SunResourceDataNode(this);
166         }
167     }
168     
169     private String JavaDoc getResource(FileObject primaryFile) {
170        String JavaDoc type = null;
171        try {
172             if((! primaryFile.isFolder()) && primaryFile.isValid()){
173                 InputStream JavaDoc in = primaryFile.getInputStream();
174                 Resources resources = DDProvider.getDefault().getResourcesGraph(in);
175                 
176                 // identify JDBC Connection Pool xml
177
JdbcConnectionPool[] pools = resources.getJdbcConnectionPool();
178                 if(pools.length != 0){
179                     ConnPoolBean currCPBean = ConnPoolBean.createBean(pools[0]);
180                     type = this.JDBC_CP;
181                     setPool(currCPBean);
182                     return type;
183                 }
184                 
185                 // identify JDBC Resources xml
186
JdbcResource[] dataSources = resources.getJdbcResource();
187                 if(dataSources.length != 0){
188                     DataSourceBean currDSBean = DataSourceBean.createBean(dataSources[0]);
189                     type = this.JDBC_DS;
190                     setDataSource(currDSBean);
191                     return type;
192                 }
193                 
194                 // import Persistence Manager Factory Resources
195
PersistenceManagerFactoryResource[] pmfResources = resources.getPersistenceManagerFactoryResource();
196                 if(pmfResources.length != 0){
197                     PersistenceManagerBean currPMFBean = PersistenceManagerBean.createBean(pmfResources[0]);
198                     type = this.PMF;
199                     setPersistenceManager(currPMFBean);
200                     return type;
201                 }
202                 
203                 // import Mail Resources
204
MailResource[] mailResources = resources.getMailResource();
205                 if(mailResources.length != 0){
206                     JavaMailSessionBean currMailBean = JavaMailSessionBean.createBean(mailResources[0]);
207                     type = this.MAIL;
208                     setMailSession(currMailBean);
209                     return type;
210                 }
211                 
212                 // import Admin Object Resources
213
AdminObjectResource[] aoResources = resources.getAdminObjectResource();
214                 if(aoResources.length != 0){
215                     JMSBean jmsBean = JMSBean.createBean(aoResources[0]);
216                     type = this.JMS;
217                     setJMS(jmsBean);
218                     return type;
219                 }
220                 
221                 ConnectorResource[] connResources = resources.getConnectorResource();
222                 ConnectorConnectionPool[] connPoolResources = resources.getConnectorConnectionPool();
223                 if(connResources.length != 0 && connPoolResources.length != 0){
224                     JMSBean jmsBean = JMSBean.createBean(resources);
225                     type = this.JMS;
226                     setJMS(jmsBean);
227                     return type;
228                 }
229                 
230                 return type;
231             }else
232                 return type;
233         }catch(NullPointerException JavaDoc npe){
234             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe);
235             return type;
236         }catch(Exception JavaDoc ex){
237             //ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getLocalizedMessage());
238
return type;
239         }
240        
241     }
242     
243     private void setPool(ConnPoolBean in_cpBean){
244         this.cpBean = in_cpBean;
245     }
246     
247     private ConnPoolBean getPool(){
248         return this.cpBean;
249     }
250     
251     private void setDataSource(DataSourceBean in_dsBean){
252         this.dsBean = in_dsBean;
253     }
254     
255     private DataSourceBean getDataSource(){
256         return this.dsBean;
257     }
258     
259     private void setPersistenceManager(PersistenceManagerBean in_pmfBean){
260         this.pmfBean = in_pmfBean;
261     }
262     
263     private PersistenceManagerBean getPersistenceManager(){
264         return this.pmfBean;
265     }
266     
267     private void setMailSession(JavaMailSessionBean in_mailBean){
268         this.mailBean = in_mailBean;
269     }
270     
271     private JavaMailSessionBean getMailSession(){
272         return this.mailBean;
273     }
274     
275     private void setJMS(JMSBean in_jmsBean){
276         this.jmsBean = in_jmsBean;
277     }
278     
279     private JMSBean getJMS(){
280         return this.jmsBean;
281     }
282     
283     public void fileAttributeChanged (FileAttributeEvent fe) {
284         updateDataObject();
285     }
286     
287     public void fileChanged (FileEvent fe) {
288         updateDataObject();
289     }
290     
291     public void fileDataCreated (FileEvent fe) {
292         updateDataObject ();
293     }
294     
295     public void fileDeleted (FileEvent fe) {
296         updateDataObject ();
297     }
298     
299     public void fileFolderCreated (FileEvent fe) {
300         updateDataObject ();
301     }
302     
303     public void fileRenamed (FileRenameEvent fe) {
304         updateDataObject ();
305     }
306     
307     private void updateDataObject(){
308         resType = getResource(this.getPrimaryFile());
309     }
310     
311     public String JavaDoc getResourceType(){
312         return resType;
313     }
314     // If you made an Editor Support you will want to add these methods:
315

316     /*public final void addSaveCookie(SaveCookie save) {
317         getCookieSet().add(save);
318     }
319      
320     public final void removeSaveCookie(SaveCookie save) {
321         getCookieSet().remove(save);
322     }*/

323   
324 }
325
Popular Tags