Instantiating and Configuring the Service Connector

You must create an instance of the Java Service Connector which can be used to access the BPM service API. This service connector instance must be configured to use the endpoint addresses exposed by particular BPM services.

The Service Connector communicates with the service endpoints exposed by the BPM server. Unless you plan to run the client application on the same machine as the BPM services, you must configure the endpoints to the required BPM runtime server.

The method setServiceConnector reads the protocol, host, and port number values from the request and calls the initializeServiceConnector method.

/*
	 * =====================================================
	 * METHOD : setServiceConnector
	 * =====================================================
	 */
	/**
	 * Reads the protocol, host, and port values from the request and
	 * initializes the ServiceConnector.
	 *
	 * @param req
	 * @throws ServletException
	 */
	private void setServiceConnector(HttpServletRequest req) throws ServletException
	{
		// Read the protocol, host, and port values from the request.
		Configuration.Protocol protocol = req.getParameter("secure") == null ? Configuration.Protocol.HTTP
				: Configuration.Protocol.HTTPS;
		req.getSession().setAttribute("protocol", protocol.toString().toLowerCase());
		String host = req.getParameter("host") == null ? "localhost" : req.getParameter("host");
		req.getSession().setAttribute("host", host);
		int port = req.getParameter("port") == null ? 8080 : Integer.parseInt(req.getParameter("port"));
		req.getSession().setAttribute("port", port);
		initializeServiceConnector(req, protocol, host, port);
	}

Before creating the Service Connector instance, make sure that:

  • You set up the security handler. See Setting up the Security Handler for more information.
  • The Service Connector is using the same SOAP version as the target BPM systems. By default, the Service Connector uses SOAP version 1.1. You can change the version either by setting a Java system property or using the Configuration method, as shown below..
    // Set the SOAP version to either "SOAP_1_2" or "SOAP_1_1", 
    // either by setting the Java system property
    System.setProperty("amxbpm.soap.specification", "SOAP_1_2");
    //or by setting the Configuration.SOAP_SPEC value
    Configuration.SOAP_SPEC spec = SOAP_SPEC.SOAP_1_2;

The method initializeServiceConnector creates an instance of the Service Connector serviceConnector using the ServiceConnectorFactory. The Service Connector Factory provides a factory interface to get the reference of the service connector instance associated with a particular BPM server. This enables the same client application to communicate with multiple BPM servers which can be hosted in different locations. The serviceConnector instance created for the user is cached within the HTTP session associated with the logged in user.

private static final String SERVICECONNECTOR = "serviceConnector";
...
/*
	 * =====================================================
	 * METHOD : initializeServiceConnector
	 * =====================================================
	 */
	/**
	 * Creates the ServiceConnector instance.  This example shows several ways to create the ServiceConnector.
	 * Use of the ServiceConnectorFactory is recommended since it creates a single instance for a given
	 * protocol, host, and port.
	 *
	 * @param protocol The protocol value.
	 * @param host The host value.
	 * @param port The port value.
	 * @throws ServletException
	 * @see ServiceConnector, ServiceConnectorFactory
	 */
	private void initializeServiceConnector(HttpServletRequest req, Configuration.Protocol protocol, String host,
			int port) throws ServletException
	{
		// Create a security handler for the logged in user.
		SecurityHandler securityHandler = new DefaultSecurityHandler(getUserName(req), getPassword(req));
		ServiceConnector serviceConnector;
		// Example of passing the protocol, host, port, and securityHandler into the constructor.
		serviceConnector = ServiceConnectorFactory.getServiceConnector(protocol, host, port, securityHandler);
		// The ServiceConnector constructors can be used directly but use of the ServiceConnectorFactory is recommended.
		//serviceConnector = new ServiceConnector(protocol, host, port, securityHandler);		
		req.getSession().setAttribute(SERVICECONNECTOR, serviceConnector);
	}
Note: The Service Connector constructors can be used directly. However, the use of the Service Connector Factory is recommended.

The method getServiceConnector is used to return the serviceConnector instance stored in the session.

/*
	 * =====================================================
	 * METHOD : getServiceConnector
	 * =====================================================
	 */
	/**
	 * Returns the ServiceConnector instance stored in the session.
	 *
	 * @param req
	 * @return The ServiceConnector instance stored in the session.
	 * @throws ServletException
	 */
	private ServiceConnector getServiceConnector(HttpServletRequest req) throws ServletException
	{
		ServiceConnector serviceConnector = (ServiceConnector) req.getSession().getAttribute(SERVICECONNECTOR);
		if (serviceConnector == null)
		{
			throw new ServletException("ServiceConnector is missing.  A valid login is required.");
		}
		return serviceConnector;
	}

You can also configure the protocol, host, and port number properties after creating the service connector by passing properties to the configuration instance.

// Example of passing configuration instance in.
		//		Properties properties = new Properties();
		//		properties.setProperty(Configuration.Property.AMX_BPM_INSTANCE_PROTOCOL.getValue(), protocol.toString());
		//		properties.setProperty(Configuration.Property.AMX_BPM_INSTANCE_HOST.getValue(), host);
		//		properties.setProperty(Configuration.Property.AMX_BPM_INSTANCE_PORT.getValue(), String.valueOf(port));
		//		Configuration configuration = new Configuration(properties);
		//		//serviceConnector = new ServiceConnector(configuration, securityHandler);
		//		serviceConnector = ServiceConnectorFactory.getServiceConnector(configuration, securityHandler);