View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    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  /**
    34   * Spring {@link org.springframework.beans.factory.FactoryBean} implementation
    35   * which makes it possible to configure {@link java.security.KeyStore} instances
    36   * using Spring.
    37   *
    38   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    39   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    40   */
    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      /**
    53       * Creates a new {@link KeyStore}. This method will be called
    54       * by the base class when Spring creates a bean using this FactoryBean.
    55       *
    56       * @return the {@link KeyStore} instance.
    57       */
    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      /**
    95       * Sets the file which contains the key store. Either this
    96       * property or {@link #setProvider(String)} have to be set.
    97       *
    98       * @param file the file to load the key store from.
    99       */
    100     public void setFile(File file) {
    101         this.file = file;
    102     }
    103
    104     /**
    105      * Sets the key store password. If this value is <code>null</code> no
    106      * password will be used to check the integrity of the key store.
    107      *
    108      * @param password the password or <code>null</code> if no password is
    109      *        needed.
    110      */
    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     /**
    120      * Sets the name of the provider to use when creating the key store. The
    121      * default is to use the platform default provider.
    122      *
    123      * @param provider the name of the provider, e.g. SUN.
    124      */
    125     public void setProvider(String provider) {
    126         this.provider = provider;
    127     }
    128
    129     /**
    130      * Sets a Spring {@link Resource} which contains the key store. Either this
    131      * property or {@link #setFile(File)} have to be set.
    132      *
    133      * @param resource the resource to load the key store from.
    134      */
    135     public void setResource(Resource resource) {
    136         this.resource = resource;
    137     }
    138
    139     /**
    140      * Sets the type of key store to create. The default is to create a
    141      * JKS key store.
    142      *
    143      * @param type the type to use when creating the key store.
    144      * @throws IllegalArgumentException if the specified value is
    145      *         <code>null</code>.
    146      */
    147     public void setType(String type) {
    148         Assert.notNull(type, "Property 'type' may not be null");
    149         this.type = type;
    150     }
    151 }