1 19 20 package org.netbeans.modules.web.project; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.api.project.FileOwnerQuery; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.modules.j2ee.metadata.ClassPathSupport; 30 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 31 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 32 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider; 33 import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider; 34 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory; 35 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 36 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider; 37 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider; 38 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 39 import org.netbeans.modules.web.project.classpath.ClassPathProviderImpl; 40 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 41 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 42 import org.openide.filesystems.FileObject; 43 44 49 public class WebPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener { 50 51 private final WebProject project; 52 private final PropertyEvaluator evaluator; 53 54 private final PersistenceScopeImplementation persistenceScopeImpl = new PersistenceScopeImpl(); 55 private final PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl); 56 57 private final PersistenceScopesHelper scopesHelper = new PersistenceScopesHelper(); 58 59 private ClassPath projectSourcesClassPath; 60 61 public WebPersistenceProvider(WebProject project, PropertyEvaluator evaluator) { 62 this.project = project; 63 this.evaluator = evaluator; 64 evaluator.addPropertyChangeListener(this); 65 locationChanged(); 66 } 67 68 public FileObject getLocation() { 69 return project.getWebModule().getConfDir(); 70 } 71 72 public FileObject createLocation() throws IOException { 73 return project.getWebModule().getConfDir(); 75 } 76 77 public PersistenceScope findPersistenceScope(FileObject fo) { 78 Project project = FileOwnerQuery.getOwner(fo); 79 if (project != null) { 80 WebPersistenceProvider provider = (WebPersistenceProvider)project.getLookup().lookup(WebPersistenceProvider.class); 81 return provider.getPersistenceScope(); 82 } 83 return null; 84 } 85 86 public PersistenceScopes getPersistenceScopes() { 87 return scopesHelper.getPersistenceScopes(); 88 } 89 90 public ClassPath getClassPath() { 91 return getProjectSourcesClassPath(); 92 } 93 94 private PersistenceScope getPersistenceScope() { 95 FileObject persistenceXml = persistenceScope.getPersistenceXml(); 96 if (persistenceXml != null && persistenceXml.isValid()) { 97 return persistenceScope; 98 } 99 return null; 100 } 101 102 private ClassPath getProjectSourcesClassPath() { 103 synchronized (this) { 104 if (projectSourcesClassPath == null) { 105 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class); 106 projectSourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] { 107 cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE), 108 cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE), 109 }); 110 } 111 return projectSourcesClassPath; 112 } 113 } 114 115 public void propertyChange(PropertyChangeEvent event) { 116 String propName = event.getPropertyName(); 117 if (propName == null || propName.equals(WebProjectProperties.CONF_DIR)) { 118 locationChanged(); 119 } 120 } 121 122 private void locationChanged() { 123 File confDirFile = project.getWebModule().getConfDirAsFile(); 124 if (confDirFile != null) { 125 File persistenceXmlFile = new File (confDirFile, "persistence.xml"); scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile); 127 } else { 128 scopesHelper.changePersistenceScope(null, null); 129 } 130 } 131 132 135 private final class PersistenceScopeImpl implements PersistenceScopeImplementation { 136 137 public FileObject getPersistenceXml() { 138 FileObject location = getLocation(); 139 if (location == null) { 140 return null; 141 } 142 return location.getFileObject("persistence.xml"); } 144 145 public ClassPath getClassPath() { 146 return getProjectSourcesClassPath(); 147 } 148 } 149 } 150 | Popular Tags |