KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebPersistenceProvider


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.web.project;
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 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 /**
45  * Provides persistence location and scope delegating to this project's WebModule.
46  *
47  * @author Andrei Badea
48  */

49 public class WebPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener JavaDoc {
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 JavaDoc {
73         // the folder should have been created when the project was generated
74
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 JavaDoc event) {
116         String JavaDoc propName = event.getPropertyName();
117         if (propName == null || propName.equals(WebProjectProperties.CONF_DIR)) {
118             locationChanged();
119         }
120     }
121
122     private void locationChanged() {
123         File JavaDoc confDirFile = project.getWebModule().getConfDirAsFile();
124         if (confDirFile != null) {
125             File JavaDoc persistenceXmlFile = new File JavaDoc(confDirFile, "persistence.xml"); // NOI18N
126
scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile);
127         } else {
128             scopesHelper.changePersistenceScope(null, null);
129         }
130     }
131
132     /**
133      * Implementation of PersistenceScopeImplementation.
134      */

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"); // NOI18N
143
}
144
145         public ClassPath getClassPath() {
146             return getProjectSourcesClassPath();
147         }
148     }
149 }
150
Popular Tags