1 20 package org.apache.mina.integration.spring.ssl; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.security.KeyStore ; 28 29 import org.springframework.beans.factory.config.AbstractFactoryBean; 30 import org.springframework.core.io.Resource; 31 import org.springframework.util.Assert; 32 33 41 public class KeyStoreFactoryBean extends AbstractFactoryBean { 42 private String type = "JKS"; 43 44 private String provider = null; 45 46 private char[] password = null; 47 48 private File file = null; 49 50 private Resource resource = null; 51 52 58 protected Object createInstance() throws Exception { 59 if (file == null && resource == null) { 60 throw new IllegalArgumentException ("Required property missing. " 61 + "Either 'file' or 'resource' have to be specified"); 62 } 63 64 KeyStore ks = null; 65 if (provider == null) { 66 ks = KeyStore.getInstance(type); 67 } else { 68 ks = KeyStore.getInstance(type, provider); 69 } 70 71 InputStream is = null; 72 if (file != null) { 73 is = new BufferedInputStream (new FileInputStream (file)); 74 } else { 75 is = resource.getInputStream(); 76 } 77 78 try { 79 ks.load(is, password); 80 } finally { 81 try { 82 is.close(); 83 } catch (IOException ignored) { 84 } 85 } 86 87 return ks; 88 } 89 90 public Class getObjectType() { 91 return KeyStore .class; 92 } 93 94 100 public void setFile(File file) { 101 this.file = file; 102 } 103 104 111 public void setPassword(String password) { 112 if (password != null) { 113 this.password = password.toCharArray(); 114 } else { 115 this.password = null; 116 } 117 } 118 119 125 public void setProvider(String provider) { 126 this.provider = provider; 127 } 128 129 135 public void setResource(Resource resource) { 136 this.resource = resource; 137 } 138 139 147 public void setType(String type) { 148 Assert.notNull(type, "Property 'type' may not be null"); 149 this.type = type; 150 } 151 } 152 | Popular Tags |