Sample protocol bridge properties user exit

For information about how to use this sample user exit, see Looking up protocol file server properties by using exit classes

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import com.ibm.wmqfte.exitroutine.api.ProtocolBridgePropertiesExit;
import com.ibm.wmqfte.exitroutine.api.ProtocolServerPropertyConstants;

/**
 * A sample protocol bridge properties exit. This exit reads a properties file
 * that contains properties for protocol servers.
 * <p>
 * The format of each entry in the properties file is: {@literal
 * <serverName>=<type>://<host>:<port>}
 * <p>
 * The location of the properties file is taken from the protocol bridge agent
 * property {code protocolBridgePropertiesConfiguration}.
 * <p>
 * To install the sample exit:
 * <ol>
 * <li>Compile the class and export to a jar file.
 * <li>Place the jar file in the {@code exits} subdirectory of the agent data
 * directory of the protocol bridge agent on which the exit is to be installed.
 * <li>In the {@code agent.properties} file of the protocol bridge agent set the
 * {code protocolBridgePropertiesExitClasses} to {@code SampleCredentialExit}.
 * <li>Create a properties file that contains the appropriate properties to
 * specify the required servers.
 * <li>In the {@code agent.properties} file of the protocol bridge agent set the
 * <code>protocolBridgePropertiesConfiguration</code> property to the absolute
 * path name of this properties file.
 * <li>To activate the changes stop and restart the protocol bridge agent.
 * </ol>
 * <p>
 * For further information on protocol bridge properties exits refer to the
 * WebSphere MQ File Transfer Edition documentation online at:
 * <p>
 * {@link https://www.ibm.com/support/knowledgecenter/SSEP7X_7.0.4/welcome/WelcomePagev7r0.html}
 */
public class SamplePropertiesExit implements ProtocolBridgePropertiesExit {

	/**
	 * Helper class to encapsulate protocol server information.
	 */
	private static class ServerInformation {
		private final String type;
		private final String host;
		private final int port;

		public ServerInformation(String url) {
			int index = url.indexOf("://");
			if (index == -1)
				throw new IllegalArgumentException("Invalid server URL: " + url);
			type = url.substring(0, index);

			int portIndex = url.indexOf(":", index + 3);
			if (portIndex == -1) {
				host = url.substring(index + 3);
				port = -1;
			} else {
				host = url.substring(index + 3, portIndex);
				port = Integer.parseInt(url.substring(portIndex + 1));
			}
		}

		public String getType() {
			return type;
		}

		public String getHost() {
			return host;
		}

		public int getPort() {
			return port;
		}
	}

	/** A {@code Map} that holds information for each configured protocol server */
	final private Map<String, ServerInformation> servers = new HashMap<String, ServerInformation>();

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.ibm.wmqfte.exitroutine.api.ProtocolBridgePropertiesExit#
	 * getProtocolServerProperties(java.lang.String)
	 */
	public Properties getProtocolServerProperties(String protocolServerName) {
		// Attempt to get the protocol server information for the given protocol
		// server name
		final ServerInformation info;
		if (protocolServerName != null && protocolServerName.length() > 0) {
			info = servers.get(protocolServerName);
		} else {
			info = null;
		}

		// Build the return set of properties from the collected protocol server
		// information, when available.
		// The properties set here is the minimal set of properties to be a
		// valid set.
		final Properties result;
		if (info != null) {
			result = new Properties();
			result.setProperty(ProtocolServerPropertyConstants.SERVER_NAME,
					protocolServerName);
			result.setProperty(ProtocolServerPropertyConstants.SERVER_TYPE,
					info.getType());
			result.setProperty(
					ProtocolServerPropertyConstants.SERVER_HOST_NAME, info
							.getHost());
			if (info.getPort() != -1)
				result.setProperty(
						ProtocolServerPropertyConstants.SERVER_PORT_VALUE, ""
								+ info.getPort());
			result.setProperty(ProtocolServerPropertyConstants.SERVER_PLATFORM,
					"UNIX");
			if (info.getType().equalsIgnoreCase("FTP")) {
				result.setProperty(
						ProtocolServerPropertyConstants.SERVER_TIMEZONE,
						"Europe/London");
				result.setProperty(
						ProtocolServerPropertyConstants.SERVER_LOCALE, "en-GB");
			}
			result.setProperty(
					ProtocolServerPropertyConstants.SERVER_FILE_ENCODING,
					"UTF-8");
		} else {
			result = null;
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ibm.wmqfte.exitroutine.api.ProtocolBridgePropertiesExit#initialize
	 * (java.util.Map)
	 */
	public boolean initialize(Map<String, String> bridgeProperties) {
		// Flag to indicate whether the exit has been successfully initialized
		// or not
		boolean initialisationResult = true;

		// Get the path of the properties file
		final String propertiesFilePath = bridgeProperties
				.get("protocolBridgePropertiesConfiguration");
		if (propertiesFilePath == null || propertiesFilePath.length() == 0) {
			// The protocol server properties file path has not been specified.
			// Output an error and return false
			System.err.println("Error initializing SamplePropertiesExit.");
			System.err
					.println("The location of the protocol server properties file has not been specified in the protocolBridgePropertiesConfiguration property");
			initialisationResult = false;
		}

		if (initialisationResult) {
			// The Properties object that holds protocol server information
			final Properties mappingProperties = new Properties();

			// Open and load the properties from the properties file
			final File propertiesFile = new File(propertiesFilePath);
			FileInputStream inputStream = null;
			try {
				// Create a file input stream to the file
				inputStream = new FileInputStream(propertiesFile);

				// Load the properties from the file
				mappingProperties.load(inputStream);
			} catch (final FileNotFoundException ex) {
				System.err.println("Error initializing SamplePropertiesExit.");
				System.err
						.println("Unable to find the protocol server properties file: "
								+ propertiesFilePath);
				initialisationResult = false;
			} catch (final IOException ex) {
				System.err.println("Error initializing SamplePropertiesExit.");
				System.err
						.println("Error loading the properties from the protocol server properties file: "
								+ propertiesFilePath);
				initialisationResult = false;
			} finally {
				// Close the inputStream
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (final IOException ex) {
						System.err
								.println("Error initializing SamplePropertiesExit.");
						System.err
								.println("Error closing the protocol server properties file: "
										+ propertiesFilePath);
						initialisationResult = false;
					}
				}
			}

			if (initialisationResult) {
				// Populate the map of protocol servers from the properties
				for (Entry<Object, Object> entry : mappingProperties.entrySet()) {
					final String serverName = (String) entry.getKey();
					final ServerInformation info = new ServerInformation(
							(String) entry.getValue());
					servers.put(serverName, info);
				}
			}
		}

		return initialisationResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ibm.wmqfte.exitroutine.api.ProtocolBridgePropertiesExit#shutdown(
	 * java.util.Map)
	 */
	public void shutdown(Map<String, String> bridgeProperties) {
		// Nothing to do in this method because there are no resources that need
		// to be released
	}

}

Reference Reference

Feedback

Timestamp icon Last updated: Tuesday, 30 January 2018
http://www.ibm.com/support/knowledgecenter/SSEP7X_7.0.4/com.ibm.wmqfte.doc/sample_properties_exit.htm