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.api.webmodule;
043:
044: import java.util.Iterator;
045: import org.netbeans.modules.j2ee.dd.api.web.WebAppMetadata;
046: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
047: import org.netbeans.modules.web.webmodule.WebModuleAccessor;
048: import org.netbeans.modules.web.spi.webmodule.*;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Lookup;
051: import org.openide.util.Parameters;
052:
053: /**
054: * This class encapsulates a web module.
055: *
056: * <p>A client may obtain a <code>WebModule</code> instance using
057: * method {@link #getWebModule}, for any
058: * {@link org.openide.filesystems.FileObject} in the web module directory structure.</p>
059: * <div class="nonnormative">
060: * <p>Use the classpath API to obtain the classpath for the document base (this classpath
061: * is used for code completion of JSPs). An example:</p>
062: * <pre>
063: * WebModule wm = ...;
064: * FileObject docRoot = wm.getDocumentBase ();
065: * ClassPath cp = ClassPath.getClassPath(docRoot, ClassPath.EXECUTE);
066: * </pre>
067: * <p>Note that no particular directory structure for web module is guaranteed
068: * by this API.</p>
069: * </div>
070: *
071: * @author Pavel Buzek
072: */
073: public final class WebModule {
074:
075: public static final String J2EE_13_LEVEL = "1.3"; //NOI18N
076: public static final String J2EE_14_LEVEL = "1.4"; //NOI18N
077: public static final String JAVA_EE_5_LEVEL = "1.5"; //NOI18N
078:
079: private final WebModuleImplementation impl;
080: private static final Lookup.Result implementations = Lookup
081: .getDefault().lookupResult(WebModuleProvider.class);
082:
083: static {
084: WebModuleAccessor.DEFAULT = new WebModuleAccessor() {
085: public WebModule createWebModule(
086: WebModuleImplementation spiWebmodule) {
087: return new WebModule(spiWebmodule);
088: }
089: };
090: }
091:
092: private WebModule(WebModuleImplementation impl) {
093: Parameters.notNull("impl", impl); // NOI18N
094: this .impl = impl;
095: }
096:
097: /**
098: * Finds the web module that a given file belongs to. The given file should
099: * be one known to be owned by a web module (e.g., it can be a file in a
100: * Java source group, such as a servlet, or it can be a file in the document
101: * base, such as a JSP page).
102: *
103: * @param file the file to find the web module for; never null.
104: * @return the web module this file belongs to or null if the file does not belong
105: * to any web module.
106: * @throws NullPointerException if the <code>file</code> parameter is null.
107: */
108: public static WebModule getWebModule(FileObject file) {
109: Parameters.notNull("file", file); // NOI18N
110: Iterator it = implementations.allInstances().iterator();
111: while (it.hasNext()) {
112: WebModuleProvider impl = (WebModuleProvider) it.next();
113: WebModule wm = impl.findWebModule(file);
114: if (wm != null) {
115: return wm;
116: }
117: }
118: return null;
119: }
120:
121: /**
122: * Returns the folder that contains sources of the static documents for
123: * the web module (html, JSPs, etc.).
124: *
125: * @return the static documents folder; can be null.
126: */
127: public FileObject getDocumentBase() {
128: return impl.getDocumentBase();
129: }
130:
131: /**
132: * Returns the WEB-INF folder for the web module.
133: * It may return null for web module that does not have any WEB-INF folder.
134: * <div class="nonnormative">
135: * <p>The WEB-INF folder would typically be a child of the folder returned
136: * by {@link #getDocumentBase} but does not need to be.</p>
137: * </div>
138: *
139: * @return the WEB-INF folder; can be null.
140: */
141: public FileObject getWebInf() {
142: return impl.getWebInf();
143: }
144:
145: /**
146: * Returns the deployment descriptor (<code>web.xml</code> file) of the web module.
147: * <div class="nonnormative">
148: * The web.xml file would typically be a child of the folder returned
149: * by {@link #getWebInf} but does not need to be.
150: * </div>
151: *
152: * @return the <code>web.xml</code> file; can be null.
153: */
154: public FileObject getDeploymentDescriptor() {
155: return impl.getDeploymentDescriptor();
156: }
157:
158: /**
159: * Returns the context path of the web module.
160: *
161: * @return the context path; can be null.
162: */
163: public String getContextPath() {
164: return impl.getContextPath();
165: }
166:
167: /**
168: * Returns the J2EE platform version of this module. The returned value is
169: * one of the constants {@link #J2EE_13_LEVEL}, {@link #J2EE_14_LEVEL} or
170: * {@link #JAVA_EE_5_LEVEL}.
171: *
172: * @return J2EE platform version; never null.
173: */
174: public String getJ2eePlatformVersion() {
175: return impl.getJ2eePlatformVersion();
176: }
177:
178: /**
179: * Returns the Java source roots associated with the web module.
180: * <div class="nonnormative">
181: * <p>Note that not all the java source roots in the project (e.g. in a freeform project)
182: * belong to the web module.</p>
183: * </div>
184: *
185: * @return this web module's Java source roots; never null.
186: *
187: * @deprecated This method is deprecated, because its return values does
188: * not contain enough information about the source roots. Source roots
189: * are usually implemented by a <code>org.netbeans.api.project.SourceGroup</code>,
190: * which is more than just a container for a {@link org.openide.filesystems.FileObject}.
191: */
192: @Deprecated
193: public FileObject[] getJavaSources() {
194: return impl.getJavaSources();
195: }
196:
197: /**
198: * Returns a model describing the metadata of this web module (servlets,
199: * resources, etc.).
200: *
201: * @return this web module's metadata model; never null.
202: */
203: public MetadataModel<WebAppMetadata> getMetadataModel() {
204: return impl.getMetadataModel();
205: }
206:
207: @Override
208: public boolean equals(Object obj) {
209: if (obj == null) {
210: return false;
211: }
212: if (!WebModule.class.isAssignableFrom(obj.getClass())) {
213: return false;
214: }
215: WebModule wm = (WebModule) obj;
216: if (!getDocumentBase().equals(wm.getDocumentBase())) {
217: return false;
218: }
219: if (!getJ2eePlatformVersion().equals(
220: wm.getJ2eePlatformVersion())) {
221: return false;
222: }
223: String contextPath = getContextPath();
224: String wmContextPath = wm.getContextPath();
225: if (contextPath != null && wmContextPath != null) {
226: return contextPath.equals(wmContextPath);
227: }
228: return true;
229: }
230:
231: @Override
232: public int hashCode() {
233: int hashCode = getDocumentBase().getPath().hashCode();
234: String contextPath = getContextPath();
235: if (contextPath != null) {
236: hashCode += contextPath.hashCode();
237: }
238: return hashCode;
239: }
240: }
|