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 * If you wish your version of this file to be governed by only the CDDL
025 * or only the GPL Version 2, indicate your decision by adding
026 * "[Contributor] elects to include this software in this distribution
027 * under the [CDDL or GPL Version 2] license." If you do not indicate a
028 * single choice of license, a recipient has the option to distribute
029 * your version of this file under either the CDDL, the GPL Version 2 or
030 * to extend the choice of license to its licensees as provided above.
031 * However, if you add GPL Version 2 code and therefore, elected the GPL
032 * Version 2 license, then the option applies only if the new code is
033 * made subject to such option by the copyright holder.
034 *
035 * Contributor(s):
036 *
037 * Portions Copyrighted 2008 Sun Microsystems, Inc.
038 */
039
040 package org.netbeans.modules.j2ee.deployment.impl.sharability;
041
042 import java.beans.Customizer;
043 import java.io.File;
044 import java.io.IOException;
045 import java.net.URI;
046 import java.net.URL;
047 import java.util.Iterator;
048 import java.util.List;
049 import org.netbeans.api.project.ProjectManager;
050 import org.netbeans.spi.project.libraries.LibraryImplementation;
051 import org.netbeans.spi.project.libraries.LibraryTypeProvider;
052 import org.netbeans.spi.project.libraries.support.LibrariesSupport;
053 import org.netbeans.spi.project.support.ant.EditableProperties;
054 import org.netbeans.spi.project.support.ant.PropertyUtils;
055 import org.openide.ErrorManager;
056 import org.openide.filesystems.FileObject;
057 import org.openide.filesystems.FileUtil;
058 import org.openide.filesystems.URLMapper;
059 import org.openide.util.Lookup;
060 import org.openide.util.NbBundle;
061
062 /**
063 *
064 * @author Petr Hejl
065 */
066 public final class ServerLibraryTypeProvider implements
067 LibraryTypeProvider {
068
069 public static final String LIBRARY_TYPE = "j2eeshared"; // NOI18N
070
071 public static final String VOLUME_CLASSPATH = "classpath";
072
073 public static final String VOLUME_WS_COMPILE_CLASSPATH = "wscompile";
074
075 public static final String VOLUME_WS_GENERATE_CLASSPATH = "wsgenerate";
076
077 public static final String VOLUME_WS_IMPORT_CLASSPATH = "wsimport";
078
079 public static final String VOLUME_WS_INTEROP_CLASSPATH = "wsinterop";
080
081 public static final String VOLUME_WS_JWSDP_CLASSPATH = "wsjwsdp";
082
083 public static final String VOLUME_JAVADOC = "javadoc";
084
085 public static final String VOLUME_SOURCE = "src";
086
087 // This is runtime only
088 //public static final String VOLUME_APP_CLIENT_CLASSPATH = "appclient";
089
090 private static final String LIB_PREFIX = "libs.";
091
092 private static final String[] VOLUME_TYPES = new String[] {
093 VOLUME_CLASSPATH, VOLUME_WS_COMPILE_CLASSPATH,
094 VOLUME_WS_GENERATE_CLASSPATH, VOLUME_WS_IMPORT_CLASSPATH,
095 VOLUME_WS_INTEROP_CLASSPATH, VOLUME_WS_JWSDP_CLASSPATH,
096 VOLUME_JAVADOC, VOLUME_SOURCE };
097
098 private ServerLibraryTypeProvider() {
099 super ();
100 }
101
102 public static LibraryTypeProvider create() {
103 return new ServerLibraryTypeProvider();
104 }
105
106 public LibraryImplementation createLibrary() {
107 return LibrariesSupport.createLibraryImplementation(
108 LIBRARY_TYPE, VOLUME_TYPES);
109 }
110
111 public Customizer getCustomizer(String volumeType) {
112 // avoid user confusion
113 if (VOLUME_CLASSPATH.equals(volumeType)
114 || VOLUME_JAVADOC.equals(volumeType)
115 || VOLUME_SOURCE.equals(volumeType)) {
116 return new ServerVolumeCustomizer(volumeType);
117 }
118 return null;
119 }
120
121 public String getDisplayName() {
122 return NbBundle.getMessage(ServerLibraryTypeProvider.class,
123 "ServerLibraryTypeProvider.typeName");
124 }
125
126 public String getLibraryType() {
127 return LIBRARY_TYPE;
128 }
129
130 public String[] getSupportedVolumeTypes() {
131 return VOLUME_TYPES.clone();
132 }
133
134 // XXX copied from j2se
135 public void libraryCreated(final LibraryImplementation libraryImpl) {
136 assert libraryImpl != null;
137 ProjectManager.mutex().postWriteRequest(new Runnable() {
138 public void run() {
139 try {
140 EditableProperties props = PropertyUtils
141 .getGlobalProperties();
142 boolean save = addLibraryIntoBuild(libraryImpl,
143 props);
144 if (save) {
145 PropertyUtils.putGlobalProperties(props);
146 }
147 } catch (IOException ioe) {
148 ErrorManager.getDefault().notify(ioe);
149 }
150 }
151 });
152 }
153
154 // XXX copied from j2se
155 public void libraryDeleted(final LibraryImplementation libraryImpl) {
156 assert libraryImpl != null;
157 ProjectManager.mutex().postWriteRequest(new Runnable() {
158 public void run() {
159 try {
160 EditableProperties props = PropertyUtils
161 .getGlobalProperties();
162 for (int i = 0; i < VOLUME_TYPES.length; i++) {
163 String property = LIB_PREFIX
164 + libraryImpl.getName() + '.'
165 + VOLUME_TYPES[i]; //NOI18N
166 props.remove(property);
167 }
168 PropertyUtils.putGlobalProperties(props);
169 } catch (IOException ioe) {
170 ErrorManager.getDefault().notify(ioe);
171 }
172 }
173 });
174 }
175
176 public Lookup getLookup() {
177 return Lookup.EMPTY;
178 }
179
180 // XXX copied from j2se
181 private static boolean addLibraryIntoBuild(
182 LibraryImplementation impl, EditableProperties props) {
183 boolean modified = false;
184 for (int i = 0; i < VOLUME_TYPES.length; i++) {
185 String propName = LIB_PREFIX + impl.getName() + '.'
186 + VOLUME_TYPES[i]; //NOI18N
187 List roots = impl.getContent(VOLUME_TYPES[i]);
188 if (roots == null) {
189 //Non valid library, but try to recover
190 continue;
191 }
192 StringBuffer propValue = new StringBuffer();
193 boolean first = true;
194 for (Iterator rootsIt = roots.iterator(); rootsIt.hasNext();) {
195 URL url = (URL) rootsIt.next();
196 if ("jar".equals(url.getProtocol())) {
197 url = FileUtil.getArchiveFile(url);
198 // XXX check whether this is really the root
199 }
200 File f = null;
201 FileObject fo = URLMapper.findFileObject(url);
202 if (fo != null) {
203 f = FileUtil.toFile(fo);
204 } else if ("file".equals(url.getProtocol())) { //NOI18N
205 //If the file does not exist (eg library from cleaned project)
206 // and it is a file protocol URL, add it.
207 URI uri = URI.create(url.toExternalForm());
208 if (uri != null) {
209 f = new File(uri);
210 }
211 }
212 if (f != null) {
213 if (!first) {
214 propValue.append(File.pathSeparatorChar);
215 }
216 first = false;
217 f = FileUtil.normalizeFile(f);
218 propValue.append(f.getAbsolutePath());
219 } else {
220 ErrorManager.getDefault().log(
221 "ServerLibraryTypeProvider: Can not resolve URL: "
222 + url); //NOI18N
223 }
224 }
225 String oldValue = props.getProperty(propName);
226 String newValue = propValue.toString();
227 if (!newValue.equals(oldValue)) {
228 props.setProperty(propName, newValue);
229 modified = true;
230 }
231 }
232 return modified;
233 }
234 }
|