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.persistence.api; 21 22 import java.util.Iterator; 23 import org.netbeans.api.java.classpath.ClassPath; 24 import org.netbeans.api.project.Project; 25 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 26 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeProvider; 27 import org.netbeans.modules.j2ee.persistenceapi.PersistenceScopeAccessor; 28 import org.openide.filesystems.FileObject; 29 import org.openide.util.Lookup; 30 31 /** 32 * Describes a persistence scope. A persistence scope is composed of 33 * a persistence.xml file and the classpath for this persistence.xml file (which 34 * contains all entity classes and JAR files referenced by the persistence units 35 * in the persistence.xml file). 36 * 37 * @author Andrei Badea 38 */ 39 public final class PersistenceScope { 40 41 private static final Lookup.Result providers = 42 Lookup.getDefault().lookup(new Lookup.Template(PersistenceScopeProvider.class)); 43 44 private final PersistenceScopeImplementation impl; 45 46 static { 47 PersistenceScopeAccessor.DEFAULT = new PersistenceScopeAccessor() { 48 public PersistenceScope createPersistenceScope(PersistenceScopeImplementation impl) { 49 return new PersistenceScope(impl); 50 } 51 }; 52 } 53 54 /** 55 * Returns the persistence scope for the given file. 56 * 57 * @param fo the file for which to find the persistence scope; cannot be null. 58 * 59 * @return the persistence scope for the given file or null if there is no 60 * persistence scope. 61 * 62 * @throws NullPointerException if the fo parameter was null. 63 */ 64 public static PersistenceScope getPersistenceScope(FileObject fo) { 65 if (fo == null) { 66 throw new NullPointerException("Passed null to PersistenceScope.getPersistenceScope(FileObject)"); // NOI18N 67 } 68 Iterator it = providers.allInstances().iterator(); 69 while (it.hasNext()) { 70 PersistenceScopeProvider provider = (PersistenceScopeProvider)it.next(); 71 PersistenceScope persistenceScope = provider.findPersistenceScope(fo); 72 if (persistenceScope != null) { 73 return persistenceScope; 74 } 75 } 76 return null; 77 } 78 79 private PersistenceScope(PersistenceScopeImplementation impl) { 80 this.impl = impl; 81 } 82 83 /** 84 * Returns the persistence.xml file of this persistence scope. 85 * 86 * @return the persistence.xml file or null if it the persistence.xml file does 87 * not exist. 88 */ 89 public FileObject getPersistenceXml() { 90 return impl.getPersistenceXml(); 91 } 92 93 /** 94 * Provides the classpath of this persistence scope, which covers the sources 95 * of the entity classes referenced by the persistence.xml file, as well 96 * as the referenced JAR files. 97 * 98 * @return the persistence scope classpath; never null. 99 */ 100 public ClassPath getClassPath() { 101 return impl.getClassPath(); 102 } 103 } 104