Package org.servicemix.client

Source Code of org.servicemix.client.ServiceContext

/**
*
* Copyright 2005 Unity Systems, LLC. http://www.unity-systems.com
*
* Licensed 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.servicemix.client;

import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.InOptionalOut;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessageExchangeFactory;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.RobustInOnly;
import javax.xml.namespace.QName;

/**
*
* The ServiceContext provides a set of helper methods to enable quick
* integration with JBI
*
* @author <a href="mailto:pdodds@unity-systems.com">Philip Dodds </a>
*
*/
public class ServiceContext {

  private ComponentContext componentContext;

  private ClientEndPointRegistry endPointRegistry;

  /**
   * Constructor takes the component context which the helper operates
   *
   * @param componentContext
   */
  public ServiceContext(ComponentContext componentContext,
      ClientEndPointRegistry registry) {
    this.componentContext = componentContext;
    this.endPointRegistry = registry;
  }

  /**
   * Creates an InOnly exchange by looking up the defined interface as a
   * 'Consumes' interface and picking up the service name, not that if the
   * interface is not registered as a <i>consumes</i> in the services section
   * of the deployment component or service unit then we will return null
   *
   * @param targetInterface
   *            The qualified name of the target interface
   * @return An InOnly exception if the interface is found or null
   * @throws MessagingException
   */
  public InOnly createInOnly(QName targetInterface) throws MessagingException {
    EndPointDefinition endPointDef = endPointRegistry
        .getEndPointDefinition(targetInterface);
    if (endPointDef != null) {
      InOnly inOnly = getMessageExchangeFactory().createInOnlyExchange();
      inOnly.setInterfaceName(targetInterface);
      inOnly.setService(endPointDef.getServiceName());
      return inOnly;
    } else {
      return null;
    }
  }

  /**
   * Creates an InOptionalOut exchange by looking up the defined interface as
   * a 'Consumes' interface and picking up the service name, not that if the
   * interface is not registered as a <i>consumes</i> in the services section
   * of the deployment component or service unit then we will return null
   *
   * @param targetInterface
   *            The qualified name of the target interface
   * @return An InOptionalOut exception if the interface is found or null
   * @throws MessagingException
   */
  public InOptionalOut createInOptionalOut(QName targetInterface)
      throws MessagingException {
    EndPointDefinition endPointDef = endPointRegistry
        .getEndPointDefinition(targetInterface);
    if (endPointDef != null) {
      InOptionalOut inOptionalOut = getMessageExchangeFactory()
          .createInOptionalOutExchange();
      inOptionalOut.setInterfaceName(targetInterface);
      inOptionalOut.setService(endPointDef.getServiceName());
      return inOptionalOut;
    } else {
      return null;
    }
  }

  /**
   * Creates an InOptionalOut exchange by looking up the defined interface as
   * a 'Consumes' interface and picking up the service name, not that if the
   * interface is not registered as a <i>consumes</i> in the services section
   * of the deployment component or service unit then we will return null
   *
   * @param targetInterface
   *            The qualified name of the target interface
   * @return An InOptionalOut exception if the interface is found or null
   * @throws MessagingException
   */
  public InOut createInOut(QName targetInterface) throws MessagingException {
    EndPointDefinition endPointDef = endPointRegistry
        .getEndPointDefinition(targetInterface);
    if (endPointDef != null) {
      InOut inOut = getMessageExchangeFactory().createInOutExchange();
      inOut.setInterfaceName(targetInterface);
      inOut.setService(endPointDef.getServiceName());
      return inOut;
    } else {
      return null;
    }
  }

  public MessageExchangeFactory createMessageExchangeFactory()
      throws MessagingException {
    return getDeliveryChannel().createExchangeFactory();
  }

  /**
   * Creates an RobustInOnly exchange by looking up the defined interface as a
   * 'Consumes' interface and picking up the service name, not that if the
   * interface is not registered as a <i>consumes</i> in the services section
   * of the deployment component or service unit then we will return null
   *
   * @param targetInterface
   *            The qualified name of the target interface
   * @return An RobustInOnly exception if the interface is found or null
   * @throws MessagingException
   */
  public RobustInOnly createRobustInOnly(QName targetInterface)
      throws MessagingException {
    EndPointDefinition endPointDef = endPointRegistry
        .getEndPointDefinition(targetInterface);
    if (endPointDef != null) {
      RobustInOnly robustInOnly = getMessageExchangeFactory()
          .createRobustInOnlyExchange();
      robustInOnly.setInterfaceName(targetInterface);
      robustInOnly.setService(endPointDef.getServiceName());
      return robustInOnly;
    } else {
      return null;
    }
  }

  /**
   * A helper method to indicate that the message exchange is complete which
   * will set the status to {@link ExchangeStatus#DONE} and send the message
   * on the delivery channel.
   *
   * @param exchange
   * @throws MessagingException
   */
  public void done(MessageExchange exchange) throws MessagingException {
    exchange.setStatus(ExchangeStatus.DONE);
    getDeliveryChannel().send(exchange);
  }

  /**
   * Returns the JBI ComponentContext (@see ComponentContext) for the
   * component under which this service has been deployed
   *
   * @return The ComponentContext
   */
  public ComponentContext getComponentContext() {
    return componentContext;
  }

  /**
   * Returns the JBI Delivery Channel (@see DeliveryChannel) for the componnt
   * under which this service has been deployed
   *
   * @return The DeliveryChannel
   * @throws MessagingException
   */
  public DeliveryChannel getDeliveryChannel() throws MessagingException {
    return getComponentContext().getDeliveryChannel();
  }

  /**
   * Returns the ClientEndPointRegistry for this service deployment, which can
   * be used to lookup service names and interfaces that have been activated
   *
   * @return The ClientEndPointRegistry for the service
   */
  public ClientEndPointRegistry getEndPointRegistry() {
    return endPointRegistry;
  }

  /**
   * Returns the JBI Message Exchange Factory (@see MessageExchangeFactory)
   *
   * @return The MessageExchangeFactory
   *
   * @throws MessagingException
   */
  public MessageExchangeFactory getMessageExchangeFactory()
      throws MessagingException {
    return getDeliveryChannel().createExchangeFactory();
  }
}
TOP

Related Classes of org.servicemix.client.ServiceContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.