1 19 20 package org.netbeans; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.lang.reflect.Method ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.security.AllPermission ; 29 import java.security.CodeSource ; 30 import java.security.PermissionCollection ; 31 import java.security.Permissions ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Enumeration ; 35 import java.util.HashSet ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.StringTokenizer ; 39 import java.util.jar.JarFile ; 40 import org.openide.util.Lookup; 41 import org.openide.util.Union2; 42 import org.openide.util.lookup.Lookups; 43 44 47 final class MainImpl extends Object { 48 49 53 public static void main (String args[]) throws Exception { 54 java.lang.reflect.Method [] m = new java.lang.reflect.Method [1]; 55 int res = execute (args, System.in, System.out, System.err, m); 56 if (res == -1) { 57 return; 59 } else if (res != 0) { 60 System.exit(res); 62 } 63 64 m[0].invoke (null, new Object [] { args }); 65 } 66 67 72 public static String usage () throws Exception { 73 java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream (); 74 java.io.ByteArrayOutputStream err = new java.io.ByteArrayOutputStream (); 75 76 String [] newArgs = { "--help" }; 77 78 execute(newArgs, System.in, os, err, null); 79 return new String (os.toByteArray ()); 80 } 81 82 91 static int execute ( 92 String [] args, 93 java.io.InputStream reader, 94 java.io.OutputStream writer, 95 java.io.OutputStream error, 96 java.lang.reflect.Method [] methodToCall 97 ) throws Exception { 98 new URLConnection (MainImpl.class.getResource("Main.class")) { public void connect() throws IOException {} 103 }.setDefaultUseCaches(false); 104 105 ArrayList <Union2<File ,JarFile >> list = new ArrayList <Union2<File ,JarFile >>(); 106 107 HashSet <File > processedDirs = new HashSet <File > (); 108 String home = System.getProperty ("netbeans.home"); if (home != null) { 110 build_cp (new File (home), list, processedDirs); 111 } 112 String nbdirs = System.getProperty("netbeans.dirs"); if (nbdirs != null) { 115 StringTokenizer tok = new StringTokenizer (nbdirs, File.pathSeparator); 116 while (tok.hasMoreTokens()) { 117 build_cp(new File (tok.nextToken()), list, processedDirs); 119 } 120 } 121 122 String prepend = System.getProperty("netbeans.classpath"); if (prepend != null) { 127 StringTokenizer tok = new StringTokenizer (prepend, File.pathSeparator); 128 while (tok.hasMoreElements()) { 129 File f = new File (tok.nextToken()); 130 list.add(0, f.isDirectory() ? 131 Union2.<File ,JarFile >createFirst(f) : 132 Union2.<File ,JarFile >createSecond(new JarFile (f, false))); 133 } 134 } 135 136 StringBuffer buf = new StringBuffer (1000); 138 for (Union2<File ,JarFile > o : list) { 139 String f = o.hasFirst() ? o.first().getAbsolutePath() : o.second().getName(); 140 if (buf.length() > 0) { 141 buf.append(File.pathSeparatorChar); 142 } 143 buf.append(f); 144 } 145 System.setProperty("netbeans.dynamic.classpath", buf.toString()); 146 147 BootClassLoader loader = new BootClassLoader(list, new ClassLoader [] { 148 MainImpl.class.getClassLoader() 149 }); 150 151 Thread.currentThread().setContextClassLoader (loader); 155 156 157 161 CLIHandler.Status result; 162 result = CLIHandler.initialize(args, reader, writer, error, loader, true, false, loader); 163 if (result.getExitCode () == CLIHandler.Status.CANNOT_CONNECT) { 164 int value = javax.swing.JOptionPane.showConfirmDialog ( 165 null, 166 java.util.ResourceBundle.getBundle("org/netbeans/Bundle").getString("MSG_AlreadyRunning"), 167 java.util.ResourceBundle.getBundle("org/netbeans/Bundle").getString("MSG_AlreadyRunningTitle"), 168 javax.swing.JOptionPane.OK_CANCEL_OPTION, 169 javax.swing.JOptionPane.WARNING_MESSAGE 170 ); 171 if (value == javax.swing.JOptionPane.OK_OPTION) { 172 result = CLIHandler.initialize(args, reader, writer, error, loader, true, true, loader); 173 } 174 175 } 176 177 String className = System.getProperty( 178 "netbeans.mainclass", "org.netbeans.core.startup.Main" ); 180 181 Class <?> c = loader.loadClass(className); 182 Method m = c.getMethod ("main", String [].class); 184 if (methodToCall != null) { 185 methodToCall[0] = m; 186 } 187 188 return result.getExitCode (); 189 } 190 191 195 public static void finishInitialization() { 196 int r = CLIHandler.finishInitialization (false); 197 if (r != 0) { 198 System.err.println ("Post-initialization command-line options could not be run."); } 202 } 203 204 static final class BootClassLoader extends JarClassLoader 205 implements Runnable { 206 private Lookup metaInf; 207 208 private List <CLIHandler> handlers; 209 210 public BootClassLoader(List <Union2<File ,JarFile >> cp, ClassLoader [] parents) { 211 super(cp, parents); 212 213 metaInf = Lookups.metaInfServices(this); 214 215 String value = null; 216 try { 217 if (cp.isEmpty ()) { 218 value = searchBuildNumber(this.getResources("META-INF/MANIFEST.MF")); 219 } else { 220 value = searchBuildNumber(this.simpleFindResources("META-INF/MANIFEST.MF")); 221 } 222 } catch (IOException ex) { 223 ex.printStackTrace(); 224 } 225 226 if (value == null) { 227 System.err.println("Cannot set netbeans.buildnumber property no OpenIDE-Module-Implementation-Version found"); } else { 229 System.setProperty ("netbeans.buildnumber", value); } 231 } 232 233 234 private static String searchBuildNumber(Enumeration <URL > en) { 235 String value = null; 236 try { 237 java.util.jar.Manifest mf; 238 URL u = null; 239 while(en.hasMoreElements()) { 240 u = en.nextElement(); 241 InputStream is = u.openStream(); 242 mf = new java.util.jar.Manifest (is); 243 is.close(); 244 value = mf.getMainAttributes().getValue("OpenIDE-Module-Implementation-Version"); if (value != null) { 246 break; 247 } 248 } 249 } catch (IOException ex) { 250 ex.printStackTrace(); 251 } 252 return value; 253 } 254 255 private boolean onlyRunRunOnce; 256 257 public void run () { 258 if (onlyRunRunOnce) return; 260 onlyRunRunOnce = true; 261 262 ArrayList <Union2<File ,JarFile >> toAdd = new ArrayList <Union2<File ,JarFile >> (); 263 String user = System.getProperty ("netbeans.user"); try { 265 if (user != null) { 266 build_cp (new File (user), toAdd, new HashSet <File > ()); 267 } 268 269 if (!toAdd.isEmpty ()) { 270 addSources (toAdd); 271 metaInf = Lookups.metaInfServices(this); 272 if (handlers != null) { 273 handlers.clear(); 274 handlers.addAll(metaInf.lookupAll(CLIHandler.class)); 275 } 276 } 277 } catch (IOException ex) { 278 ex.printStackTrace(); 279 } 280 } 281 282 283 284 protected PermissionCollection getPermissions(CodeSource cs) { 285 return getAllPermission(); 286 } 287 288 private static PermissionCollection modulePermissions; 289 290 private static synchronized PermissionCollection getAllPermission() { 291 if (modulePermissions == null) { 292 modulePermissions = new Permissions (); 293 modulePermissions.add(new AllPermission ()); 294 modulePermissions.setReadOnly(); 295 } 296 return modulePermissions; 297 } 298 299 301 public final Collection allCLIs () { 302 if (handlers == null) { 303 handlers = new ArrayList <CLIHandler>(metaInf.lookupAll(CLIHandler.class)); 304 } 305 return handlers; 306 } 307 308 protected boolean isSpecialResource (String pkg) { 309 boolean retValue = super.isSpecialResource (pkg); 310 if (retValue) return true; 311 312 return false; 313 } 314 } 316 private static void append_jars_to_cp (File dir, Collection <Union2<File ,JarFile >> toAdd) throws IOException { 317 if (!dir.isDirectory()) return; 318 319 File [] arr = dir.listFiles(); 320 for (int i = 0; i < arr.length; i++) { 321 String n = arr[i].getName (); 322 329 if (n.endsWith("jar") || n.endsWith ("zip")) { toAdd.add(Union2.<File ,JarFile >createSecond(new JarFile (arr[i], false))); 331 } 332 } 333 } 334 335 336 private static void build_cp(File base, Collection <Union2<File ,JarFile >> toAdd, Set <File > processedDirs) 337 throws java.io.IOException { 338 if (!processedDirs.add (base)) { 339 return; 341 } 342 343 append_jars_to_cp(new File (base, "core/patches"), toAdd); append_jars_to_cp(new File (base, "core"), toAdd); append_jars_to_cp(new File (base, "core/locale"), toAdd); } 359 } 360 | Popular Tags |