[spring-extensions] branch maint-5 updated: Factory bean for KeyStore.

Scott Cantor cantor.2 at osu.edu
Thu Apr 12 17:03:07 EDT 2018


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch maint-5
in repository spring-extensions.

View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=c273b889ba17ed82304e71ebfa6f47bcc7488eb5

The following commit(s) were added to refs/heads/maint-5 by this push:
       new  c273b88   Factory bean for KeyStore.
c273b88 is described below

commit c273b889ba17ed82304e71ebfa6f47bcc7488eb5
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Apr 12 17:03:04 2018 -0400

    Factory bean for KeyStore.
---
 .../ext/spring/factory/KeyStoreFactoryBean.java    | 119 +++++++++++++++++++++
 1 file changed, 119 insertions(+)

diff --git a/src/main/java/net/shibboleth/ext/spring/factory/KeyStoreFactoryBean.java b/src/main/java/net/shibboleth/ext/spring/factory/KeyStoreFactoryBean.java
new file mode 100644
index 0000000..38f5f92
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/factory/KeyStoreFactoryBean.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.ext.spring.factory;
+
+import java.io.InputStream;
+import java.security.KeyStore;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.core.io.Resource;
+
+/**
+ * Spring bean factory for producing a {@link KeyStore} from a {@link Resource}.
+ * 
+ * @since 5.4.0
+ */
+public class KeyStoreFactoryBean implements FactoryBean<KeyStore> {
+
+    /** KeyStore resource. */
+    private Resource resource;
+
+    /** Password for the keystore. */
+    private String keyPass;
+
+    /** KeyStore type. */
+    private String type;
+
+    /** KeyStore provider. */
+    private String provider;
+    
+    /** The singleton instance of the private key produced by this factory. */
+    private KeyStore keyStore;
+
+    /**
+     * Set the resource containing the keystore.
+     * 
+     * @param res private key resource
+     */
+    public void setResource(@Nonnull final Resource res) {
+        resource = Constraint.isNotNull(res, "KeyStore resource cannot be null");
+    }
+
+    /**
+     * Set the password for the keystore.
+     * 
+     * @param password password for the keystore
+     */
+    public void setPassword(@Nullable final String password) {
+        keyPass = password;
+    }
+
+    /**
+     * Set the KeyStore provider, if non-defaulted.
+     * 
+     * @param prov provider
+     */
+    public void setProvider(@Nullable final String prov) {
+        provider = StringSupport.trimOrNull(prov);
+    }
+    
+    /**
+     * Set the KeyStore type, if non-defaulted.
+     * 
+     * @param typ KeyStore type
+     */
+    public void setType(@Nullable final String typ) {
+        type = StringSupport.trimOrNull(typ);
+    }
+    
+    /** {@inheritDoc} */
+    @Override public KeyStore getObject() throws Exception {
+        if (keyStore == null) {
+            if (provider != null && type != null) {
+                keyStore = KeyStore.getInstance(type, provider);
+            }
+            else if (type != null) {
+                keyStore = KeyStore.getInstance(type);
+            }
+            else {
+                keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
+            }
+            try (final InputStream is = resource.getInputStream()) {
+                keyStore.load(is, keyPass.toCharArray());
+            }
+        }
+
+        return keyStore;
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public Class<?> getObjectType() {
+        return KeyStore.class;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isSingleton() {
+        return true;
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list