KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > WebServicePersistenceManager


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 /**
21  * WebServicePersistenceManager.java
22  * @author Winston Prakash
23  */

24
25 package org.netbeans.modules.websvc.registry;
26
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.BufferedOutputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import java.beans.XMLDecoder JavaDoc;
36 import java.beans.XMLEncoder JavaDoc;
37 import java.beans.ExceptionListener JavaDoc;
38
39 import org.openide.ErrorManager;
40
41 import org.netbeans.modules.websvc.registry.model.WebServiceData;
42 import org.netbeans.modules.websvc.registry.model.WebServiceDataPersistenceDelegate;
43 import org.netbeans.modules.websvc.registry.model.WebServiceGroup;
44 import org.netbeans.modules.websvc.registry.model.WebServiceListModel;
45
46 public class WebServicePersistenceManager implements ExceptionListener JavaDoc, org.netbeans.modules.websvc.registry.netbeans.PersistenceManagerInterface {
47
48     private static final String JavaDoc SAXParserFactory_PROP = "javax.xml.parsers.SAXParserFactory"; // NOI18N
49
private static final String JavaDoc SAX_PARSER = "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"; // NOI18N
50

51         private File JavaDoc websvcDir = new File JavaDoc(System.getProperty("netbeans.user"), "websvc"); // NOI18N
52
private File JavaDoc websvcRefFile = new File JavaDoc(websvcDir, "websvc_ref.xml"); // NOI18N
53

54     public WebServicePersistenceManager() {
55     }
56     
57     public void load(ClassLoader JavaDoc cl) {
58             //Thread.dumpStack();
59
// System.out.println("WebServicePersistenceManager load called");
60

61              WebServiceListModel wsListModel = WebServiceListModel.getInstance();
62         if(websvcRefFile.exists()) {
63                         String JavaDoc originalParserFactory = System.getProperty(SAXParserFactory_PROP);
64             ClassLoader JavaDoc origClassLoader = null;
65             XMLDecoder JavaDoc decoder = null;
66
67             try {
68                 System.getProperties().put(SAXParserFactory_PROP, SAX_PARSER);
69
70                 origClassLoader = Thread.currentThread().getContextClassLoader();
71                 Thread.currentThread().setContextClassLoader(cl);
72                 
73                 decoder = new XMLDecoder JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(websvcRefFile)));
74                                 Object JavaDoc o = decoder.readObject();
75                                 int wsDataNums = 0;
76                                 
77                                 if (o instanceof Integer JavaDoc) {
78                                     wsDataNums = ((Integer JavaDoc)o).intValue();
79                                 } else {
80                                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while loading in WS registry: " + o);
81                                     return;
82                                 }
83
84                 for(int i = 0; i< wsDataNums; i++) {
85                     try {
86                         WebServiceData wsData = (WebServiceData) decoder.readObject();
87                         wsListModel.addWebService(wsData);
88                     } catch(Exception JavaDoc exc) {
89                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc);
90                     }
91                 }
92
93                                 o = decoder.readObject();
94                                 int wsGroups = 0;
95                                 if (o instanceof Integer JavaDoc) {
96                                     wsGroups = ((Integer JavaDoc)o).intValue();
97                                 } else {
98                                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while loading in WS registry: " + o);
99                                     return;
100                                 }
101                 
102                                 for(int i = 0; i< wsGroups; i++) {
103                     try {
104                         WebServiceGroup group = (WebServiceGroup) decoder.readObject();
105                         wsListModel.addWebServiceGroup(group);
106                     } catch(Exception JavaDoc ex) {
107                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
108                     }
109                 }
110             } catch(Throwable JavaDoc thrown) {
111                 ErrorManager.getDefault().notify(ErrorManager.ERROR, thrown);
112             } finally {
113                 // Restore the SAXParserFactor property that was changed, restore
114
// this threads context classloader and close the decoder stream
115
// if it was opened.
116
System.getProperties().put(SAXParserFactory_PROP,originalParserFactory);
117                             
118                 if(origClassLoader != null) {
119                     Thread.currentThread().setContextClassLoader(origClassLoader);
120                 }
121
122                 if(decoder != null) {
123                     decoder.close();
124                 }
125             }
126         }
127     }
128
129     public void save(ClassLoader JavaDoc cl) {
130         //System.out.println("WebServicePersistenceManager save called");
131
//System.out.println("No webservices " + WSListModel.getInstance().getWSList().size());
132
WebServiceListModel wsListModel = WebServiceListModel.getInstance();
133         ClassLoader JavaDoc origClassLoader = null;
134         XMLEncoder JavaDoc encoder = null;
135         
136         try {
137             if(!websvcDir.exists()) {
138                 websvcDir.mkdirs();
139             }
140
141             if(websvcRefFile.exists()) {
142                 websvcRefFile.delete();
143             }
144
145             encoder = new XMLEncoder JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(websvcRefFile)));
146             encoder.setExceptionListener(this);
147             WebServiceDataPersistenceDelegate wsDelegate = new WebServiceDataPersistenceDelegate();
148             encoder.setPersistenceDelegate(Class.forName("javax.xml.namespace.QName", false, cl),
149                 new WebServiceDataPersistenceDelegate()); // NOI18N
150
encoder.setPersistenceDelegate(Class.forName("com.sun.xml.rpc.wsdl.document.soap.SOAPStyle", false, cl),
151                 new WebServiceDataPersistenceDelegate()); // NOI18N
152
encoder.setPersistenceDelegate(Class.forName("com.sun.xml.rpc.wsdl.document.soap.SOAPUse", false, cl),
153                 new WebServiceDataPersistenceDelegate()); // NOI18N
154
Set JavaDoc wsDataSet = wsListModel.getWebServiceSet();
155             encoder.writeObject(new Integer JavaDoc(wsDataSet.size()));
156             Iterator JavaDoc iter = wsDataSet.iterator();
157             
158             origClassLoader = Thread.currentThread().getContextClassLoader();
159             Thread.currentThread().setContextClassLoader(cl);
160             
161             while(iter.hasNext()) {
162                 WebServiceData wsData = (WebServiceData) iter.next();
163                 encoder.writeObject(wsData);
164             }
165             
166             Set JavaDoc wsGroupSet = wsListModel.getWebServiceGroupSet();
167             encoder.writeObject(new Integer JavaDoc(wsGroupSet.size()));
168             iter = wsGroupSet.iterator();
169
170             while(iter.hasNext()) {
171                 WebServiceGroup group = (WebServiceGroup) iter.next();
172                 encoder.writeObject(group);
173             }
174         } catch(Throwable JavaDoc thrown) {
175             ErrorManager.getDefault().notify(ErrorManager.ERROR, thrown);
176         } finally {
177             // Restore this threads context classloader and close the encoder
178
// stream if it was opened.
179
if(origClassLoader != null) {
180                 Thread.currentThread().setContextClassLoader(origClassLoader);
181             }
182             
183             if(encoder != null) {
184                 encoder.close();//was encoder
185
encoder.flush();
186             }
187         }
188     }
189
190     public void exceptionThrown(Exception JavaDoc e) {
191         e.printStackTrace();
192     }
193 }
194
Popular Tags