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-2007 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:
042: package org.netbeans.modules.compapp.javaee.sunresources.tool.archive;
043:
044: import java.io.File;
045: import java.io.FileOutputStream;
046: import java.io.PrintWriter;
047: import java.util.Enumeration;
048: import java.util.Iterator;
049: import java.util.Properties;
050:
051: import javax.xml.bind.JAXBContext;
052: import javax.xml.bind.Marshaller;
053: import javax.xml.bind.Unmarshaller;
054:
055: import org.netbeans.modules.compapp.javaee.sunresources.generated.sunresources13.*;
056:
057: /**
058: * @author echou
059: *
060: */
061: public class SunResourcesDDJaxbHandler {
062:
063: private File resourceFile;
064: private JAXBContext jc;
065: private Resources resources;
066: private Marshaller marshaller;
067:
068: public SunResourcesDDJaxbHandler(File file) throws Exception {
069: this .resourceFile = file;
070:
071: jc = JAXBContext.newInstance(
072: "com.sun.wasilla.jaxb.sunresources13", // NOI18N
073: this .getClass().getClassLoader());
074: boolean unmarshalSuccess = true;
075: // unmarshal it to memory
076: try {
077: Unmarshaller unmarshaller = jc.createUnmarshaller();
078: resources = (Resources) unmarshaller
079: .unmarshal(resourceFile);
080: } catch (Exception e) {
081: unmarshalSuccess = false;
082: }
083:
084: // if unable to marshal xml, then create a new empty one
085: if (!unmarshalSuccess) {
086: // construct JAXB tree
087: ObjectFactory factory = new ObjectFactory();
088: resources = factory.createResources();
089: }
090:
091: }
092:
093: public boolean containsResource(String jndiName) {
094: for (Iterator<Object> iter = resources
095: .getCustomResourceOrExternalJndiResourceOrJdbcResourceOrMailResourceOrPersistenceManagerFactoryResourceOrAdminObjectResourceOrConnectorResourceOrResourceAdapterConfigOrJdbcConnectionPoolOrConnectorConnectionPool()
096: .iterator(); iter.hasNext();) {
097: Object obj = iter.next();
098: if (obj instanceof CustomResource) {
099: CustomResource customResource = (CustomResource) obj;
100: if (customResource.getJndiName().equals(jndiName)) {
101: return true;
102: }
103: } else if (obj instanceof ExternalJndiResource) {
104: ExternalJndiResource externalJndiResource = (ExternalJndiResource) obj;
105: if (externalJndiResource.getJndiName().equals(jndiName)) {
106: return true;
107: }
108: } else if (obj instanceof JdbcResource) {
109: JdbcResource jdbcResource = (JdbcResource) obj;
110: if (jdbcResource.getJndiName().equals(jndiName)) {
111: return true;
112: }
113: } else if (obj instanceof MailResource) {
114: MailResource mailResource = (MailResource) obj;
115: if (mailResource.getJndiName().equals(jndiName)) {
116: return true;
117: }
118: } else if (obj instanceof PersistenceManagerFactoryResource) {
119: PersistenceManagerFactoryResource pmfResource = (PersistenceManagerFactoryResource) obj;
120: if (pmfResource.getJndiName().equals(jndiName)) {
121: return true;
122: }
123: } else if (obj instanceof AdminObjectResource) {
124: AdminObjectResource adminObjectResource = (AdminObjectResource) obj;
125: if (adminObjectResource.getJndiName().equals(jndiName)) {
126: return true;
127: }
128: } else if (obj instanceof ConnectorResource) {
129: ConnectorResource connectorResource = (ConnectorResource) obj;
130: if (connectorResource.getJndiName().equals(jndiName)) {
131: return true;
132: }
133: }
134: /*
135: else if (obj instanceof ResourceAdapterConfig) {
136:
137: } else if (obj instanceof JdbcConnectionPool) {
138:
139: } else if (obj instanceof ConnectorConnectionPool) {
140:
141: }
142: */
143:
144: }
145:
146: return false;
147: }
148:
149: public void saveXML() throws Exception {
150: if (marshaller == null) {
151: marshaller = jc.createMarshaller();
152: marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
153: Boolean.TRUE);
154: marshaller.setProperty("com.sun.xml.bind.xmlDeclaration",
155: Boolean.FALSE); // NOI18N
156: }
157: // write preamble
158: FileOutputStream fos = new FileOutputStream(resourceFile);
159: PrintWriter out = new PrintWriter(fos);
160: try {
161: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
162: out
163: .println("<!DOCTYPE resources PUBLIC \"-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN\" \"http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd\">"); // NOI18N
164: marshaller.marshal(resources, out);
165: } finally {
166: FileUtil.safeclose(out);
167: FileUtil.safeclose(fos);
168: }
169: }
170:
171: public void addAdminObjectResource(String jndiName, String resType,
172: String resAdapter, Properties adminObjProps) {
173: AdminObjectResource adminObjResource = new AdminObjectResource();
174: adminObjResource.setJndiName(jndiName);
175: adminObjResource.setResType(resType);
176: adminObjResource.setResAdapter(resAdapter);
177: for (Enumeration<?> e = adminObjProps.propertyNames(); e
178: .hasMoreElements();) {
179: String propName = (String) e.nextElement();
180: String propValue = adminObjProps.getProperty(propName);
181: Property p = new Property();
182: p.setName(propName);
183: p.setValue(propValue);
184: adminObjResource.getProperty().add(p);
185: }
186: resources
187: .getCustomResourceOrExternalJndiResourceOrJdbcResourceOrMailResourceOrPersistenceManagerFactoryResourceOrAdminObjectResourceOrConnectorResourceOrResourceAdapterConfigOrJdbcConnectionPoolOrConnectorConnectionPool()
188: .add(adminObjResource);
189: }
190:
191: public Resources getResources() {
192: return this.resources;
193: }
194: }
|