1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Set ; 30 import org.netbeans.api.java.classpath.ClassPath; 31 import org.netbeans.api.project.FileOwnerQuery; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 34 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 35 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 37 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathProviderImpl; 38 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 39 import org.netbeans.modules.j2ee.metadata.ClassPathSupport; 40 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 41 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 42 import org.netbeans.modules.j2ee.persistence.provider.Provider; 43 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 44 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider; 45 import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider; 46 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory; 47 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 48 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider; 49 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider; 50 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier; 51 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 52 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 53 import org.openide.filesystems.FileObject; 54 55 61 public class EjbJarPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener , PersistenceProviderSupplier{ 62 63 private final EjbJarProject project; 64 private final PropertyEvaluator evaluator; 65 66 private final PersistenceScopeImplementation persistenceScopeImpl = new PersistenceScopeImpl(); 67 private final PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl); 68 69 private final PersistenceScopesHelper scopesHelper = new PersistenceScopesHelper(); 70 71 private ClassPath projectSourcesClassPath; 72 73 public EjbJarPersistenceProvider(EjbJarProject project, PropertyEvaluator evaluator) { 74 this.project = project; 75 this.evaluator = evaluator; 76 evaluator.addPropertyChangeListener(this); 77 locationChanged(); 78 } 79 80 public FileObject getLocation() { 81 return project.getEjbModule().getMetaInf(); 82 } 83 84 public FileObject createLocation() throws IOException { 85 return project.getAPIEjbJar().getMetaInf(); 87 } 88 89 public PersistenceScope findPersistenceScope(FileObject fo) { 90 Project project = FileOwnerQuery.getOwner(fo); 91 if (project != null) { 92 EjbJarPersistenceProvider provider = (EjbJarPersistenceProvider)project.getLookup().lookup(EjbJarPersistenceProvider.class); 93 return provider.getPersistenceScope(); 94 } 95 return null; 96 } 97 98 public PersistenceScopes getPersistenceScopes() { 99 return scopesHelper.getPersistenceScopes(); 100 } 101 102 public ClassPath getClassPath() { 103 return getProjectSourcesClassPath(); 104 } 105 106 private PersistenceScope getPersistenceScope() { 107 FileObject persistenceXml = persistenceScope.getPersistenceXml(); 108 if (persistenceXml != null && persistenceXml.isValid()) { 109 return persistenceScope; 110 } 111 return null; 112 } 113 114 private ClassPath getProjectSourcesClassPath() { 115 synchronized (this) { 116 if (projectSourcesClassPath == null) { 117 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class); 118 projectSourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] { 119 cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE), 120 cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE), 121 }); 122 } 123 return projectSourcesClassPath; 124 } 125 } 126 127 public void propertyChange(PropertyChangeEvent event) { 128 String propName = event.getPropertyName(); 129 if (propName == null || propName.equals(EjbJarProjectProperties.META_INF)) { 130 locationChanged(); 131 } 132 } 133 134 private void locationChanged() { 135 File metaInfFile = project.getEjbModule().getMetaInfAsFile(); 136 if (metaInfFile != null) { 137 File persistenceXmlFile = new File (metaInfFile, "persistence.xml"); scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile); 139 } else { 140 scopesHelper.changePersistenceScope(null, null); 141 } 142 } 143 144 public List <Provider> getSupportedProviders() { 145 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 146 J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID()); 147 148 if (platform == null){ 149 return Collections.<Provider>emptyList(); 150 } 151 List <Provider> result = new ArrayList <Provider>(); 152 153 addPersistenceProvider(ProviderUtil.HIBERNATE_PROVIDER, "hibernatePersistenceProviderIsDefault", platform, result); addPersistenceProvider(ProviderUtil.TOPLINK_PROVIDER, "toplinkPersistenceProviderIsDefault", platform, result); addPersistenceProvider(ProviderUtil.KODO_PROVIDER, "kodoPersistenceProviderIsDefault", platform, result); 157 return result; 158 } 159 160 private void addPersistenceProvider(Provider provider, String defaultProvider, J2eePlatform platform, List <Provider> providers){ 161 if (platform.isToolSupported(provider.getProviderClass())){ 163 if (platform.isToolSupported(defaultProvider)){ 164 providers.add(0, provider); 165 } else { 166 providers.add(provider); 167 } 168 } 169 } 170 171 public boolean supportsDefaultProvider() { 172 173 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 174 J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID()); 175 176 if (platform == null){ 177 return false; 179 } 180 181 Set <String > supportedVersions = platform.getSupportedSpecVersions(j2eeModuleProvider.getJ2eeModule().getModuleType()); 182 183 return supportedVersions.contains(J2eeModule.JAVA_EE_5); 184 } 185 186 189 private final class PersistenceScopeImpl implements PersistenceScopeImplementation { 190 191 public FileObject getPersistenceXml() { 192 FileObject location = getLocation(); 193 if (location == null) { 194 return null; 195 } 196 return location.getFileObject("persistence.xml"); } 198 199 public ClassPath getClassPath() { 200 return getProjectSourcesClassPath(); 201 } 202 } 203 } 204 | Popular Tags |