1 11 package org.eclipse.jdt.internal.launching.environments; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.FileLocator; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.jdt.core.IAccessRule; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.launching.IVMInstall; 29 import org.eclipse.jdt.launching.LibraryLocation; 30 import org.eclipse.jdt.launching.environments.IAccessRuleParticipant; 31 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 32 import org.osgi.framework.Bundle; 33 import org.osgi.framework.Constants; 34 35 41 public class DefaultAccessRuleParticipant implements IAccessRuleParticipant { 42 43 46 private static Map fgRules = new HashMap (); 47 48 51 public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) { 52 IAccessRule[][] allRules = null; 53 allRules = (IAccessRule[][]) fgRules.get(environment.getId()); 54 if (allRules == null || allRules.length != libraries.length) { 55 String [] packages = retrieveSystemPackages(environment.getId()); 57 IAccessRule[] packageRules = null; 58 if (packages.length > 0) { 59 packageRules = new IAccessRule[packages.length + 1]; 60 for (int i = 0; i < packages.length; i++) { 61 packageRules[i] = JavaCore.newAccessRule(new Path(packages[i].replace('.', IPath.SEPARATOR)), IAccessRule.K_ACCESSIBLE); 62 } 63 packageRules[packages.length] = JavaCore.newAccessRule(new Path("**/*"), IAccessRule.K_NON_ACCESSIBLE); } else { 65 packageRules = new IAccessRule[0]; 66 } 67 allRules = new IAccessRule[libraries.length][]; 68 for (int i = 0; i < allRules.length; i++) { 69 allRules[i] = packageRules; 70 } 71 fgRules.put(environment.getId(), allRules); 72 } 73 return allRules; 74 } 75 76 private String [] retrieveSystemPackages(String ee) { 77 Properties profile = getJavaProfileProperties(ee); 78 if (profile != null) { 79 String packages = profile.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES); 80 if (packages != null) { 81 StringTokenizer tokenizer = new StringTokenizer (packages, ","); String [] result = new String [tokenizer.countTokens() + 1]; 83 result[0] = "java.**"; for (int i = 1; i < result.length; i++) { 85 result[i] = tokenizer.nextToken().trim() + ".*"; } 87 return result; 88 } 89 } 90 return new String [0]; 91 } 92 93 private Properties getJavaProfileProperties(String ee) { 94 Bundle osgiBundle = Platform.getBundle("org.eclipse.osgi"); if (osgiBundle == null) 96 return null; 97 URL profileURL = osgiBundle.getEntry(ee.replace('/', '_') + ".profile"); if (profileURL != null) { 99 InputStream is = null; 100 try { 101 profileURL = FileLocator.resolve(profileURL); 102 is = profileURL.openStream(); 103 if (is != null) { 104 Properties profile = new Properties (); 105 profile.load(is); 106 return profile; 107 } 108 } catch (IOException e) { 109 } finally { 110 try { 111 if (is != null) { 112 is.close(); 113 } 114 } catch (IOException e) { 115 } 116 } 117 } 118 return null; 119 } 120 } 121 | Popular Tags |