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.security.ProtectionDomain;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.eclipse.jetty.policy.PolicyContext;
34  import org.eclipse.jetty.policy.PolicyBlock;
35  import org.eclipse.jetty.policy.PolicyException;
36  import org.eclipse.jetty.policy.entry.GrantEntry;
37  import org.eclipse.jetty.policy.entry.KeystoreEntry;
38  
39  /**
40   * Load the policies within the stream and resolve into protection domains and permission collections 
41   * 
42   */
43  public class DefaultPolicyLoader
44  {
45      
46      public static Map<ProtectionDomain, PolicyBlock> load( InputStream policyStream, PolicyContext context ) throws PolicyException
47      {
48          Map<ProtectionDomain, PolicyBlock> policies = new HashMap<ProtectionDomain, PolicyBlock>();
49          KeyStore keystore = null;
50          
51          try
52          {
53              PolicyFileScanner loader = new PolicyFileScanner();
54              
55              Collection<GrantEntry> grantEntries = new ArrayList<GrantEntry>();
56              List<KeystoreEntry> keystoreEntries = new ArrayList<KeystoreEntry>();
57              
58              loader.scanStream( new InputStreamReader(policyStream), grantEntries, keystoreEntries );
59              
60              for ( Iterator<KeystoreEntry> i = keystoreEntries.iterator(); i.hasNext();)
61              {
62                  KeystoreEntry node = i.next();
63                  node.expand( context );
64                  
65                  keystore = node.toKeyStore();
66                  
67                  if ( keystore != null )
68                  {
69                      // we only process the first valid keystore
70                      context.setKeystore( keystore );
71                      break;
72                  }
73              }
74              
75              for ( Iterator<GrantEntry> i = grantEntries.iterator(); i.hasNext(); )
76              {            
77                  GrantEntry grant = i.next();
78                  grant.expand( context );
79                  
80                  PolicyBlock policy = new PolicyBlock();             
81                  
82                  policy.setCodeSource( grant.getCodeSource() );
83                  policy.setPrincipals( grant.getPrincipals() );
84                  policy.setPermissions( grant.getPermissions() );
85                  
86                  policies.put( policy.toProtectionDomain(), policy );                                        
87              }      
88              
89              return policies;
90          }
91          catch ( Exception e )
92          {
93              throw new PolicyException( e );
94          }
95      }
96  }
97  
98  
99  
100 
101