Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19d50271dacc1049321f21920882cb86c48a1e0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//  Portions of this file adapted for use from Apache Harmony code by written
//  and contributed to that project by Alexey V. Varlamov under the ASL
//  ========================================================================
//

package org.eclipse.jetty.policy.loader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.jetty.policy.PolicyBlock;
import org.eclipse.jetty.policy.PolicyContext;
import org.eclipse.jetty.policy.PolicyException;
import org.eclipse.jetty.policy.entry.GrantEntry;
import org.eclipse.jetty.policy.entry.KeystoreEntry;

/**
 * Load the policies within the stream and resolve into protection domains and permission collections 
 * 
 */
public class DefaultPolicyLoader
{
    
    public static Set<PolicyBlock> load( InputStream policyStream, PolicyContext context ) throws PolicyException
    {
        Set<PolicyBlock> policies = new HashSet<PolicyBlock>();
        KeyStore keystore = null;
        
        try
        {
            PolicyFileScanner loader = new PolicyFileScanner();
            
            Collection<GrantEntry> grantEntries = new ArrayList<GrantEntry>();
            List<KeystoreEntry> keystoreEntries = new ArrayList<KeystoreEntry>();
            
            loader.scanStream( new InputStreamReader(policyStream), grantEntries, keystoreEntries );
            
            for ( Iterator<KeystoreEntry> i = keystoreEntries.iterator(); i.hasNext();)
            {
                KeystoreEntry node = i.next();
                node.expand( context );
                
                keystore = node.toKeyStore();
                
                if ( keystore != null )
                {
                    // we only process the first valid keystore
                    context.setKeystore( keystore );
                    break;
                }
            }
            
            for ( Iterator<GrantEntry> i = grantEntries.iterator(); i.hasNext(); )
            {            
                GrantEntry grant = i.next();
                grant.expand( context );
                
                PolicyBlock policy = new PolicyBlock();             
                
                policy.setCodeSource( grant.getCodeSource() );
                policy.setPrincipals( grant.getPrincipals() );
                policy.setPermissions( grant.getPermissions() );
                
                policies.add(policy);
            }      
            
            return policies;
        }
        catch ( Exception e )
        {
            throw new PolicyException( e );
        }
    }
}





Back to the top