1 19 20 package org.netbeans.modules.websvc.api.jaxws.project; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.net.URI ; 31 import java.net.URL ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import org.netbeans.api.java.project.JavaProjectConstants; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.SourceGroup; 38 import org.netbeans.api.project.Sources; 39 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 40 import org.netbeans.modules.xml.retriever.RetrieveEntry; 41 import org.netbeans.modules.xml.retriever.Retriever; 42 import org.netbeans.modules.xml.retriever.RetrieverImpl; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.openide.DialogDisplayer; 45 import org.openide.ErrorManager; 46 import org.openide.NotifyDescriptor; 47 import org.openide.cookies.SaveCookie; 48 import org.openide.filesystems.FileLock; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileSystem; 51 import org.openide.filesystems.FileSystem.AtomicAction; 52 import org.openide.filesystems.FileUtil; 53 import org.openide.loaders.DataFolder; 54 import org.openide.loaders.DataObject; 55 import org.openide.util.NbBundle; 56 import org.openide.util.Utilities; 57 58 62 public class WSUtils { 63 64 private static final String ENDORSED_DIR_PROPERTY="jaxws.endorsed.dir"; 72 public static FileObject retrieveResource(FileObject targetFolder, URI source) 73 throws java.net.UnknownHostException , java.net.URISyntaxException , IOException { 74 try { 75 Retriever retriever = new RetrieverImpl(); 76 FileObject result = retriever.retrieveResource(targetFolder, source); 77 if (result==null) { 78 Map map = retriever.getRetrievedResourceExceptionMap(); 79 if (map!=null) { 80 Set keys = map.keySet(); 81 Iterator it = keys.iterator(); 82 while (it.hasNext()) { 83 RetrieveEntry key = (RetrieveEntry)it.next(); 84 Object exc = map.get(key); 85 if (exc instanceof IOException ) { 86 throw (IOException )exc; 87 } else if (exc instanceof java.net.URISyntaxException ) { 88 throw (java.net.URISyntaxException )exc; 89 } else if (exc instanceof Exception ) { 90 IOException ex = new IOException (NbBundle.getMessage(WSUtils.class,"ERR_retrieveResource",key.getCurrentAddress())); 91 ex.initCause((Exception )exc); 92 throw (IOException )(ex); 93 } 94 } 95 } 96 } 97 return result; 98 } catch (RuntimeException ex) { 99 throw (IOException )(new IOException (ex.getLocalizedMessage()).initCause(ex)); 100 } 101 } 102 103 public static String findProperServiceName(String name, JaxWsModel jaxWsModel) { 104 if (jaxWsModel.findServiceByName(name)==null) return name; 105 for (int i = 1;; i++) { 106 String destName = name + "_" + i; if (jaxWsModel.findServiceByName(destName)==null) 108 return destName; 109 } 110 } 111 112 public static void retrieveJaxWsFromResource(FileObject projectDir) throws IOException { 113 final String jaxWsContent = 114 readResource(WSUtils.class.getResourceAsStream("/org/netbeans/modules/websvc/jaxwsmodel/resources/jax-ws.xml")); final FileObject nbprojFo = projectDir.getFileObject("nbproject"); assert nbprojFo != null : "Cannot find nbproject directory"; FileSystem fs = nbprojFo.getFileSystem(); 118 fs.runAtomicAction(new FileSystem.AtomicAction() { 119 public void run() throws IOException { 120 FileObject jaxWsFo = FileUtil.createData(nbprojFo, "jax-ws.xml"); FileLock lock = jaxWsFo.lock(); 122 try { 123 BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (jaxWsFo.getOutputStream(lock))); 124 bw.write(jaxWsContent); 125 bw.close(); 126 } finally { 127 lock.releaseLock(); 128 } 129 } 130 }); 131 } 132 133 public static void retrieveHandlerConfigFromResource(final FileObject targetDir, final String handlerConfigName) throws IOException { 134 final String handlerContent = 135 readResource(WSUtils.class.getResourceAsStream("/org/netbeans/modules/websvc/jaxwsmodel/resources/handler.xml")); FileSystem fs = targetDir.getFileSystem(); 137 fs.runAtomicAction(new FileSystem.AtomicAction() { 138 public void run() throws IOException { 139 FileObject handlerFo = FileUtil.createData(targetDir, handlerConfigName); FileLock lock = handlerFo.lock(); 141 BufferedWriter bw = null; 142 OutputStream os = null; 143 try { 144 os = handlerFo.getOutputStream(lock); 145 bw = new BufferedWriter (new OutputStreamWriter (os)); 146 bw.write(handlerContent); 147 bw.close(); 148 } finally { 149 if(bw != null) 150 bw.close(); 151 if(os != null) 152 os.close(); 153 if(lock != null) 154 lock.releaseLock(); 155 } 156 } 157 }); 158 } 159 160 public static void generateSunJaxwsFile(final FileObject targetDir) throws IOException { 161 final String sunJaxwsContent = 162 readResource(WSUtils.class.getResourceAsStream("/org/netbeans/modules/websvc/jaxwsmodel/resources/sun-jaxws.xml")); FileSystem fs = targetDir.getFileSystem(); 164 fs.runAtomicAction(new FileSystem.AtomicAction() { 165 public void run() throws IOException { 166 FileObject sunJaxwsFo = FileUtil.createData(targetDir, "sun-jaxws.xml"); FileLock lock = sunJaxwsFo.lock(); 168 BufferedWriter bw = null; 169 OutputStream os = null; 170 OutputStreamWriter osw = null; 171 try { 172 os = sunJaxwsFo.getOutputStream(lock); 173 osw = new OutputStreamWriter (os); 174 bw = new BufferedWriter (osw); 175 bw.write(sunJaxwsContent); 176 } finally { 177 if(bw != null) 178 bw.close(); 179 if(os != null) 180 os.close(); 181 if(osw != null) 182 osw.close(); 183 if(lock != null) 184 lock.releaseLock(); 185 } 186 } 187 }); 188 } 189 190 private static String readResource(InputStream is) throws IOException { 191 StringBuffer sb = new StringBuffer (); 193 String lineSep = System.getProperty("line.separator"); BufferedReader br = new BufferedReader (new InputStreamReader (is)); 195 String line = br.readLine(); 196 while (line != null) { 197 sb.append(line); 198 sb.append(lineSep); 199 line = br.readLine(); 200 } 201 br.close(); 202 return sb.toString(); 203 } 204 205 public static void removeImplClass(Project project, String implClass) { 206 Sources sources = (Sources)project.getLookup().lookup(Sources.class); 207 String resource = implClass.replace('.','/')+".java"; if (sources!=null) { 209 SourceGroup[] srcGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 210 for (int i=0;i<srcGroup.length;i++) { 211 final FileObject srcRoot = srcGroup[i].getRootFolder(); 212 final FileObject implClassFo = srcRoot.getFileObject(resource); 213 if (implClassFo!=null) { 214 try { 215 FileSystem fs = implClassFo.getFileSystem(); 216 fs.runAtomicAction(new AtomicAction() { 217 public void run() { 218 FileObject parent = implClassFo.getParent(); 219 deleteFile(implClassFo); 220 while (parent!=srcRoot && parent.getChildren().length==0) { 221 FileObject fileToDelete=parent; 222 parent = parent.getParent(); 223 deleteFile(fileToDelete); 224 } 225 } 226 }); 227 } catch (IOException ex) { 228 ErrorManager.getDefault().notify(ex); 229 } 230 return; 231 } 232 } 233 } 234 } 235 236 private static void deleteFile(FileObject f) { 237 FileLock lock = null; 238 try { 239 lock = f.lock(); 240 if (f.isFolder()) { 241 DataFolder folder = DataFolder.findFolder(f); 242 if (folder!=null) { 244 DataObject[] children = folder.getChildren(); 245 for (int i=0;i<children.length;i++) { 246 SaveCookie save = (SaveCookie)children[i].getCookie(SaveCookie.class); 247 if (save!=null) save.save(); 248 } 249 } 250 } 251 f.delete(lock); 252 } catch(java.io.IOException e) { 253 NotifyDescriptor ndd = 254 new NotifyDescriptor.Message(NbBundle.getMessage(WSUtils.class, "MSG_Unable_Delete_File", f.getNameExt()), 255 NotifyDescriptor.ERROR_MESSAGE); 256 DialogDisplayer.getDefault().notify(ndd); 257 } finally { 258 if(lock != null) { 259 lock.releaseLock(); 260 } 261 } 262 } 263 264 265 public static void copyFiles(FileObject sourceFolder, FileObject targetFolder) throws IOException { 266 FileObject[] children = sourceFolder.getChildren(); 267 for (int i=0;i<children.length;i++) { 268 if (children[i].isFolder()) { 269 FileObject folder = targetFolder.createFolder(children[i].getName()); 270 copyFiles(children[i],folder); 271 } else { 272 children[i].copy(targetFolder, children[i].getName(), children[i].getExt()); 273 } 274 } 275 } 276 277 public static FileObject backupAndGenerateJaxWs(FileObject projectDir, FileObject oldJaxWs, RuntimeException reason) throws IOException { 278 DialogDisplayer.getDefault().notify( 279 new NotifyDescriptor.Message(NbBundle.getMessage(WSUtils.class,"ERR_corruptedJaxWs",oldJaxWs.getPath(),reason.getMessage()),NotifyDescriptor.ERROR_MESSAGE)); 280 FileObject parent = oldJaxWs.getParent(); 281 FileObject oldBackup = parent.getFileObject("jax-ws.xml.old"); FileLock lock = null; 283 if (oldBackup!=null) { 284 try { 286 lock = oldBackup.lock(); 287 oldBackup.delete(lock); 288 } finally { 289 if (lock!=null) lock.releaseLock(); 290 } 291 } 292 try { 294 lock = oldJaxWs.lock(); 295 oldJaxWs.rename(lock, "jax-ws.xml","old"); } finally { 297 if (lock!=null) lock.releaseLock(); 298 } 299 retrieveJaxWsFromResource(projectDir); 300 return projectDir.getFileObject(GeneratedFilesHelper.JAX_WS_XML_PATH); 301 } 302 303 306 public static void setJaxWsEndorsedDirProperty(EditableProperties ep) { 307 String oldJaxWsEndorsedDirs = ep.getProperty(ENDORSED_DIR_PROPERTY); 308 String javaVersion = System.getProperty("java.specification.version"); if ("1.6".equals(javaVersion)) { String jaxWsEndorsedDirs = getJaxWsApiDir(); 311 if (jaxWsEndorsedDirs!=null && !jaxWsEndorsedDirs.equals(oldJaxWsEndorsedDirs)) 312 ep.setProperty(ENDORSED_DIR_PROPERTY, jaxWsEndorsedDirs); 313 } else { 314 if (oldJaxWsEndorsedDirs!=null) { 315 ep.remove(ENDORSED_DIR_PROPERTY); 316 } 317 } 318 } 319 320 private static String getJaxWsApiDir() { 321 URL wsFeatureUrl = WSUtils.class.getClassLoader().getResource("javax/xml/ws/WebServiceFeature.class"); 322 if (wsFeatureUrl!=null) { 323 String prefix = "jar:file:"; String postfix = "/jaxws-api.jar!/javax/xml/ws/WebServiceFeature.class"; String jaxWsUrl = wsFeatureUrl.toExternalForm(); 326 if (jaxWsUrl.startsWith(prefix) && jaxWsUrl.endsWith(postfix)); 327 int startPosition = Utilities.isWindows() ? 10 : 9; 330 int endPosition = jaxWsUrl.indexOf(postfix); 331 return jaxWsUrl.substring(startPosition,endPosition); 332 } 333 return null; 334 } 335 } | Popular Tags |