connectorresource.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 914 行 · 第 1/2 页

JAVA
914
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.jca;import com.caucho.config.Config;import com.caucho.config.ConfigException;import com.caucho.config.program.ContainerProgram;import com.caucho.jca.cfg.ObjectConfig;import com.caucho.lifecycle.Lifecycle;import com.caucho.loader.Environment;import com.caucho.loader.EnvironmentClassLoader;import com.caucho.loader.EnvironmentListener;import com.caucho.naming.Jndi;import com.caucho.util.L10N;import com.caucho.webbeans.manager.*;import javax.annotation.PostConstruct;import javax.resource.spi.*;import javax.resource.spi.endpoint.MessageEndpointFactory;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;/** * Configuration for the <connector> pattern. */public class ConnectorResource implements EnvironmentListener {  private static L10N L = new L10N(ConnectorResource.class);  private static Logger log    = Logger.getLogger(ConnectorResource.class.getName());  private static int _idGen;  private String _url;  private String _name;  private String _jndiName;  private String _type;  private ResourceArchive _rar;  private ResourceAdapterConfig _resourceAdapter = new ResourceAdapterConfig();  private ContainerProgram _init;  private ArrayList<ConnectionFactory> _outboundList    = new ArrayList<ConnectionFactory>();  private ArrayList<ConnectionListener> _inboundList    = new ArrayList<ConnectionListener>();  private ArrayList<ConnectorBean> _beanList    = new ArrayList<ConnectorBean>();  private ResourceAdapter _ra;  private boolean _isInitRA;  private final Lifecycle _lifecycle = new Lifecycle();  /**   * Sets the name   */  public void setName(String name)  {    _name = name;  }  /**   * Sets the name   */  public void setJndiName(String name)  {    _jndiName = name;  }  /**   * Gets the name   */  public String getName()  {    return _name;  }  /**   * Sets the type of the connector.   */  public void setType(String type)    throws Exception  {    setClass(type);  }  /**   * Sets the type of the connector using known types.   */  public void setURL(String url)    throws Exception  {      }  /**   * Sets the type of the connector.   */  public void setClass(String type)    throws Exception  {    _type = type;    _rar = ResourceArchiveManager.findResourceArchive(_type);    ClassLoader loader = Thread.currentThread().getContextClassLoader();    if (_rar != null) {      ObjectConfig raConfig = _rar.getResourceAdapter();      if (raConfig.getType() != null)	_ra = (ResourceAdapter) raConfig.instantiate();    }    else {      try {	Class raClass = Class.forName(_type, false, loader);		_ra = (ResourceAdapter) raClass.newInstance();      } catch (Exception e) {	throw new ConfigException(L.l("'{0}' is not a known connector.  The type must match the resource adaptor or managed connection factory of one of the installed *.rar files or specify a ResourceAdapter implementation.",				      _type),				  e);      }    }  }  /**   * Gets the type   */  public String getType()  {    return _type;  }  public void addInit(ContainerProgram init)  {    _init = init;  }  /**   * Configures the resource adapter.   */  public ResourceAdapterConfig createResourceAdapter()    throws ConfigException  {    if (_ra == null)      throw new ConfigException(L.l("'{0}' may not have a <resource-adapter> section.  Old-style connectors must use <connection-factory>, but not <resource-adapter>.",				    _type));    return _resourceAdapter;  }  /**   * Sets the configured resource adapter.   */  public void setResourceAdapter(ResourceAdapterConfig raConfig)    throws Exception  {  }  /**   * Configures a connection-factory   */  public ConnectionFactory createConnectionFactory()    throws Exception  {    initRA();        return new ConnectionFactory();  }  /**   * Configures a connection-factory   */  public void addConnectionFactory(ConnectionFactory factory)    throws Exception  {    _outboundList.add(factory);  }  /**   * Configures a connection-listener   */  public ConnectionListener createMessageListener()    throws Exception  {    initRA();        return new ConnectionListener();  }  /**   * Adds the configured connection-listener   */  public void addMessageListener(ConnectionListener listener)    throws Throwable  {    _inboundList.add(listener);        String listenerType = listener.getType();    if (_ra == null)      throw new ConfigException(L.l("message-listener requires a resource-adapter."));    ActivationSpec activationSpec;        if (_rar != null) {      ObjectConfig objectCfg = _rar.getMessageListener(listenerType);      if (objectCfg == null)	throw new ConfigException(L.l("`{0}' is an unknown type of <connection-listener> for `{1}'.  The connector has no matching inbound connection-listener.",				      listenerType,				      _type));      activationSpec = (ActivationSpec) objectCfg.instantiate();    }    else {      ClassLoader loader = Thread.currentThread().getContextClassLoader();      Class listenerClass = null;            try {	listenerClass = Class.forName(listenerType, false, loader);      } catch (Throwable e) {	throw new ConfigException(L.l("`{0}' is not a known listener.  The type must match the activation spec for an inbound connection of one of the installed *.rar files or specify an ActivationSpec implementation.",				      listenerType), e);      }            activationSpec = (ActivationSpec) listenerClass.newInstance();    }          if (listener.getInit() != null)      listener.getInit().configure(activationSpec);    /*      TypeBuilderFactory.init(activationSpec);    */    activationSpec.setResourceAdapter(_ra);    // activationSpec.validate();    EndpointFactory endpointFactoryCfg = listener.getEndpointFactory();          if (endpointFactoryCfg == null)      throw new ConfigException(L.l("connection-listener needs endpoint factory."));    Class endpointClass = endpointFactoryCfg.getType();    MessageEndpointFactory endpointFactory;    endpointFactory = (MessageEndpointFactory) endpointClass.newInstance();    if (endpointFactoryCfg.getInit() != null)      endpointFactoryCfg.getInit().configure(endpointFactory);    Config.init(endpointFactory);    listener.setEndpoint(endpointFactory);    listener.setActivation(activationSpec);  }  /**   * Configures a connection-resource   */  public ConnectorBean createBean()  {    return new ConnectorBean();  }  /**   * Configures a connection-resource   */  public ConnectorBean createResource()  {    return createBean();  }  /**   * Initialize the resource.   */  @PostConstruct  public void init()    throws Exception  {    if (_type == null)      throw new ConfigException(L.l("<connector> requires a <type>."));    if (_name == null)      _name = _jndiName;    if (_name == null && _rar != null)      _name = _rar.getDisplayName() + "-" + _idGen++;    if (_ra == null)      throw new ConfigException(L.l("<connector> does not have a resource adapter."));        if (_resourceAdapter.getInit() != null)      _resourceAdapter.getInit().configure(_ra);    if (_init != null)      _init.configure(_ra);    ResourceManagerImpl.addResource(_ra);    WebBeansContainer webBeans = WebBeansContainer.create();        if (_resourceAdapter.getName() != null) {      Jndi.bindDeepShort(_resourceAdapter.getName(), _ra);      webBeans.addSingleton(_ra, _resourceAdapter.getName());    }    else      webBeans.addSingleton(_ra, _name);    // create a default outbound factory    if (_outboundList.size() == 0 && _jndiName != null && _rar != null) {      ObjectConfig factoryConfig = _rar.getConnectionDefinition(null);      if (factoryConfig != null) {        ConnectionFactory factory = createConnectionFactory();        factory.setJndiName(_jndiName);        factory.init();        addConnectionFactory(factory);      }    }    /*    if (close != null && ! (obj instanceof ResourceAdapter))      Environment.addEnvironmentListener(new CloseListener(obj, close));    */    initRA();        Environment.addEnvironmentListener(this);        start();    log.fine("Connector[" + _type + "] active");  }  /**   * Start the resource.   */  public void start()    throws Exception  {    if (! _lifecycle.toActive())      return;        initRA();        for (int i = 0; i < _outboundList.size(); i++) {      ConnectionFactory factory = _outboundList.get(i);      factory.start();    }        for (int i = 0; i < _inboundList.size(); i++) {      ConnectionListener listener = _inboundList.get(i);            _ra.endpointActivation(listener.getEndpoint(),			     listener.getActivation());    }  }  /**   * Initializes the ra.   */  public void initRA()    throws Exception  {    /*    if (! _isInitRA) {      _isInitRA = true;            if (_ra != null) {	// TypeBuilderFactory.init(_ra);	ResourceManagerImpl.addResource(_ra);      }    }    */  }  /**   * Stops the connector.   */  public void stop()  {    if (! _lifecycle.toStop())      return;    for (int i = 0; i < _inboundList.size(); i++) {      ConnectionListener listener = _inboundList.get(i);      MessageEndpointFactory endpointFactory = listener.getEndpoint();      ActivationSpec activation = listener.getActivation();      if (_ra != null)	_ra.endpointDeactivation(endpointFactory, activation);    }  }    /**   * Handles the case where the environment config phase   */  public void environmentConfigure(EnvironmentClassLoader loader)  {  }    /**   * Handles the case where the environment bind phase   */  public void environmentBind(EnvironmentClassLoader loader)  {  }    /**   * Handles the case where the environment is starting (after init).   */  public void environmentStart(EnvironmentClassLoader loader)  {    try {      start();    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);    }  }    /**   * Handles the case where the environment is stopping

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?