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.libraries.provider;
043:
044: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
045: import org.netbeans.spi.project.libraries.LibraryImplementation;
046: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
047: import org.netbeans.spi.project.support.ant.EditableProperties;
048: import org.netbeans.spi.project.support.ant.PropertyUtils;
049: import org.netbeans.api.project.ProjectManager;
050: import org.openide.ErrorManager;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.filesystems.URLMapper;
053: import org.openide.filesystems.FileObject;
054:
055: import java.beans.Customizer;
056: import java.io.IOException;
057: import java.io.File;
058: import java.util.List;
059: import java.util.Iterator;
060: import java.net.URL;
061: import java.net.URI;
062: import java.net.URISyntaxException;
063: import org.openide.util.Lookup;
064: import org.openide.util.NbBundle;
065:
066: /**
067: *
068: * <lu>
069: * <li><code>parserdb</code> volume type can be used to associate prebuild parserDB
070: * with the library. The resouce in such a case must specify path to .jsc file.</li>
071: * </lu>
072: */
073: public final class ComponentLibraryTypeProvider implements
074: LibraryTypeProvider {
075:
076: private ComponentLibraryTypeProvider() {
077: }
078:
079: private static final String LIB_PREFIX = "libs.";
080: public static final String LIBRARY_TYPE = "complib"; //NOI18N
081: public static final String VOLUME_TYPE_CLASSPATH = "classpath"; //NOI18N
082: public static final String VOLUME_TYPE_SRC = "src"; //NOI18N
083: public static final String VOLUME_TYPE_JAVADOC = "javadoc"; //NOI18N
084: public static final String VOLUME_TYPE_DESIGNTIME = "visual-web-designtime"; //NOI18N
085: public static final String[] VOLUME_TYPES = new String[] {
086: VOLUME_TYPE_CLASSPATH, VOLUME_TYPE_SRC,
087: VOLUME_TYPE_JAVADOC, VOLUME_TYPE_DESIGNTIME, };
088:
089: public String getLibraryType() {
090: return LIBRARY_TYPE;
091: }
092:
093: public String getDisplayName() {
094: return NbBundle.getMessage(ComponentLibraryTypeProvider.class,
095: "TXT_ComponentLibraryType");
096: }
097:
098: public String[] getSupportedVolumeTypes() {
099: return VOLUME_TYPES;
100: }
101:
102: public LibraryImplementation createLibrary() {
103: return LibrariesSupport.createLibraryImplementation(
104: LIBRARY_TYPE, VOLUME_TYPES);
105: }
106:
107: public void libraryCreated(final LibraryImplementation libraryImpl) {
108: assert libraryImpl != null;
109: ProjectManager.mutex().postWriteRequest(new Runnable() {
110: public void run() {
111: try {
112: EditableProperties props = PropertyUtils
113: .getGlobalProperties();
114: boolean save = addLibraryIntoBuild(libraryImpl,
115: props);
116: if (save) {
117: PropertyUtils.putGlobalProperties(props);
118: }
119: } catch (IOException ioe) {
120: ErrorManager.getDefault().notify(ioe);
121: }
122: }
123: });
124: }
125:
126: public void libraryDeleted(final LibraryImplementation libraryImpl) {
127: assert libraryImpl != null;
128: ProjectManager.mutex().postWriteRequest(new Runnable() {
129: public void run() {
130: try {
131: EditableProperties props = PropertyUtils
132: .getGlobalProperties();
133: for (int i = 0; i < VOLUME_TYPES.length; i++) {
134: String property = LIB_PREFIX
135: + libraryImpl.getName() + '.'
136: + VOLUME_TYPES[i]; //NOI18N
137: props.remove(property);
138: }
139: PropertyUtils.putGlobalProperties(props);
140: } catch (IOException ioe) {
141: ErrorManager.getDefault().notify(ioe);
142: }
143: }
144: });
145: }
146:
147: public Customizer getCustomizer(String volumeType) {
148: if (VOLUME_TYPES[0].equals(volumeType)
149: || VOLUME_TYPES[1].equals(volumeType)
150: || VOLUME_TYPES[2].equals(volumeType)
151: || VOLUME_TYPES[3].equals(volumeType)) {
152: return new ComponentVolumeCustomizer(volumeType);
153: } else {
154: return null;
155: }
156: }
157:
158: public Lookup getLookup() {
159: return Lookup.EMPTY;
160: }
161:
162: public static LibraryTypeProvider create() {
163: return new ComponentLibraryTypeProvider();
164: }
165:
166: private static boolean addLibraryIntoBuild(
167: LibraryImplementation impl, EditableProperties props) {
168: boolean modified = false;
169: for (int i = 0; i < VOLUME_TYPES.length; i++) {
170: String propName = LIB_PREFIX + impl.getName() + '.'
171: + VOLUME_TYPES[i]; //NOI18N
172: List roots = impl.getContent(VOLUME_TYPES[i]);
173: if (roots == null) {
174: //Non valid library, but try to recover
175: continue;
176: }
177: StringBuffer propValue = new StringBuffer();
178: boolean first = true;
179: for (Iterator rootsIt = roots.iterator(); rootsIt.hasNext();) {
180: URL url = (URL) rootsIt.next();
181: if ("jar".equals(url.getProtocol())) {
182: url = FileUtil.getArchiveFile(url);
183: // XXX check whether this is really the root
184: }
185: File f = null;
186: FileObject fo = URLMapper.findFileObject(url);
187: if (fo != null) {
188: f = FileUtil.toFile(fo);
189: } else if ("file".equals(url.getProtocol())) { //NOI18N
190: //If the file does not exist (eg library from cleaned project)
191: // and it is a file protocol URL, add it.
192: URI uri = URI.create(url.toExternalForm());
193: if (uri != null) {
194: f = new File(uri);
195: }
196: }
197: if (f != null) {
198: if (!first) {
199: propValue.append(File.pathSeparatorChar);
200: }
201: first = false;
202: f = FileUtil.normalizeFile(f);
203: propValue.append(f.getAbsolutePath());
204: } else {
205: ErrorManager.getDefault().log(
206: "J2SELibraryTypeProvider: Can not resolve URL: "
207: + url);
208: }
209: }
210: String oldValue = props.getProperty(propName);
211: String newValue = propValue.toString();
212: if (!newValue.equals(oldValue)) {
213: if (newValue.length() > 0) {
214: props.setProperty(propName, newValue);
215: modified = true;
216: } else if (oldValue != null) {
217: props.remove(propName);
218: modified = true;
219: }
220: }
221: }
222: return modified;
223: }
224:
225: }
|