Remove bundle extension (de)serializers
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / SslKeyStore.java
index 8d4e23744a0d7a31650a456468a726ace8753d84..e4efea9f765dcfdcfc58cc46537148cba747b863 100644 (file)
@@ -1,8 +1,22 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.openflowjava.protocol.impl.core;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
 
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Class for storing keys
  *
@@ -10,32 +24,43 @@ import java.io.InputStream;
  */
 public final class SslKeyStore {
 
-    private static final String filename = "/key.bin";
+    private static final Logger LOG = LoggerFactory.getLogger(SslKeyStore.class);
+
+    private SslKeyStore() {
+        throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
+    }
 
     /**
-     * InputStream instance of key
+     * InputStream instance of key - key location is on classpath
+     * @param filename keystore location
+     * @param pathType keystore location type - "classpath" or "path"
      *
      * @return key as InputStream
      */
-    public static InputStream asInputStream() {
-        InputStream in = SslKeyStore.class.getResourceAsStream(filename);
-        if (in == null) {
-            throw new IllegalStateException("KeyStore file not found: " + filename);
+    public static InputStream asInputStream(String filename, PathType pathType) {
+        InputStream in;
+        switch (pathType) {
+        case CLASSPATH:
+            in = SslKeyStore.class.getResourceAsStream(filename);
+            if (in == null) {
+                throw new IllegalStateException("KeyStore file not found: "
+                        + filename);
+            }
+            break;
+        case PATH:
+            LOG.debug("Current dir using System:"
+                    + System.getProperty("user.dir"));
+            File keystorefile = new File(filename);
+            try {
+                in = new FileInputStream(keystorefile);
+            } catch (FileNotFoundException e) {
+                throw new IllegalStateException("KeyStore file not found: "
+                        + filename,e);
+            }
+            break;
+        default:
+            throw new IllegalArgumentException("Unknown path type: " + pathType);
         }
         return in;
     }
-
-    /**
-     * @return certificate password as char[]
-     */
-    public static char[] getCertificatePassword() {
-        return "secret".toCharArray();
-    }
-
-    /**
-     * @return KeyStore password as char[]
-     */
-    public static char[] getKeyStorePassword() {
-        return "secret".toCharArray();
-    }
-}
\ No newline at end of file
+}