Package org.servicemix.client

Source Code of org.servicemix.client.SpringComponentLifeCycle

/**
*
* 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.JBIException;
import javax.jbi.component.ComponentContext;
import javax.jbi.component.ComponentLifeCycle;
import javax.jbi.management.DeploymentException;
import javax.jbi.messaging.DeliveryChannel;
import javax.management.ObjectName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.servicemix.jbi.deployment.Descriptor;
import org.springframework.context.ApplicationContext;

/**
*
* Spring implementation of the JBI ComponentLifeCycle (@see
* ComponentLifeCycle). this can be used to manage JBI components defined in a
* Spring configuration.
*
* This basically means that we will parse the JBI descriptor and build a
* ClientEndPointRegistry based on the services element. This can be used to activate
* the ServiceEndPoints that you wish to use, we will also look for a
* META-INF/jbi-spring.xml and if found create a Spring Context from it.
*
* We will look through the Beans created and determine which ones expose the
* 'Easy JBI' interfaces such as ServiceEndPointImplementation,
* ServiceInterfaceImplementation and ServiceLifeCycleImplementation
*
* @author <a href="mailto:pdodds@unity-systems.com">Philip Dodds </a>
*
*/
public class SpringComponentLifeCycle implements ComponentLifeCycle {

  private static final Log log = LogFactory
      .getLog(SpringComponentLifeCycle.class.getName());

  private DeliveryChannel channel;

  private SpringComponent component;

  private ComponentContext componentContext;

  private Descriptor descriptor;

  private SpringEndPointRouter endPointRouter;

  private Thread endPointRouterThead;

  private SpringServiceRegistry springRegistry;

  public SpringComponentLifeCycle(SpringComponent component) {
    this.component = component;
  }

  /**
   * Configures spring context underneath the LifeCycle
   *
   * @param serviceUnitName
   *            The name of the service unit
   * @param serviceUnitRootDirectory
   *            The service unit's root directory
   */
  private void configureSpring() {
    log.debug("Initializing component lifecycle, for spring configuration");
    if (SpringBuilder.hasSpringXml(componentContext.getInstallRoot())) {
      ApplicationContext context = SpringBuilder
          .getSpringBeans(componentContext.getInstallRoot());
      getSpringRegistry().addComponentSpringContext(context);
    }
  }

  public ComponentContext getComponentContext() {
    return componentContext;
  }

  /**
   * Returns the JBI descriptor for the Component if one is found
   *
   * @return The JBI descriptor
   */
  public Descriptor getDescriptor() {
    return descriptor;
  }

  public ClientEndPointRegistry getEndPointRegistry() {
    return springRegistry.getEndPointRegistry();
  }

  /*
   * (non-Javadoc)
   *
   * @see javax.jbi.component.ComponentLifeCycle#getExtensionMBeanName()
   */
  public ObjectName getExtensionMBeanName() {
    return null;
  }

  public SpringServiceRegistry getSpringRegistry() {
    return springRegistry;
  }

  /*
   * (non-Javadoc)
   *
   * @see javax.jbi.component.ComponentLifeCycle#init(javax.jbi.component.ComponentContext)
   */
  public void init(ComponentContext componentContext) throws JBIException {
    this.componentContext = componentContext;
    log
    .debug("Initializing base lifecycle, looking for spring configuration");
    springRegistry = new SpringServiceRegistry(componentContext);
    getSpringRegistry().getEndPointRegistry().loadComponentServices(
        componentContext.getInstallRoot());   
    configureSpring();   

    if (getSpringRegistry().getComponentLifeCycle() != null) {
      getSpringRegistry().getComponentLifeCycle().init(
          springRegistry.getServiceContext());
    }
  }

  /**
   * Sets the ComponentContext for this component's lifecycle (@see
   * ComponentContext)
   *
   * @param componentContext
   *            The ComponentContext
   */
  public void setComponentContext(ComponentContext componentContext) {
    this.componentContext = componentContext;
  }

  /*
   * (non-Javadoc)
   *
   * @see javax.jbi.component.ComponentLifeCycle#shutDown()
   */
  public void shutDown() throws JBIException {

  }

  /*
   * (non-Javadoc)
   *
   * @see javax.jbi.component.ComponentLifeCycle#start()
   */
  public void start() throws JBIException {

    if (getSpringRegistry().getComponentLifeCycle() != null) {
      getSpringRegistry().getComponentLifeCycle().start();
    }

    channel = componentContext.getDeliveryChannel();
    endPointRouter = new SpringEndPointRouter(channel, getSpringRegistry());
    endPointRouterThead = new Thread(endPointRouter);
    endPointRouterThead.start();
    try {
      component.getEndPointRegistry().activeComponentEndpoints();
    } catch (JBIException e) {
      throw new DeploymentException(
          "Unable to activate component endpoints", e);
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see javax.jbi.component.ComponentLifeCycle#stop()
   */
  public void stop() throws JBIException {
    try {
      if (getSpringRegistry().getComponentLifeCycle() != null) {
        getSpringRegistry().getComponentLifeCycle().stop();
      }

      if (endPointRouterThead != null) {
        endPointRouter.stop();
        endPointRouterThead.interrupt();
        endPointRouterThead.join();
      }
      if (channel != null) {
        channel.close();
      }

      try {
        component.getEndPointRegistry().deactiveComponentEndpoints();
      } catch (JBIException e) {
        throw new DeploymentException(
            "Unable to activate component endpoints", e);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of org.servicemix.client.SpringComponentLifeCycle

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.