1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.library; 21 22 import java.io.IOException ; 23 import java.io.OutputStreamWriter ; 24 import java.io.PrintWriter ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Comparator ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import org.netbeans.api.project.libraries.Library; 32 import org.netbeans.api.project.libraries.LibraryManager; 33 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 34 import org.netbeans.modules.j2ee.persistence.provider.Provider; 35 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 36 import org.netbeans.spi.project.libraries.LibraryImplementation; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.URLMapper; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.Repository; 44 import org.openide.xml.XMLUtil; 45 46 50 public class PersistenceLibrarySupport { 51 52 public static final String VOLUME_TYPE_CLASSPATH = "classpath"; public static final String VOLUME_TYPE_SRC = "src"; public static final String VOLUME_TYPE_JAVADOC = "javadoc"; public static final String LIBRARY_TYPE = "j2se"; public static final String [] VOLUME_TYPES = new String [] { 57 VOLUME_TYPE_CLASSPATH, 58 VOLUME_TYPE_SRC, 59 VOLUME_TYPE_JAVADOC, 60 }; 61 62 private static final String LIBRARIES_REPOSITORY = "org-netbeans-api-project-libraries/Libraries"; private static int MAX_DEPTH = 3; 64 65 private FileObject storage = null; 66 private static PersistenceLibrarySupport instance; 67 68 private PersistenceLibrarySupport() { 69 } 70 71 public static PersistenceLibrarySupport getDefault() { 72 if (instance == null) { 73 instance = new PersistenceLibrarySupport(); 74 } 75 return instance; 76 } 77 78 public void addLibrary(LibraryImplementation library) { 79 this.initStorage(); 80 assert this.storage != null : "Storage is not initialized"; 81 try { 82 writeLibrary(this.storage,library); 83 } catch (IOException ex) { 84 ErrorManager.getDefault().notify(ex); 85 } 86 } 87 88 private static final FileObject createStorage() { 89 FileSystem storageFS = Repository.getDefault().getDefaultFileSystem(); 90 try { 91 return FileUtil.createFolder(storageFS.getRoot(), LIBRARIES_REPOSITORY); 92 } catch (IOException e) { 93 return null; 94 } 95 } 96 97 private synchronized void initStorage() { 98 if (this.storage == null) { 99 this.storage = createStorage(); 100 if (storage == null) { 101 return; 102 } 103 } 104 } 105 106 private void writeLibrary(final FileObject storage, final LibraryImplementation library) throws IOException { 107 storage.getFileSystem().runAtomicAction( 108 new FileSystem.AtomicAction() { 109 public void run() throws IOException { 110 FileObject fo = storage.createData(library.getName(),"xml"); writeLibraryDefinition(fo, library); 112 } 113 } 114 ); 115 } 116 117 private static void writeLibraryDefinition(final FileObject definitionFile, final LibraryImplementation library) throws IOException { 118 FileLock lock = null; 119 PrintWriter out = null; 120 try { 121 lock = definitionFile.lock(); 122 out = new PrintWriter (new OutputStreamWriter (definitionFile.getOutputStream(lock),"UTF-8")); 123 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.println("<!DOCTYPE library PUBLIC \"-//NetBeans//DTD Library Declaration 1.0//EN\" \"http://www.netbeans.org/dtds/library-declaration-1_0.dtd\">"); out.println("<library version=\"1.0\">"); out.println("\t<name>"+library.getName()+"</name>"); out.println("\t<type>"+library.getType()+"</type>"); 128 String description = library.getDescription(); 129 if (description != null && description.length() > 0) { 130 out.println("\t<description>"+description+"</description>"); } 132 String localizingBundle = library.getLocalizingBundle(); 133 if (localizingBundle != null && localizingBundle.length() > 0) { 134 out.println("\t<localizing-bundle>"+XMLUtil.toElementContent(localizingBundle)+"</localizing-bundle>"); } 136 String [] volumeTypes = VOLUME_TYPES; 137 for (int i = 0; i < volumeTypes.length; i++) { 138 out.println("\t<volume>"); out.println("\t\t<type>"+volumeTypes[i]+"</type>"); List volume = library.getContent(volumeTypes[i]); 141 if (volume != null) { 142 for (Iterator eit = volume.iterator(); eit.hasNext();) { 144 URL url = (URL ) eit.next(); 145 out.println("\t\t<resource>"+XMLUtil.toElementContent(url.toExternalForm())+"</resource>"); } 147 } 148 out.println("\t</volume>"); } 150 out.println("</library>"); } finally { 152 if (out != null) 153 out.close(); 154 if (lock != null) 155 lock.releaseLock(); 156 } 157 } 158 159 161 168 public static boolean isValidLibraryJavadocRoot(final URL rootURL) { 169 assert rootURL != null && rootURL.toExternalForm().endsWith("/"); 170 final FileObject root = URLMapper.findFileObject(rootURL); 171 if (root == null) { 172 return false; 173 } 174 return findIndexFolder(root,1) != null; 175 } 176 177 private static FileObject findIndexFolder(FileObject fo, int depth) { 178 if (depth > MAX_DEPTH) { 179 return null; 180 } 181 if (fo.getFileObject("index-files",null)!=null || fo.getFileObject("index-all.html",null)!=null) { return fo; 183 } 184 FileObject[] children = fo.getChildren(); 185 for (int i=0; i< children.length; i++) { 186 if (children[i].isFolder()) { 187 FileObject result = findIndexFolder(children[i], depth+1); 188 if (result != null) { 189 return result; 190 } 191 } 192 } 193 return null; 194 } 195 196 199 public static boolean containsClass(Library library, String className) { 200 String classRelativePath = className.replace('.', '/') + ".class"; return containsPath(library.getContent("classpath"), classRelativePath); } 203 204 207 public static boolean containsService(Library library, String serviceName) { 208 String serviceRelativePath = "META-INF/services/" + serviceName; return containsPath(library.getContent("classpath"), serviceRelativePath); } 211 212 215 public static boolean containsClass(LibraryImplementation library, String className) { 216 String classRelativePath = className.replace('.', '/') + ".class"; return containsPath(library.getContent("classpath"), classRelativePath); } 219 220 223 public static boolean containsService(LibraryImplementation library, String serviceName) { 224 String serviceRelativePath = "META-INF/services/" + serviceName; return containsPath(library.getContent("classpath"), serviceRelativePath); } 227 228 private static boolean containsPath(List <URL > roots, String relativePath) { 229 for (URL each :roots){ 230 FileObject root = URLMapper.findFileObject(each); 231 if (root != null && "jar".equals(each.getProtocol())) { FileObject archiveRoot = FileUtil.getArchiveRoot(FileUtil.getArchiveFile(root)); 233 if (archiveRoot.getFileObject(relativePath) != null) { 234 return true; 235 } 236 } 237 } 238 return false; 239 } 240 241 245 public static Library getLibrary(PersistenceUnit pu) { 246 return getLibrary(ProviderUtil.getProvider(pu)); 247 } 248 249 253 public static Library getLibrary(Provider provider){ 254 List <Library> libraries = createLibraries(); 255 for (Library each : libraries){ 256 if (provider.getProviderClass().equals(extractProvider(each))) { 257 return each; 258 } 259 } 260 return null; 261 } 262 263 private static List <Library> createLibraries() { 264 List <Library> providerLibs = new ArrayList <Library>(); 265 for (Library each : LibraryManager.getDefault().getLibraries()){ 266 if (PersistenceLibrarySupport.containsClass(each, "javax.persistence.EntityManager") && extractProvider(each) != null) { 267 providerLibs.add(each); 268 } 269 } 270 Collections.sort(providerLibs, new Comparator () { 271 public int compare(Object o1, Object o2) { 272 assert (o1 instanceof Library) && (o2 instanceof Library); 273 String name1 = ((Library)o1).getDisplayName(); 274 String name2 = ((Library)o2).getDisplayName(); 275 return name1.compareToIgnoreCase(name2); 276 } 277 }); 278 return providerLibs; 279 } 280 281 287 public static List <Provider> getProvidersFromLibraries() { 288 List <Provider> providerLibs = new ArrayList <Provider>(); 289 Library[] libs = LibraryManager.getDefault().getLibraries(); 290 for (Library each : libs){ 291 Provider provider = extractProvider(each); 292 if (PersistenceLibrarySupport.containsClass(each, "javax.persistence.EntityManager") && provider != null) { 293 providerLibs.add(provider); 294 } 295 } 296 Collections.sort(providerLibs, new Comparator () { 297 public int compare(Object o1, Object o2) { 298 String name1 = ((Provider)o1).getDisplayName(); 299 String name2 = ((Provider)o2).getDisplayName(); 300 return name1.compareToIgnoreCase(name2); 301 } 302 }); 303 304 return providerLibs; 305 } 306 307 311 public static Library getFirstProviderLibrary() { 312 List <Library> libraries = createLibraries(); 313 if (libraries.size() > 0) { 314 return libraries.iterator().next(); 315 } 316 return null; 317 } 318 319 320 private static Provider extractProvider(Library library) { 321 for (Provider each : ProviderUtil.getAllProviders()){ 322 if (PersistenceLibrarySupport.containsClass(library, each.getProviderClass())){ 323 return each; 324 } 325 } 326 return null; 327 } 328 329 } 330 | Popular Tags |