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-2007 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.visualweb.project.jsf.framework;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.OutputStreamWriter;
047: import java.io.PrintWriter;
048: import org.netbeans.api.project.libraries.Library;
049: import org.netbeans.api.project.libraries.LibraryManager;
050: import org.openide.filesystems.FileLock;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileSystem;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.filesystems.Repository;
055:
056: /**
057: *
058: * @author Po-Ting Wu
059: */
060: public class JSFUtils {
061: private static final String LIB_JSF_NAME = "JSF";
062: private static final String LIBS_FOLDER = "org-netbeans-api-project-libraries/Libraries"; //NOI18N
063:
064: private static final String JSF_API_JAR = "jsf-api.jar";
065: private static final String JSF_IMPL_JAR = "jsf-impl.jar";
066: private static final String DIGESTER_JAR = "commons-digester.jar";
067: private static final String LOGGING_JAR = "commons-logging.jar";
068: private static final String COLLECTIONS_JAR = "commons-collections.jar";
069: private static final String BEANUTILS_JAR = "commons-beanutils.jar";
070:
071: public static final String FACES_EXCEPTION = "javax.faces.FacesException"; //NOI18N
072: public static final String JSF_1_2__API_SPECIFIC_CLASS = "javax.faces.application.StateManagerWrapper"; //NOI18N
073: public static final String MYFACES_SPAECIFIC_CLASS = "org.apache.myfaces.webapp.StartupServletContextListener"; //NOI18N
074:
075: public static boolean isJSFInstallFolder(File file) {
076: boolean result = false;
077: if (file.exists() && file.isDirectory()) {
078: File jsf_impl = new File(file, JSF_IMPL_JAR);
079: if (jsf_impl.exists())
080: result = true;
081: }
082: return result;
083: }
084:
085: //XXX: Replace this when API for managing libraries is available
086: public static boolean createJSFUserLibrary(File folder,
087: final String version) throws IOException {
088: assert folder != null;
089:
090: final File jsfLibFolder = new File(folder.getPath()); // NOI18N
091: if (!jsfLibFolder.exists() || !jsfLibFolder.isDirectory())
092: return false;
093:
094: final File jsfApiJar = new File(jsfLibFolder, JSF_API_JAR);
095: final File jsfImplJar = new File(jsfLibFolder, JSF_IMPL_JAR);
096: final File digesterJar = new File(jsfLibFolder, DIGESTER_JAR);
097: final File loggingJar = new File(jsfLibFolder, LOGGING_JAR);
098: final File collectionsJar = new File(jsfLibFolder,
099: COLLECTIONS_JAR);
100: final File beanutilsJar = new File(jsfLibFolder, BEANUTILS_JAR);
101: if (!jsfApiJar.exists() || !jsfImplJar.exists()
102: || !digesterJar.exists() || !loggingJar.exists()
103: || !collectionsJar.exists() || !beanutilsJar.exists())
104: return false;
105:
106: final FileSystem sysFs = Repository.getDefault()
107: .getDefaultFileSystem();
108: final FileObject libsFolder = sysFs.findResource(LIBS_FOLDER);
109: final String convertedVersion = convertLibraryVersion(version);
110: assert libsFolder != null && libsFolder.isFolder();
111:
112: sysFs.runAtomicAction(new FileSystem.AtomicAction() {
113: public void run() throws IOException {
114: String fileName = LIB_JSF_NAME + "-" + convertedVersion; //NOI18N
115: FileObject jsf = libsFolder.getFileObject(fileName
116: + ".xml");
117: if (jsf == null) {
118: jsf = libsFolder.createData(fileName + ".xml");
119: }
120: FileLock lock = jsf.lock();
121: try {
122: PrintWriter out = new PrintWriter(
123: new OutputStreamWriter(jsf
124: .getOutputStream(lock)));
125: try {
126: String[] resources = new String[] {
127: FileUtil.getArchiveRoot(
128: jsfApiJar.toURI().toURL())
129: .toString(),
130: FileUtil.getArchiveRoot(
131: jsfImplJar.toURI().toURL())
132: .toString(),
133: FileUtil.getArchiveRoot(
134: digesterJar.toURI().toURL())
135: .toString(),
136: FileUtil.getArchiveRoot(
137: loggingJar.toURI().toURL())
138: .toString(),
139: FileUtil.getArchiveRoot(
140: collectionsJar.toURI().toURL())
141: .toString(),
142: FileUtil.getArchiveRoot(
143: beanutilsJar.toURI().toURL())
144: .toString() };
145: createLibraryFile(out, fileName, resources);
146: } finally {
147: out.close();
148: }
149: } finally {
150: lock.releaseLock();
151: }
152: }
153: });
154: return true;
155: }
156:
157: public static Library getJSFLibrary(final String version) {
158: String convertedVersion = convertLibraryVersion(version);
159: return LibraryManager.getDefault().getLibrary(
160: LIB_JSF_NAME + "-" + convertedVersion);
161: }
162:
163: /* Converts a string to the text, which is fit for the LibraryManager as a library name
164: */
165: public static String convertLibraryVersion(String version) {
166: String converted = version;
167: converted = converted.replace('.', '-');
168: converted = converted.replace(' ', '_');
169: return converted;
170: }
171:
172: private static void createLibraryFile(PrintWriter out, String name,
173: String[] resources) {
174: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //NOI18N
175: out
176: .println("<!DOCTYPE library PUBLIC \"-//NetBeans//DTD Library Declaration 1.0//EN\" \"http://www.netbeans.org/dtds/library-declaration-1_0.dtd\">"); //NOI18N
177: out.println("<library version=\"1.0\">"); //NOI18N
178: out.println("\t<name>" + name + "</name>"); //NOI18N
179: out.println("\t<type>j2se</type>"); //NOI18N
180: out.println("\t<volume>"); //NOI18N
181: out.println("\t\t<type>classpath</type>"); //NOI18N
182: for (int i = 0; i < resources.length; i++) {
183: out
184: .println("\t\t<resource>" + resources[i]
185: + "</resource>"); //NOI18N
186: }
187: out.println("\t</volume>"); //NOI18N
188: out.println("\t<volume>"); //NOI18N
189: out.println("\t\t<type>src</type>"); //NOI18N
190: out.println("\t</volume>"); //NOI18N
191: out.println("\t<volume>"); //NOI18N
192: out.println("\t\t<type>javadoc</type>"); //NOI18N
193: out.println("\t</volume>"); //NOI18N
194: out.println("</library>"); //NOI18N
195: }
196:
197: }
|