Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/stores/ResourceSecurityWrapperStore.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.apache.commons.logging.LogFactory;
19
20 import org.apache.commons.logging.Log;
21
22 import java.io.File;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32
33 import org.globus.util.GlobusResource;
34 import org.globus.util.GlobusPathMatchingResourcePatternResolver;
35
36 /**
37 * Created by IntelliJ IDEA. User: turtlebender Date: Dec 29, 2009 Time:
38 * 12:29:45 PM To change this template use File | Settings | File Templates.
39 *
40 * @param <T>
41 * @param <V>
42 */
43 public abstract class ResourceSecurityWrapperStore<T extends AbstractResourceSecurityWrapper<V>, V> {
44 private Collection<V> rootObjects;
45 private GlobusPathMatchingResourcePatternResolver globusResolver = new GlobusPathMatchingResourcePatternResolver();
46 private Map<String, T> wrapperMap = new HashMap<String, T>();
47 private Log logger = LogFactory.getLog(ResourceSecurityWrapperStore.class.getCanonicalName());
48
49 public Map<String, T> getWrapperMap() {
50 return this.wrapperMap;
51 }
52
53 public void loadWrappers(String[] locations) throws ResourceStoreException {
54 for (String location : locations) {
55 File file = new File(location);
56 GlobusResource globusResource = new GlobusResource(file.getAbsolutePath());
57 try {
58 loadWrappers(globusResource.getURL().toExternalForm());
59 } catch (IOException ioe) {
60 throw new ResourceStoreException(ioe);
61 }
62 }
63 }
64
65 public void loadWrappers(String locationPattern)
66 throws ResourceStoreException {
67 Set<V> updatedList = new HashSet<V>();
68 boolean changed = false;
69 Map<String, T> newWrapperMap = new HashMap<String, T>();
70 if (locationPattern == null) {
71 this.rootObjects = updatedList;
72 this.wrapperMap = newWrapperMap;
73 return;
74 }
75 if (locationPattern.indexOf(",") >= 0) {
76 String[] locationPatterns = locationPattern.split(",");
77 boolean tmpChanged = false;
78 for (String lp : locationPatterns) {
79 if (!tmpChanged) {
80 tmpChanged = loadResources(lp, updatedList, newWrapperMap);
81 }
82 changed = tmpChanged;
83 }
84 } else {
85 changed = loadResources(locationPattern, updatedList, newWrapperMap);
86 }
87 // in case certificates were removed
88 if (!changed && this.rootObjects != null
89 && this.wrapperMap.size() != newWrapperMap.size()) {
90 changed = true;
91 }
92 if (changed) {
93 this.rootObjects = updatedList;
94 }
95 this.wrapperMap = newWrapperMap;
96 }
97
98 private boolean loadResources(String locationPattern, Set<V> updatedList,
99 Map<String, T> newWrapperMap) throws ResourceStoreException {
100 boolean changed = false;
101 try {
102 GlobusResource[] globusResources = globusResolver.getResources(locationPattern);
103 for (GlobusResource globusResource : globusResources){
104 URI uri =globusResource.getURI();
105 if (!globusResource.isReadable()) {
106 getLog().warn("Cannot read: " + uri.toASCIIString());
107 continue;
108 }
109 changed = load(globusResource, updatedList, newWrapperMap);
110 }
111 }
112 catch (IOException e) {
113 throw new ResourceStoreException(e);
114 }
115 return changed;
116 }
117
118 private boolean load(GlobusResource resource, Set<V> currentRoots,
119 Map<String, T> newWrapperMap) throws ResourceStoreException {
120 if (!resource.isReadable()) {
121 throw new ResourceStoreException("Cannot read file");
122 }
123 try {
124 if (resource.getFile().isDirectory()) {
125 File directory = resource.getFile();
126 currentRoots.addAll(addCredentials(directory, newWrapperMap));
127 return true;
128 }
129 } catch (IOException e) {
130 // This is ok, it just means the resource is not a
131 // filesystemresources
132 logger.debug("Not a filesystem resource", e);
133 }
134 try {
135 String resourceUri = resource.getURL().toExternalForm();
136 T fbo = this.wrapperMap.get(resourceUri);
137 if (fbo == null) {
138 fbo = create(resource);
139 }
140 V target = fbo.create(resource);
141 newWrapperMap.put(resourceUri, fbo);
142 currentRoots.add(target);
143 return true;
144 } catch (IOException e) {
145 throw new ResourceStoreException(e);
146 }
147
148 }
149
150 private Set<V> addCredentials(File directory, Map<String, T> newWrapperMap) throws ResourceStoreException {
151 FilenameFilter filter = getDefaultFilenameFilter();
152 String[] children = directory.list(filter);
153 Set<V> roots = new HashSet<V>();
154 if (children == null) {
155 return roots;
156 }
157 try {
158 for (String child : children) {
159 File childFile = new File(directory, child);
160 if (childFile.isDirectory()) {
161 roots.addAll(addCredentials(childFile, newWrapperMap));
162 } else {
163 GlobusResource resource = new GlobusResource(childFile.getAbsolutePath());
164 String resourceUri = resource.getURI().toASCIIString();
165 T fbo = this.wrapperMap.get(resourceUri);
166 if (fbo == null) {
167 fbo = create(new GlobusResource(childFile.getAbsolutePath()));
168 }
169 V target = fbo.create(resource);
170 newWrapperMap.put(resourceUri, fbo);
171 roots.add(target);
172 }
173 }
174 return roots;
175 } catch (IOException e) {
176 throw new ResourceStoreException(e);
177 }
178 }
179
180 public abstract T create(GlobusResource resource) throws ResourceStoreException;
181
182 public abstract FilenameFilter getDefaultFilenameFilter();
183
184 public Collection<V> getCollection() {
185 return this.rootObjects;
186 }
187
188 protected abstract Log getLog();
189 }