View Javadoc

1   package org.eclipse.jetty.policy.loader;
2   
3   //========================================================================
4   //Copyright (c) Webtide LLC
5   //------------------------------------------------------------------------
6   //All rights reserved. This program and the accompanying materials
7   //are made available under the terms of the Eclipse Public License v1.0
8   //and Apache License v2.0 which accompanies this distribution.
9   //
10  //The Eclipse Public License is available at
11  //http://www.eclipse.org/legal/epl-v10.html
12  //
13  //The Apache License v2.0 is available at
14  //http://www.apache.org/licenses/LICENSE-2.0.txt
15  //
16  //You may elect to redistribute this code under either of these licenses.
17  //========================================================================
18  //Portions of this file adapted for use from Apache Harmony code by written
19  //and contributed to that project by Alexey V. Varlamov under the ASL
20  //========================================================================
21  
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.security.KeyStore;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  
32  import org.eclipse.jetty.policy.PolicyBlock;
33  import org.eclipse.jetty.policy.PolicyContext;
34  import org.eclipse.jetty.policy.PolicyException;
35  import org.eclipse.jetty.policy.entry.GrantEntry;
36  import org.eclipse.jetty.policy.entry.KeystoreEntry;
37  
38  /**
39   * Load the policies within the stream and resolve into protection domains and permission collections 
40   * 
41   */
42  public class DefaultPolicyLoader
43  {
44      
45      public static Set<PolicyBlock> load( InputStream policyStream, PolicyContext context ) throws PolicyException
46      {
47          Set<PolicyBlock> policies = new HashSet<PolicyBlock>();
48          KeyStore keystore = null;
49          
50          try
51          {
52              PolicyFileScanner loader = new PolicyFileScanner();
53              
54              Collection<GrantEntry> grantEntries = new ArrayList<GrantEntry>();
55              List<KeystoreEntry> keystoreEntries = new ArrayList<KeystoreEntry>();
56              
57              loader.scanStream( new InputStreamReader(policyStream), grantEntries, keystoreEntries );
58              
59              for ( Iterator<KeystoreEntry> i = keystoreEntries.iterator(); i.hasNext();)
60              {
61                  KeystoreEntry node = i.next();
62                  node.expand( context );
63                  
64                  keystore = node.toKeyStore();
65                  
66                  if ( keystore != null )
67                  {
68                      // we only process the first valid keystore
69                      context.setKeystore( keystore );
70                      break;
71                  }
72              }
73              
74              for ( Iterator<GrantEntry> i = grantEntries.iterator(); i.hasNext(); )
75              {            
76                  GrantEntry grant = i.next();
77                  grant.expand( context );
78                  
79                  PolicyBlock policy = new PolicyBlock();             
80                  
81                  policy.setCodeSource( grant.getCodeSource() );
82                  policy.setPrincipals( grant.getPrincipals() );
83                  policy.setPermissions( grant.getPermissions() );
84                  
85                  policies.add(policy);
86              }      
87              
88              return policies;
89          }
90          catch ( Exception e )
91          {
92              throw new PolicyException( e );
93          }
94      }
95  }
96  
97  
98  
99  
100