KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > api > ejbjar > Car


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 package org.netbeans.modules.j2ee.api.ejbjar;
20
21 import java.util.Collections JavaDoc;
22 import org.netbeans.api.java.classpath.ClassPath;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.modules.j2ee.ejbjar.CarAccessor;
25 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
26 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
27 import org.netbeans.modules.j2ee.spi.ejbjar.CarImplementation;
28 import org.netbeans.modules.j2ee.spi.ejbjar.CarProvider;
29 import org.netbeans.modules.j2ee.spi.ejbjar.CarsInProject;
30 import org.netbeans.spi.java.classpath.PathResourceImplementation;
31 import org.openide.filesystems.FileObject;
32 import org.openide.util.Lookup;
33
34 /**
35  * Car should be used to access properties of an Enterprise application client module.
36  * <p>
37  * A client may obtain a Car instance using
38  * <code>Car.getCar(fileObject)</code> static method, for any
39  * FileObject in the application client module directory structure.
40  * </p>
41  * <div class="nonnormative">
42  * Note that the particular directory structure for application client module
43  * is not guaranteed by this API.
44  * </div>
45  *
46  *
47  * @author Pavel Buzek
48  * @author Lukas Jungmann
49  */

50 public final class Car implements MetadataUnit {
51     private CarImplementation impl;
52     private static final Lookup.Result<CarProvider> implementations =
53         Lookup.getDefault().lookup(new Lookup.Template<CarProvider>(CarProvider.class));
54     
55     static {
56         CarAccessor.DEFAULT = new CarAccessor() {
57             public Car createCar(CarImplementation spiEjbJar) {
58                 return new Car(spiEjbJar);
59             }
60
61             public CarImplementation getCarImplementation(Car wm) {
62                 return wm == null ? null : wm.impl;
63             }
64         };
65     }
66     
67     private Car (CarImplementation impl) {
68         if (impl == null)
69             throw new IllegalArgumentException JavaDoc ();
70         this.impl = impl;
71     }
72     
73     /**
74      * Find the Car for given file or null if the file does not belong
75      * to any application client module.
76      */

77     public static Car getCar (FileObject f) {
78         if (f == null) {
79             throw new NullPointerException JavaDoc("Passed null to Car.getCar(FileObject)"); // NOI18N
80
}
81         for (CarProvider impl : implementations.allInstances()) {
82             Car wm = impl.findCar (f);
83             if (wm != null) {
84                 return wm;
85             }
86         }
87         return null;
88     }
89
90     /** Find Car(s) for all application clients within a given project.
91      * @return an array of Car instance (empty array if no instance are found).
92      */

93     public static Car[] getCars (Project project) {
94         CarsInProject providers = project.getLookup().lookup(CarsInProject.class);
95         if (providers != null) {
96             Car jars [] = providers.getCars();
97             if (jars != null) {
98                 return jars;
99             }
100         }
101         return new Car[] {};
102     }
103     
104     /** J2EE platform version - one of the constants
105      * defined in {@link org.netbeans.modules.j2ee.api.common.J2eeProjectConstants}.
106      * @return J2EE platform version
107      */

108     public String JavaDoc getJ2eePlatformVersion () {
109         return impl.getJ2eePlatformVersion();
110     }
111     
112     /** Deployment descriptor (application-client.xml file) of the application client module.
113      */

114     public FileObject getDeploymentDescriptor () {
115         return impl.getDeploymentDescriptor();
116     }
117
118     /** Source roots associated with the Car module.
119      * <div class="nonnormative">
120      * Note that not all the java source roots in the project (e.g. in a freeform project)
121      * belong to the Car module.
122      * </div>
123      */

124     public FileObject[] getJavaSources() {
125         return impl.getJavaSources();
126     }
127     
128     /** Meta-inf
129      */

130     public FileObject getMetaInf() {
131         return impl.getMetaInf();
132     }
133
134     /**
135      * Provides unit's classpath, which covers sources and libraries logically
136      * coupled with XML descriptor. Note, that if XML doesn't exist, this
137      * classpath is only place where metadata comes from.
138      *
139      * @return classpath of the unit
140      */

141     public ClassPath getClassPath() {
142         FileObject[] roots = getJavaSources();
143         if (roots.length > 0) {
144             FileObject fo = roots[0];
145             return ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
146                 ClassPath.getClassPath(fo, ClassPath.SOURCE),
147                 ClassPath.getClassPath(fo, ClassPath.COMPILE)
148             });
149         } else {
150             return org.netbeans.spi.java.classpath.support.ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
151         }
152     }
153 }
154
Popular Tags