001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader;
042:
043: import java.io.InputStream;
044: import org.xml.sax.InputSource;
045:
046: import org.openide.loaders.DataObjectExistsException;
047: import org.openide.loaders.XMLDataObject;
048: import org.openide.nodes.Node;
049: import org.openide.util.HelpCtx;
050: import org.openide.util.NbBundle;
051: import org.openide.ErrorManager;
052:
053: import org.netbeans.api.xml.cookies.CheckXMLCookie;
054: import org.netbeans.api.xml.cookies.ValidateXMLCookie;
055: import org.netbeans.spi.xml.cookies.CheckXMLSupport;
056: import org.netbeans.spi.xml.cookies.DataObjectAdapters;
057: import org.netbeans.spi.xml.cookies.ValidateXMLSupport;
058:
059: import org.netbeans.modules.j2ee.sun.dd.api.DDProvider;
060: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcConnectionPool;
061: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcResource;
062: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.AdminObjectResource;
063: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorResource;
064: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorConnectionPool;
065: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JmsResource;
066: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.MailResource;
067: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.PersistenceManagerFactoryResource;
068: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
069: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBean;
070: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBeanDataNode;
071: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBean;
072: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBeanDataNode;
073: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBean;
074: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBeanDataNode;
075: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBean;
076: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBeanDataNode;
077: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBean;
078: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBeanDataNode;
079: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
080:
081: import org.openide.filesystems.FileAttributeEvent;
082: import org.openide.filesystems.FileChangeListener;
083: import org.openide.filesystems.FileEvent;
084: import org.openide.filesystems.FileObject;
085: import org.openide.filesystems.FileRenameEvent;
086: import org.openide.util.WeakListeners;
087:
088: /** Represents a SunResource object in the Repository.
089: *
090: * @author nityad
091: */
092: public class SunResourceDataObject extends XMLDataObject implements
093: FileChangeListener { // extends MultiDataObject{
094: private static String JDBC_CP = "jdbc-connection-pool"; //NOI18N
095: private static String JDBC_DS = "jdbc-resource"; //NOI18N
096: private static String PMF = "persistence-manager-factory-resource"; //NOI18N
097: private static String MAIL = "mail-resource"; //NOI18N
098: private static String JMS = "jms-resource"; //NOI18N
099:
100: private ValidateXMLCookie validateCookie = null;
101: private CheckXMLCookie checkCookie = null;
102:
103: ConnPoolBean cpBean = null;
104: DataSourceBean dsBean = null;
105: PersistenceManagerBean pmfBean = null;
106: JavaMailSessionBean mailBean = null;
107: JMSBean jmsBean = null;
108:
109: String resType;
110:
111: public SunResourceDataObject(FileObject pf,
112: SunResourceDataLoader loader)
113: throws DataObjectExistsException {
114: super (pf, loader);
115: pf.addFileChangeListener((FileChangeListener) WeakListeners
116: .create(FileChangeListener.class, this , pf));
117:
118: resType = getResource(pf);
119: }
120:
121: public <T extends Node.Cookie> T getCookie(Class<T> c) {
122: Node.Cookie retValue = null;
123: if (ValidateXMLCookie.class.isAssignableFrom(c)) {
124: if (validateCookie == null) {
125: InputSource in = DataObjectAdapters.inputSource(this );
126: validateCookie = new ValidateXMLSupport(in);
127: }
128: return (T) validateCookie;
129: } else if (CheckXMLCookie.class.isAssignableFrom(c)) {
130: if (checkCookie == null) {
131: InputSource in = DataObjectAdapters.inputSource(this );
132: checkCookie = new CheckXMLSupport(in);
133: }
134: return (T) checkCookie;
135: }
136:
137: if (retValue == null) {
138: retValue = super .getCookie(c);
139: }
140: return (T) retValue;
141: }
142:
143: public HelpCtx getHelpCtx() {
144: return null; // HelpCtx.DEFAULT_HELP;
145: // If you add context help, change to:
146: // return new HelpCtx(SunResourceDataObject.class);
147: }
148:
149: protected Node createNodeDelegate() {
150: if (resType != null) {
151: if (this .resType.equals(this .JDBC_CP)) {
152: Node node = new ConnPoolBeanDataNode(this , getPool());
153: return node;
154: }
155: if (this .resType.equals(this .JDBC_DS)) {
156: Node node = new DataSourceBeanDataNode(this ,
157: getDataSource());
158: return node;
159: }
160: if (this .resType.equals(this .PMF)) {
161: Node node = new PersistenceManagerBeanDataNode(this ,
162: getPersistenceManager());
163: return node;
164: }
165: if (this .resType.equals(this .MAIL)) {
166: Node node = new JavaMailSessionBeanDataNode(this ,
167: getMailSession());
168: return node;
169: }
170: if (this .resType.equals(this .JMS)) {
171: Node node = new JMSBeanDataNode(this , getJMS());
172: return node;
173: } else {
174: String mess = NbBundle.getMessage(
175: SunResourceDataObject.class,
176: "Info_notSunResource"); //NOI18N
177: ErrorManager.getDefault().log(
178: ErrorManager.INFORMATIONAL, mess);
179: return new SunResourceDataNode(this );
180: }
181: } else {
182: return new SunResourceDataNode(this );
183: }
184: }
185:
186: private String getResource(FileObject primaryFile) {
187: String type = null;
188: try {
189: if ((!primaryFile.isFolder()) && primaryFile.isValid()) {
190: ResourceUtils.migrateResources(primaryFile.getParent());
191:
192: InputStream in = primaryFile.getInputStream();
193: Resources resources = DDProvider.getDefault()
194: .getResourcesGraph(in);
195:
196: // identify JDBC Connection Pool xml
197: JdbcConnectionPool[] pools = resources
198: .getJdbcConnectionPool();
199: if (pools.length != 0) {
200: ConnPoolBean currCPBean = ConnPoolBean
201: .createBean(pools[0]);
202: type = this .JDBC_CP;
203: setPool(currCPBean);
204: return type;
205: }
206:
207: // identify JDBC Resources xml
208: JdbcResource[] dataSources = resources
209: .getJdbcResource();
210: if (dataSources.length != 0) {
211: DataSourceBean currDSBean = DataSourceBean
212: .createBean(dataSources[0]);
213: type = this .JDBC_DS;
214: setDataSource(currDSBean);
215: return type;
216: }
217:
218: // import Persistence Manager Factory Resources
219: PersistenceManagerFactoryResource[] pmfResources = resources
220: .getPersistenceManagerFactoryResource();
221: if (pmfResources.length != 0) {
222: PersistenceManagerBean currPMFBean = PersistenceManagerBean
223: .createBean(pmfResources[0]);
224: type = this .PMF;
225: setPersistenceManager(currPMFBean);
226: return type;
227: }
228:
229: // import Mail Resources
230: MailResource[] mailResources = resources
231: .getMailResource();
232: if (mailResources.length != 0) {
233: JavaMailSessionBean currMailBean = JavaMailSessionBean
234: .createBean(mailResources[0]);
235: type = this .MAIL;
236: setMailSession(currMailBean);
237: return type;
238: }
239:
240: // import JMS Resources and convert to Admin Object
241: JmsResource[] jmsResources = resources.getJmsResource();
242: if (jmsResources.length != 0) {
243: JMSBean jmsBean = JMSBean
244: .createBean(jmsResources[0]);
245: type = this .JMS;
246: setJMS(jmsBean);
247: return type;
248: }
249:
250: // import Admin Object Resources
251: AdminObjectResource[] aoResources = resources
252: .getAdminObjectResource();
253: if (aoResources.length != 0) {
254: JMSBean jmsBean = JMSBean
255: .createBean(aoResources[0]);
256: type = this .JMS;
257: setJMS(jmsBean);
258: return type;
259: }
260:
261: ConnectorResource[] connResources = resources
262: .getConnectorResource();
263: ConnectorConnectionPool[] connPoolResources = resources
264: .getConnectorConnectionPool();
265: if (connResources.length != 0
266: && connPoolResources.length != 0) {
267: JMSBean jmsBean = JMSBean.createBean(resources);
268: type = this .JMS;
269: setJMS(jmsBean);
270: return type;
271: }
272:
273: return type;
274: } else
275: return type;
276: } catch (NullPointerException npe) {
277: ErrorManager.getDefault().notify(
278: ErrorManager.INFORMATIONAL, npe);
279: return type;
280: } catch (Exception ex) {
281: //ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getLocalizedMessage());
282: return type;
283: }
284:
285: }
286:
287: private void setPool(ConnPoolBean in_cpBean) {
288: this .cpBean = in_cpBean;
289: }
290:
291: private ConnPoolBean getPool() {
292: return this .cpBean;
293: }
294:
295: private void setDataSource(DataSourceBean in_dsBean) {
296: this .dsBean = in_dsBean;
297: }
298:
299: private DataSourceBean getDataSource() {
300: return this .dsBean;
301: }
302:
303: private void setPersistenceManager(PersistenceManagerBean in_pmfBean) {
304: this .pmfBean = in_pmfBean;
305: }
306:
307: private PersistenceManagerBean getPersistenceManager() {
308: return this .pmfBean;
309: }
310:
311: private void setMailSession(JavaMailSessionBean in_mailBean) {
312: this .mailBean = in_mailBean;
313: }
314:
315: private JavaMailSessionBean getMailSession() {
316: return this .mailBean;
317: }
318:
319: private void setJMS(JMSBean in_jmsBean) {
320: this .jmsBean = in_jmsBean;
321: }
322:
323: private JMSBean getJMS() {
324: return this .jmsBean;
325: }
326:
327: public void fileAttributeChanged(FileAttributeEvent fe) {
328: updateDataObject();
329: }
330:
331: public void fileChanged(FileEvent fe) {
332: updateDataObject();
333: }
334:
335: public void fileDataCreated(FileEvent fe) {
336: updateDataObject();
337: }
338:
339: public void fileDeleted(FileEvent fe) {
340: updateDataObject();
341: }
342:
343: public void fileFolderCreated(FileEvent fe) {
344: updateDataObject();
345: }
346:
347: public void fileRenamed(FileRenameEvent fe) {
348: updateDataObject();
349: }
350:
351: private void updateDataObject() {
352: resType = getResource(this .getPrimaryFile());
353: }
354:
355: public String getResourceType() {
356: return resType;
357: }
358:
359: }
|