You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by as...@apache.org on 2009/04/21 12:45:07 UTC
svn commit: r767111 - in /synapse/trunk/java/modules:
core/src/main/java/org/apache/synapse/
core/src/main/java/org/apache/synapse/security/enumeration/
core/src/main/java/org/apache/synapse/security/secret/handler/
core/src/main/java/org/apache/synaps...
Author: asankha
Date: Tue Apr 21 10:45:06 2009
New Revision: 767111
URL: http://svn.apache.org/viewvc?rev=767111&view=rev
Log:
commit files I have missed from the patch for SYNAPSE-526
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/JmxAdapter.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/CipherOperationMode.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/EncodingType.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/secret/handler/JBossEncryptionSecretCallbackHandler.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/tool/EncodingHelper.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxConfigurationConstants.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformation.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformationFactory.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxSecretAuthenticator.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRegistrar.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRepository.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretConfigurationConstants.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformation.java
synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformationFactory.java
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/JmxAdapter.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/JmxAdapter.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/JmxAdapter.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/JmxAdapter.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,229 @@
+/*
+ * 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.synapse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.commons.util.RMIRegistryController;
+import org.apache.synapse.commons.util.jmx.JmxInformation;
+import org.apache.synapse.commons.util.jmx.JmxSecretAuthenticator;
+
+import javax.management.MBeanServer;
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXConnectorServerFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.rmi.ssl.SslRMIClientSocketFactory;
+import javax.rmi.ssl.SslRMIServerSocketFactory;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * JMX Adaptor class providing a JMX server connector to be able to query MBeans via JConsole or any
+ * other JMX-compatible management solution.<br>
+ * The provided JNDI port will be used to create a local RMI registry. If no RMI port will be
+ * provided dynamic RMI ports will be used for remote MBean queries.<br>
+ * If the JMXAdaptor shall be used in a firewalled environment, additionally a fixed RMI port should
+ * be provided and both ports should be opened in the firewall.<br>
+ * JMX URL used if only JNDI port is provided:<br>
+ * <code>service:jmx:rmi:///jndi/rmi://<hostname>:<jndiPort>/synapse</code><br>
+ * JMX URL used if JNDI port and RMI port are provided:<br>
+ * <code>service:jmx:rmi://<hostname>:<rmiPort>/jndi/rmi://<hostname>:<jndiPort>/synapse</code><br>
+ */
+public class JmxAdapter {
+
+ /**
+ * Logger of this class.
+ */
+ private static Log log = LogFactory.getLog(JmxAdapter.class);
+
+ /**
+ * Base port to start with if automatic free port detection is used (default). Configurable in
+ * synapse.properties via synapse.jmx.jndiPort=0.
+ */
+ private static final int JNDI_AUTO_PORT_OFFSET = 1099;
+
+ /**
+ * Encapsulates all information needed to configure the JMX Adapter.
+ */
+ private JmxInformation jmxInformation;
+
+ /**
+ * @see JMXConnectorServer
+ */
+ private JMXConnectorServer connectorServer;
+
+ /**
+ * Creates a new instance of a JMX Adaptor using the provided JMX information.
+ *
+ * @param jmxInformation any JMX related information
+ */
+ public JmxAdapter(JmxInformation jmxInformation) {
+ this.jmxInformation = jmxInformation;
+ }
+
+ /**
+ * Lazily creates the RMI registry and starts the JMX connector server based on the
+ *
+ * @throws SynapseException if the JMX configuration is erroneous and/or the connector server
+ * cannot be started
+ */
+ public void start() {
+ initConfiguration();
+
+ try {
+ boolean registryCreated = false;
+ int jndiPort = jmxInformation.getJndiPort();
+
+ // automatic detection starting at base port
+ if (jndiPort == 0) {
+ jndiPort = JNDI_AUTO_PORT_OFFSET;
+ for (int retries = 0; !registryCreated && (retries < 100); retries++) {
+ try {
+ RMIRegistryController.getInstance().createLocalRegistry(jndiPort);
+ registryCreated = true;
+ } catch (Exception ignored) {
+ jndiPort++;
+ log.warn("Trying alternate port " + jndiPort);
+ }
+ }
+ jmxInformation.setJndiPort(jndiPort);
+ } else {
+ RMIRegistryController.getInstance().createLocalRegistry(jndiPort);
+ registryCreated = true;
+ }
+
+ if (registryCreated) {
+ jmxInformation.updateJMXUrl();
+ JMXServiceURL url = new JMXServiceURL(jmxInformation.getJmxUrl());
+ Map<String, Object> env = createContextMap();
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+ connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
+ try {
+ connectorServer.start();
+ } catch (IOException ex) {
+ log.warn("Cannot start JMXConnectorServer on " + jmxInformation.getJmxUrl(), ex);
+ }
+ }
+ } catch (Exception ex) {
+ log.error("Error while setting up remote JMX connector", ex);
+ }
+ }
+
+ /**
+ * Stops the JMX connector server.
+ */
+ public void stop() {
+ if (connectorServer != null) {
+ log.info("JMXConnectorServer stopping on " + jmxInformation.getJmxUrl());
+ try {
+ connectorServer.stop();
+ RMIRegistryController.getInstance().removeLocalRegistry(jmxInformation.getJndiPort());
+ jmxInformation = null;
+ } catch (IOException ex) {
+ log.error("Error while stopping remote JMX connector", ex);
+ }
+ connectorServer = null;
+ }
+ }
+
+ /**
+ * Initialized the JMX configuration.
+ *
+ * @throws SynapseException if the port or host configuration is erroneous
+ */
+ private void initConfiguration() {
+ int jndiPort = jmxInformation.getJndiPort();
+ if ((jndiPort < 0) || (65535 < jndiPort)) {
+ throw new SynapseException("JNDI Port for Remote Registry not properly configured");
+ }
+
+ int rmiPort = jmxInformation.getRmiPort();
+ if ((rmiPort < 0) || (65535 < rmiPort)) {
+ rmiPort = 0;
+ log.info("No or invalid value specified for JMX RMI port - using dynamic port");
+ }
+
+ String hostname = jmxInformation.getHostName();
+ if ((hostname == null) || (hostname.trim().length() == 0)) {
+ try {
+ InetAddress address = InetAddress.getLocalHost();
+ jmxInformation.setHostName(address.getHostName());
+ } catch (UnknownHostException ex) {
+ throw new SynapseException("Hostname of loopback could not be determined", ex);
+ }
+ }
+ }
+
+ /**
+ * Determines whether the JMX Connector server has been started and is running.
+ *
+ * @return true, if the connector server is running, otherwise false
+ */
+ public boolean isRunning() {
+ return connectorServer != null && connectorServer.isActive();
+ }
+
+ public JmxInformation getJmxInformation() {
+ return jmxInformation;
+ }
+
+ public void setJmxInformation(JmxInformation jmxInformation) {
+ this.jmxInformation = jmxInformation;
+ }
+
+ /**
+ * Creates an environment context map containing the configuration used to start the
+ * server connector.
+ *
+ * @return an environment context map containing the configuration used to start the server
+ * connector
+ */
+ private Map<String, Object> createContextMap() {
+ Map<String, Object> env = new HashMap<String, Object>();
+
+ if (jmxInformation.isAuthenticate()) {
+
+ if (jmxInformation.getRemotePasswordFile() != null) {
+ env.put("jmx.remote.x.password.file", jmxInformation.getRemotePasswordFile());
+ } else {
+ env.put(JMXConnectorServer.AUTHENTICATOR,
+ new JmxSecretAuthenticator(jmxInformation.getSecretInformation()));
+ }
+
+ if (jmxInformation.getRemoteAccessFile() != null) {
+ env.put("jmx.remote.x.access.file", jmxInformation.getRemoteAccessFile());
+ }
+ } else {
+ log.warn("Using unsecured JMX remote access!");
+ }
+
+ if (jmxInformation.isRemoteSSL()) {
+ log.info("Activated SSL communication");
+ env.put("jmx.remote.rmi.client.socket.factory", new SslRMIClientSocketFactory());
+ env.put("jmx.remote.rmi.server.socket.factory", new SslRMIServerSocketFactory());
+ }
+
+ return env;
+ }
+}
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/CipherOperationMode.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/CipherOperationMode.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/CipherOperationMode.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/CipherOperationMode.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,27 @@
+/*
+ * 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.synapse.security.enumeration;
+
+/**
+ * Cipher Operation Mode.
+ */
+public enum CipherOperationMode {
+ ENCRYPT,
+ DECRYPT;
+}
\ No newline at end of file
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/EncodingType.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/EncodingType.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/EncodingType.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/enumeration/EncodingType.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,27 @@
+/*
+ * 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.synapse.security.enumeration;
+
+/**
+ * Encoding Types
+ */
+public enum EncodingType {
+ BASE64,
+ BIGINTEGER16;
+}
\ No newline at end of file
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/secret/handler/JBossEncryptionSecretCallbackHandler.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/secret/handler/JBossEncryptionSecretCallbackHandler.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/secret/handler/JBossEncryptionSecretCallbackHandler.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/secret/handler/JBossEncryptionSecretCallbackHandler.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,65 @@
+/*
+ * 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.synapse.security.secret.handler;
+
+import org.apache.synapse.commons.util.secret.AbstractSecretCallbackHandler;
+import org.apache.synapse.commons.util.secret.SingleSecretCallback;
+import org.apache.synapse.security.definition.CipherInformation;
+import org.apache.synapse.security.enumeration.CipherOperationMode;
+import org.apache.synapse.security.enumeration.EncodingType;
+import org.apache.synapse.security.wrappers.CipherWrapper;
+
+import java.io.ByteArrayInputStream;
+
+/**
+ * SecretCallbackHandler implementation which is compatible to the default encryption used
+ * within the JBoss Application Server to decrypt database passwords.
+ */
+public class JBossEncryptionSecretCallbackHandler extends AbstractSecretCallbackHandler {
+
+ private static final String PASSPHRASE = "jaas is the way";
+ private static final String ALGORITHM = "Blowfish";
+
+ /**
+ * Decrypts the encrypted secret provided by the specified callback handler.
+ *
+ * @param singleSecretCallback The singleSecretCallback which secret has to be decrypted
+ */
+ @Override
+ protected void handleSingleSecretCallback(SingleSecretCallback singleSecretCallback) {
+ singleSecretCallback.setSecret(decrypt(singleSecretCallback.getId()));
+ }
+
+ /**
+ * Decrypts the encrypted secret using the Blowfish algorithm and the same hard-coded
+ * passphrase the JBoss application server uses to decrypt database passwords.
+ *
+ * @param encryptedSecret the encrypted secret
+ *
+ * @return the decrypted secret.
+ */
+ private static String decrypt(String encryptedSecret) {
+ CipherInformation cipherInformation = new CipherInformation();
+ cipherInformation.setAlgorithm(ALGORITHM);
+ cipherInformation.setCipherOperationMode(CipherOperationMode.DECRYPT);
+ cipherInformation.setInType(EncodingType.BIGINTEGER16);
+ CipherWrapper cipherWrapper = new CipherWrapper(cipherInformation, PASSPHRASE);
+ return cipherWrapper.getSecret(new ByteArrayInputStream(encryptedSecret.getBytes()));
+ }
+}
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/tool/EncodingHelper.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/tool/EncodingHelper.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/tool/EncodingHelper.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/tool/EncodingHelper.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,113 @@
+/*
+* 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.synapse.security.tool;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.security.enumeration.EncodingType;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+
+/**
+ * Helper to handle encoding and decoding of data streams.
+ */
+public final class EncodingHelper {
+
+ private static Log log = LogFactory.getLog(EncodingHelper.class);
+
+ private EncodingHelper() {
+ }
+
+ /**
+ * Encodes the provided ByteArrayOutputStream using the specified encoding type.
+ *
+ * @param baos The ByteArrayOutputStream to encode
+ * @param encodingType The encoding to use
+ *
+ * @return The encoded ByteArrayOutputStream as a String
+ */
+ public static String encode(ByteArrayOutputStream baos, EncodingType encodingType) {
+ String encodedString;
+
+ switch(encodingType) {
+ case BASE64:
+ if (log.isDebugEnabled()) {
+ log.debug("base64 encoding on output ");
+ }
+ encodedString = new BASE64Encoder().encode(baos.toByteArray());
+ break;
+ case BIGINTEGER16:
+ if (log.isDebugEnabled()) {
+ log.debug("BigInteger 16 encoding on output ");
+ }
+ encodedString = new BigInteger(baos.toByteArray()).toString(16);
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported encoding type");
+ }
+
+ return encodedString;
+ }
+
+ /**
+ * Decodes the provided InputStream using the specified encoding type.
+ *
+ * @param inputStream The InputStream to decode
+ * @param encodingType The encoding to use
+ *
+ * @throws IOException If an error occurs decoding the input stream
+ * @throws IllegalArgumentException if the specified encodingType is not supported
+ *
+ * @return The decoded InputStream
+ */
+ public static InputStream decode(InputStream inputStream, EncodingType encodingType)
+ throws IOException {
+
+ InputStream decodedInputStream = null;
+ switch(encodingType) {
+ case BASE64:
+ if (log.isDebugEnabled()) {
+ log.debug("base64 decoding on input ");
+ }
+ decodedInputStream = new ByteArrayInputStream(
+ new BASE64Decoder().decodeBuffer(inputStream));
+ break;
+ case BIGINTEGER16:
+ if (log.isDebugEnabled()) {
+ log.debug("BigInteger 16 encoding on output ");
+ }
+
+ BigInteger n = new BigInteger(IOUtils.toString(inputStream), 16);
+ decodedInputStream = new ByteArrayInputStream(n.toByteArray());
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported encoding type");
+ }
+
+ return decodedInputStream;
+ }
+}
\ No newline at end of file
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxConfigurationConstants.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxConfigurationConstants.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxConfigurationConstants.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxConfigurationConstants.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,25 @@
+package org.apache.synapse.commons.util.jmx;
+
+public class JmxConfigurationConstants {
+
+ /** Prefix for all properties in property file*/
+ public static final String PROP_SYNAPSE_PREFIX_JMX = "synapse.jmx.";
+
+ /** JNDI port property used for the JMX naming directory (RMI registry) */
+ public static final String PROP_JNDI_PORT = "jndiPort";
+
+ /** RMI port property used to configure the JMX RMI port (firewalled setup) */
+ public static final String PROP_RMI_PORT = "rmiPort";
+
+ /** Hostname property used to configure JMX Adapter */
+ public static final String PROP_HOSTNAME = "hostname";
+
+ /** Property for location of remote access file. */
+ public static final String PROP_REMOTE_ACCESS_FILE = "remote.access.file";
+
+ /** Property to activate remote SSL support (same as com.sun.management.jmxremote.ssl) */
+ public static final String PROP_REMOTE_SSL = "remote.ssl";
+
+
+
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformation.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformation.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformation.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformation.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,119 @@
+package org.apache.synapse.commons.util.jmx;
+
+import org.apache.synapse.commons.util.secret.SecretInformation;
+
+public class JmxInformation {
+
+ private SecretInformation secretInformation;
+
+ /** JNDI port used for the local RMI registry. */
+ private int jndiPort;
+
+ /** Optional RMI port to avoid usage of dynamic RMI ports which hinder firewall usage. */
+ private int rmiPort;
+
+ /** Hostname to be used to bind the RMI registry to. */
+ private String hostName;
+
+ /** Use authentication? */
+ private boolean authenticate;
+
+ /** Location of the JMX remote access file. */
+ private String remoteAccessFile;
+
+ /** Location of the JMX remote password file. */
+ private String remotePasswordFile;
+
+ /** Use remote SSL? */
+ private boolean remoteSSL;
+
+ /**
+ * The jmxUrl to connect to.
+ */
+ private String jmxUrl;
+
+ public SecretInformation getSecretInformation() {
+ return secretInformation;
+ }
+
+ public void setSecretInformation(SecretInformation secretInformation) {
+ this.secretInformation = secretInformation;
+ }
+
+ public String getHostName() {
+ return hostName;
+ }
+
+ public void setHostName(String host) {
+ this.hostName = host;
+ }
+
+ public int getRmiPort() {
+ return rmiPort;
+ }
+
+ public void setRmiPort(int rmiPort) {
+ this.rmiPort = rmiPort;
+ }
+
+ public int getJndiPort() {
+ return jndiPort;
+ }
+
+ public void setJndiPort(int jndiPort) {
+ this.jndiPort = jndiPort;
+ }
+
+ public boolean isAuthenticate() {
+ return authenticate;
+ }
+
+ public void setAuthenticate(boolean authenticate) {
+ this.authenticate = authenticate;
+ }
+
+ public String getRemoteAccessFile() {
+ return remoteAccessFile;
+ }
+
+ public void setRemoteAccessFile(String remoteAccessFile) {
+ this.remoteAccessFile = remoteAccessFile;
+ }
+
+ public String getRemotePasswordFile() {
+ return remotePasswordFile;
+ }
+
+ public void setRemotePasswordFile(String remotePasswordFile) {
+ this.remotePasswordFile = remotePasswordFile;
+ }
+
+ public boolean isRemoteSSL() {
+ return remoteSSL;
+ }
+
+ public void setRemoteSSL(boolean remoteSSL) {
+ this.remoteSSL = remoteSSL;
+ }
+
+ /**
+ * Builds the JMX URL depending on the existence of RMI port.
+ *
+ * @return the JMX URL to connect the server to
+ */
+ public void updateJMXUrl() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("service:jmx:rmi://");
+ if (rmiPort > 0) {
+ sb.append(hostName).append(":").append(rmiPort);
+ }
+ sb.append("/jndi/rmi://");
+ sb.append(hostName).append(":").append(jndiPort);
+ sb.append("/synapse");
+ jmxUrl = sb.toString();
+ }
+
+ public String getJmxUrl() {
+ return jmxUrl;
+ }
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformationFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformationFactory.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformationFactory.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxInformationFactory.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,183 @@
+/*
+ * 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.synapse.commons.util.jmx;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.commons.util.MiscellaneousUtil;
+import org.apache.synapse.commons.util.SynapseUtilException;
+import org.apache.synapse.commons.util.secret.SecretConfigurationConstants;
+import org.apache.synapse.commons.util.secret.SecretInformation;
+import org.apache.synapse.commons.util.secret.SecretInformationFactory;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * Factory to create a JmxInformation based on given properties.
+ */
+
+public class JmxInformationFactory {
+
+ private static final Log log = LogFactory.getLog(JmxInformationFactory.class);
+
+ private JmxInformationFactory() {
+ }
+
+ /**
+ * Factory method to create a JmxInformation instance based on given properties
+ *
+ * @param properties Properties to create and configure DataSource
+ * @return DataSourceInformation instance
+ */
+ public static JmxInformation createJmxInformation(Properties properties, String defaultHostName) {
+
+ // Prefix for getting particular JMX properties
+ String prefix = JmxConfigurationConstants.PROP_SYNAPSE_PREFIX_JMX;
+
+ JmxInformation jmxInformation = new JmxInformation();
+
+ SecretInformation secretInformation = SecretInformationFactory.createSecretInformation(
+ properties, prefix, null);
+
+ jmxInformation.setSecretInformation(secretInformation);
+
+ int jndiPort = MiscellaneousUtil.getProperty(
+ properties, prefix + JmxConfigurationConstants.PROP_JNDI_PORT, -1, Integer.class);
+ jmxInformation.setJndiPort(jndiPort);
+
+ int rmiPort = MiscellaneousUtil.getProperty(
+ properties, prefix + JmxConfigurationConstants.PROP_RMI_PORT, 0, Integer.class);
+ jmxInformation.setRmiPort(rmiPort);
+
+ String jmxHostName = MiscellaneousUtil.getProperty(
+ properties, prefix + JmxConfigurationConstants.PROP_HOSTNAME, null);
+ if (jmxHostName == null || jmxHostName.trim().length() == 0) {
+ jmxHostName = defaultHostName;
+ }
+ jmxInformation.setHostName(jmxHostName);
+
+ // begin of special JMX security options
+ Properties managementProperties = readManagementProperties();
+
+ Boolean authenticate;
+ String value = getConfigProperty(
+ managementProperties, "com.sun.management.jmxremote.authenticate");
+ if (value != null) {
+ authenticate = Boolean.valueOf(value);
+ } else {
+ if (secretInformation.getUser() == null) {
+ authenticate = Boolean.FALSE;
+ } else {
+ authenticate = Boolean.TRUE;
+ }
+ }
+ jmxInformation.setAuthenticate(authenticate);
+
+ value = getConfigProperty(managementProperties, "com.sun.management.jmxremote.access.file");
+ if (value == null || value.trim().length() == 0) {
+ value = MiscellaneousUtil.getProperty(
+ properties, prefix + JmxConfigurationConstants.PROP_REMOTE_ACCESS_FILE, null);
+ }
+ if (value != null && value.trim().length() > 0) {
+ jmxInformation.setRemoteAccessFile(value);
+ }
+
+ value = getConfigProperty(managementProperties, "com.sun.management.jmxremote.password.file");
+ if (value != null && value.trim().length() > 0) {
+ jmxInformation.setRemotePasswordFile(value);
+ }
+
+ Boolean remoteSSL;
+ value = getConfigProperty(managementProperties, "com.sun.management.jmxremote.ssl");
+ if (value != null) {
+ remoteSSL = Boolean.valueOf(value);
+ } else {
+ remoteSSL = MiscellaneousUtil.getProperty( properties,
+ prefix + JmxConfigurationConstants.PROP_REMOTE_SSL, Boolean.FALSE, Boolean.class);
+ }
+ jmxInformation.setRemoteSSL(remoteSSL);
+
+ return jmxInformation;
+ }
+
+ /**
+ * Retrieves the management properties if a JMX config file has been specified via the system
+ * property <code>com.sun.management.config.file</code>.
+ *
+ * @return JMX management properties
+ */
+ private static Properties readManagementProperties() {
+
+ Properties managementProperties = new Properties();
+ String configFileName = System.getProperty("com.sun.management.config.file");
+ if (configFileName != null) {
+ FileInputStream configFile = null;
+ try {
+ configFile = new FileInputStream(configFileName);
+ managementProperties.load(configFile);
+ if (log.isDebugEnabled()) {
+ log.debug("Initialized management properties from file " + configFileName);
+ }
+ } catch (FileNotFoundException ex) {
+ log.error("Cannot open " + configFileName, ex);
+ } catch (IOException ex) {
+ log.error("Error while reading " + configFileName, ex);
+ } finally {
+ if (configFile != null) {
+ try {
+ configFile.close();
+ } catch (IOException ignore) {
+ // nothing to do here
+ }
+ }
+ }
+ }
+ return managementProperties;
+ }
+
+ /**
+ * Retrieves a JMX configuration property (first by looking for a Java system property and if
+ * not present by looking for a management property specified in a file specified via<code>
+ * com.sun.management.config.file</code> system property.
+ *
+ * @param name the name of the property to look up
+ *
+ * @return the config property value or null if the property is not configured
+ */
+ private static String getConfigProperty(Properties managementProperties, String name) {
+ String result = System.getProperty(name);
+ if ((result == null) && (managementProperties != null)) {
+ result = managementProperties.getProperty(name);
+ }
+ return result;
+ }
+
+ /**
+ * Helper methods for handle errors.
+ *
+ * @param msg The error message
+ */
+ private static void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseUtilException(msg);
+ }
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxSecretAuthenticator.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxSecretAuthenticator.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxSecretAuthenticator.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/JmxSecretAuthenticator.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,53 @@
+package org.apache.synapse.commons.util.jmx;
+
+
+import org.apache.synapse.commons.util.secret.SecretInformation;
+
+import javax.management.remote.JMXAuthenticator;
+import javax.management.remote.JMXPrincipal;
+import javax.security.auth.Subject;
+import java.util.Collections;
+
+/**
+ * Handles the authentication for JMX management.
+ */
+
+public class JmxSecretAuthenticator implements JMXAuthenticator {
+
+ private SecretInformation secretInformation;
+
+ public JmxSecretAuthenticator(SecretInformation secretInformation) {
+ this.secretInformation = secretInformation;
+ }
+
+ public Subject authenticate(Object credentials) {
+
+ if (credentials == null) {
+ throw new SecurityException("Credentials required");
+ }
+
+ if (!(credentials instanceof String[])) {
+ throw new SecurityException("Credentials should be String[]");
+ }
+
+ // Only expect username/password, therefore the credentials should have two entries
+ final String[] aCredentials = (String[]) credentials;
+ if (aCredentials.length < 2) {
+ throw new SecurityException("Credentials should have the username and password");
+ }
+
+ String username = aCredentials[0];
+ String password = (aCredentials[1] != null ? aCredentials[1] : "");
+
+ // perform authentication
+ if (secretInformation.getUser().equals(username) && password.equals(secretInformation.getResolvedPassword())) {
+ return new Subject(true,
+ Collections.singleton(new JMXPrincipal(username)),
+ Collections.EMPTY_SET,
+ Collections.EMPTY_SET);
+ } else {
+ throw new SecurityException("Username and/or password are incorrect, " +
+ "or you do not have the necessary access rights.");
+ }
+ }
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRegistrar.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRegistrar.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRegistrar.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRegistrar.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,101 @@
+/*
+ * 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.synapse.commons.util.jmx;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.commons.util.SynapseUtilException;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+import java.util.Set;
+
+public class MBeanRegistrar {
+
+ private static final MBeanRegistrar ourInstance = new MBeanRegistrar();
+ private static final Log log = LogFactory.getLog(MBeanRegistrar.class);
+
+ public static MBeanRegistrar getInstance() {
+ return ourInstance;
+ }
+
+ private MBeanRegistrar() {
+ }
+
+ public void registerMBean(Object mbeanInstance, String category, String id) {
+ assertNull(mbeanInstance, "Mbean instance is null");
+ assertNull(category, "Mbean instance category is null");
+ assertNull(id, "Mbean instance name is null");
+ try {
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+ ObjectName name = new ObjectName(getObjectName(category, id));
+ Set set = mbs.queryNames(name, null);
+ if (set != null && set.isEmpty()) {
+ mbs.registerMBean(mbeanInstance, name);
+ } else {
+ mbs.unregisterMBean(name);
+ mbs.registerMBean(mbeanInstance, name);
+ }
+ } catch (Exception e) {
+ log.warn("Error registering a MBean with name ' " + id +
+ " ' and category name ' " + category + "' for JMX management", e);
+ }
+ }
+
+ public void unRegisterMBean(String category, String id) {
+ try {
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+ ObjectName objName = new ObjectName(getObjectName(category, id));
+ if (mbs.isRegistered(objName)) {
+ mbs.unregisterMBean(objName);
+ }
+ } catch (Exception e) {
+ log.warn("Error un-registering a MBean with name ' " + id +
+ " ' and category name ' " + category + "' for JMX management", e);
+ }
+ }
+
+ private String getObjectName(String category, String id) {
+
+ String jmxAgentName = System.getProperty("jmx.agent.name");
+ if (jmxAgentName == null || "".equals(jmxAgentName)) {
+ jmxAgentName = "org.apache.synapse";
+ }
+ return jmxAgentName + ":Type=" + category + ",Name=" + id;
+ }
+
+ private void assertNull(String name, String msg) {
+ if (name == null || "".equals(name)) {
+ handleException(msg);
+ }
+ }
+
+ private void assertNull(Object object, String msg) {
+ if (object == null) {
+ handleException(msg);
+ }
+ }
+
+ private static void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseUtilException(msg);
+ }
+
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRepository.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRepository.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRepository.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/jmx/MBeanRepository.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.synapse.commons.util.jmx;
+
+/**
+ */
+public interface MBeanRepository {
+
+ public void addMBean(String name, Object MBean);
+
+ public Object getMBean(String name);
+
+ public void removeMBean(String name);
+
+ public void clear();
+
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretConfigurationConstants.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretConfigurationConstants.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretConfigurationConstants.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretConfigurationConstants.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,28 @@
+/*
+ * 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.synapse.commons.util.secret;
+
+public class SecretConfigurationConstants {
+
+ public static final String GLOBAL_PREFIX = "synapse.";
+ public static final String PROP_USER_NAME = "username";
+ public static final String PROP_PASSWORD = "password";
+ public static final String PROP_PASSWORD_PROVIDER = "passwordProvider";
+ public static final String PROP_PASSWORD_PROMPT = "passwordPrompt";
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformation.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformation.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformation.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformation.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,91 @@
+/*
+ * 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.synapse.commons.util.secret;
+
+
+/**
+ * Encapsulates the All information related to a DataSource
+ */
+public class SecretInformation {
+
+ private String user;
+ private String aliasPassword;
+ private String passwordPrompt;
+ private SecretCallbackHandler passwordProvider;
+
+ public String getUser() {
+ return user;
+ }
+
+ public void setUser(String user) {
+ this.user = user;
+ }
+
+ public String getAliasPassword() {
+ return aliasPassword;
+ }
+
+ public void setAliasPassword(String aliasPassword) {
+ this.aliasPassword = aliasPassword;
+ }
+
+ public String getPasswordPrompt() {
+ return passwordPrompt;
+ }
+
+ public void setPasswordPrompt(String passwordPrompt) {
+ this.passwordPrompt = passwordPrompt;
+ }
+
+ /**
+ * Get actual password based on SecretCallbackHandler and alias password
+ * If SecretCallbackHandler is null, then returns alias password
+ * @return Actual password
+ */
+ public String getResolvedPassword() {
+
+ if (passwordProvider != null) {
+ if (aliasPassword != null && !"".equals(aliasPassword)) {
+ return getSecret(passwordProvider, aliasPassword, passwordPrompt);
+ }
+ }
+ return aliasPassword;
+ }
+
+ public SecretCallbackHandler getPasswordProvider() {
+ return passwordProvider;
+ }
+
+ public void setPasswordProvider(SecretCallbackHandler passwordProvider) {
+ this.passwordProvider = passwordProvider;
+ }
+
+ private String getSecret(SecretCallbackHandler secretCallbackHanlder, String encryptedPassword, String prompt) {
+ SecretLoadingModule secretLoadingModule = new SecretLoadingModule();
+ secretLoadingModule.init(new SecretCallbackHandler[]{secretCallbackHanlder});
+ SingleSecretCallback secretCallback = new SingleSecretCallback(encryptedPassword);
+ if (prompt != null) {
+ secretCallback.setPrompt(prompt);
+ }
+ secretLoadingModule.load(new SecretCallback[]{secretCallback});
+ return secretCallback.getSecret();
+ }
+
+
+}
Added: synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformationFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformationFactory.java?rev=767111&view=auto
==============================================================================
--- synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformationFactory.java (added)
+++ synapse/trunk/java/modules/utils/src/main/java/org/apache/synapse/commons/util/secret/SecretInformationFactory.java Tue Apr 21 10:45:06 2009
@@ -0,0 +1,85 @@
+/*
+ * 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.synapse.commons.util.secret;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.commons.util.MiscellaneousUtil;
+
+import java.util.Properties;
+
+/**
+ * Factory to create a DataSourceInformation based on given properties
+ */
+
+public class SecretInformationFactory {
+
+ private static final Log log = LogFactory.getLog(SecretInformationFactory.class);
+
+ private SecretInformationFactory() {
+ }
+
+ /**
+ * Factory method to create a SecretInformation instance based on the given properties
+ *
+ * @param properties Properties to create a SecretInformation
+ * @param configurationPrefix The configuration prefix to use
+ * @param passwordPrompt A specific password prompt to use
+ * (only for interactive authentication providers)
+ *
+ * @return SecretInformation instance
+ */
+ public static SecretInformation createSecretInformation(
+ Properties properties, String configurationPrefix, String passwordPrompt) {
+
+ SecretInformation secretInformation = new SecretInformation();
+
+ String user = (String) MiscellaneousUtil.getProperty(
+ properties, configurationPrefix + SecretConfigurationConstants.PROP_USER_NAME, null,
+ String.class);
+ if (user != null && !"".equals(user)) {
+ secretInformation.setUser(user);
+ }
+
+ String password = (String) MiscellaneousUtil.getProperty(
+ properties, configurationPrefix + SecretConfigurationConstants.PROP_PASSWORD, null,
+ String.class);
+
+ if (password != null && !"".equals(password)) {
+ secretInformation.setAliasPassword(password);
+ }
+
+ // set specific password provider if configured
+ SecretCallbackHandler passwordProvider =
+ SecretCallbackHandlerFactory.createSecretCallbackHandler(properties,
+ configurationPrefix + SecretConfigurationConstants.PROP_PASSWORD_PROVIDER);
+
+ // if no specific password provider configured, use default password provider
+ if (passwordProvider == null) {
+ passwordProvider = SecretCallbackHandlerFactory.createSecretCallbackHandler(
+ properties,
+ SecretConfigurationConstants.GLOBAL_PREFIX
+ + SecretConfigurationConstants.PROP_PASSWORD_PROVIDER);
+ }
+ secretInformation.setPasswordProvider(passwordProvider);
+ secretInformation.setPasswordPrompt(passwordPrompt);
+
+ return secretInformation;
+ }
+}