summaryrefslogtreecommitdiff
path: root/qpid/java/common
diff options
context:
space:
mode:
Diffstat (limited to 'qpid/java/common')
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/properties/ConnectionStartProperties.java27
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java11
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java18
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java32
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java64
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/SystemUtils.java48
6 files changed, 140 insertions, 60 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/properties/ConnectionStartProperties.java b/qpid/java/common/src/main/java/org/apache/qpid/properties/ConnectionStartProperties.java
index 4f88fe7071..3569b4b460 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/properties/ConnectionStartProperties.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/properties/ConnectionStartProperties.java
@@ -20,9 +20,6 @@
*/
package org.apache.qpid.properties;
-import java.lang.management.ManagementFactory;
-import java.lang.management.RuntimeMXBean;
-
import org.apache.qpid.transport.util.Logger;
import org.apache.qpid.util.SystemUtils;
@@ -62,30 +59,18 @@ public class ConnectionStartProperties
public static final String QPID_CONFIRMED_PUBLISH_SUPPORTED = "qpid.confirmed_publish_supported";
- public static int _pid;
+ public static final int _pid;
public static final String _platformInfo;
static
{
- RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();
- String processName = rtb.getName();
- if (processName != null && processName.indexOf('@') > 0)
- {
- try
- {
- _pid = Integer.parseInt(processName.substring(0,processName.indexOf('@')));
- }
- catch(Exception e)
- {
- LOGGER.warn("Unable to get the PID due to error",e);
- _pid = -1;
- }
- }
- else
+
+ _pid = SystemUtils.getProcessPidAsInt();
+
+ if (_pid == -1)
{
- LOGGER.warn("Unable to get the PID due to unsupported format : " + processName);
- _pid = -1;
+ LOGGER.warn("Unable to get the process's PID");
}
StringBuilder fullSystemInfo = new StringBuilder(System.getProperty("java.runtime.name"));
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
index 0dccf37979..c61684e2bb 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
@@ -27,6 +27,7 @@ import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import java.io.IOException;
import java.net.Socket;
+import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Principal;
@@ -50,6 +51,16 @@ public class QpidClientX509KeyManager extends X509ExtendedKeyManager
this.delegate = (X509ExtendedKeyManager)kmf.getKeyManagers()[0];
}
+ public QpidClientX509KeyManager(String alias, URL keyStoreUrl, String keyStoreType,
+ String keyStorePassword, String keyManagerFactoryAlgorithmName) throws GeneralSecurityException, IOException
+ {
+ this.alias = alias;
+ KeyStore ks = SSLUtil.getInitializedKeyStore(keyStoreUrl,keyStorePassword,keyStoreType);
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithmName);
+ kmf.init(ks, keyStorePassword.toCharArray());
+ this.delegate = (X509ExtendedKeyManager)kmf.getKeyManagers()[0];
+ }
+
public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)
{
log.debug("chooseClientAlias:Returning alias " + alias);
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
index 98229fd2a1..b6ae2ab4a3 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
@@ -24,6 +24,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Principal;
@@ -248,6 +249,23 @@ public class SSLUtil
return ks;
}
+ public static KeyStore getInitializedKeyStore(URL storePath, String storePassword, String keyStoreType) throws GeneralSecurityException, IOException
+ {
+ KeyStore ks = KeyStore.getInstance(keyStoreType);
+ try(InputStream in = storePath.openStream())
+ {
+ if (in == null && !"PKCS11".equalsIgnoreCase(keyStoreType)) // PKCS11 will not require an explicit path
+ {
+ throw new IOException("Unable to load keystore resource: " + storePath);
+ }
+
+ char[] storeCharPassword = storePassword == null ? null : storePassword.toCharArray();
+
+ ks.load(in, storeCharPassword);
+ }
+ return ks;
+ }
+
public static void removeSSLv3Support(final SSLEngine engine)
{
List<String> enabledProtocols = Arrays.asList(engine.getEnabledProtocols());
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java
new file mode 100644
index 0000000000..16c5012d88
--- /dev/null
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.util;
+
+import javax.xml.bind.DatatypeConverter;
+
+public class DataUrlUtils
+{
+ public static String getDataUrlForBytes(final byte[] bytes)
+ {
+ StringBuilder inlineURL = new StringBuilder("data:;base64,");
+ inlineURL.append(DatatypeConverter.printBase64Binary(bytes));
+ return inlineURL.toString();
+ }
+}
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
index dd347b54eb..70607f49db 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
@@ -22,6 +22,7 @@ package org.apache.qpid.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -51,39 +52,32 @@ public class FileUtils
*
* @return The contents of the file.
*/
- public static String readFileAsString(String filename)
+ public static byte[] readFileAsBytes(String filename)
{
- BufferedInputStream is = null;
- try
+ try(BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename)))
{
- try
- {
- is = new BufferedInputStream(new FileInputStream(filename));
- }
- catch (FileNotFoundException e)
- {
- throw new RuntimeException(e);
- }
-
return readStreamAsString(is);
}
- finally
+ catch (IOException e)
{
- if (is != null)
- {
- try
- {
- is.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- }
+ throw new RuntimeException(e);
}
}
+
+ /**
+ * Reads a text file as a string.
+ *
+ * @param filename The name of the file.
+ *
+ * @return The contents of the file.
+ */
+ public static String readFileAsString(String filename)
+ {
+ return new String(readFileAsBytes(filename));
+ }
+
/**
* Reads a text file as a string.
*
@@ -93,18 +87,15 @@ public class FileUtils
*/
public static String readFileAsString(File file)
{
- BufferedInputStream is = null;
-
- try
+ try(BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)))
{
- is = new BufferedInputStream(new FileInputStream(file));
+
+ return new String(readStreamAsString(is));
}
- catch (FileNotFoundException e)
+ catch (IOException e)
{
throw new RuntimeException(e);
}
-
- return readStreamAsString(is);
}
/**
@@ -115,23 +106,20 @@ public class FileUtils
*
* @return The contents of the reader.
*/
- private static String readStreamAsString(BufferedInputStream is)
+ private static byte[] readStreamAsString(BufferedInputStream is)
{
- try
+ try(ByteArrayOutputStream inBuffer = new ByteArrayOutputStream())
{
byte[] data = new byte[4096];
- StringBuffer inBuffer = new StringBuffer();
-
int read;
while ((read = is.read(data)) != -1)
{
- String s = new String(data, 0, read);
- inBuffer.append(s);
+ inBuffer.write(data, 0, read);
}
- return inBuffer.toString();
+ return inBuffer.toByteArray();
}
catch (IOException e)
{
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/SystemUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/SystemUtils.java
index 55c7ae9b96..5825276760 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/util/SystemUtils.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/SystemUtils.java
@@ -20,6 +20,9 @@
*/
package org.apache.qpid.util;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+
/**
* SystemUtils provides some simple helper methods for working with the current
* Operating System.
@@ -38,9 +41,29 @@ public class SystemUtils
private static final String _osName = System.getProperty("os.name", UNKNOWN_OS);
private static final String _osVersion = System.getProperty("os.version", UNKNOWN_VERSION);
private static final String _osArch = System.getProperty("os.arch", UNKNOWN_ARCH);
-
private static final boolean _isWindows = _osName.toLowerCase().contains("windows");
+ /** Process identifier of underlying process or null if it cannot be determined */
+ private static final String _osPid;
+ private static int _osPidInt;
+
+ static
+ {
+ RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();
+ String processName = rtb.getName();
+ int atIndex;
+ if(processName != null && (atIndex = processName.indexOf('@')) > 0)
+ {
+ _osPid = processName.substring(0, atIndex);
+ _osPidInt = parseInt(_osPid, -1);
+ }
+ else
+ {
+ _osPid = null;
+ }
+ }
+
+
private SystemUtils()
{
}
@@ -60,6 +83,16 @@ public class SystemUtils
return _osArch;
}
+ public final static String getProcessPid()
+ {
+ return _osPid;
+ }
+
+ public final static int getProcessPidAsInt()
+ {
+ return _osPidInt;
+ }
+
public final static boolean isWindows()
{
return _isWindows;
@@ -78,4 +111,17 @@ public class SystemUtils
{
return _osName + " " + _osVersion + " " + _osArch;
}
+
+ private static int parseInt(String str, int defaultVal)
+ {
+ try
+ {
+ return Integer.parseInt(str);
+ }
+ catch(NumberFormatException e)
+ {
+ return defaultVal;
+ }
+ }
+
}