KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > api > webmodule > WebModule


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.web.api.webmodule;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.api.java.classpath.ClassPath;
25 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
26 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
27 import org.netbeans.modules.web.webmodule.WebModuleAccessor;
28 import org.netbeans.modules.web.spi.webmodule.*;
29 import org.openide.filesystems.FileObject;
30 import org.openide.util.Lookup;
31
32 /** WebModule should be used to access contens and properties of web module.
33  * <p>
34  * A client may obtain a WebModule instance using
35  * <code>WebModule.getWebModule(fileObject)</code> static method, for any
36  * FileObject in the web module directory structure.
37  * </p>
38  * <div class="nonnormative">
39  * <p>
40  * Use classpath API to obtain classpath for the document base (this classpath
41  * is used for code completion of JSPs). An example:
42  * <pre>
43  * WebModule wm = ...;
44  * FileObject docRoot = wm.getDocumentBase ();
45  * ClassPath cp = ClassPath.getClassPath(docRoot, ClassPath.EXECUTE);
46  *
47  * </pre>
48  * <p>
49  * Note that the particular directory structure for web module is not guaranteed
50  * by this API.
51  * </div>
52  *
53  * @author Pavel Buzek
54  */

55 public final class WebModule implements MetadataUnit {
56     
57     //TO-DO: the J2EE_13_LEVEL and J2EE_14_LEVEL constants should be got from org.netbeans.modules.j2ee.common.J2eeProjectConstants
58
public static final String JavaDoc J2EE_13_LEVEL = "1.3"; //NOI18N
59
public static final String JavaDoc J2EE_14_LEVEL = "1.4"; //NOI18N
60
public static final String JavaDoc JAVA_EE_5_LEVEL = "1.5"; //NOI18N
61

62     private WebModuleImplementation impl;
63     private static final Lookup.Result implementations =
64         Lookup.getDefault().lookup(new Lookup.Template(WebModuleProvider.class));
65     
66     static {
67         WebModuleAccessor.DEFAULT = new WebModuleAccessor() {
68             public WebModule createWebModule(WebModuleImplementation spiWebmodule) {
69                 return new WebModule(spiWebmodule);
70             }
71
72             public WebModuleImplementation getWebModuleImplementation(WebModule wm) {
73                 return wm == null ? null : wm.impl;
74             }
75         };
76     }
77     
78     private WebModule (WebModuleImplementation impl) {
79         if (impl == null)
80             throw new IllegalArgumentException JavaDoc ();
81         this.impl = impl;
82     }
83     
84     /** Find the WebModule for given file or null if the file does not belong
85      * to any web module.
86      */

87     public static WebModule getWebModule (FileObject f) {
88         if (f == null) {
89             throw new NullPointerException JavaDoc("Passed null to WebModule.getWebModule(FileObject)"); // NOI18N
90
}
91         Iterator JavaDoc it = implementations.allInstances().iterator();
92         while (it.hasNext()) {
93             WebModuleProvider impl = (WebModuleProvider)it.next();
94             WebModule wm = impl.findWebModule (f);
95             if (wm != null) {
96                 return wm;
97             }
98         }
99         return null;
100     }
101
102     /** Folder that contains sources of the static documents for
103      * the web module (html, JSPs, etc.).
104      */

105     public FileObject getDocumentBase () {
106         return impl.getDocumentBase ();
107     }
108     
109     /** WEB-INF folder for the web module.
110      * It may return null for web module that does not have any WEB-INF folder.
111      * <div class="nonnormative">
112      * The WEB-INF folder would typically be a child of the folder returned
113      * by {@link #getDocumentBase} but does not need to be.
114      * </div>
115      */

116     public FileObject getWebInf () {
117         return impl.getWebInf ();
118     }
119
120     /** Deployment descriptor (web.xml file) of the web module.
121      * <div class="nonnormative">
122      * The web.xml file would typically be a child of the folder returned
123      * by {@link #getWebInf} but does not need to be.
124      * </div>
125      */

126     public FileObject getDeploymentDescriptor () {
127         return impl.getDeploymentDescriptor ();
128     }
129     
130     /** Context path of the web module.
131      */

132     public String JavaDoc getContextPath () {
133         return impl.getContextPath ();
134     }
135     
136     /** J2EE platform version - one of the constants {@link #J2EE_13_LEVEL},
137      * {@link #J2EE_14_LEVEL}.
138      * @return J2EE platform version
139      */

140     public String JavaDoc getJ2eePlatformVersion () {
141         return impl.getJ2eePlatformVersion ();
142     }
143     
144     /** Source roots associated with the web module.
145      * <div class="nonnormative">
146      * Note that not all the java source roots in the project (e.g. in a freeform project)
147      * belong to the web module.
148      * </div>
149      */

150     public FileObject[] getJavaSources() {
151         return impl.getJavaSources();
152     }
153     
154     /** Returns true if the object represents the same web module.
155      */

156     public boolean equals (Object JavaDoc obj) {
157         if (obj == null) {
158             return false;
159         }
160         if (!WebModule.class.isAssignableFrom(obj.getClass()))
161             return false;
162         WebModule wm = (WebModule) obj;
163         return getDocumentBase().equals(wm.getDocumentBase())
164             && getJ2eePlatformVersion().equals (wm.getJ2eePlatformVersion())
165             && getContextPath().equals(wm.getContextPath());
166     }
167     
168     public int hashCode () {
169         return getDocumentBase ().getPath ().length () + getContextPath ().length ();
170     }
171     
172     public ClassPath getClassPath() {
173         FileObject[] roots = getJavaSources();
174         if (roots.length > 0) {
175             FileObject fo = roots[0];
176             return ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
177                 ClassPath.getClassPath(fo, ClassPath.SOURCE),
178                 ClassPath.getClassPath(fo, ClassPath.COMPILE)
179             });
180         } else {
181             return org.netbeans.spi.java.classpath.support.ClassPathSupport.createClassPath(Collections.emptyList());
182         }
183     }
184 }
185
Popular Tags