KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > PreScanTest


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.netbeans.jmi.javamodel.codegen;
20
21 import java.io.File JavaDoc;
22 import java.lang.InterruptedException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
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 /**
45  *
46  * @author Pavel Flaska
47  */

48 public class PreScanTest extends NbTestCase {
49
50     private static final String JavaDoc[] jdks = {
51         // "/usr/java/j2sdk1.4.2_04/",
52
// "/usr/java/j2sdk1.4.2_05/",
53
"/usr/java/jdk1.5.0_02/"
54     };
55
56     /**
57      * Creates a new instance of PreScanTest
58      */

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 JavaDoc {
72         for (int i = 0; i < jdks.length; i++) {
73             preparseFile(jdks[i] + "jre/lib/rt.jar", "rt", false); // NOI18N
74
preparseFile(jdks[i] + "src.zip", "src", true); // NOI18N
75
}
76     }
77     
78     private void preparseFile(String JavaDoc filename, String JavaDoc type, boolean preparseFeatures) throws FileStateInvalidException, MalformedURLException JavaDoc {
79         File JavaDoc file = new File JavaDoc(filename);
80         FileObject archive = FileUtil.toFileObject(file);
81         FileObject archiveRoot = FileUtil.getArchiveRoot(archive);
82         URL JavaDoc archiveUrl = archiveRoot.getURL();
83         MergedClassPathImplementation.getDefault().addRoot(archiveUrl);
84         ((JMManager) JMManager.getManager()).waitScanFinished();
85         // pre-parse
86
String JavaDoc 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 JavaDoc e) {
116                 e.printStackTrace();
117             } finally {
118                 impl.endTrans();
119             }
120         }
121         impl.beginTrans(true);
122         impl.disableEvents();
123         try {
124             String JavaDoc hash = JMManager.computePreparsedHash(file, JMManager.BYTES4MD5);
125             String JavaDoc 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 JavaDoc 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 JavaDoc e) {
137             e.printStackTrace();
138         } finally {
139             impl.endTrans();
140         }
141     }
142     
143     public static void main(String JavaDoc[] args) {
144         TestRunner.run(suite());
145     }
146 }
147
Popular Tags