1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.project.FileOwnerQuery; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 34 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 35 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 36 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider; 37 import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider; 38 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory; 39 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 40 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider; 41 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider; 42 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 43 import org.netbeans.modules.java.j2seproject.classpath.ClassPathProviderImpl; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.filesystems.URLMapper; 47 48 52 public class J2SEPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener { 53 54 private final J2SEProject project; 55 56 private final PersistenceScopeImplementation persistenceScopeImpl = new PersistenceScopeImpl(); 57 private final PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl); 58 59 private final PersistenceScopesHelper scopesHelper = new PersistenceScopesHelper(); 60 61 private ClassPath projectSourcesClassPath; 62 63 public J2SEPersistenceProvider(J2SEProject project) { 64 this.project = project; 65 project.getSourceRoots().addPropertyChangeListener(this); 66 sourcesChanged(); 67 } 68 69 public FileObject getLocation() { 70 FileObject root = getLocationSourceRoot(); 71 if (root == null) { 72 return null; 73 } 74 FileObject metaInf = root.getFileObject("META-INF"); if (metaInf == null || !metaInf.isFolder()) { 76 return null; 77 } 78 return metaInf; 79 } 80 81 public FileObject createLocation() throws IOException { 82 FileObject root = getLocationSourceRoot(); 83 if (root == null) { 84 throw new IOException ("There are no source roots in the project or the first source root does not exist."); } 86 FileObject metaInf = root.getFileObject("META-INF"); if (metaInf != null) { 88 if (!metaInf.isFolder()) { 89 throw new IOException ("The META-INF directory exists, but is not a folder."); } 91 } else { 92 metaInf = root.createFolder("META-INF"); } 94 return metaInf; 95 } 96 97 public PersistenceScope findPersistenceScope(FileObject fo) { 98 Project project = FileOwnerQuery.getOwner(fo); 99 if (project != null) { 100 J2SEPersistenceProvider provider = (J2SEPersistenceProvider)project.getLookup().lookup(J2SEPersistenceProvider.class); 101 return provider.getPersistenceScope(); 102 } 103 return null; 104 } 105 106 public PersistenceScopes getPersistenceScopes() { 107 return scopesHelper.getPersistenceScopes(); 108 } 109 110 public ClassPath getClassPath() { 111 return getProjectSourcesClassPath(); 112 } 113 114 private File getFirstSourceRoot() { 115 URL [] urls = project.getSourceRoots().getRootURLs(); 116 if (urls.length == 0) { 117 return null; 118 } 119 return new File (URI.create(urls[0].toExternalForm())); 120 } 121 122 private FileObject getLocationSourceRoot() { 123 File sourceRoot = getFirstSourceRoot(); 124 if (sourceRoot != null) { 125 return FileUtil.toFileObject(sourceRoot); 126 } 127 return null; 128 } 129 130 private PersistenceScope getPersistenceScope() { 131 FileObject persistenceXml = persistenceScope.getPersistenceXml(); 132 if (persistenceXml != null && persistenceXml.isValid()) { 133 return persistenceScope; 134 } 135 return null; 136 } 137 138 private ClassPath getProjectSourcesClassPath() { 139 synchronized (this) { 140 if (projectSourcesClassPath == null) { 141 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class); 142 projectSourcesClassPath = ClassPathSupport.createProxyClassPath(new ClassPath[] { 144 cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE), 146 cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE), 147 }); 148 } 149 return projectSourcesClassPath; 150 } 151 } 152 153 public void propertyChange(PropertyChangeEvent event) { 154 sourcesChanged(); 155 } 156 157 private void sourcesChanged() { 158 synchronized (this) { 159 File sourceRootFile = getFirstSourceRoot(); 160 if (sourceRootFile != null) { 161 File persistenceXmlFile = new File (sourceRootFile, "META-INF/persistence.xml"); scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile); 163 } else { 164 scopesHelper.changePersistenceScope(null, null); 165 } 166 } 167 } 168 169 172 private final class PersistenceScopeImpl implements PersistenceScopeImplementation { 173 174 public FileObject getPersistenceXml() { 175 FileObject location = getLocation(); 176 if (location == null) { 177 return null; 178 } 179 return location.getFileObject("persistence.xml"); } 181 182 public ClassPath getClassPath() { 183 return getProjectSourcesClassPath(); 184 } 185 } 186 } 187 | Popular Tags |