View Javadoc

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