Merge "Remove redundant type specifiers"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / SslKeyStore.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.InputStream;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Class for storing keys.
21  *
22  * @author michal.polkorab
23  */
24 public final class SslKeyStore {
25
26     private static final Logger LOG = LoggerFactory.getLogger(SslKeyStore.class);
27
28     private SslKeyStore() {
29     }
30
31     /**
32      * InputStream instance of key - key location is on classpath.
33      *
34      * @param filename keystore location
35      * @param pathType keystore location type - "classpath" or "path"
36      *
37      * @return key as InputStream
38      */
39     public static InputStream asInputStream(String filename, PathType pathType) {
40         InputStream in;
41         switch (pathType) {
42             case CLASSPATH:
43                 in = SslKeyStore.class.getResourceAsStream(filename);
44                 if (in == null) {
45                     throw new IllegalStateException("KeyStore file not found: "
46                             + filename);
47                 }
48                 break;
49             case PATH:
50                 LOG.debug("Current dir using System: {}",
51                         System.getProperty("user.dir"));
52                 File keystorefile = new File(filename);
53                 try {
54                     in = new FileInputStream(keystorefile);
55                 } catch (FileNotFoundException e) {
56                     throw new IllegalStateException("KeyStore file not found: "
57                             + filename,e);
58                 }
59                 break;
60             default:
61                 throw new IllegalArgumentException("Unknown path type: " + pathType);
62         }
63         return in;
64     }
65 }