1 19 20 24 25 package org.netbeans.modules.websvc.registry; 26 27 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.util.Iterator ; 33 import java.util.Set ; 34 35 import java.beans.XMLDecoder ; 36 import java.beans.XMLEncoder ; 37 import java.beans.ExceptionListener ; 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 , org.netbeans.modules.websvc.registry.netbeans.PersistenceManagerInterface { 47 48 private static final String SAXParserFactory_PROP = "javax.xml.parsers.SAXParserFactory"; private static final String SAX_PARSER = "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"; 51 private File websvcDir = new File (System.getProperty("netbeans.user"), "websvc"); private File websvcRefFile = new File (websvcDir, "websvc_ref.xml"); 54 public WebServicePersistenceManager() { 55 } 56 57 public void load(ClassLoader cl) { 58 61 WebServiceListModel wsListModel = WebServiceListModel.getInstance(); 62 if(websvcRefFile.exists()) { 63 String originalParserFactory = System.getProperty(SAXParserFactory_PROP); 64 ClassLoader origClassLoader = null; 65 XMLDecoder 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 (new BufferedInputStream (new FileInputStream (websvcRefFile))); 74 Object o = decoder.readObject(); 75 int wsDataNums = 0; 76 77 if (o instanceof Integer ) { 78 wsDataNums = ((Integer )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 exc) { 89 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc); 90 } 91 } 92 93 o = decoder.readObject(); 94 int wsGroups = 0; 95 if (o instanceof Integer ) { 96 wsGroups = ((Integer )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 ex) { 107 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 108 } 109 } 110 } catch(Throwable thrown) { 111 ErrorManager.getDefault().notify(ErrorManager.ERROR, thrown); 112 } finally { 113 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 cl) { 130 WebServiceListModel wsListModel = WebServiceListModel.getInstance(); 133 ClassLoader origClassLoader = null; 134 XMLEncoder 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 (new BufferedOutputStream (new FileOutputStream (websvcRefFile))); 146 encoder.setExceptionListener(this); 147 WebServiceDataPersistenceDelegate wsDelegate = new WebServiceDataPersistenceDelegate(); 148 encoder.setPersistenceDelegate(Class.forName("javax.xml.namespace.QName", false, cl), 149 new WebServiceDataPersistenceDelegate()); encoder.setPersistenceDelegate(Class.forName("com.sun.xml.rpc.wsdl.document.soap.SOAPStyle", false, cl), 151 new WebServiceDataPersistenceDelegate()); encoder.setPersistenceDelegate(Class.forName("com.sun.xml.rpc.wsdl.document.soap.SOAPUse", false, cl), 153 new WebServiceDataPersistenceDelegate()); Set wsDataSet = wsListModel.getWebServiceSet(); 155 encoder.writeObject(new Integer (wsDataSet.size())); 156 Iterator 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 wsGroupSet = wsListModel.getWebServiceGroupSet(); 167 encoder.writeObject(new Integer (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 thrown) { 175 ErrorManager.getDefault().notify(ErrorManager.ERROR, thrown); 176 } finally { 177 if(origClassLoader != null) { 180 Thread.currentThread().setContextClassLoader(origClassLoader); 181 } 182 183 if(encoder != null) { 184 encoder.close(); encoder.flush(); 186 } 187 } 188 } 189 190 public void exceptionThrown(Exception e) { 191 e.printStackTrace(); 192 } 193 } 194 | Popular Tags |