KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > multifs > MultiXMLFSTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.filesystems.multifs;
20
21 import java.io.*;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.openide.*;
34 import org.openide.filesystems.*;
35 import org.openide.filesystems.localfs.LocalFSTest;
36 import org.openide.filesystems.xmlfs.XMLFSTest;
37 import org.openide.filesystems.xmlfs.XMLFSTest.ResourceComposer;
38
39 import org.netbeans.performance.DataManager;
40 import org.netbeans.performance.DataDescriptor;
41
42 /**
43  * Base class for simulation of module layers. It creates several layers, each filled
44  * with some number of .instance files. Each layer is zipped into one jar.
45  * The jars also contain class files.
46  */

47 public class MultiXMLFSTest extends FSTest implements DataManager {
48     
49     public static final String JavaDoc XMLFS_NO_KEY = "XMLFS_NO";
50     private FileWrapper[] wrappers;
51     private static final String JavaDoc RES_EXT = ".instance";
52     private MultiFileSystem mfs;
53     
54     protected List JavaDoc ddescs;
55     
56     // used for testCreateXMLFS
57
private URL JavaDoc[] resources;
58     
59     private static final String JavaDoc getResource(int base) {
60         return LocalFSTest.getPackage(base).replace('/', '-').concat(LocalFSTest.RES_NAME);
61     }
62     
63     /** Creates new XMLFSGenerator */
64     public MultiXMLFSTest(String JavaDoc name) {
65         super(name);
66         init();
67     }
68
69     /** Creates new XMLFSGenerator */
70     public MultiXMLFSTest(String JavaDoc name, Object JavaDoc[] args) {
71         super(name, args);
72         init();
73     }
74     
75     /** init */
76     private void init() {
77         ddescs = new ArrayList JavaDoc();
78     }
79     
80     /** Set up given number of FileObjects */
81     public FileObject[] setUpFileObjects(int foCount) throws Exception JavaDoc {
82         
83         int fsCount = getIntValue(XMLFS_NO_KEY);
84         int foChunk = foCount / fsCount;
85         int delta = foCount - (foCount / fsCount) * fsCount;
86         
87         int last = wrappers.length;
88         FileSystem[] fss = new FileSystem[last];
89         int[] bases = new int[last];
90         resources = new URL JavaDoc[last];
91         
92         for (int i = 0; i < last; i++) {
93             if (wrappers[i].isLocal()) {
94                 LocalFileSystem lfs = new LocalFileSystem();
95                 File mnt = wrappers[i].getMnt();
96                 if (mnt == null) {
97                     wrappers[i] = createLocal(wrappers[i].getFoCount(), wrappers[i].getFoBase());
98                 }
99                 lfs.setRootDirectory(wrappers[i].getMnt());
100                 fss[i] = lfs;
101             } else {
102                 URLClassLoader JavaDoc cloader = new URLClassLoader JavaDoc(new URL JavaDoc[] { wrappers[i].getMnt().toURL() });
103                 URL JavaDoc res = cloader.findResource(wrappers[i].getXResource());
104                 resources[i] = res;
105                 XMLFileSystem xmlfs = new XMLFileSystem();
106                 xmlfs.setXmlUrl(res, false);
107                 fss[i] = xmlfs;
108             }
109             
110             if (i > 0) {
111                 bases[i] = bases[i - 1] + foChunk;
112             }
113         }
114         
115         FileObject[] ret = new FileObject[foCount];
116         mfs = new MultiFileSystem(fss);
117         for (int i = 0; i < last; i++) {
118             FileObject res = mfs.findResource(LocalFSTest.getPackage(bases[i]));
119             FileObject[] tmp = res.getChildren();
120             int pos = i * foChunk + Math.min(i, 1) * delta;
121             System.arraycopy(tmp, 0, ret, pos, tmp.length);
122         }
123         
124         return ret;
125     }
126     
127     /** Empty */
128     protected void postSetUp() {
129     }
130     
131     /** Creates args for this instance of Benchmark */
132     protected Map JavaDoc[] createArguments() {
133         Map JavaDoc[] map = super.createArguments();
134         Map JavaDoc[] newMap = new Map JavaDoc[map.length * 2];
135         
136         System.arraycopy(map, 0, newMap, 0, map.length);
137         
138         for (int i = map.length; i < newMap.length; i++) {
139             newMap[i] = cloneMap(map[i - map.length]);
140             newMap[i].put(XMLFS_NO_KEY, new Integer JavaDoc(50));
141         }
142         
143         return newMap;
144     }
145     
146     /** Creates a Map with default arguments values */
147     protected Map JavaDoc createDefaultMap() {
148         Map JavaDoc map = super.createDefaultMap();
149         map.put(XMLFS_NO_KEY, new Integer JavaDoc(10));
150         return map;
151     }
152     
153     /** Clones given Map by casting to a cloneable class - HashMap, Hashtable, or TreeMap */
154     private static final Map JavaDoc cloneMap(Map JavaDoc toClone) {
155         if (toClone instanceof HashMap JavaDoc) {
156             return (Map JavaDoc) ((HashMap JavaDoc) toClone).clone();
157         } else if (toClone instanceof Hashtable JavaDoc) {
158             return (Map JavaDoc) ((Hashtable JavaDoc) toClone).clone();
159         } else if (toClone instanceof TreeMap JavaDoc) {
160             return (Map JavaDoc) ((TreeMap JavaDoc) toClone).clone();
161         }
162         
163         return null;
164     }
165     
166     /** @return this mfs */
167     public MultiFileSystem getMultiFileSystem() {
168         return mfs;
169     }
170     
171     /** @return wrappers array */
172     public FileWrapper[] getFileWrappers() {
173         return wrappers;
174     }
175
176     /** Creates a FileWrapper suitable for mounting a LocalFileSystem */
177     private static FileWrapper createLocal(int foCount, int foBase) throws Exception JavaDoc {
178         File mnt = createTempFolder();
179         LocalFSTest.createFiles(foCount, 0, mnt);
180         return new FileWrapper(mnt, mnt, foCount, foBase, true, null);
181     }
182     
183     /** Creates a FileWrapper suitable for mounting an XMLFileSystem */
184     private static FileWrapper createXMLinJar(int foCount, int foBase) throws Exception JavaDoc {
185         File tmp = createTempFolder();
186         File destFolder = LocalFSTest.createFiles(foCount, foBase, tmp);
187         compileFolder(tmp, destFolder);
188         File xmlbase = XMLFSTest.generateXMLFile(destFolder, new ResourceComposer(getResource(foBase), RES_EXT, foCount, foBase));
189         File jar = Utilities.createJar(tmp, "jarxmlfs.jar");
190         String JavaDoc xres = LocalFSTest.getPackage(foBase) + xmlbase.getName();
191         return new FileWrapper(tmp, jar, foCount, foBase, false, xres);
192     }
193     
194     /** Compiles folder */
195     private static void compileFolder(File root, File destFolder) throws Exception JavaDoc {
196         File[] files = destFolder.listFiles();
197         //StringBuffer sb = new StringBuffer(3000);
198
String JavaDoc[] args = new String JavaDoc[files.length + 3];
199         args[0] = "javac";
200         args[1] = "-classpath";
201         args[2] = System.getProperty("java.class.path");
202         
203         for (int i = 3; i < args.length; i++) {
204             args[i] = files[i - 3].getCanonicalPath();
205         }
206         
207         File stdlog = new File(root, "stdcompilerlog.txt");
208         File errlog = new File(root, "errcompilerlog.txt");
209         
210         PrintStream stdps = new PrintStream(new FileOutputStream(stdlog));
211         PrintStream errps = new PrintStream(new FileOutputStream(errlog));
212         
213         Process JavaDoc p = Runtime.getRuntime().exec(args);
214         CopyMaker cma, cmb;
215         Thread JavaDoc tha = new Thread JavaDoc(cma = new CopyMaker(p.getInputStream(), stdps));
216         tha.start();
217         Thread JavaDoc thb = new Thread JavaDoc(cmb = new CopyMaker(p.getErrorStream(), errps));
218         thb.start();
219         
220         p.waitFor();
221         tha.join();
222         thb.join();
223         
224         stdps.close();
225         errps.close();
226         
227         if (cma.e != null) {
228             throw cma.e;
229         }
230         if (cmb.e != null) {
231             throw cmb.e;
232         }
233     }
234     
235     /** Called after tearDown() */
236     public void tearDownData() throws Exception JavaDoc {
237         for (Iterator JavaDoc it = ddescs.iterator(); it.hasNext(); ) {
238             MFSDataDescriptor dd = (MFSDataDescriptor) it.next();
239             FileWrapper[] wrappers = dd.getFileWrappers();
240             if (wrappers != null) {
241                 for (int i = 0; i < wrappers.length; i++) {
242                     delete(wrappers[i].getRootDir());
243                 }
244             }
245         }
246     }
247     
248     /** Called before setUp() */
249     public DataDescriptor createDataDescriptor() {
250         return new MFSDataDescriptor(getIntValue(FILE_NO_KEY), getIntValue(XMLFS_NO_KEY));
251     }
252     
253     /** Called before setUp() */
254     public void setUpData(DataDescriptor ddesc) throws Exception JavaDoc {
255         MFSDataDescriptor dd = (MFSDataDescriptor) ddesc;
256         ddescs.add(dd);
257         FileWrapper fwrappers[] = dd.getFileWrappers();
258         
259         if (fwrappers == null) {
260             int foCount = dd.getFoCount();
261             int fsCount = dd.getFsCount();
262             int foChunk = foCount / fsCount;
263             int delta = foCount - (foCount / fsCount) * fsCount;
264             wrappers = new FileWrapper[fsCount];
265             int[] bases = new int[fsCount];
266             for (int i = 1; i < fsCount; i++) {
267                 int ibase = i * foChunk;
268                 wrappers[i] = createXMLinJar(foChunk, ibase);
269                 bases[i] = ibase;
270             }
271
272             wrappers[0] = createLocal(foChunk + delta, 0);
273             dd.setFileWrappers(wrappers);
274         } else {
275             wrappers = fwrappers;
276         }
277     }
278     
279     // test method
280
public void testCreateXMLFS() throws Exception JavaDoc {
281         int iters = iterations;
282         FileWrapper[] wrappers = this.wrappers;
283         int len = wrappers.length;
284         while (iters-- > 0) {
285             // first is LocalFS
286
for (int i = 1; i < len; i++) {
287                 XMLFileSystem xmlfs = new XMLFileSystem();
288                 xmlfs.setXmlUrl(resources[i], false);
289             }
290         }
291     }
292     
293     static final class CopyMaker implements Runnable JavaDoc {
294         InputStream is;
295         PrintStream os;
296         Exception JavaDoc e;
297         
298         CopyMaker(InputStream is, PrintStream os) {
299             this.is = is;
300             this.os = os;
301         }
302         
303         public void run() {
304             try {
305                 Utilities.copyIS(is, os);
306             } catch (Exception JavaDoc ee) {
307                 e = ee;
308             }
309         }
310     }
311     
312     /** Wraps Files */
313     public static final class FileWrapper implements Serializable {
314         private transient File rootDir;
315         private transient File mnt;
316         private int foCount;
317         private int foBase;
318         
319         private boolean isLocal;
320         
321         // xml specific
322
private String JavaDoc xresource;
323         
324         /** New FileWrapper */
325         public FileWrapper(File rootDir, File mnt, int foCount, int foBase, boolean isLocal, String JavaDoc xresource) {
326             this.rootDir = rootDir;
327             this.mnt = mnt;
328             this.foCount = foCount;
329             this.foBase = foBase;
330             this.isLocal = isLocal;
331             this.xresource = xresource;
332         }
333         
334         public File getRootDir() {
335             return rootDir;
336         }
337         
338         public File getMnt() {
339             return mnt;
340         }
341         
342         public int getFoCount() {
343             return foCount;
344         }
345         
346         public int getFoBase() {
347             return foBase;
348         }
349         
350         public boolean isLocal() {
351             return isLocal;
352         }
353         
354         public String JavaDoc getXResource() {
355             return xresource;
356         }
357         
358         private void writeObject(ObjectOutputStream obtos) throws IOException {
359             obtos.defaultWriteObject();
360             if (! isLocal()) {
361                 Utilities.writeFile(getMnt(), obtos);
362             }
363         }
364         
365         private void readObject(ObjectInputStream obtis) throws IOException, ClassNotFoundException JavaDoc {
366             obtis.defaultReadObject();
367             if (! isLocal()) {
368                 rootDir = createTempFolder();
369                 mnt = Utilities.readFile(rootDir, obtis);
370             }
371         }
372     }
373     
374     /*
375     public static void main(String[] args) throws Exception {
376         MultiXMLFSTest mtest = new MultiXMLFSTest("first test");
377         mtest.setUpFileObjects(500);
378         System.out.println("done");
379         
380         System.out.println(mtest.wrappers[1].getClassLoader().loadClass("org.openide.filesystems.data50.JavaSrc55"));
381     }
382      */

383 }
384
Popular Tags