| 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.IOException; |
| 24 |
import java.net.URL; |
| 25 |
import org.globus.common.CoGProperties; |
| 26 |
|
| 27 |
import org.globus.util.GlobusResource; |
| 28 |
import org.globus.util.GlobusPathMatchingResourcePatternResolver; |
| 29 |
|
| 30 |
/** |
| 31 |
* // JGLOBUS-91 : add javadoc |
| 32 |
* |
| 33 |
* @param <T> |
| 34 |
* Type of security object |
| 35 |
*/ |
| 36 |
public abstract class AbstractResourceSecurityWrapper<T> implements |
| 37 |
SecurityObjectWrapper<T>, Storable { |
| 38 |
|
| 39 |
protected GlobusPathMatchingResourcePatternResolver globusResolver = new GlobusPathMatchingResourcePatternResolver(); |
| 40 |
protected GlobusResource globusResource; |
| 41 |
|
| 42 |
private Log logger = LogFactory.getLog(getClass().getCanonicalName()); |
| 43 |
|
| 44 |
private boolean changed; |
| 45 |
private T securityObject; |
| 46 |
private long lastModified = -1; |
| 47 |
private long lastRefresh; |
| 48 |
private final long cacheLifetime; |
| 49 |
private String alias; |
| 50 |
private boolean inMemory = false; |
| 51 |
|
| 52 |
protected AbstractResourceSecurityWrapper(boolean inMemory) { |
| 53 |
this.inMemory = inMemory; |
| 54 |
cacheLifetime = CoGProperties.getDefault().getCertCacheLifetime(); |
| 55 |
} |
| 56 |
|
| 57 |
protected void init(String locationPattern) throws ResourceStoreException { |
| 58 |
init(globusResolver.getResource(locationPattern)); |
| 59 |
} |
| 60 |
|
| 61 |
protected void init(GlobusResource initialResource) throws ResourceStoreException { |
| 62 |
init(initialResource, create(initialResource)); |
| 63 |
} |
| 64 |
|
| 65 |
public String getAlias() { |
| 66 |
return alias; |
| 67 |
} |
| 68 |
|
| 69 |
protected void init(String locationPattern, T initialSecurityObject) |
| 70 |
throws ResourceStoreException { |
| 71 |
init(globusResolver.getResource(locationPattern), initialSecurityObject); |
| 72 |
} |
| 73 |
|
| 74 |
protected void init(GlobusResource initialResource, T initialSecurityObject) |
| 75 |
throws ResourceStoreException { |
| 76 |
if (initialSecurityObject == null) { |
| 77 |
// JGLOBUS-88 : better exception? |
| 78 |
throw new IllegalArgumentException("Object cannot be null"); |
| 79 |
} |
| 80 |
this.securityObject = initialSecurityObject; |
| 81 |
this.globusResource = initialResource; |
| 82 |
logger.debug(String.format("Loading initialResource: %s", this.globusResource.toString())); |
| 83 |
try { |
| 84 |
this.alias = this.globusResource.getURL().toExternalForm(); |
| 85 |
if(!inMemory){ |
| 86 |
this.lastModified = this.globusResource.lastModified(); |
| 87 |
this.lastRefresh = System.currentTimeMillis(); |
| 88 |
} |
| 89 |
} catch (IOException e) { |
| 90 |
throw new ResourceStoreException(e); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public GlobusResource getGlobusResource(){ |
| 95 |
return globusResource; |
| 96 |
} |
| 97 |
|
| 98 |
public URL getResourceURL() { |
| 99 |
try { |
| 100 |
return globusResource.getURL(); |
| 101 |
} catch (IOException e) { |
| 102 |
logger.warn("Unable to extract url", e); |
| 103 |
return null; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
public File getFile() { |
| 109 |
try { |
| 110 |
return globusResource.getFile(); |
| 111 |
} catch (IOException e) { |
| 112 |
logger.debug("Resource is not a file", e); |
| 113 |
return null; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public void refresh() throws ResourceStoreException { |
| 118 |
if(!inMemory){ |
| 119 |
long now = System.currentTimeMillis(); |
| 120 |
if (this.lastRefresh + this.cacheLifetime < now) { |
| 121 |
this.changed = false; |
| 122 |
long latestLastModified; |
| 123 |
try { |
| 124 |
latestLastModified = this.globusResource.lastModified(); |
| 125 |
} catch (IOException e) { |
| 126 |
throw new ResourceStoreException(e); |
| 127 |
} |
| 128 |
if (this.lastModified < latestLastModified) { |
| 129 |
this.securityObject = create(this.globusResource); |
| 130 |
this.lastModified = latestLastModified; |
| 131 |
this.changed = true; |
| 132 |
} |
| 133 |
this.lastRefresh = now; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
protected abstract T create(GlobusResource targetResource) |
| 139 |
throws ResourceStoreException; |
| 140 |
|
| 141 |
public T getSecurityObject() throws ResourceStoreException { |
| 142 |
refresh(); |
| 143 |
return this.securityObject; |
| 144 |
} |
| 145 |
|
| 146 |
public boolean hasChanged() { |
| 147 |
return this.changed; |
| 148 |
} |
| 149 |
} |