KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientPersistenceProvider


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

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

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