1 19 package org.openide.filesystems.multifs; 20 21 import java.io.*; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.TreeMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 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 47 public class MultiXMLFSTest extends FSTest implements DataManager { 48 49 public static final String XMLFS_NO_KEY = "XMLFS_NO"; 50 private FileWrapper[] wrappers; 51 private static final String RES_EXT = ".instance"; 52 private MultiFileSystem mfs; 53 54 protected List ddescs; 55 56 private URL [] resources; 58 59 private static final String getResource(int base) { 60 return LocalFSTest.getPackage(base).replace('/', '-').concat(LocalFSTest.RES_NAME); 61 } 62 63 64 public MultiXMLFSTest(String name) { 65 super(name); 66 init(); 67 } 68 69 70 public MultiXMLFSTest(String name, Object [] args) { 71 super(name, args); 72 init(); 73 } 74 75 76 private void init() { 77 ddescs = new ArrayList (); 78 } 79 80 81 public FileObject[] setUpFileObjects(int foCount) throws Exception { 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 [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 cloader = new URLClassLoader (new URL [] { wrappers[i].getMnt().toURL() }); 103 URL 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 128 protected void postSetUp() { 129 } 130 131 132 protected Map [] createArguments() { 133 Map [] map = super.createArguments(); 134 Map [] newMap = new Map [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 (50)); 141 } 142 143 return newMap; 144 } 145 146 147 protected Map createDefaultMap() { 148 Map map = super.createDefaultMap(); 149 map.put(XMLFS_NO_KEY, new Integer (10)); 150 return map; 151 } 152 153 154 private static final Map cloneMap(Map toClone) { 155 if (toClone instanceof HashMap ) { 156 return (Map ) ((HashMap ) toClone).clone(); 157 } else if (toClone instanceof Hashtable ) { 158 return (Map ) ((Hashtable ) toClone).clone(); 159 } else if (toClone instanceof TreeMap ) { 160 return (Map ) ((TreeMap ) toClone).clone(); 161 } 162 163 return null; 164 } 165 166 167 public MultiFileSystem getMultiFileSystem() { 168 return mfs; 169 } 170 171 172 public FileWrapper[] getFileWrappers() { 173 return wrappers; 174 } 175 176 177 private static FileWrapper createLocal(int foCount, int foBase) throws Exception { 178 File mnt = createTempFolder(); 179 LocalFSTest.createFiles(foCount, 0, mnt); 180 return new FileWrapper(mnt, mnt, foCount, foBase, true, null); 181 } 182 183 184 private static FileWrapper createXMLinJar(int foCount, int foBase) throws Exception { 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 xres = LocalFSTest.getPackage(foBase) + xmlbase.getName(); 191 return new FileWrapper(tmp, jar, foCount, foBase, false, xres); 192 } 193 194 195 private static void compileFolder(File root, File destFolder) throws Exception { 196 File[] files = destFolder.listFiles(); 197 String [] args = new String [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 p = Runtime.getRuntime().exec(args); 214 CopyMaker cma, cmb; 215 Thread tha = new Thread (cma = new CopyMaker(p.getInputStream(), stdps)); 216 tha.start(); 217 Thread thb = new Thread (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 236 public void tearDownData() throws Exception { 237 for (Iterator 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 249 public DataDescriptor createDataDescriptor() { 250 return new MFSDataDescriptor(getIntValue(FILE_NO_KEY), getIntValue(XMLFS_NO_KEY)); 251 } 252 253 254 public void setUpData(DataDescriptor ddesc) throws Exception { 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 public void testCreateXMLFS() throws Exception { 281 int iters = iterations; 282 FileWrapper[] wrappers = this.wrappers; 283 int len = wrappers.length; 284 while (iters-- > 0) { 285 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 { 294 InputStream is; 295 PrintStream os; 296 Exception 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 ee) { 307 e = ee; 308 } 309 } 310 } 311 312 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 private String xresource; 323 324 325 public FileWrapper(File rootDir, File mnt, int foCount, int foBase, boolean isLocal, String 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 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 { 366 obtis.defaultReadObject(); 367 if (! isLocal()) { 368 rootDir = createTempFolder(); 369 mnt = Utilities.readFile(rootDir, obtis); 370 } 371 } 372 } 373 374 383 } 384 | Popular Tags |