Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/stores/ResourceSigningPolicy.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15
16 package org.globus.gsi.stores;
17
18 import org.globus.gsi.SigningPolicyException;
19
20 import org.globus.gsi.provider.SigningPolicyStoreException;
21
22 import org.apache.commons.logging.LogFactory;
23
24 import org.apache.commons.logging.Log;
25
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.io.InputStreamReader;
29 import java.util.Collection;
30 import java.util.Map;
31
32
33 import javax.security.auth.x500.X500Principal;
34
35 import org.globus.gsi.SigningPolicy;
36 import org.globus.gsi.SigningPolicyParser;
37 import org.globus.util.GlobusResource;
38
39 /**
40 * Created by IntelliJ IDEA.
41 * User: turtlebender
42 * Date: Dec 28, 2009
43 * Time: 2:57:09 PM
44 * To change this template use File | Settings | File Templates.
45 */
46 public class ResourceSigningPolicy {
47 protected GlobusResource globusResource;
48
49 private Log logger = LogFactory.getLog(ResourceSigningPolicy.class.getCanonicalName());
50 private boolean changed;
51 private Map<X500Principal, SigningPolicy> signingPolicyMap;
52 private long lastModified = -1;
53
54 public ResourceSigningPolicy(GlobusResource resource) throws ResourceStoreException {
55 init(resource);
56 }
57
58 protected void init(GlobusResource initResource) throws ResourceStoreException {
59 this.globusResource = initResource;
60 this.signingPolicyMap = create(this.globusResource);
61 logger.debug(String.format("Loading initResource: %s", this.globusResource.toString()));
62 try {
63 this.lastModified = this.globusResource.lastModified();
64 } catch (IOException e) {
65 throw new ResourceStoreException(e);
66 }
67 }
68
69 protected void init(GlobusResource initResource, Map<X500Principal, SigningPolicy> initSigningPolicy)
70 throws ResourceStoreException {
71 if (initSigningPolicy == null) {
72 // JGLOBUS-88 : better exception?
73 throw new IllegalArgumentException("Object cannot be null");
74 }
75 this.signingPolicyMap = initSigningPolicy;
76 this.globusResource = initResource;
77 }
78
79 public Collection<SigningPolicy> getSigningPolicies()
80 throws SigningPolicyStoreException {
81
82 try {
83 Map<X500Principal, SigningPolicy> object = getObject();
84 if (object != null) {
85 return object.values();
86 }
87 } catch (ResourceStoreException e) {
88 throw new SigningPolicyStoreException(e);
89 }
90 return null;
91 }
92
93 public Map<X500Principal, SigningPolicy> create(GlobusResource signingPolicyResource) throws ResourceStoreException {
94 SigningPolicyParser parser = new SigningPolicyParser();
95 Map<X500Principal, SigningPolicy> policies;
96 Reader reader;
97 try {
98 reader = new InputStreamReader(signingPolicyResource.getInputStream());
99 } catch (IOException e) {
100 throw new ResourceStoreException(e);
101 }
102 try {
103 policies = parser.parse(reader);
104 } catch (SigningPolicyException e) {
105 throw new ResourceStoreException(e);
106 } finally {
107 try {
108 reader.close();
109 } catch (IOException e) {
110 throw new ResourceStoreException(e);
111 }
112 }
113
114 return policies;
115 }
116
117
118 protected void reload() throws ResourceStoreException {
119
120 this.changed = false;
121 long latestLastModified;
122 try {
123 latestLastModified = this.globusResource.lastModified();
124 } catch (IOException e) {
125 throw new ResourceStoreException(e);
126 }
127 if (this.lastModified < latestLastModified) {
128 this.signingPolicyMap = create(this.globusResource);
129 this.lastModified = latestLastModified;
130 this.changed = true;
131 }
132 }
133
134 public GlobusResource getResource() {
135 return this.globusResource;
136 }
137
138 protected Map<X500Principal, SigningPolicy> getObject() throws ResourceStoreException {
139 reload();
140 return this.signingPolicyMap;
141 }
142
143 public boolean hasChanged() {
144 return this.changed;
145 }
146 }