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.web.jsf;
043:
044: import java.io.File;
045: import java.io.FileFilter;
046: import java.io.IOException;
047: import java.net.URL;
048: import java.util.ArrayList;
049: import java.util.Arrays;
050: import java.util.List;
051: import org.netbeans.api.project.libraries.LibraryManager;
052: import org.netbeans.modules.j2ee.common.Util;
053: import org.netbeans.modules.web.jsf.api.facesmodel.JSFVersion;
054: import org.netbeans.spi.project.libraries.LibraryFactory;
055: import org.netbeans.spi.project.libraries.LibraryImplementation;
056: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.util.Exceptions;
059: import org.openide.util.NbBundle;
060:
061: /**
062: *
063: * @author Petr Pisl, Radko Najman
064: */
065: public class JSFUtils {
066:
067: private static final String LIB_FOLDER = "lib"; //NOI18N
068:
069: // the names of bundled jsf libraries
070: public static String DEFAULT_JSF_1_1_NAME = "jsf1102"; //NOI18N
071: public static String DEFAULT_JSF_1_2_NAME = "jsf12"; //NOI18N
072: // the name of jstl libraryr
073: public static String DEFAULT_JSTL_1_1_NAME = "jstl11"; //NOI18N
074:
075: public static final String FACES_EXCEPTION = "javax.faces.FacesException"; //NOI18N
076: public static final String JSF_1_2__API_SPECIFIC_CLASS = "javax.faces.application.StateManagerWrapper"; //NOI18N
077: public static final String MYFACES_SPAECIFIC_CLASS = "org.apache.myfaces.webapp.StartupServletContextListener"; //NOI18N
078:
079: /** This method finds out, whether the input file is a folder that contains
080: * a jsf implementation, which < = max version. It looks for lib folder and in this lib looks
081: * through jars, whether their contain javax.faces.FacesException class.
082: *
083: * @return null if the folder contains a jsf implemention or an error message
084: */
085: public static String isJSFInstallFolder(File folder,
086: JSFVersion maxVersion) {
087: String result = null;
088: if (folder.exists() && folder.isDirectory()) {
089: File libFolder = new File(folder, LIB_FOLDER);
090: if (libFolder.exists()) {
091: File[] files = libFolder.listFiles(new FileFilter() {
092:
093: public boolean accept(File pathname) {
094: boolean accepted = false;
095: if (pathname.getName().endsWith(".jar")) { //NOI18N
096: accepted = true;
097: }
098: return accepted;
099: }
100:
101: });
102: boolean isJSF = false;
103: JSFVersion jsfVersion = JSFVersion.JSF_1_1;
104: try {
105: List<File> list = Arrays.asList(files);
106: isJSF = Util.containsClass(list, FACES_EXCEPTION); //NOI18N
107: if (Util.containsClass(list,
108: JSF_1_2__API_SPECIFIC_CLASS)) { //NOI18N
109: jsfVersion = JSFVersion.JSF_1_2;
110: }
111: } catch (IOException exception) {
112: Exceptions.printStackTrace(exception);
113: }
114: if (!isJSF) {
115: result = NbBundle
116: .getMessage(JSFUtils.class,
117: "ERROR_IS_NOT_JSF_API", libFolder
118: .getPath());
119: } else {
120: if (jsfVersion.compareTo(maxVersion) > 0) {
121: result = NbBundle.getMessage(JSFUtils.class,
122: "ERROR_REQUIRED_JSF_VERSION");
123: }
124: }
125: } else {
126: result = NbBundle.getMessage(JSFUtils.class,
127: "ERROR_THERE_IS_NOT_LIB_FOLDER", folder
128: .getPath());
129: }
130: } else {
131: result = NbBundle.getMessage(JSFUtils.class,
132: "ERROR_IS_NOT_VALID_PATH", folder.getPath());
133: }
134: return result;
135: }
136:
137: public static boolean createJSFUserLibrary(File folder,
138: final String libraryName) throws IOException {
139: boolean result = false;
140:
141: // find all jars in the folder/lib
142: File libFolder = new File(folder, LIB_FOLDER);
143: if (libFolder.exists() && libFolder.isDirectory()) {
144: File[] jars = libFolder.listFiles(new FileFilter() {
145: public boolean accept(File pathname) {
146: return pathname.getName().endsWith(".jar"); //NOI18N
147: }
148: });
149:
150: // obtain URLs of the jar file
151: List<URL> urls = new ArrayList<URL>();
152: for (int i = 0; i < jars.length; i++) {
153: URL url = jars[i].toURL();
154: url = FileUtil.getArchiveRoot(url);
155: urls.add(url);
156: }
157:
158: // create new library and regist in the Library Manager.
159: LibraryManager libraryManager = LibraryManager.getDefault();
160: LibraryImplementation libImpl = LibrariesSupport
161: .getLibraryTypeProvider("j2se").createLibrary(); //NOI18N
162: libImpl.setName(libraryName); //NOI18N
163: libImpl.setDescription(libraryName);
164: libImpl.setContent("classpath", urls); //NOI18N
165: libraryManager.addLibrary(LibraryFactory
166: .createLibrary(libImpl));
167:
168: result = true;
169: }
170:
171: return result;
172: }
173:
174: }
|