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:
042: /**
043: * WebServicePersistenceManager.java
044: * @author Winston Prakash
045: */package org.netbeans.modules.websvc.registry;
046:
047: import java.io.BufferedInputStream;
048: import java.io.BufferedOutputStream;
049: import java.io.File;
050: import java.io.FileInputStream;
051: import java.io.FileOutputStream;
052: import java.util.Iterator;
053: import java.util.Set;
054:
055: import java.beans.XMLDecoder;
056: import java.beans.XMLEncoder;
057: import java.beans.ExceptionListener;
058:
059: import org.openide.ErrorManager;
060:
061: import org.netbeans.modules.websvc.registry.model.WebServiceData;
062: import org.netbeans.modules.websvc.registry.model.WebServiceDataPersistenceDelegate;
063: import org.netbeans.modules.websvc.registry.model.WebServiceGroup;
064: import org.netbeans.modules.websvc.registry.model.WebServiceListModel;
065:
066: public class WebServicePersistenceManager
067: implements
068: ExceptionListener,
069: org.netbeans.modules.websvc.registry.netbeans.PersistenceManagerInterface {
070:
071: private static final String SAXParserFactory_PROP = "javax.xml.parsers.SAXParserFactory"; // NOI18N
072: private static final String SAX_PARSER = "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"; // NOI18N
073:
074: private File websvcDir = new File(System
075: .getProperty("netbeans.user"), "websvc"); // NOI18N
076: private File websvcRefFile = new File(websvcDir, "websvc_ref.xml"); // NOI18N
077:
078: public WebServicePersistenceManager() {
079: }
080:
081: public void load(ClassLoader cl) {
082: //Thread.dumpStack();
083: // System.out.println("WebServicePersistenceManager load called");
084:
085: WebServiceListModel wsListModel = WebServiceListModel
086: .getInstance();
087: if (websvcRefFile.exists()) {
088: String originalParserFactory = System
089: .getProperty(SAXParserFactory_PROP);
090: ClassLoader origClassLoader = null;
091: XMLDecoder decoder = null;
092:
093: try {
094: System.getProperties().put(SAXParserFactory_PROP,
095: SAX_PARSER);
096:
097: origClassLoader = Thread.currentThread()
098: .getContextClassLoader();
099: Thread.currentThread().setContextClassLoader(cl);
100:
101: decoder = new XMLDecoder(new BufferedInputStream(
102: new FileInputStream(websvcRefFile)));
103: Object o = decoder.readObject();
104: int wsDataNums = 0;
105:
106: if (o instanceof Integer) {
107: wsDataNums = ((Integer) o).intValue();
108: } else {
109: ErrorManager.getDefault().log(
110: ErrorManager.INFORMATIONAL,
111: "Error while loading in WS registry: " + o);
112: return;
113: }
114:
115: for (int i = 0; i < wsDataNums; i++) {
116: try {
117: WebServiceData wsData = (WebServiceData) decoder
118: .readObject();
119: wsListModel.addWebService(wsData);
120: } catch (Exception exc) {
121: ErrorManager.getDefault().notify(
122: ErrorManager.EXCEPTION, exc);
123: }
124: }
125:
126: o = decoder.readObject();
127: int wsGroups = 0;
128: if (o instanceof Integer) {
129: wsGroups = ((Integer) o).intValue();
130: } else {
131: ErrorManager.getDefault().log(
132: ErrorManager.INFORMATIONAL,
133: "Error while loading in WS registry: " + o);
134: return;
135: }
136:
137: for (int i = 0; i < wsGroups; i++) {
138: try {
139: WebServiceGroup group = (WebServiceGroup) decoder
140: .readObject();
141: wsListModel.addWebServiceGroup(group);
142: } catch (Exception ex) {
143: ErrorManager.getDefault().notify(
144: ErrorManager.EXCEPTION, ex);
145: }
146: }
147: } catch (Throwable thrown) {
148: ErrorManager.getDefault().notify(ErrorManager.ERROR,
149: thrown);
150: } finally {
151: // Restore the SAXParserFactor property that was changed, restore
152: // this threads context classloader and close the decoder stream
153: // if it was opened.
154: System.getProperties().put(SAXParserFactory_PROP,
155: originalParserFactory);
156:
157: if (origClassLoader != null) {
158: Thread.currentThread().setContextClassLoader(
159: origClassLoader);
160: }
161:
162: if (decoder != null) {
163: decoder.close();
164: }
165: }
166: }
167: }
168:
169: public void save(ClassLoader cl) {
170: //System.out.println("WebServicePersistenceManager save called");
171: //System.out.println("No webservices " + WSListModel.getInstance().getWSList().size());
172: WebServiceListModel wsListModel = WebServiceListModel
173: .getInstance();
174: ClassLoader origClassLoader = null;
175: XMLEncoder encoder = null;
176:
177: try {
178: if (!websvcDir.exists()) {
179: websvcDir.mkdirs();
180: }
181:
182: if (websvcRefFile.exists()) {
183: websvcRefFile.delete();
184: }
185:
186: encoder = new XMLEncoder(new BufferedOutputStream(
187: new FileOutputStream(websvcRefFile)));
188: encoder.setExceptionListener(this );
189: WebServiceDataPersistenceDelegate wsDelegate = new WebServiceDataPersistenceDelegate();
190: encoder.setPersistenceDelegate(Class.forName(
191: "javax.xml.namespace.QName", false, cl),
192: new WebServiceDataPersistenceDelegate()); // NOI18N
193: encoder
194: .setPersistenceDelegate(
195: Class
196: .forName(
197: "com.sun.xml.rpc.wsdl.document.soap.SOAPStyle",
198: false, cl),
199: new WebServiceDataPersistenceDelegate()); // NOI18N
200: encoder
201: .setPersistenceDelegate(
202: Class
203: .forName(
204: "com.sun.xml.rpc.wsdl.document.soap.SOAPUse",
205: false, cl),
206: new WebServiceDataPersistenceDelegate()); // NOI18N
207: Set wsDataSet = wsListModel.getWebServiceSet();
208: encoder.writeObject(new Integer(wsDataSet.size()));
209: Iterator iter = wsDataSet.iterator();
210:
211: origClassLoader = Thread.currentThread()
212: .getContextClassLoader();
213: Thread.currentThread().setContextClassLoader(cl);
214:
215: while (iter.hasNext()) {
216: WebServiceData wsData = (WebServiceData) iter.next();
217: encoder.writeObject(wsData);
218: }
219:
220: Set wsGroupSet = wsListModel.getWebServiceGroupSet();
221: encoder.writeObject(new Integer(wsGroupSet.size()));
222: iter = wsGroupSet.iterator();
223:
224: while (iter.hasNext()) {
225: WebServiceGroup group = (WebServiceGroup) iter.next();
226: encoder.writeObject(group);
227: }
228: } catch (Throwable thrown) {
229: ErrorManager.getDefault()
230: .notify(ErrorManager.ERROR, thrown);
231: } finally {
232: // Restore this threads context classloader and close the encoder
233: // stream if it was opened.
234: if (origClassLoader != null) {
235: Thread.currentThread().setContextClassLoader(
236: origClassLoader);
237: }
238:
239: if (encoder != null) {
240: encoder.close();//was encoder
241: encoder.flush();
242: }
243: }
244: }
245:
246: public void exceptionThrown(Exception e) {
247: e.printStackTrace();
248: }
249: }
|