1 19 20 package org.netbeans.modules.j2ee.clientproject; 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.clientproject.classpath.ClassPathProviderImpl; 30 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 31 import org.netbeans.modules.j2ee.metadata.ClassPathSupport; 32 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 33 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 34 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider; 35 import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider; 36 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory; 37 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 38 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider; 39 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider; 40 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 41 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 42 import org.openide.filesystems.FileObject; 43 44 49 public class AppClientPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener { 50 51 private final AppClientProject 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 AppClientPersistenceProvider(AppClientProject 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.getAPICar().getMetaInf(); 70 } 71 72 public FileObject createLocation() throws IOException { 73 return project.getAPICar().getMetaInf(); 75 } 76 77 78 public PersistenceScope findPersistenceScope(FileObject fo) { 79 Project project = FileOwnerQuery.getOwner(fo); 80 if (project != null) { 81 AppClientPersistenceProvider provider = (AppClientPersistenceProvider)project.getLookup().lookup(AppClientPersistenceProvider.class); 82 return provider.getPersistenceScope(); 83 } 84 return null; 85 } 86 87 public PersistenceScopes getPersistenceScopes() { 88 return scopesHelper.getPersistenceScopes(); 89 } 90 91 public ClassPath getClassPath() { 92 return getProjectSourcesClassPath(); 93 } 94 95 private PersistenceScope getPersistenceScope() { 96 FileObject persistenceXml = persistenceScope.getPersistenceXml(); 97 if (persistenceXml != null && persistenceXml.isValid()) { 98 return persistenceScope; 99 } 100 return null; 101 } 102 103 private ClassPath getProjectSourcesClassPath() { 104 synchronized (this) { 105 if (projectSourcesClassPath == null) { 106 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class); 107 projectSourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] { 108 cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE), 109 cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE), 110 }); 111 } 112 return projectSourcesClassPath; 113 } 114 } 115 116 public void propertyChange(PropertyChangeEvent event) { 117 String propName = event.getPropertyName(); 118 if (propName == null || propName.equals(AppClientProjectProperties.META_INF)) { 119 locationChanged(); 120 } 121 } 122 123 private void locationChanged() { 124 File metaInfFile = project.getCarModule().getMetaInfAsFile(); 125 if (metaInfFile != null) { 126 File persistenceXmlFile = new File (metaInfFile, "persistence.xml"); scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile); 128 } else { 129 scopesHelper.changePersistenceScope(null, null); 130 } 131 } 132 133 136 private final class PersistenceScopeImpl implements PersistenceScopeImplementation { 137 138 public FileObject getPersistenceXml() { 139 FileObject location = getLocation(); 140 if (location == null) { 141 return null; 142 } 143 return location.getFileObject("persistence.xml"); } 145 146 public ClassPath getClassPath() { 147 return getProjectSourcesClassPath(); 148 } 149 } 150 } 151 | Popular Tags |