001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.project;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.IOException;
048: import org.netbeans.api.java.classpath.ClassPath;
049: import org.netbeans.api.project.FileOwnerQuery;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
052: import org.netbeans.modules.j2ee.persistence.api.EntityClassScope;
053: import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
054: import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes;
055: import org.netbeans.modules.j2ee.persistence.api.metadata.orm.EntityMappingsMetadata;
056: import org.netbeans.modules.j2ee.persistence.spi.EntityClassScopeFactory;
057: import org.netbeans.modules.j2ee.persistence.spi.EntityClassScopeImplementation;
058: import org.netbeans.modules.j2ee.persistence.spi.EntityClassScopeProvider;
059: import org.netbeans.modules.j2ee.persistence.spi.PersistenceLocationProvider;
060: import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory;
061: import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation;
062: import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider;
063: import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider;
064: import org.netbeans.modules.j2ee.persistence.spi.support.EntityMappingsMetadataModelHelper;
065: import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper;
066: import org.netbeans.modules.web.project.classpath.ClassPathProviderImpl;
067: import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
068: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
069: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
070: import org.openide.filesystems.FileObject;
071:
072: /**
073: * Provides persistence location and scope delegating to this project's WebModule.
074: *
075: * @author Andrei Badea
076: */
077: public class WebPersistenceProvider implements
078: PersistenceLocationProvider, PersistenceScopeProvider,
079: PersistenceScopesProvider, EntityClassScopeProvider,
080: PropertyChangeListener {
081:
082: private final WebProject project;
083: private final PropertyEvaluator evaluator;
084: private final ClassPathProviderImpl cpProvider;
085:
086: private final ScopeImpl scopeImpl = new ScopeImpl();
087: private final PersistenceScope persistenceScope = PersistenceScopeFactory
088: .createPersistenceScope(scopeImpl);
089: private final EntityClassScope entityClassScope = EntityClassScopeFactory
090: .createEntityClassScope(scopeImpl);
091:
092: private final PersistenceScopesHelper scopesHelper = new PersistenceScopesHelper();
093: private final EntityMappingsMetadataModelHelper modelHelper;
094:
095: private ClassPath projectSourcesClassPath;
096:
097: public WebPersistenceProvider(WebProject project,
098: PropertyEvaluator evaluator,
099: ClassPathProviderImpl cpProvider) {
100: this .project = project;
101: this .evaluator = evaluator;
102: this .cpProvider = cpProvider;
103: modelHelper = createEntityMappingsHelper();
104: evaluator.addPropertyChangeListener(this );
105: locationChanged();
106: }
107:
108: public FileObject getLocation() {
109: return project.getWebModule().getConfDir();
110: }
111:
112: public FileObject createLocation() throws IOException {
113: // the folder should have been created when the project was generated
114: return project.getWebModule().getConfDir();
115: }
116:
117: public PersistenceScope findPersistenceScope(FileObject fo) {
118: Project project = FileOwnerQuery.getOwner(fo);
119: if (project != null) {
120: WebPersistenceProvider provider = (WebPersistenceProvider) project
121: .getLookup().lookup(WebPersistenceProvider.class);
122: return provider.getPersistenceScope();
123: }
124: return null;
125: }
126:
127: public EntityClassScope findEntityClassScope(FileObject fo) {
128: Project project = FileOwnerQuery.getOwner(fo);
129: if (project != null) {
130: WebPersistenceProvider provider = (WebPersistenceProvider) project
131: .getLookup().lookup(WebPersistenceProvider.class);
132: return provider.getEntityClassScope();
133: }
134: return null;
135: }
136:
137: public PersistenceScopes getPersistenceScopes() {
138: return scopesHelper.getPersistenceScopes();
139: }
140:
141: private PersistenceScope getPersistenceScope() {
142: FileObject persistenceXml = persistenceScope
143: .getPersistenceXml();
144: if (persistenceXml != null && persistenceXml.isValid()) {
145: return persistenceScope;
146: }
147: return null;
148: }
149:
150: private EntityClassScope getEntityClassScope() {
151: return entityClassScope;
152: }
153:
154: private ClassPath getProjectSourcesClassPath() {
155: synchronized (this ) {
156: if (projectSourcesClassPath == null) {
157: ClassPathProviderImpl cpProvider = project
158: .getClassPathProvider();
159: projectSourcesClassPath = ClassPathSupport
160: .createProxyClassPath(new ClassPath[] {
161: cpProvider
162: .getProjectSourcesClassPath(ClassPath.SOURCE),
163: cpProvider
164: .getProjectSourcesClassPath(ClassPath.COMPILE), });
165: }
166: return projectSourcesClassPath;
167: }
168: }
169:
170: private EntityMappingsMetadataModelHelper createEntityMappingsHelper() {
171: return EntityMappingsMetadataModelHelper
172: .create(
173: cpProvider
174: .getProjectSourcesClassPath(ClassPath.BOOT),
175: cpProvider
176: .getProjectSourcesClassPath(ClassPath.COMPILE),
177: cpProvider
178: .getProjectSourcesClassPath(ClassPath.SOURCE));
179: }
180:
181: public void propertyChange(PropertyChangeEvent event) {
182: String propName = event.getPropertyName();
183: if (propName == null
184: || propName.equals(WebProjectProperties.CONF_DIR)) {
185: locationChanged();
186: }
187: }
188:
189: private void locationChanged() {
190: File confDirFile = project.getWebModule().getConfDirAsFile();
191: if (confDirFile != null) {
192: File persistenceXmlFile = new File(confDirFile,
193: "persistence.xml"); // NOI18N
194: scopesHelper.changePersistenceScope(persistenceScope,
195: persistenceXmlFile);
196: modelHelper.changePersistenceXml(persistenceXmlFile);
197: } else {
198: scopesHelper.changePersistenceScope(null, null);
199: modelHelper.changePersistenceXml(null);
200: }
201: }
202:
203: /**
204: * Implementation of PersistenceScopeImplementation and EntityClassScopeImplementation.
205: */
206: private final class ScopeImpl implements
207: PersistenceScopeImplementation,
208: EntityClassScopeImplementation {
209:
210: public FileObject getPersistenceXml() {
211: FileObject location = getLocation();
212: if (location == null) {
213: return null;
214: }
215: return location.getFileObject("persistence.xml"); // NOI18N
216: }
217:
218: public ClassPath getClassPath() {
219: return getProjectSourcesClassPath();
220: }
221:
222: public MetadataModel<EntityMappingsMetadata> getEntityMappingsModel(
223: String persistenceUnitName) {
224: return modelHelper
225: .getEntityMappingsModel(persistenceUnitName);
226: }
227:
228: public MetadataModel<EntityMappingsMetadata> getEntityMappingsModel(
229: boolean withDeps) {
230: return modelHelper.getDefaultEntityMappingsModel(withDeps);
231: }
232: }
233: }
|