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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.websvc.manager.test;
030:
031: import java.io.File;
032: import java.io.FileFilter;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.net.URL;
038: import java.nio.channels.FileChannel;
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.Properties;
042: import java.util.jar.Manifest;
043: import org.netbeans.junit.MockServices;
044: import org.netbeans.modules.websvc.manager.WebServiceManager;
045: import org.openide.DialogDisplayer;
046: import org.openide.filesystems.FileSystem;
047: import org.openide.filesystems.FileUtil;
048: import org.openide.filesystems.LocalFileSystem;
049: import org.openide.filesystems.MultiFileSystem;
050: import org.openide.filesystems.Repository;
051: import org.openide.filesystems.XMLFileSystem;
052: import org.openide.modules.InstalledFileLocator;
053: import org.openide.util.Lookup;
054: import org.openide.util.LookupEvent;
055: import org.openide.util.LookupListener;
056: import org.openide.util.NbCollections;
057:
058: /**
059: *
060: * @author quynguyen
061: */
062: public class SetupUtil {
063: private static final String WORKDIR_SPACES = "user directory";
064: private static final String WORKDIR = "userdirectory";
065: private static final String TEST_WSDL = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
066: private static final String TEST_CATALOG = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/catalog.xml";
067: private static final String CATALOG_FILE = "uszip-asmx-catalog/catalog.xml";
068: private static final String WSDL_FILE = "uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
069:
070: private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
071: private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
072:
073: public static SetupData commonSetUp(File workingDir)
074: throws Exception {
075: SetupData data = new SetupData();
076:
077: File userDir = new File(
078: workingDir.getParentFile(),
079: System.getProperty("os.name").startsWith("Windows") ? WORKDIR_SPACES
080: : WORKDIR);
081: System.getProperties().setProperty("netbeans.user",
082: userDir.getAbsolutePath());
083:
084: File websvcHome = new File(WebServiceManager.WEBSVC_HOME);
085: data.setWebsvcHome(websvcHome);
086:
087: File websvcUserDir = new File(WebServiceManager.WEBSVC_HOME);
088: websvcUserDir.mkdirs();
089:
090: File wsdlFile = new File(websvcUserDir, WSDL_FILE);
091: File catalogFile = new File(websvcUserDir, CATALOG_FILE);
092:
093: retrieveURL(wsdlFile, SetupUtil.class.getResource(TEST_WSDL));
094: retrieveURL(catalogFile, SetupUtil.class
095: .getResource(TEST_CATALOG));
096:
097: copy(wsdlFile, workingDir);
098:
099: data.setLocalWsdlFile(wsdlFile);
100: data.setLocalCatalogFile(catalogFile);
101: data.setLocalOriginalWsdl(new File(workingDir, wsdlFile
102: .getName()));
103:
104: MainFS fs = new MainFS();
105: fs.setConfigRootDir(websvcHome.getParentFile());
106: TestRepository.defaultFileSystem = fs;
107:
108: MockServices.setServices(DialogDisplayerNotifier.class,
109: InstalledFileLocatorImpl.class, TestRepository.class);
110:
111: InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl) Lookup
112: .getDefault().lookup(InstalledFileLocator.class);
113: locator.setUserConfigRoot(websvcHome.getParentFile());
114:
115: File targetBuildProperties = new File(websvcUserDir
116: .getParentFile().getParentFile(), "build.properties");
117: generatePropertiesFile(targetBuildProperties);
118:
119: return data;
120: }
121:
122: public static void commonTearDown() throws Exception {
123: DialogDisplayer dd = DialogDisplayer.getDefault();
124: if (dd instanceof DialogDisplayerNotifier) {
125: ((DialogDisplayerNotifier) dd).removeAllListeners();
126: }
127:
128: MockServices.setServices();
129: }
130:
131: public static void copy(File src, File target) throws Exception {
132: if (src.isFile()) {
133: File targetFile = new File(target, src.getName());
134:
135: FileInputStream is = new FileInputStream(src);
136: FileOutputStream os = new FileOutputStream(targetFile);
137:
138: FileChannel inputChannel = is.getChannel();
139: FileChannel outputChannel = os.getChannel();
140:
141: inputChannel.transferTo(0, inputChannel.size(),
142: outputChannel);
143: inputChannel.close();
144: outputChannel.close();
145: } else {
146: File newDir = new File(target, src.getName());
147: newDir.mkdirs();
148:
149: File[] dirFiles = src.listFiles();
150: if (dirFiles != null) {
151: for (int i = 0; i < dirFiles.length; i++) {
152: copy(dirFiles[i], newDir);
153: }
154: }
155: }
156: }
157:
158: public static void retrieveURL(File targetFile, URL url)
159: throws IOException {
160: targetFile.getParentFile().mkdirs();
161: FileOutputStream fos = new FileOutputStream(targetFile);
162: byte[] readBuffer = new byte[1024];
163:
164: InputStream is = url.openStream();
165: int bytesRead = 0;
166: while ((bytesRead = is.read(readBuffer, 0, 1024)) > 0) {
167: fos.write(readBuffer, 0, bytesRead);
168: }
169: fos.flush();
170: fos.close();
171: is.close();
172: }
173:
174: private static void generatePropertiesFile(File target)
175: throws IOException {
176: String separator = System.getProperty("path.separator");
177: File apiBase = InstalledFileLocator.getDefault().locate(
178: ENDORSED_REF, null, true).getParentFile();
179: File jaxWsBase = apiBase.getParentFile();
180:
181: FileFilter jarFilter = new FileFilter() {
182: public boolean accept(File pathname) {
183: return pathname.isFile()
184: && pathname.getName().endsWith(".jar");
185: }
186: };
187:
188: File[] apiJars = apiBase.listFiles(jarFilter);
189: File[] implJars = jaxWsBase.listFiles(jarFilter);
190:
191: Properties result = new Properties();
192: StringBuffer classpath = new StringBuffer();
193:
194: for (int i = 0; i < apiJars.length; i++) {
195: String pathElement = apiJars[i].getAbsolutePath()
196: + separator;
197: classpath.append(pathElement);
198: }
199:
200: for (int i = 0; i < implJars.length; i++) {
201: classpath.append(implJars[i].getAbsolutePath());
202: if (i != implJars.length - 1) {
203: classpath.append(separator);
204: }
205: }
206:
207: result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
208:
209: FileOutputStream fos = new FileOutputStream(target);
210: result.store(fos, "build.properties file");
211: }
212:
213: public static final class TestRepository extends Repository {
214: static FileSystem defaultFileSystem = null;
215:
216: public TestRepository() {
217: super (defaultFileSystem);
218: }
219: }
220:
221: // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
222: // loaded into the default filesystem (since core/startup is in the classpath
223: // and registers a default Repository that we do not want)
224: public static final class MainFS extends MultiFileSystem implements
225: LookupListener {
226: private final Lookup.Result<FileSystem> ALL = Lookup
227: .getDefault().lookupResult(FileSystem.class);
228: private final FileSystem MEMORY = FileUtil
229: .createMemoryFileSystem();
230: private final XMLFileSystem layers = new XMLFileSystem();
231:
232: private final LocalFileSystem configRoot = new LocalFileSystem();
233:
234: public void setConfigRootDir(File root) throws Exception {
235: configRoot.setRootDirectory(root);
236: }
237:
238: public MainFS() {
239: ALL.addLookupListener(this );
240:
241: List<URL> layerUrls = new ArrayList<URL>();
242: ClassLoader l = Thread.currentThread()
243: .getContextClassLoader();
244: try {
245: for (URL manifest : NbCollections.iterable(l
246: .getResources("META-INF/MANIFEST.MF"))) { // NOI18N
247: InputStream is = manifest.openStream();
248: try {
249: Manifest mani = new Manifest(is);
250: String layerLoc = mani.getMainAttributes()
251: .getValue("OpenIDE-Module-Layer"); // NOI18N
252: if (layerLoc != null) {
253: URL layer = l.getResource(layerLoc);
254: if (layer != null) {
255: layerUrls.add(layer);
256: }
257: }
258: } finally {
259: is.close();
260: }
261: }
262: layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls
263: .size()]));
264: } catch (Exception x) {
265: }
266: resultChanged(null); // run after add listener - see PN1 in #26338
267: }
268:
269: private FileSystem[] computeDelegates() {
270: List<FileSystem> arr = new ArrayList<FileSystem>();
271: arr.add(MEMORY);
272: arr.add(layers);
273: arr.add(configRoot);
274: arr.addAll(ALL.allInstances());
275: return arr.toArray(new FileSystem[0]);
276: }
277:
278: public void resultChanged(LookupEvent ev) {
279: setDelegates(computeDelegates());
280: }
281: }
282:
283: }
|