| 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.CredentialException; |
| 19 |
import org.globus.gsi.X509Credential; |
| 20 |
|
| 21 |
import org.apache.commons.logging.LogFactory; |
| 22 |
|
| 23 |
import org.apache.commons.logging.Log; |
| 24 |
|
| 25 |
import java.io.BufferedInputStream; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.InputStream; |
| 28 |
import java.security.cert.CertificateEncodingException; |
| 29 |
|
| 30 |
import org.globus.util.GlobusResource; |
| 31 |
|
| 32 |
/** |
| 33 |
* JGLOBUS-87 : document me |
| 34 |
* |
| 35 |
* @author Tom Howe |
| 36 |
*/ |
| 37 |
public class ResourceProxyCredential extends AbstractResourceSecurityWrapper<X509Credential> |
| 38 |
implements CredentialWrapper { |
| 39 |
|
| 40 |
private Log logger = LogFactory.getLog(getClass().getCanonicalName()); |
| 41 |
|
| 42 |
public ResourceProxyCredential(String locationPattern) throws ResourceStoreException { |
| 43 |
super(false); |
| 44 |
init(locationPattern); |
| 45 |
} |
| 46 |
|
| 47 |
public ResourceProxyCredential(GlobusResource globusResource) throws ResourceStoreException { |
| 48 |
super(false); |
| 49 |
init(globusResource); |
| 50 |
} |
| 51 |
|
| 52 |
public ResourceProxyCredential(String filename, X509Credential object) throws ResourceStoreException { |
| 53 |
super(false); |
| 54 |
init(filename, object); |
| 55 |
} |
| 56 |
|
| 57 |
public ResourceProxyCredential(boolean inMemory, GlobusResource globusResource, X509Credential object) throws ResourceStoreException { |
| 58 |
super(inMemory); |
| 59 |
init(globusResource, object); |
| 60 |
} |
| 61 |
|
| 62 |
public X509Credential getCredential() throws ResourceStoreException { |
| 63 |
return getSecurityObject(); |
| 64 |
} |
| 65 |
|
| 66 |
protected X509Credential create(GlobusResource globusResource) throws ResourceStoreException { |
| 67 |
InputStream keyInputStream = null; |
| 68 |
InputStream certInputStream = null; |
| 69 |
try { |
| 70 |
keyInputStream = new BufferedInputStream(globusResource.getInputStream()); |
| 71 |
certInputStream = new BufferedInputStream(globusResource.getInputStream()); |
| 72 |
return new X509Credential(keyInputStream, certInputStream); |
| 73 |
} catch (IOException e) { |
| 74 |
throw new ResourceStoreException(e); |
| 75 |
} catch (CredentialException e) { |
| 76 |
throw new ResourceStoreException(e); |
| 77 |
} finally { |
| 78 |
|
| 79 |
if (keyInputStream != null) { |
| 80 |
try { |
| 81 |
keyInputStream.close(); |
| 82 |
} catch (Exception e) { |
| 83 |
logger.warn("Unable to close stream."); |
| 84 |
} |
| 85 |
} |
| 86 |
if (certInputStream != null) { |
| 87 |
try { |
| 88 |
certInputStream.close(); |
| 89 |
} catch (Exception e) { |
| 90 |
logger.warn("Unable to close stream."); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public void store() throws ResourceStoreException { |
| 97 |
try { |
| 98 |
X509Credential credential = getCredential(); |
| 99 |
credential.writeToFile(globusResource.getFile()); |
| 100 |
} catch (IOException ioe) { |
| 101 |
throw new ResourceStoreException(ioe); |
| 102 |
} catch (CertificateEncodingException e) { |
| 103 |
throw new ResourceStoreException(e); |
| 104 |
} |
| 105 |
} |
| 106 |
} |