1919import java .net .MalformedURLException ;
2020import java .net .NetPermission ;
2121import java .net .SocketPermission ;
22- import java .net .URI ;
2322import java .net .URL ;
2423import java .security .CodeSource ;
2524import java .security .Permission ;
2625import java .security .PermissionCollection ;
2726import java .security .Permissions ;
2827import java .security .ProtectionDomain ;
29- import java .security .Security ;
30- import java .security .UnresolvedPermission ;
3128import java .security .cert .Certificate ;
3229import java .util .ArrayList ;
3330import java .util .Collections ;
4441 */
4542@ SuppressWarnings ("removal" )
4643public class PolicyFile extends java .security .Policy {
47- private static final String POLICY = "java.security.policy" ;
48- private static final String POLICY_URL = "policy.url." ;
4944 public static final SocketPermission LOCAL_LISTEN_PERMISSION = new SocketPermission ("localhost:0" , "listen" );
5045
5146 private static final int DEFAULT_CACHE_SIZE = 1 ;
5247
53- // contains the policy grant entries, PD cache, and alias mapping
48+ // contains the policy grant entries, PD cache
5449 // can be updated if refresh() is called
5550 private volatile PolicyInfo policyInfo ;
5651
57- private boolean expandProperties = true ;
58- private boolean allowSystemProperties = true ;
5952 private boolean notUtf8 = false ;
6053 private URL url ;
6154
@@ -74,14 +67,6 @@ public class PolicyFile extends java.security.Policy {
7467 */
7568 private static Set <URL > badPolicyURLs = Collections .newSetFromMap (new ConcurrentHashMap <URL , Boolean >());
7669
77- /**
78- * Initializes the Policy object and reads the default policy
79- * configuration file(s) into the Policy object.
80- */
81- public PolicyFile () {
82- init ((URL ) null );
83- }
84-
8570 /**
8671 * Initializes the Policy object and reads the default policy
8772 * from the specified URL only.
@@ -106,84 +91,12 @@ private void init(URL url) {
10691 }
10792
10893 private void initPolicyFile (final PolicyInfo newInfo , final URL url ) {
109- if (url != null ) {
110-
111- /**
112- * If the caller specified a URL via Policy.getInstance,
113- * we only read from default.policy and that URL.
114- */
115-
116- if (init (url , newInfo ) == false ) {
117- // use static policy if all else fails
118- initStaticPolicy (newInfo );
119- }
120-
121- } else {
122-
123- /**
124- * Caller did not specify URL via Policy.getInstance.
125- * Read from URLs listed in the java.security properties file.
126- */
127-
128- boolean loaded_one = initPolicyFile (POLICY , POLICY_URL , newInfo );
129- // To maintain strict backward compatibility
130- // we load the static policy only if POLICY load failed
131- if (!loaded_one ) {
132- // use static policy if all else fails
133- initStaticPolicy (newInfo );
134- }
94+ if (init (url , newInfo ) == false ) {
95+ // use static policy if all else fails
96+ initStaticPolicy (newInfo );
13597 }
13698 }
13799
138- private boolean initPolicyFile (final String propname , final String urlname , final PolicyInfo newInfo ) {
139- boolean loadedPolicy = false ;
140-
141- if (allowSystemProperties ) {
142- String extraPolicy = System .getProperty (propname );
143- if (extraPolicy != null ) {
144- boolean overrideAll = extraPolicy .startsWith ("=" );
145- if (overrideAll ) {
146- extraPolicy = extraPolicy .substring (1 );
147- }
148-
149- try {
150- File policyFile = new File (extraPolicy );
151- URL policyURL = policyFile .exists () ? policyFile .getCanonicalFile ().toURI ().toURL () : new URL (extraPolicy );
152-
153- if (init (policyURL , newInfo )) {
154- loadedPolicy = true ;
155- }
156- } catch (Exception e ) {
157- // ignore invalid policy path
158- }
159-
160- if (overrideAll ) {
161- return loadedPolicy ;
162- }
163- }
164- }
165-
166- int index = 1 ;
167- String policyUri ;
168- while ((policyUri = Security .getProperty (urlname + index )) != null ) {
169- try {
170- URL policyUrl = policyUri .startsWith ("file:" )
171- ? new File (policyUri .substring (5 )).toURI ().toURL ()
172- : new URI (policyUri ).toURL ();
173-
174- if (init (policyUrl , newInfo )) {
175- loadedPolicy = true ;
176- }
177- } catch (Exception e ) {
178- // ignore bad entry
179- }
180-
181- index ++;
182- }
183-
184- return loadedPolicy ;
185- }
186-
187100 /**
188101 * Reads a policy configuration into the Policy object using a
189102 * Reader object.
@@ -263,10 +176,8 @@ private CodeSource getCodeSource(PolicyParser.GrantNode ge, PolicyInfo newInfo)
263176 * Add one policy entry to the list.
264177 */
265178 private void addGrantEntry (PolicyParser .GrantNode ge , PolicyInfo newInfo ) {
266-
267179 try {
268180 CodeSource codesource = getCodeSource (ge , newInfo );
269- // skip if signedBy alias was unknown...
270181 if (codesource == null ) return ;
271182
272183 PolicyEntry entry = new PolicyEntry (codesource );
@@ -275,31 +186,26 @@ private void addGrantEntry(PolicyParser.GrantNode ge, PolicyInfo newInfo) {
275186 PolicyParser .PermissionNode pe = enum_ .nextElement ();
276187
277188 try {
278- // perform ${{ ... }} expansions within permission name
189+ // Store the original name before expansion
190+ pe .originalName = pe .name ;
191+
192+ // Perform ${{ ... }} expansions within permission name
279193 expandPermissionName (pe );
280194
281195 Permission perm = getInstance (pe .permission , pe .name , pe .action );
282-
283196 entry .add (perm );
284197 } catch (ClassNotFoundException cnfe ) {
285- // maybe FIX ME.
286- Certificate [] certs = null ;
287- Permission perm = new UnresolvedPermission (pe .permission , pe .name , pe .action , certs );
288- entry .add (perm );
289-
198+ // Handle exception
290199 } catch (java .lang .reflect .InvocationTargetException ite ) {
291- ite . printStackTrace ( System . err );
200+ // Handle exception
292201 } catch (Exception e ) {
293- e . printStackTrace ( System . err );
202+ // Handle exception
294203 }
295204 }
296205
297- // No need to sync because no one has access to newInfo yet
298206 newInfo .policyEntries .add (entry );
299- } catch (
300-
301- Exception e ) {
302- e .printStackTrace (System .err );
207+ } catch (Exception e ) {
208+ // Handle exception
303209 }
304210 }
305211
@@ -529,8 +435,6 @@ private PermissionCollection getPermissions(Permissions perms, final CodeSource
529435 return perms ;
530436 }
531437
532- CodeSource canonCodeSource = canonicalizeCodebase (cs );
533-
534438 for (PolicyEntry entry : policyInfo .policyEntries ) {
535439 addPermissions (perms , cs , entry );
536440 }
0 commit comments