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.websvc.manager.util;
043:
044: import java.io.DataInputStream;
045: import java.io.DataOutputStream;
046: import java.io.DataOutputStream;
047: import java.io.File;
048: import java.io.FileInputStream;
049: import java.io.IOException;
050: import java.io.OutputStream;
051: import java.net.URL;
052: import java.util.ArrayList;
053: import java.util.List;
054: import org.netbeans.api.project.libraries.LibraryManager;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.api.project.libraries.Library;
057: import org.netbeans.modules.websvc.manager.api.WebServiceDescriptor;
058: import org.netbeans.modules.websvc.manager.model.WebServiceData;
059: import org.netbeans.modules.websvc.manager.util.ManagerUtil;
060: import org.openide.ErrorManager;
061: import org.openide.filesystems.FileLock;
062: import org.openide.filesystems.FileObject;
063:
064: /**
065: *
066: * @author cao
067: */
068: public class WebServiceLibReferenceHelper {
069:
070: private static final String WEBSERVICE_CLIENTS_SUB_DIR = "webservice_clients"; // NOI18N
071: private static final AddLibrary DESIGN_RUNTIME_LIBRARY_ADD = new AddLibraryToProject();
072:
073: /**
074: * Adds the given jars to the project as archive references
075: *
076: * @param project the project to be added to
077: * @param jars jar files to be added to the project (filename Strings)
078: */
079: public static void addArchiveRefsToProject(Project project,
080: List<String> jars) {
081: try {
082: // Obtain the path to the project's library directory
083: FileObject projectLibDir = ManagerUtil
084: .getProjectLibraryDirectory(project);
085: FileObject wsClientsSubDir = projectLibDir
086: .getFileObject(WEBSERVICE_CLIENTS_SUB_DIR);
087: if (wsClientsSubDir == null)
088: wsClientsSubDir = projectLibDir
089: .createFolder(WEBSERVICE_CLIENTS_SUB_DIR);
090:
091: // Copy over the jar files into the project library directory
092: ArrayList<URL> copiedArchiveJars = new ArrayList<URL>();
093: for (String jarFilePath : jars) {
094: try {
095: String jarFileName = new File(jarFilePath)
096: .getName();
097: FileObject destJar = wsClientsSubDir
098: .getFileObject(jarFileName);
099:
100: if (destJar == null) {
101: destJar = wsClientsSubDir
102: .createData(jarFileName);
103: copyJarFile(jarFilePath, destJar);
104: }
105:
106: copiedArchiveJars.add(new URL(destJar.getURL()
107: .toExternalForm()
108: + "/")); // NOI18N
109: } catch (IOException ex) {
110: ErrorManager.getDefault().notify(ex);
111: }
112: }
113:
114: // add archive references to the project
115: addRefsToProject(project, copiedArchiveJars,
116: new AddArchiveReferences(), new URL[0]);
117: } catch (IOException ioe) {
118: ErrorManager.getDefault().notify(ioe);
119: ioe.printStackTrace();
120: return;
121: }
122: }
123:
124: private static void copyJarFile(String srcPath, FileObject destJar)
125: throws IOException {
126: FileLock fileLock = destJar.lock();
127: try {
128: OutputStream outStream = destJar.getOutputStream(fileLock);
129: DataInputStream in = new DataInputStream(
130: new FileInputStream(new File(srcPath)));
131: DataOutputStream out = new DataOutputStream(outStream);
132:
133: byte[] bytes = new byte[1024];
134: int byteCount = in.read(bytes);
135:
136: while (byteCount > -1) {
137: out.write(bytes, 0, byteCount);
138: byteCount = in.read(bytes);
139: }
140: out.flush();
141: out.close();
142: outStream.close();
143: in.close();
144: } finally {
145: fileLock.releaseLock();
146: }
147: }
148:
149: /**
150: * Adds the given library definitions to the project
151: *
152: * @param project The project to be added to
153: * @param libDefs The libraries to be added to the project.
154: * @param role The role type to add (ClassPath.COMPILE for most cases)
155: */
156: public static void addLibRefsToProject(Project project,
157: List<Library> libraries, String role) {
158: addRefsToProject(project, libraries, new AddLibraryFromRole(
159: role), new Library[0]);
160: }
161:
162: public static void addLibRefsToProject(Project project,
163: List<Library> libraries) {
164: addRefsToProject(project, libraries,
165: DESIGN_RUNTIME_LIBRARY_ADD, new Library[0]);
166: }
167:
168: private static <T> void addRefsToProject(Project project,
169: List<T> libDefs, AddLibrary<T> libraryAdder, T[] arr) {
170: ArrayList<T> libsToAdd = new ArrayList<T>();
171:
172: /**
173: * Go through and check to see which libraries have already been added.
174: */
175: for (T lib : libDefs) {
176: if (!libraryAdder.hasLibraryReference(project, lib)) {
177: libsToAdd.add(lib);
178: }
179: }
180:
181: try {
182: libraryAdder.addLibraryReferences(project, libsToAdd
183: .toArray(arr));
184: } catch (IOException ie) {
185: ErrorManager
186: .getDefault()
187: .getInstance(
188: "org.netbeans.modules.websvc.manager.node.WebServiceLibReferenceHelper")
189: .log(ErrorManager.ERROR,
190: "Failed to add library references to project. IOException");
191: ie.printStackTrace();
192: }
193: }
194:
195: private static interface AddLibrary<T> {
196: public boolean hasLibraryReference(Project project, T library);
197:
198: public void addLibraryReferences(Project project, T[] libraries)
199: throws IOException;
200: }
201:
202: private static class AddLibraryFromRole implements
203: AddLibrary<Library> {
204: private String role;
205:
206: public AddLibraryFromRole(String role) {
207: this .role = role;
208: }
209:
210: public boolean hasLibraryReference(Project project,
211: Library library) {
212: return ManagerUtil.hasLibraryReference(project, library,
213: role);
214: }
215:
216: public void addLibraryReferences(Project project,
217: Library[] libraries) throws IOException {
218: ManagerUtil.addLibraryReferences(project, libraries, role);
219: }
220: }
221:
222: private static class AddArchiveReferences implements
223: AddLibrary<URL> {
224:
225: public AddArchiveReferences() {
226: }
227:
228: public boolean hasLibraryReference(Project project, URL library) {
229: return ManagerUtil.hasRootReference(project, library);
230: }
231:
232: public void addLibraryReferences(Project project,
233: URL[] libraries) throws IOException {
234: ManagerUtil.addRootReferences(project, libraries);
235: }
236:
237: }
238:
239: private static class AddLibraryToProject implements
240: AddLibrary<Library> {
241: public AddLibraryToProject() {
242: }
243:
244: public boolean hasLibraryReference(Project project,
245: Library library) {
246: return ManagerUtil.hasLibraryReference(project, library,
247: null);
248: }
249:
250: public void addLibraryReferences(Project project,
251: Library[] libraries) throws IOException {
252: ManagerUtil.addLibraryReferences(project, libraries);
253: }
254: }
255:
256: public static void addDefaultJaxWsClientJar(Project project,
257: WebServiceData data) {
258: List<String> jars = getDefaultJaxWsClientJars(data);
259: addArchiveRefsToProject(project, jars);
260: }
261:
262: public static List<String> getDefaultJaxWsClientJars(
263: WebServiceData data) {
264: List<String> jarPaths = new ArrayList<String>();
265: File basePath = new File(WebServiceDescriptor.WEBSVC_HOME, data
266: .getJaxWsDescriptorPath()).getParentFile();
267: for (WebServiceDescriptor.JarEntry jar : data
268: .getJaxWsDescriptor().getJars()) {
269: if (jar.getType().equals(
270: WebServiceDescriptor.JarEntry.PROXY_JAR_TYPE)) {
271: File jarPath = new File(basePath, jar.getName());
272: jarPaths.add(jarPath.getAbsolutePath());
273: }
274: }
275: return jarPaths;
276: }
277:
278: }
|