1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.io.File ; 22 import java.lang.InterruptedException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import javax.jmi.reflect.RefPackage; 28 import junit.textui.TestRunner; 29 import org.netbeans.jmi.javamodel.JavaModelPackage; 30 import org.netbeans.jmi.javamodel.Resource; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.junit.NbTestSuite; 33 import org.netbeans.mdr.NBMDRepositoryImpl; 34 import org.netbeans.mdr.persistence.MOFID; 35 import org.netbeans.modules.javacore.JMManager; 36 import org.netbeans.modules.javacore.classpath.MergedClassPathImplementation; 37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 38 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileStateInvalidException; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.modules.InstalledFileLocator; 43 44 48 public class PreScanTest extends NbTestCase { 49 50 private static final String [] jdks = { 51 "/usr/java/jdk1.5.0_02/" 54 }; 55 56 59 public PreScanTest() { 60 super("PreScanTest"); 61 } 62 63 protected void setUp() { 64 } 65 66 public static NbTestSuite suite() { 67 NbTestSuite suite = new NbTestSuite(PreScanTest.class); 68 return suite; 69 } 70 71 public void testPrebuildStorage() throws FileStateInvalidException, MalformedURLException { 72 for (int i = 0; i < jdks.length; i++) { 73 preparseFile(jdks[i] + "jre/lib/rt.jar", "rt", false); preparseFile(jdks[i] + "src.zip", "src", true); } 76 } 77 78 private void preparseFile(String filename, String type, boolean preparseFeatures) throws FileStateInvalidException, MalformedURLException { 79 File file = new File (filename); 80 FileObject archive = FileUtil.toFileObject(file); 81 FileObject archiveRoot = FileUtil.getArchiveRoot(archive); 82 URL archiveUrl = archiveRoot.getURL(); 83 MergedClassPathImplementation.getDefault().addRoot(archiveUrl); 84 ((JMManager) JMManager.getManager()).waitScanFinished(); 85 String saveToDir = System.getProperty("netbeans.home") + "/mdrstorage/"; 87 NBMDRepositoryImpl impl = (NBMDRepositoryImpl) JavaMetamodel.getDefaultRepository(); 88 if (preparseFeatures) { 89 impl.beginTrans(true); 90 impl.disableEvents(); 91 try { 92 JavaModelPackage javaPckg = JavaMetamodel.getManager().getJavaExtent(archiveRoot); 93 long start = System.currentTimeMillis(); 94 ResourceImpl[] resources = (ResourceImpl[]) javaPckg.getResource().refAllOfClass().toArray(new ResourceImpl[0]); 95 MOFID[] mofids = new MOFID[resources.length]; 96 for (int i = 0; i < resources.length; i++) { 97 mofids[i] = resources[i]._getMofId(); 98 } 99 resources = null; 100 int printed = 0; 101 for (int i = 0; i < mofids.length; i++) { 102 Resource r = (Resource) impl.getByMofId(mofids[i]); 103 r.getImports(); 104 int perCent = i*100/mofids.length; 105 if (perCent > printed) { 106 printed = perCent; 107 System.err.println(printed + "% completed.\r"); 108 } 109 impl.endTrans(); 110 impl.beginTrans(true); 111 impl.disableEvents(); 112 } 113 System.err.println(filename + " contains " + mofids.length + " classes."); 114 System.err.println(filename + " took " + (System.currentTimeMillis() - start) + " ms."); 115 } catch (Exception e) { 116 e.printStackTrace(); 117 } finally { 118 impl.endTrans(); 119 } 120 } 121 impl.beginTrans(true); 122 impl.disableEvents(); 123 try { 124 String hash = JMManager.computePreparsedHash(file, JMManager.BYTES4MD5); 125 String uri = ((JMManager) JMManager.getManager()).getRootURI(archiveRoot); 126 RefPackage extent = (JavaModelPackage) impl.getExtent("codebase:" + uri); 127 impl.renameExtent(extent, "codebase:" + type); 128 JMManager m = (JMManager) JMManager.getManager(); 129 String strName = m.getFileName(m.getValidName(m.getRootURI(archiveRoot))); 130 System.err.print("cp " + strName + ".btd "); 131 System.err.println(saveToDir + type + "-" + hash + ".btd"); 132 System.err.print("cp " + strName + ".btx "); 133 System.err.println(saveToDir + type + "-" + hash + ".btx"); 134 System.err.print("cp " + strName + ".cdx "); 135 System.err.println(saveToDir + type + "-" + hash + ".cdx"); 136 } catch (Exception e) { 137 e.printStackTrace(); 138 } finally { 139 impl.endTrans(); 140 } 141 } 142 143 public static void main(String [] args) { 144 TestRunner.run(suite()); 145 } 146 } 147 | Popular Tags |