BUG 2245 - Fixed 'Preserve Stack Trace'
[openflowjava.git] / 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
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Class for storing keys
22  *
23  * @author michal.polkorab
24  */
25 public final class SslKeyStore {
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(SslKeyStore.class);
28
29     private SslKeyStore() {
30         throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
31     }
32
33     /**
34      * InputStream instance of key - key location is on classpath
35      * @param filename keystore location
36      * @param pathType keystore location type - "classpath" or "path"
37      *
38      * @return key as InputStream
39      */
40     public static InputStream asInputStream(String filename, PathType pathType) {
41         InputStream in;
42         switch (pathType) {
43         case CLASSPATH:
44             in = SslKeyStore.class.getResourceAsStream(filename);
45             if (in == null) {
46                 throw new IllegalStateException("KeyStore file not found: "
47                         + filename);
48             }
49             break;
50         case PATH:
51             LOGGER.debug("Current dir using System:"
52                     + System.getProperty("user.dir"));
53             File keystorefile = new File(filename);
54             try {
55                 in = new FileInputStream(keystorefile);
56             } catch (FileNotFoundException e) {
57                 throw new IllegalStateException("KeyStore file not found: "
58                         + filename,e);
59             }
60             break;
61         default:
62             throw new IllegalArgumentException("Unknown path type: " + pathType);
63         }
64         return in;
65     }
66 }