1 19 20 package org.netbeans.modules.java.j2seplatform.platformdefinition; 21 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import java.util.*; 25 import java.net.URL ; 26 import java.net.MalformedURLException ; 27 import java.io.File ; 28 29 import org.netbeans.api.java.classpath.ClassPath; 30 import org.netbeans.api.java.platform.JavaPlatform; 31 import org.netbeans.api.java.platform.Specification; 32 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.URLMapper; 36 import org.openide.ErrorManager; 37 38 42 public class J2SEPlatformImpl extends JavaPlatform { 43 44 public static final String PROP_ANT_NAME = "antName"; public static final String PLATFORM_J2SE = "j2se"; 47 protected static final String PLAT_PROP_ANT_NAME="platform.ant.name"; protected static final String PLAT_PROP_ARCH_FOLDER="platform.arch.folder"; protected static final String SYSPROP_BOOT_CLASSPATH = "sun.boot.class.path"; protected static final String SYSPROP_JAVA_CLASS_PATH = "java.class.path"; protected static final String SYSPROP_JAVA_EXT_PATH = "java.ext.dirs"; 53 56 private String displayName; 57 60 private Map properties; 61 62 65 private ClassPath sources; 66 67 70 private List javadoc; 71 72 75 private List installFolders; 76 77 80 Reference bootstrap = new WeakReference (null); 81 84 Reference standardLibs = new WeakReference (null); 85 86 89 private Specification spec; 90 91 J2SEPlatformImpl (String dispName, List installFolders, Map initialProperties, Map sysProperties, List sources, List javadoc) { 92 super(); 93 this.displayName = dispName; 94 if (installFolders != null) { 95 this.installFolders = installFolders; } 97 else { 98 String home = (String ) initialProperties.remove ("platform.home"); if (home != null) { 101 this.installFolders = new ArrayList (); 102 StringTokenizer tk = new StringTokenizer (home, File.pathSeparator); 103 while (tk.hasMoreTokens()) { 104 File f = new File (tk.nextToken()); 105 try { 106 this.installFolders.add (f.toURI().toURL()); 107 } catch (MalformedURLException mue) { 108 ErrorManager.getDefault().notify (mue); 109 } 110 } 111 } 112 else { 113 throw new IllegalArgumentException ("Invalid platform, platform must have install folder."); } 115 } 116 this.properties = initialProperties; 117 this.sources = createClassPath(sources); 118 if (javadoc != null) { 119 this.javadoc = Collections.unmodifiableList(javadoc); } 121 else { 122 this.javadoc = Collections.EMPTY_LIST; 123 } 124 setSystemProperties(sysProperties); 125 } 126 127 protected J2SEPlatformImpl (String dispName, String antName, List installFolders, Map initialProperties, Map sysProperties, List sources, List javadoc) { 128 this (dispName, installFolders, initialProperties, sysProperties,sources, javadoc); 129 this.properties.put (PLAT_PROP_ANT_NAME,antName); 130 } 131 132 135 public String getDisplayName() { 136 return displayName; 137 } 138 139 143 public void setDisplayName(String name) { 144 this.displayName = name; 145 firePropertyChange(PROP_DISPLAY_NAME, null, null); } 147 148 154 final protected void internalSetDisplayName (String name) { 155 this.displayName = name; 156 } 157 158 159 public String getAntName () { 160 return (String ) this.properties.get (PLAT_PROP_ANT_NAME); 161 } 162 163 public void setAntName (String antName) { 164 if (antName == null || antName.length()==0) { 165 throw new IllegalArgumentException (); 166 } 167 this.properties.put(PLAT_PROP_ANT_NAME, antName); 168 this.firePropertyChange (PROP_ANT_NAME,null,null); 169 } 170 171 public void setArchFolder (final String folder) { 172 if (folder == null || folder.length() == 0) { 173 throw new IllegalArgumentException (); 174 } 175 this.properties.put (PLAT_PROP_ARCH_FOLDER, folder); 176 } 177 178 179 public ClassPath getBootstrapLibraries() { 180 synchronized (this) { 181 ClassPath cp = (ClassPath) (bootstrap == null ? null : bootstrap.get()); 182 if (cp != null) 183 return cp; 184 String pathSpec = (String )getSystemProperties().get(SYSPROP_BOOT_CLASSPATH); 185 String extPathSpec = Util.getExtensions((String )getSystemProperties().get(SYSPROP_JAVA_EXT_PATH)); 186 if (extPathSpec != null) { 187 pathSpec = pathSpec + File.pathSeparator + extPathSpec; 188 } 189 cp = Util.createClassPath (pathSpec); 190 bootstrap = new WeakReference (cp); 191 return cp; 192 } 193 } 194 195 200 public ClassPath getStandardLibraries() { 201 synchronized (this) { 202 ClassPath cp = (ClassPath) (standardLibs == null ? null : standardLibs.get()); 203 if (cp != null) 204 return cp; 205 String pathSpec = (String )getSystemProperties().get(SYSPROP_JAVA_CLASS_PATH); 206 cp = Util.createClassPath (pathSpec); 207 standardLibs = new WeakReference (cp); 208 return cp; 209 } 210 } 211 212 217 public final Collection getInstallFolders() { 218 Collection result = new ArrayList (); 219 for (Iterator it = this.installFolders.iterator(); it.hasNext();) { 220 URL url = (URL ) it.next (); 221 FileObject root = URLMapper.findFileObject(url); 222 if (root != null) { 223 result.add (root); 224 } 225 } 226 return result; 227 } 228 229 230 public final FileObject findTool(final String toolName) { 231 String archFolder = (String ) getProperties().get(PLAT_PROP_ARCH_FOLDER); 232 FileObject tool = null; 233 if (archFolder != null) { 234 tool = Util.findTool (toolName, this.getInstallFolders(), archFolder); 235 } 236 if (tool == null) { 237 tool = Util.findTool (toolName, this.getInstallFolders()); 238 } 239 return tool; 240 } 241 242 243 247 public final ClassPath getSourceFolders () { 248 return this.sources; 249 } 250 251 public final void setSourceFolders (ClassPath c) { 252 assert c != null; 253 this.sources = c; 254 this.firePropertyChange(PROP_SOURCE_FOLDER, null, null); 255 } 256 257 261 public final List getJavadocFolders () { 262 return this.javadoc; 263 } 264 265 public final void setJavadocFolders (List c) { 266 assert c != null; 267 List safeCopy = Collections.unmodifiableList (new ArrayList (c)); 268 for (Iterator it = safeCopy.iterator(); it.hasNext();) { 269 URL url = (URL ) it.next (); 270 if (!"jar".equals (url.getProtocol()) && FileUtil.isArchiveFile(url)) { 271 throw new IllegalArgumentException ("JavadocFolder must be a folder."); 272 } 273 } 274 this.javadoc = safeCopy; 275 this.firePropertyChange(PROP_JAVADOC_FOLDER, null, null); 276 } 277 278 public String getVendor() { 279 String s = (String )getSystemProperties().get("java.vm.vendor"); return s == null ? "" : s; } 282 283 public Specification getSpecification() { 284 if (spec == null) { 285 spec = new Specification (PLATFORM_J2SE, Util.getSpecificationVersion(this)); } 287 return spec; 288 } 289 290 public Map getProperties() { 291 return Collections.unmodifiableMap (this.properties); 292 } 293 294 Collection getInstallFolderURLs () { 295 return Collections.unmodifiableList(this.installFolders); 296 } 297 298 299 private static ClassPath createClassPath (List urls) { 300 List resources = new ArrayList (); 301 if (urls != null) { 302 for (Iterator it = urls.iterator(); it.hasNext();) { 303 URL url = (URL ) it.next(); 304 resources.add (ClassPathSupport.createResource (url)); 305 } 306 } 307 return ClassPathSupport.createClassPath (resources); 308 } 309 } 310 | Popular Tags |