KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarPersistenceProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbjarproject;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
56  * Provides persistence location and scope delegating to this project's EjbJar
57  * module.
58  *
59  * @author Andrei Badea
60  */

61 public class EjbJarPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener JavaDoc, 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 JavaDoc {
85         // the folder should have be created when the project was generated
86
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 JavaDoc event) {
128         String JavaDoc propName = event.getPropertyName();
129         if (propName == null || propName.equals(EjbJarProjectProperties.META_INF)) {
130             locationChanged();
131         }
132     }
133     
134     private void locationChanged() {
135         File JavaDoc metaInfFile = project.getEjbModule().getMetaInfAsFile();
136         if (metaInfFile != null) {
137             File JavaDoc persistenceXmlFile = new File JavaDoc(metaInfFile, "persistence.xml"); // NOI18N
138
scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile);
139         } else {
140             scopesHelper.changePersistenceScope(null, null);
141         }
142     }
143     
144     public List JavaDoc<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 JavaDoc<Provider> result = new ArrayList JavaDoc<Provider>();
152         
153         addPersistenceProvider(ProviderUtil.HIBERNATE_PROVIDER, "hibernatePersistenceProviderIsDefault", platform, result); // NOI18N
154
addPersistenceProvider(ProviderUtil.TOPLINK_PROVIDER, "toplinkPersistenceProviderIsDefault", platform, result);// NOI18N
155
addPersistenceProvider(ProviderUtil.KODO_PROVIDER, "kodoPersistenceProviderIsDefault", platform, result); // NOI18N
156

157         return result;
158     }
159     
160     private void addPersistenceProvider(Provider provider, String JavaDoc defaultProvider, J2eePlatform platform, List JavaDoc<Provider> providers){
161         // would need an api for this..
162
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             // server probably not registered, can't resolve whether default provider is supported (see #79856)
178
return false;
179         }
180         
181         Set JavaDoc<String JavaDoc> supportedVersions = platform.getSupportedSpecVersions(j2eeModuleProvider.getJ2eeModule().getModuleType());
182         
183         return supportedVersions.contains(J2eeModule.JAVA_EE_5);
184     }
185
186     /**
187      * Implementation of PersistenceScopeImplementation.
188      */

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"); // NOI18N
197
}
198         
199         public ClassPath getClassPath() {
200             return getProjectSourcesClassPath();
201         }
202     }
203 }
204
Popular Tags