1 19 20 package org.netbeans.lib.java.storagebuilder; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.nio.channels.FileChannel ; 30 import java.util.ArrayList ; 31 import javax.jmi.reflect.RefPackage; 32 import org.netbeans.api.mdr.MDRManager; 33 import org.netbeans.api.mdr.MDRepository; 34 import org.netbeans.jmi.javamodel.JavaModelPackage; 35 import org.netbeans.jmi.javamodel.Resource; 36 import org.netbeans.mdr.NBMDRepositoryImpl; 37 import org.netbeans.modules.javacore.classpath.MergedClassPathImplementation; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.netbeans.modules.javacore.JMManager; 41 import org.netbeans.modules.javacore.CodebasesResolver; 42 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 43 import org.openide.filesystems.FileStateInvalidException; 44 45 public class Main { 46 47 static String [] names = new String [0]; 48 49 static { 50 System.setProperty("org.netbeans.javacore.eagerlyParse", ""); String s = System.getProperty("preparse.files", ""); 53 if (!"".equals(s)) { 54 File f = new File (s); 55 StringBuffer classes = new StringBuffer (1024); 56 FileInputStream is = null; 57 try { 58 is = new FileInputStream (f); 59 byte[] c = new byte[1024]; 60 int bytesread; 61 while ((bytesread=is.read(c)) > 0) { 62 classes.append(new String (c,0,bytesread)); 63 } 64 names = classes.toString().split(":"); } catch (FileNotFoundException e) { 66 System.err.println("File does not exist."); 67 System.err.println(e); 68 } catch (IOException e) { 69 System.err.println("File does not exist."); 70 System.err.println(e); 71 } finally { 72 try { 73 if (is != null) { 74 is.close(); 75 } 76 } catch (IOException e) { 77 } 79 } 80 } 81 } 82 83 private static JavaModelPackage javaPckg = null; 84 85 public static final int OK = 0; 86 public static final int FAILED = 1; 87 88 93 public static int prebuildJDKStorages(String [] jdks, String destDir) { 94 int result; 95 File dir; 96 97 ArrayList jarsList = new ArrayList (jdks.length * 2); 98 ArrayList resNamesList = new ArrayList (jdks.length * 2); 99 for (int i = 0; i < jdks.length; i++) { 100 if (jdks[i].endsWith(".jar") || jdks[i].endsWith(".zip")) { jarsList.add(jdks[i]); 102 resNamesList.add(new String [0]); 103 } else { 104 dir = new File (jdks[i]); 105 if (!dir.exists()) { 106 System.out.println("JDK home directory not found: " + jdks[i]); return FAILED; 108 } 109 if (!jdks[i].endsWith(File.separator)) { 110 jdks[i] = jdks[i] + File.separator; 111 } 112 jarsList.add(jdks[i] + "jre" + File.separator + "lib" + File.separator + "rt.jar"); String resNames[] = new String [names.length]; 114 for (int j = 0; j < names.length; j++) { 115 resNames[j] = names[j].concat(".class"); } 117 resNamesList.add(resNames); 118 jarsList.add(jdks[i] + "src.zip"); resNames = new String [names.length]; 120 for (int j = 0; j < names.length; j++) { 121 resNames[j] = names[j].concat(".java"); } 123 resNamesList.add(resNames); 124 } 125 } 126 String jars[] = (String []) jarsList.toArray(new String [jarsList.size()]); 127 String resNames[][] = (String [][]) resNamesList.toArray(new String [jarsList.size()][]); 128 return prebuildJars(jars, new boolean[jars.length], resNames, destDir); 129 } 130 131 139 public static int prebuildJars(String [] jars, boolean[] deepParse, String [][] names, String destDir) { 140 int result; 141 File dir; 142 143 dir = new File (destDir); 144 if (!dir.exists()) { 145 System.out.println("Target directory does not exist: " + destDir); return FAILED; 147 } 148 String storagesDir = System.getProperty("mdr.filename"); if (storagesDir == null) { 150 System.out.println("mdr.filename property not specified"); return FAILED; 152 } 153 int index = storagesDir.lastIndexOf(File.separator); 154 if (index > 0) { 155 String dirName = storagesDir.substring(0,index); 156 dir = new File (dirName); 157 if (!dir.exists()) { 158 System.out.println("mdr storage directory does not exist: " + dirName); return FAILED; 160 } 161 } 162 for (int x = 0; x < jars.length; x++) { 163 dir = new File (jars[x]); 164 if (!dir.exists()) { 165 System.out.println("Jar not found: " + jars[x]); return FAILED; 167 } 168 } 169 170 if (!destDir.endsWith(File.separator)) { 171 destDir = destDir + File.separator; 172 } 173 try { 174 String jarSimpleNames[] = new String [jars.length]; 175 for (int i = 0; i < jars.length; i++) { 176 int pos = jars[i].lastIndexOf(File.separator); 177 jarSimpleNames[i] = pos >= 0 ? jars[i].substring(pos + 1) : jars[i]; 178 pos = jarSimpleNames[i].lastIndexOf('.'); 179 if (pos >= 0) jarSimpleNames[i] = jarSimpleNames[i].substring(0, pos); 180 result = preparseFile(jars[i], jarSimpleNames[i], deepParse[i]); 181 if (result != OK) 182 return result; 183 if (names[i].length > 0 && javaPckg != null) { 184 preparseResources(names[i]); 185 } 186 } 187 MDRepository repository = MDRManager.getDefault().getRepository("org.netbeans.java"); repository.shutdown(); 189 for (int i = 0; i < jars.length; i++) { 190 result = saveFile(jars[i], jarSimpleNames[i], destDir); if (result != OK) 192 return result; 193 } 194 } catch (FileStateInvalidException e) { 195 System.out.println("process failed: " + e.getMessage()); System.out.flush(); 197 return FAILED; 198 } catch (MalformedURLException e) { 199 System.out.println("process failed: " + e.getMessage()); System.out.flush(); 201 return FAILED; 202 } 203 return OK; 204 } 205 206 private static int preparseFile(String filename, String type, boolean preparseFeatures) throws FileStateInvalidException, MalformedURLException { 207 System.out.println("preparsing " + filename); File file = new File (filename); 209 FileObject archive = FileUtil.toFileObject(file); 210 if (archive == null) { 211 System.out.println("file not found: " + filename); return FAILED; 213 } 214 FileObject archiveRoot = FileUtil.getArchiveRoot(archive); 215 URL archiveUrl = archiveRoot.getURL(); 216 MergedClassPathImplementation.getDefault().addRoot(archiveUrl); 217 CodebasesResolver.resolve(); 218 NBMDRepositoryImpl impl = (NBMDRepositoryImpl) JavaMetamodel.getDefaultRepository(); 220 javaPckg = JavaMetamodel.getManager().getJavaExtent(archiveRoot); 221 if (preparseFeatures) { 222 Resource[] resources = (Resource[]) javaPckg.getResource().refAllOfClass().toArray(new Resource[0]); 223 int p = 0, closed = 0; 224 impl.beginTrans(true); 225 impl.disableEvents(); 226 try { 227 for (int i = 0; i < resources.length; i++) { 228 resources[i].getImports(); 229 resources[i] = null; 230 int newP = i * 100 / resources.length; 231 long free = Runtime.getRuntime().freeMemory(); 232 long total = Runtime.getRuntime().totalMemory(); 233 if (free < (total / 4)) { 234 impl.endTrans(); 235 System.gc(); 236 impl.beginTrans(true); 237 impl.disableEvents(); 238 closed = i; 239 } 240 if (newP != p) { 241 if (newP % 10 == 0) { 242 System.out.println(newP + "%"); System.out.flush(); 244 } 245 p = newP; 246 } 247 } 248 System.out.println("100%"); System.out.flush(); 250 } catch (Exception e) { 251 System.out.println(" ... failed: " + e.getMessage()); System.out.flush(); 253 return FAILED; 254 } finally { 255 impl.endTrans(); 256 } 257 } 258 System.out.println(" ... done"); String uri = ((JMManager) JMManager.getManager()).getRootURI(archiveRoot); 260 RefPackage extent = (JavaModelPackage)impl.getExtent("codebase:" + uri); if ((JavaModelPackage)impl.getExtent("codebase:" + type) != null) { System.out.println("Cannot rename extent, there is already one of the same name."); System.out.println("It seems that storage files already existed in the temp mdr directory (specified by mdr.filename property)."); return FAILED; 265 } 266 impl.renameExtent(extent, "codebase:" + type); return OK; 268 } 269 270 private static int saveFile(String filename, String type, String saveToDir) throws FileStateInvalidException, MalformedURLException { 271 System.out.print("copying storages for " + filename); File file = new File (filename); 273 FileObject archive = FileUtil.toFileObject(file); 274 FileObject archiveRoot = FileUtil.getArchiveRoot(archive); 275 URL archiveUrl = archiveRoot.getURL(); 276 NBMDRepositoryImpl impl = (NBMDRepositoryImpl) JavaMetamodel.getDefaultRepository(); 277 try { 278 String hash = JMManager.computePreparsedHash(file, JMManager.BYTES4MD5); 279 String uri = ((JMManager) JMManager.getManager()).getRootURI(archiveRoot); 280 JMManager m = (JMManager) JMManager.getManager(); 281 String strName = m.getFileName(m.getValidName(m.getRootURI(archiveRoot))); 282 if (!JMManager.copyStorage(strName, saveToDir + type + "-" + hash, true)) { return FAILED; 284 } 285 System.out.println(" ... done"); } catch (Exception e) { 287 System.out.println(" ... failed: " + e.getMessage()); return FAILED; 289 } 290 return OK; 291 } 292 293 private static int preparseResources(String [] names) { 294 NBMDRepositoryImpl impl = (NBMDRepositoryImpl) JavaMetamodel.getDefaultRepository(); 295 for (int i = 0; i < names.length; i++) { 296 impl.beginTrans(true); 297 impl.disableEvents(); 298 try { 299 Resource r = javaPckg.getResource().resolveResource(names[i], false); 300 if (r == null) { 301 System.out.println("null for name: '" + names[i] + "'."); } else { 303 System.out.println("name: '" + r.getName() + "'."); r.getImports(); 305 } 306 } catch (Exception e) { 307 System.out.println(" ... failed: " + e.getMessage()); System.out.flush(); 309 return FAILED; 310 } finally { 311 impl.endTrans(); 312 } 313 } 314 return OK; 315 } 316 317 public static void main(String [] args) throws IOException { 318 String [] jdks = new String [args.length - 1]; 319 for (int x = 1; x < args.length; x++) { 320 jdks[x - 1] = args[x]; 321 } 322 System.exit(prebuildJDKStorages(jdks, args[0])); 323 } 324 325 } 326 | Popular Tags |