KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEPersistenceProvider


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.java.j2seproject;
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.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 //retouche:
32
//import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
33
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
35 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes;
36 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider;
37 import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider;
38 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory;
39 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation;
40 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider;
41 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider;
42 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper;
43 import org.netbeans.modules.java.j2seproject.classpath.ClassPathProviderImpl;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.filesystems.URLMapper;
47
48 /**
49  *
50  * @author Andrei Badea
51  */

52 public class J2SEPersistenceProvider implements PersistenceLocationProvider, PersistenceScopeProvider, PersistenceScopesProvider, PersistenceClassPathProvider, PropertyChangeListener JavaDoc {
53
54     private final J2SEProject project;
55
56     private final PersistenceScopeImplementation persistenceScopeImpl = new PersistenceScopeImpl();
57     private final PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl);
58
59     private final PersistenceScopesHelper scopesHelper = new PersistenceScopesHelper();
60
61     private ClassPath projectSourcesClassPath;
62
63     public J2SEPersistenceProvider(J2SEProject project) {
64         this.project = project;
65         project.getSourceRoots().addPropertyChangeListener(this);
66         sourcesChanged();
67     }
68
69     public FileObject getLocation() {
70         FileObject root = getLocationSourceRoot();
71         if (root == null) {
72             return null;
73         }
74         FileObject metaInf = root.getFileObject("META-INF"); // NOI18N
75
if (metaInf == null || !metaInf.isFolder()) {
76             return null;
77         }
78         return metaInf;
79     }
80
81     public FileObject createLocation() throws IOException JavaDoc {
82         FileObject root = getLocationSourceRoot();
83         if (root == null) {
84             throw new IOException JavaDoc("There are no source roots in the project or the first source root does not exist."); // NOI18N
85
}
86         FileObject metaInf = root.getFileObject("META-INF"); // NOI18N
87
if (metaInf != null) {
88             if (!metaInf.isFolder()) {
89                 throw new IOException JavaDoc("The META-INF directory exists, but is not a folder."); // NOI18N
90
}
91         } else {
92             metaInf = root.createFolder("META-INF"); // NOI18N
93
}
94         return metaInf;
95     }
96
97     public PersistenceScope findPersistenceScope(FileObject fo) {
98         Project project = FileOwnerQuery.getOwner(fo);
99         if (project != null) {
100             J2SEPersistenceProvider provider = (J2SEPersistenceProvider)project.getLookup().lookup(J2SEPersistenceProvider.class);
101             return provider.getPersistenceScope();
102         }
103         return null;
104     }
105
106     public PersistenceScopes getPersistenceScopes() {
107         return scopesHelper.getPersistenceScopes();
108     }
109
110     public ClassPath getClassPath() {
111         return getProjectSourcesClassPath();
112     }
113
114     private File JavaDoc getFirstSourceRoot() {
115         URL JavaDoc[] urls = project.getSourceRoots().getRootURLs();
116         if (urls.length == 0) {
117             return null;
118         }
119         return new File JavaDoc(URI.create(urls[0].toExternalForm()));
120     }
121
122     private FileObject getLocationSourceRoot() {
123         File JavaDoc sourceRoot = getFirstSourceRoot();
124         if (sourceRoot != null) {
125             return FileUtil.toFileObject(sourceRoot);
126         }
127         return null;
128     }
129
130     private PersistenceScope getPersistenceScope() {
131         FileObject persistenceXml = persistenceScope.getPersistenceXml();
132         if (persistenceXml != null && persistenceXml.isValid()) {
133             return persistenceScope;
134         }
135         return null;
136     }
137
138     private ClassPath getProjectSourcesClassPath() {
139         synchronized (this) {
140             if (projectSourcesClassPath == null) {
141                 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class);
142                 //retouche:
143
projectSourcesClassPath = ClassPathSupport.createProxyClassPath(new ClassPath[] {
144 // projectSourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
145
cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE),
146                     cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE),
147                 });
148             }
149             return projectSourcesClassPath;
150         }
151     }
152
153     public void propertyChange(PropertyChangeEvent JavaDoc event) {
154         sourcesChanged();
155     }
156
157     private void sourcesChanged() {
158         synchronized (this) {
159             File JavaDoc sourceRootFile = getFirstSourceRoot();
160             if (sourceRootFile != null) {
161                 File JavaDoc persistenceXmlFile = new File JavaDoc(sourceRootFile, "META-INF/persistence.xml"); //NOI18N
162
scopesHelper.changePersistenceScope(persistenceScope, persistenceXmlFile);
163             } else {
164                 scopesHelper.changePersistenceScope(null, null);
165             }
166         }
167     }
168
169     /**
170      * Implementation of PersistenceScopeImplementation.
171      */

172     private final class PersistenceScopeImpl implements PersistenceScopeImplementation {
173
174         public FileObject getPersistenceXml() {
175             FileObject location = getLocation();
176             if (location == null) {
177                 return null;
178             }
179             return location.getFileObject("persistence.xml"); // NOI18N
180
}
181
182         public ClassPath getClassPath() {
183             return getProjectSourcesClassPath();
184         }
185     }
186 }
187
Popular Tags