5c49c083d6d1fbbda919f3cc63cffcb1036a2164
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tls / SslKeyStore.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.protocol.pcep.impl.tls;
9
10 import com.google.common.base.Preconditions;
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.params.xml.ns.yang.controller.pcep.app.config.rev160707.PathType;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public final class SslKeyStore {
20
21     private static final Logger LOG = LoggerFactory.getLogger(SslKeyStore.class);
22
23     private SslKeyStore() {
24         throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
25     }
26
27     /**
28      * InputStream instance of key - key location is on classpath or specific path
29      *
30      * @param filename
31      *          keystore location
32      * @param pathType
33      *          keystore location type - "classpath" or "path"
34      *
35      * @return key as InputStream
36      */
37     public static InputStream asInputStream(final String filename, final PathType pathType) {
38         InputStream in;
39         switch (pathType) {
40         case CLASSPATH:
41             in = SslKeyStore.class.getResourceAsStream(filename);
42             Preconditions.checkArgument(in != null, "KeyStore file not found: %s", filename);
43             break;
44         case PATH:
45             LOG.debug("Current dir using System: {}", System.getProperty("user.dir"));
46             final File keystorefile = new File(filename);
47             try {
48                 in = new FileInputStream(keystorefile);
49             } catch (final FileNotFoundException e) {
50                 throw new IllegalStateException("KeyStore file not found: " + filename,e);
51             }
52             break;
53         default:
54             throw new IllegalArgumentException("Unknown path type: " + pathType);
55         }
56         return in;
57     }
58 }
59