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-2006 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: package org.openide.filesystems.multifs;
042:
043: import java.io.*;
044: import java.net.URL;
045: import java.net.URLClassLoader;
046: import java.util.List;
047: import java.util.ArrayList;
048: import java.util.Enumeration;
049: import java.util.Map;
050: import java.util.HashMap;
051: import java.util.TreeMap;
052: import java.util.Hashtable;
053: import java.util.Iterator;
054:
055: import org.openide.*;
056: import org.openide.filesystems.*;
057: import org.openide.filesystems.localfs.LocalFSTest;
058: import org.openide.filesystems.xmlfs.XMLFSTest;
059: import org.openide.filesystems.xmlfs.XMLFSTest.ResourceComposer;
060:
061: import org.netbeans.performance.DataManager;
062: import org.netbeans.performance.DataDescriptor;
063:
064: /**
065: * Base class for simulation of module layers. It creates several layers, each filled
066: * with some number of .instance files. Each layer is zipped into one jar.
067: * The jars also contain class files.
068: */
069: public class MultiXMLFSTest extends FSTest implements DataManager {
070:
071: public static final String XMLFS_NO_KEY = "XMLFS_NO";
072: private FileWrapper[] wrappers;
073: private static final String RES_EXT = ".instance";
074: private MultiFileSystem mfs;
075:
076: protected List ddescs;
077:
078: // used for testCreateXMLFS
079: private URL[] resources;
080:
081: private static final String getResource(int base) {
082: return LocalFSTest.getPackage(base).replace('/', '-').concat(
083: LocalFSTest.RES_NAME);
084: }
085:
086: /** Creates new XMLFSGenerator */
087: public MultiXMLFSTest(String name) {
088: super (name);
089: init();
090: }
091:
092: /** Creates new XMLFSGenerator */
093: public MultiXMLFSTest(String name, Object[] args) {
094: super (name, args);
095: init();
096: }
097:
098: /** init */
099: private void init() {
100: ddescs = new ArrayList();
101: }
102:
103: /** Set up given number of FileObjects */
104: public FileObject[] setUpFileObjects(int foCount) throws Exception {
105:
106: int fsCount = getIntValue(XMLFS_NO_KEY);
107: int foChunk = foCount / fsCount;
108: int delta = foCount - (foCount / fsCount) * fsCount;
109:
110: int last = wrappers.length;
111: FileSystem[] fss = new FileSystem[last];
112: int[] bases = new int[last];
113: resources = new URL[last];
114:
115: for (int i = 0; i < last; i++) {
116: if (wrappers[i].isLocal()) {
117: LocalFileSystem lfs = new LocalFileSystem();
118: File mnt = wrappers[i].getMnt();
119: if (mnt == null) {
120: wrappers[i] = createLocal(wrappers[i].getFoCount(),
121: wrappers[i].getFoBase());
122: }
123: lfs.setRootDirectory(wrappers[i].getMnt());
124: fss[i] = lfs;
125: } else {
126: URLClassLoader cloader = new URLClassLoader(
127: new URL[] { wrappers[i].getMnt().toURL() });
128: URL res = cloader.findResource(wrappers[i]
129: .getXResource());
130: resources[i] = res;
131: XMLFileSystem xmlfs = new XMLFileSystem();
132: xmlfs.setXmlUrl(res, false);
133: fss[i] = xmlfs;
134: }
135:
136: if (i > 0) {
137: bases[i] = bases[i - 1] + foChunk;
138: }
139: }
140:
141: FileObject[] ret = new FileObject[foCount];
142: mfs = new MultiFileSystem(fss);
143: for (int i = 0; i < last; i++) {
144: FileObject res = mfs.findResource(LocalFSTest
145: .getPackage(bases[i]));
146: FileObject[] tmp = res.getChildren();
147: int pos = i * foChunk + Math.min(i, 1) * delta;
148: System.arraycopy(tmp, 0, ret, pos, tmp.length);
149: }
150:
151: return ret;
152: }
153:
154: /** Empty */
155: protected void postSetUp() {
156: }
157:
158: /** Creates args for this instance of Benchmark */
159: protected Map[] createArguments() {
160: Map[] map = super .createArguments();
161: Map[] newMap = new Map[map.length * 2];
162:
163: System.arraycopy(map, 0, newMap, 0, map.length);
164:
165: for (int i = map.length; i < newMap.length; i++) {
166: newMap[i] = cloneMap(map[i - map.length]);
167: newMap[i].put(XMLFS_NO_KEY, new Integer(50));
168: }
169:
170: return newMap;
171: }
172:
173: /** Creates a Map with default arguments values */
174: protected Map createDefaultMap() {
175: Map map = super .createDefaultMap();
176: map.put(XMLFS_NO_KEY, new Integer(10));
177: return map;
178: }
179:
180: /** Clones given Map by casting to a cloneable class - HashMap, Hashtable, or TreeMap */
181: private static final Map cloneMap(Map toClone) {
182: if (toClone instanceof HashMap) {
183: return (Map) ((HashMap) toClone).clone();
184: } else if (toClone instanceof Hashtable) {
185: return (Map) ((Hashtable) toClone).clone();
186: } else if (toClone instanceof TreeMap) {
187: return (Map) ((TreeMap) toClone).clone();
188: }
189:
190: return null;
191: }
192:
193: /** @return this mfs */
194: public MultiFileSystem getMultiFileSystem() {
195: return mfs;
196: }
197:
198: /** @return wrappers array */
199: public FileWrapper[] getFileWrappers() {
200: return wrappers;
201: }
202:
203: /** Creates a FileWrapper suitable for mounting a LocalFileSystem */
204: private static FileWrapper createLocal(int foCount, int foBase)
205: throws Exception {
206: File mnt = createTempFolder();
207: LocalFSTest.createFiles(foCount, 0, mnt);
208: return new FileWrapper(mnt, mnt, foCount, foBase, true, null);
209: }
210:
211: /** Creates a FileWrapper suitable for mounting an XMLFileSystem */
212: private static FileWrapper createXMLinJar(int foCount, int foBase)
213: throws Exception {
214: File tmp = createTempFolder();
215: File destFolder = LocalFSTest.createFiles(foCount, foBase, tmp);
216: compileFolder(tmp, destFolder);
217: File xmlbase = XMLFSTest.generateXMLFile(destFolder,
218: new ResourceComposer(getResource(foBase), RES_EXT,
219: foCount, foBase));
220: File jar = Utilities.createJar(tmp, "jarxmlfs.jar");
221: String xres = LocalFSTest.getPackage(foBase)
222: + xmlbase.getName();
223: return new FileWrapper(tmp, jar, foCount, foBase, false, xres);
224: }
225:
226: /** Compiles folder */
227: private static void compileFolder(File root, File destFolder)
228: throws Exception {
229: File[] files = destFolder.listFiles();
230: //StringBuffer sb = new StringBuffer(3000);
231: String[] args = new String[files.length + 3];
232: args[0] = "javac";
233: args[1] = "-classpath";
234: args[2] = System.getProperty("java.class.path");
235:
236: for (int i = 3; i < args.length; i++) {
237: args[i] = files[i - 3].getCanonicalPath();
238: }
239:
240: File stdlog = new File(root, "stdcompilerlog.txt");
241: File errlog = new File(root, "errcompilerlog.txt");
242:
243: PrintStream stdps = new PrintStream(
244: new FileOutputStream(stdlog));
245: PrintStream errps = new PrintStream(
246: new FileOutputStream(errlog));
247:
248: Process p = Runtime.getRuntime().exec(args);
249: CopyMaker cma, cmb;
250: Thread tha = new Thread(cma = new CopyMaker(p.getInputStream(),
251: stdps));
252: tha.start();
253: Thread thb = new Thread(cmb = new CopyMaker(p.getErrorStream(),
254: errps));
255: thb.start();
256:
257: p.waitFor();
258: tha.join();
259: thb.join();
260:
261: stdps.close();
262: errps.close();
263:
264: if (cma.e != null) {
265: throw cma.e;
266: }
267: if (cmb.e != null) {
268: throw cmb.e;
269: }
270: }
271:
272: /** Called after tearDown() */
273: public void tearDownData() throws Exception {
274: for (Iterator it = ddescs.iterator(); it.hasNext();) {
275: MFSDataDescriptor dd = (MFSDataDescriptor) it.next();
276: FileWrapper[] wrappers = dd.getFileWrappers();
277: if (wrappers != null) {
278: for (int i = 0; i < wrappers.length; i++) {
279: delete(wrappers[i].getRootDir());
280: }
281: }
282: }
283: }
284:
285: /** Called before setUp() */
286: public DataDescriptor createDataDescriptor() {
287: return new MFSDataDescriptor(getIntValue(FILE_NO_KEY),
288: getIntValue(XMLFS_NO_KEY));
289: }
290:
291: /** Called before setUp() */
292: public void setUpData(DataDescriptor ddesc) throws Exception {
293: MFSDataDescriptor dd = (MFSDataDescriptor) ddesc;
294: ddescs.add(dd);
295: FileWrapper fwrappers[] = dd.getFileWrappers();
296:
297: if (fwrappers == null) {
298: int foCount = dd.getFoCount();
299: int fsCount = dd.getFsCount();
300: int foChunk = foCount / fsCount;
301: int delta = foCount - (foCount / fsCount) * fsCount;
302: wrappers = new FileWrapper[fsCount];
303: int[] bases = new int[fsCount];
304: for (int i = 1; i < fsCount; i++) {
305: int ibase = i * foChunk;
306: wrappers[i] = createXMLinJar(foChunk, ibase);
307: bases[i] = ibase;
308: }
309:
310: wrappers[0] = createLocal(foChunk + delta, 0);
311: dd.setFileWrappers(wrappers);
312: } else {
313: wrappers = fwrappers;
314: }
315: }
316:
317: // test method
318: public void testCreateXMLFS() throws Exception {
319: int iters = iterations;
320: FileWrapper[] wrappers = this .wrappers;
321: int len = wrappers.length;
322: while (iters-- > 0) {
323: // first is LocalFS
324: for (int i = 1; i < len; i++) {
325: XMLFileSystem xmlfs = new XMLFileSystem();
326: xmlfs.setXmlUrl(resources[i], false);
327: }
328: }
329: }
330:
331: static final class CopyMaker implements Runnable {
332: InputStream is;
333: PrintStream os;
334: Exception e;
335:
336: CopyMaker(InputStream is, PrintStream os) {
337: this .is = is;
338: this .os = os;
339: }
340:
341: public void run() {
342: try {
343: Utilities.copyIS(is, os);
344: } catch (Exception ee) {
345: e = ee;
346: }
347: }
348: }
349:
350: /** Wraps Files */
351: public static final class FileWrapper implements Serializable {
352: private transient File rootDir;
353: private transient File mnt;
354: private int foCount;
355: private int foBase;
356:
357: private boolean isLocal;
358:
359: // xml specific
360: private String xresource;
361:
362: /** New FileWrapper */
363: public FileWrapper(File rootDir, File mnt, int foCount,
364: int foBase, boolean isLocal, String xresource) {
365: this .rootDir = rootDir;
366: this .mnt = mnt;
367: this .foCount = foCount;
368: this .foBase = foBase;
369: this .isLocal = isLocal;
370: this .xresource = xresource;
371: }
372:
373: public File getRootDir() {
374: return rootDir;
375: }
376:
377: public File getMnt() {
378: return mnt;
379: }
380:
381: public int getFoCount() {
382: return foCount;
383: }
384:
385: public int getFoBase() {
386: return foBase;
387: }
388:
389: public boolean isLocal() {
390: return isLocal;
391: }
392:
393: public String getXResource() {
394: return xresource;
395: }
396:
397: private void writeObject(ObjectOutputStream obtos)
398: throws IOException {
399: obtos.defaultWriteObject();
400: if (!isLocal()) {
401: Utilities.writeFile(getMnt(), obtos);
402: }
403: }
404:
405: private void readObject(ObjectInputStream obtis)
406: throws IOException, ClassNotFoundException {
407: obtis.defaultReadObject();
408: if (!isLocal()) {
409: rootDir = createTempFolder();
410: mnt = Utilities.readFile(rootDir, obtis);
411: }
412: }
413: }
414:
415: /*
416: public static void main(String[] args) throws Exception {
417: MultiXMLFSTest mtest = new MultiXMLFSTest("first test");
418: mtest.setUpFileObjects(500);
419: System.out.println("done");
420:
421: System.out.println(mtest.wrappers[1].getClassLoader().loadClass("org.openide.filesystems.data50.JavaSrc55"));
422: }
423: */
424: }
|