1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
package org.codehaus.spice.xmlpolicy.reader;
|
9
|
|
|
10
|
|
import java.util.ArrayList;
|
11
|
|
import org.codehaus.spice.xmlpolicy.metadata.GrantMetaData;
|
12
|
|
import org.codehaus.spice.xmlpolicy.metadata.KeyStoreMetaData;
|
13
|
|
import org.codehaus.spice.xmlpolicy.metadata.PermissionMetaData;
|
14
|
|
import org.codehaus.spice.xmlpolicy.metadata.PolicyMetaData;
|
15
|
|
import org.w3c.dom.Element;
|
16
|
|
import org.w3c.dom.NodeList;
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
public class PolicyReader
|
26
|
|
{
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
2
|
public PolicyMetaData readPolicy( final Element element )
|
35
|
|
throws Exception
|
36
|
|
{
|
37
|
2
|
final String version = element.getAttribute( "version" );
|
38
|
2
|
if( !"1.0".equals( version ) )
|
39
|
|
{
|
40
|
0
|
final String message = "Bad version:" + version;
|
41
|
0
|
throw new Exception( message );
|
42
|
|
}
|
43
|
|
|
44
|
2
|
final NodeList keyStoreConfigs = element.getElementsByTagName( "keystore" );
|
45
|
2
|
final KeyStoreMetaData[] keyStores = buildKeyStores( keyStoreConfigs );
|
46
|
|
|
47
|
0
|
final NodeList grantConfigs =
|
48
|
|
element.getElementsByTagName( "grant" );
|
49
|
0
|
final GrantMetaData[] grants = buildGrants( grantConfigs );
|
50
|
|
|
51
|
0
|
return new PolicyMetaData( keyStores, grants );
|
52
|
|
}
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
0
|
private GrantMetaData[] buildGrants( final NodeList elements )
|
61
|
|
throws Exception
|
62
|
|
{
|
63
|
0
|
final ArrayList grants = new ArrayList();
|
64
|
0
|
final int length = elements.getLength();
|
65
|
|
|
66
|
0
|
for( int i = 0; i < length; i++ )
|
67
|
|
{
|
68
|
0
|
final Element element = (Element)elements.item( i );
|
69
|
0
|
final GrantMetaData grant = buildGrant( element );
|
70
|
0
|
grants.add( grant );
|
71
|
|
}
|
72
|
|
|
73
|
0
|
return (GrantMetaData[])grants.toArray( new GrantMetaData[ grants.size() ] );
|
74
|
|
}
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
0
|
private GrantMetaData buildGrant( final Element element )
|
83
|
|
throws Exception
|
84
|
|
{
|
85
|
0
|
final String codeBase = getAttribute( element, "code-base" );
|
86
|
0
|
final String signedBy = getAttribute( element, "signed-by" );
|
87
|
0
|
String keyStore = getAttribute( element, "key-store" );
|
88
|
0
|
if( null != signedBy && null == keyStore )
|
89
|
|
{
|
90
|
0
|
keyStore = "default";
|
91
|
|
}
|
92
|
0
|
final NodeList permissionElements =
|
93
|
|
element.getElementsByTagName( "permission" );
|
94
|
0
|
final PermissionMetaData[] permissions = buildPermissions( permissionElements );
|
95
|
0
|
return new GrantMetaData( codeBase, signedBy, keyStore, permissions );
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
0
|
private PermissionMetaData[] buildPermissions( final NodeList elements )
|
105
|
|
throws Exception
|
106
|
|
{
|
107
|
0
|
final ArrayList grants = new ArrayList();
|
108
|
0
|
final int length = elements.getLength();
|
109
|
|
|
110
|
0
|
for( int i = 0; i < length; i++ )
|
111
|
|
{
|
112
|
0
|
final Element element = (Element)elements.item( i );
|
113
|
0
|
final PermissionMetaData permission = buildPermission( element );
|
114
|
0
|
grants.add( permission );
|
115
|
|
}
|
116
|
|
|
117
|
0
|
return (PermissionMetaData[])grants.toArray( new PermissionMetaData[ grants.size() ] );
|
118
|
|
}
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
0
|
private PermissionMetaData buildPermission( final Element element )
|
127
|
|
throws Exception
|
128
|
|
{
|
129
|
0
|
final String classname = getAttribute( element, "class" );
|
130
|
0
|
final String target = getAttribute( element, "target" );
|
131
|
0
|
final String action = getAttribute( element, "action" );
|
132
|
0
|
final String signedBy = getAttribute( element, "signed-by" );
|
133
|
0
|
String keyStore = getAttribute( element, "key-store" );
|
134
|
0
|
if( null != signedBy && null == keyStore )
|
135
|
|
{
|
136
|
0
|
keyStore = "default";
|
137
|
|
}
|
138
|
0
|
return new PermissionMetaData( classname, target, action,
|
139
|
|
signedBy, keyStore );
|
140
|
|
}
|
141
|
|
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
2
|
private KeyStoreMetaData[] buildKeyStores( final NodeList elements )
|
149
|
|
throws Exception
|
150
|
|
{
|
151
|
2
|
final ArrayList keyStores = new ArrayList();
|
152
|
2
|
final int length = elements.getLength();
|
153
|
|
|
154
|
2
|
for( int i = 0; i < length; i++ )
|
155
|
|
{
|
156
|
2
|
final Element element = (Element)elements.item( i );
|
157
|
2
|
final KeyStoreMetaData keyStore = buildKeyStore( element );
|
158
|
0
|
keyStores.add( keyStore );
|
159
|
|
}
|
160
|
|
|
161
|
0
|
return (KeyStoreMetaData[])keyStores.toArray( new KeyStoreMetaData[ keyStores.size() ] );
|
162
|
|
}
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
2
|
private KeyStoreMetaData buildKeyStore( final Element element )
|
171
|
|
throws Exception
|
172
|
|
{
|
173
|
2
|
final String name = element.getAttribute( "name" );
|
174
|
2
|
final String location = getAttribute( element, "location" );
|
175
|
0
|
final String type = getAttribute( element, "type" );
|
176
|
0
|
return new KeyStoreMetaData( name, location, type );
|
177
|
|
}
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
2
|
private String getAttribute( final Element element,
|
189
|
|
final String name )
|
190
|
|
{
|
191
|
2
|
final String value = element.getAttribute( name );
|
192
|
2
|
if( "".equals( value ) )
|
193
|
|
{
|
194
|
0
|
return null;
|
195
|
|
}
|
196
|
|
else
|
197
|
|
{
|
198
|
2
|
return value;
|
199
|
|
}
|
200
|
|
}
|
201
|
|
}
|
202
|
|
|