| 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 java.io.FileNotFoundException; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
import java.security.cert.CertificateEncodingException; |
| 25 |
|
| 26 |
import org.apache.commons.io.FileUtils; |
| 27 |
import org.globus.util.GlobusResource; |
| 28 |
|
| 29 |
/** |
| 30 |
* Fill Me |
| 31 |
*/ |
| 32 |
|
| 33 |
public class CertKeyCredential implements SecurityObjectWrapper<X509Credential>, Storable, CredentialWrapper { |
| 34 |
|
| 35 |
protected GlobusResource globusCertFile; |
| 36 |
protected GlobusResource globusKeyFile; |
| 37 |
|
| 38 |
private long certLastModified = -1; |
| 39 |
private long keyLastModified = -1; |
| 40 |
private X509Credential credential; |
| 41 |
private boolean changed; |
| 42 |
|
| 43 |
public CertKeyCredential(GlobusResource certResource, GlobusResource keyResource) throws ResourceStoreException { |
| 44 |
init(certResource, keyResource); |
| 45 |
} |
| 46 |
|
| 47 |
public CertKeyCredential(GlobusResource certResource, GlobusResource keyResource, X509Credential credential) |
| 48 |
throws ResourceStoreException { |
| 49 |
this.globusCertFile = certResource; |
| 50 |
try { |
| 51 |
if (!certResource.exists()) { |
| 52 |
FileUtils.touch(certResource.getFile()); |
| 53 |
this.certLastModified = certResource.lastModified(); |
| 54 |
} |
| 55 |
this.globusKeyFile = keyResource; |
| 56 |
if (!keyResource.exists()) { |
| 57 |
FileUtils.touch(keyResource.getFile()); |
| 58 |
this.keyLastModified = keyResource.lastModified(); |
| 59 |
} |
| 60 |
} catch (IOException e) { |
| 61 |
throw new ResourceStoreException(e); |
| 62 |
} |
| 63 |
this.credential = credential; |
| 64 |
} |
| 65 |
|
| 66 |
protected void init(GlobusResource initCertResource, GlobusResource initKeyResource) throws ResourceStoreException { |
| 67 |
|
| 68 |
if ((initCertResource == null) || (initKeyResource == null)) { |
| 69 |
throw new IllegalArgumentException(); |
| 70 |
} |
| 71 |
|
| 72 |
this.globusCertFile = initCertResource; |
| 73 |
this.globusKeyFile = initKeyResource; |
| 74 |
this.credential = createObject(this.globusCertFile, this.globusKeyFile); |
| 75 |
try { |
| 76 |
this.certLastModified = this.globusCertFile.lastModified(); |
| 77 |
this.keyLastModified = this.globusKeyFile.lastModified(); |
| 78 |
} catch (IOException ioe) { |
| 79 |
throw new ResourceStoreException(ioe); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
protected void init(GlobusResource initCertFile, GlobusResource keyResource, X509Credential initCredential) |
| 84 |
throws ResourceStoreException { |
| 85 |
|
| 86 |
if (initCredential == null) { |
| 87 |
// JGLOBUS-88 : better exception? |
| 88 |
throw new IllegalArgumentException("Object cannot be null"); |
| 89 |
} |
| 90 |
this.credential = initCredential; |
| 91 |
this.globusCertFile = initCertFile; |
| 92 |
this.globusKeyFile = keyResource; |
| 93 |
} |
| 94 |
|
| 95 |
public void refresh() throws ResourceStoreException { |
| 96 |
long cLatestLastModified; |
| 97 |
long kLatestLastModified; |
| 98 |
this.changed = false; |
| 99 |
try { |
| 100 |
cLatestLastModified = this.globusCertFile.lastModified(); |
| 101 |
kLatestLastModified = this.globusKeyFile.lastModified(); |
| 102 |
} catch (IOException ioe) { |
| 103 |
throw new ResourceStoreException(ioe); |
| 104 |
} |
| 105 |
if ((this.certLastModified < cLatestLastModified) || (this.keyLastModified < kLatestLastModified)) { |
| 106 |
this.credential = createObject(this.globusCertFile, this.globusKeyFile); |
| 107 |
this.certLastModified = cLatestLastModified; |
| 108 |
this.keyLastModified = kLatestLastModified; |
| 109 |
this.changed = true; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public GlobusResource getCertificateFile() { |
| 114 |
return this.globusCertFile; |
| 115 |
} |
| 116 |
|
| 117 |
public GlobusResource getKeyFile() { |
| 118 |
return this.globusKeyFile; |
| 119 |
} |
| 120 |
|
| 121 |
// for creation of credential from a file |
| 122 |
protected X509Credential createObject(GlobusResource certSource, GlobusResource keySource) |
| 123 |
throws ResourceStoreException { |
| 124 |
InputStream certIns; |
| 125 |
InputStream keyIns; |
| 126 |
try { |
| 127 |
certIns = certSource.getInputStream(); |
| 128 |
keyIns = keySource.getInputStream(); |
| 129 |
return new X509Credential(certIns, keyIns); |
| 130 |
} catch (FileNotFoundException e) { |
| 131 |
throw new ResourceStoreException(e); |
| 132 |
} catch (CredentialException e) { |
| 133 |
throw new ResourceStoreException(e); |
| 134 |
} catch (IOException ioe) { |
| 135 |
throw new ResourceStoreException(ioe); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public X509Credential getSecurityObject() throws ResourceStoreException { |
| 140 |
refresh(); |
| 141 |
return credential; |
| 142 |
} |
| 143 |
|
| 144 |
public boolean hasChanged() { |
| 145 |
return this.changed; |
| 146 |
} |
| 147 |
|
| 148 |
public X509Credential getCredential() throws ResourceStoreException { |
| 149 |
return getSecurityObject(); |
| 150 |
} |
| 151 |
|
| 152 |
public void store() throws ResourceStoreException { |
| 153 |
try { |
| 154 |
this.credential.writeToFile(this.globusCertFile.getFile(), this.globusKeyFile.getFile()); |
| 155 |
} catch (IOException e) { |
| 156 |
throw new ResourceStoreException(e); |
| 157 |
} catch (CertificateEncodingException e) { |
| 158 |
throw new ResourceStoreException(e); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public String getAlias() { |
| 163 |
return null; //To change body of implemented methods use File | Settings | File Templates. |
| 164 |
} |
| 165 |
} |