KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > environments > DefaultAccessRuleParticipant


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching.environments;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
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 /**
36  * Creates default access rules for standard execution environments
37  * based on OSGi profiles.
38  *
39  * @since 3.3
40  */

41 public class DefaultAccessRuleParticipant implements IAccessRuleParticipant {
42     
43     /**
44      * Cache of access rules per environment. Re-use rules between projects.
45      */

46     private static Map JavaDoc fgRules = new HashMap JavaDoc();
47
48     /* (non-Javadoc)
49      * @see org.eclipse.jdt.launching.environments.IAccessRuleParticipant#getAccessRules(org.eclipse.jdt.launching.environments.IExecutionEnvironment, org.eclipse.jdt.launching.IVMInstall, org.eclipse.jdt.launching.LibraryLocation[], org.eclipse.jdt.core.IJavaProject)
50      */

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             // if a different number of libraries, create a new set of rules
56
String JavaDoc[] 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); //$NON-NLS-1$
64
} 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 JavaDoc[] retrieveSystemPackages(String JavaDoc ee) {
77         Properties JavaDoc profile = getJavaProfileProperties(ee);
78         if (profile != null) {
79             String JavaDoc packages = profile.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
80             if (packages != null) {
81                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(packages, ","); //$NON-NLS-1$
82
String JavaDoc[] result = new String JavaDoc[tokenizer.countTokens() + 1];
83                 result[0] = "java.**"; //$NON-NLS-1$
84
for (int i = 1; i < result.length; i++) {
85                     result[i] = tokenizer.nextToken().trim() + ".*"; //$NON-NLS-1$
86
}
87                 return result;
88             }
89         }
90         return new String JavaDoc[0];
91     }
92
93     private Properties JavaDoc getJavaProfileProperties(String JavaDoc ee) {
94         Bundle osgiBundle = Platform.getBundle("org.eclipse.osgi"); //$NON-NLS-1$
95
if (osgiBundle == null)
96             return null;
97         URL JavaDoc profileURL = osgiBundle.getEntry(ee.replace('/', '_') + ".profile"); //$NON-NLS-1$
98
if (profileURL != null) {
99             InputStream JavaDoc is = null;
100             try {
101                 profileURL = FileLocator.resolve(profileURL);
102                 is = profileURL.openStream();
103                 if (is != null) {
104                     Properties JavaDoc profile = new Properties JavaDoc();
105                     profile.load(is);
106                     return profile;
107                 }
108             } catch (IOException JavaDoc e) {
109             } finally {
110                 try {
111                     if (is != null) {
112                         is.close();
113                     }
114                 } catch (IOException JavaDoc e) {
115                 }
116             }
117         }
118         return null;
119     }
120 }
121
Popular Tags