ambercontainer.java

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

JAVA
1,170
字号
/* * 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.amber.manager;import com.caucho.amber.AmberRuntimeException;import com.caucho.amber.cfg.*;import com.caucho.amber.gen.AmberEnhancer;import com.caucho.amber.gen.AmberGenerator;import com.caucho.amber.type.*;import com.caucho.config.*;import com.caucho.config.program.ConfigProgram;import com.caucho.loader.*;import com.caucho.loader.enhancer.EnhancerManager;import com.caucho.loader.enhancer.ScanListener;import com.caucho.util.*;import com.caucho.vfs.*;import com.caucho.webbeans.manager.*;import javax.sql.DataSource;import javax.persistence.*;import javax.persistence.spi.*;import java.io.InputStream;import java.net.*;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * Environment-based container. */public class AmberContainer implements ScanListener, EnvironmentListener {  private static final L10N L = new L10N(AmberContainer.class);  private static final Logger log    = Logger.getLogger(AmberContainer.class.getName());  private static final EnvironmentLocal<AmberContainer> _localContainer    = new EnvironmentLocal<AmberContainer>();  private EnvironmentClassLoader _parentLoader;  private ClassLoader _tempLoader;  // private EnhancingClassLoader _enhancedLoader;  private AmberContainer _parentAmberContainer;  private AmberEnhancer _enhancer;  private DataSource _dataSource;  private DataSource _readDataSource;  private DataSource _xaDataSource;  private boolean _createDatabaseTables;  private ArrayList<ConfigProgram> _unitDefaultList    = new ArrayList<ConfigProgram>();  private HashMap<String,ArrayList<ConfigProgram>> _unitDefaultMap    = new HashMap<String,ArrayList<ConfigProgram>>();    private ArrayList<PersistenceUnitConfig> _unitConfigList    = new ArrayList<PersistenceUnitConfig>();  private HashMap<String,AmberPersistenceUnit> _unitMap    = new HashMap<String,AmberPersistenceUnit>();  private HashMap<String,EntityManagerFactory> _factoryMap    = new HashMap<String,EntityManagerFactory>();  private HashMap<String,EntityManager> _persistenceContextMap    = new HashMap<String,EntityManager>();  private HashMap<String,EmbeddableType> _embeddableMap    = new HashMap<String,EmbeddableType>();  private HashMap<String,EntityType> _entityMap    = new HashMap<String,EntityType>();  private HashMap<String,MappedSuperclassType> _mappedSuperclassMap    = new HashMap<String,MappedSuperclassType>();  private HashMap<String,ListenerType> _defaultListenerMap    = new HashMap<String,ListenerType>();  private HashMap<String, ArrayList<ListenerType>>    _entityListenerMap = new HashMap<String, ArrayList<ListenerType>>();  private Throwable _exception;  private HashMap<String,Throwable> _embeddableExceptionMap    = new HashMap<String,Throwable>();  private HashMap<String,Throwable> _entityExceptionMap    = new HashMap<String,Throwable>();  private HashMap<String,Throwable> _listenerExceptionMap    = new HashMap<String,Throwable>();  private HashMap<Path,RootContext> _persistenceRootMap    = new HashMap<Path,RootContext>();    private ArrayList<RootContext> _pendingRootList    = new ArrayList<RootContext>();    private ArrayList<AmberPersistenceUnit> _pendingUnitList    = new ArrayList<AmberPersistenceUnit>();  private ArrayList<LazyEntityManagerFactory> _pendingFactoryList    = new ArrayList<LazyEntityManagerFactory>();    private HashSet<URL> _persistenceURLSet = new HashSet<URL>();  private ArrayList<String> _pendingClasses = new ArrayList<String>();  private AmberContainer(ClassLoader loader)  {    _parentAmberContainer = _localContainer.get(loader);    _parentLoader = Environment.getEnvironmentClassLoader(loader);    _localContainer.set(this, _parentLoader);    _tempLoader = _parentLoader.getNewTempClassLoader();    // --- ok        _enhancer = new AmberEnhancer(this);    EnhancerManager.create(_parentLoader).addClassEnhancer(_enhancer);    if (_parentAmberContainer != null)      copyContainerDefaults(_parentAmberContainer);    _parentLoader.addScanListener(this);    Environment.addEnvironmentListener(this, _parentLoader);    try {      if (_parentLoader instanceof DynamicClassLoader)        ((DynamicClassLoader) _parentLoader).make();    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new RuntimeException(e);    }  }  /**   * Returns the local container.   */  public static AmberContainer create()  {    return create(Thread.currentThread().getContextClassLoader());  }  /**   * Returns the local container.   */  public static AmberContainer create(ClassLoader loader)  {    synchronized (_localContainer) {      AmberContainer container = _localContainer.getLevel(loader);      if (container == null) {        container = new AmberContainer(loader);        _localContainer.set(container, loader);      }      return container;    }  }  /**   * Returns the local container.   */  public static AmberContainer getCurrent()  {    return getCurrent(Thread.currentThread().getContextClassLoader());  }  /**   * Returns the current environment container.   */  public static AmberContainer getCurrent(ClassLoader loader)  {    synchronized (_localContainer) {      return _localContainer.get(loader);    }  }  /**   * Sets the primary data source.   */  public void setDataSource(DataSource dataSource)  {    _dataSource = dataSource;  }  /**   * Gets the primary data source.   */  public DataSource getDataSource()  {    return _dataSource;  }  /**   * Sets the read data source.   */  public void setReadDataSource(DataSource dataSource)  {    _readDataSource = dataSource;  }  /**   * Gets the read data source.   */  public DataSource getReadDataSource()  {    return _readDataSource;  }  /**   * Sets the xa data source.   */  public void setXADataSource(DataSource dataSource)  {    _xaDataSource = dataSource;  }  /**   * Gets the XA data source.   */  public DataSource getXADataSource()  {    return _xaDataSource;  }  /**   * True if database tables should be created automatically.   */  public boolean getCreateDatabaseTables()  {    return _createDatabaseTables;  }  /**   * True if database tables should be created automatically.   */  public void setCreateDatabaseTables(boolean isCreate)  {    _createDatabaseTables = isCreate;  }  /**   * Returns the parent loader   */  public ClassLoader getParentClassLoader()  {    return _parentLoader;  }  /**   * Returns the parent loader   */  public ClassLoader getEnhancedLoader()  {    return _parentLoader;  }  /**   * Returns the enhancer.   */  public AmberGenerator getGenerator()  {    return _enhancer;  }  /**   * Returns the persistence unit JNDI context.   */  public static String getPersistenceUnitJndiPrefix()  {    return "java:comp/env/persistence/_amber_PersistenceUnit/";  }  /**   * Adds a persistence-unit default   */  public void addPersistenceUnitDefault(ConfigProgram program)  {    _unitDefaultList.add(program);  }  /**   * Returns the persistence-unit default list.   */  public ArrayList<ConfigProgram> getPersistenceUnitDefaultList()  {    return _unitDefaultList;  }  /**   * Adds a persistence-unit default   */  public void addPersistenceUnitProxy(String name,				      ArrayList<ConfigProgram> program)  {    ArrayList<ConfigProgram> oldProgram = _unitDefaultMap.get(name);    if (oldProgram == null)      oldProgram = new ArrayList<ConfigProgram>();    oldProgram.addAll(program);        _unitDefaultMap.put(name, oldProgram);  }  public ArrayList<ConfigProgram> getProxyProgram(String name)  {    return _unitDefaultMap.get(name);  }  /**   * Returns the persistence unit JNDI context.   */  public static String getPersistenceContextJndiPrefix()  {    //return "java:comp/env/persistence/PersistenceContext/";    return "java:comp/env/persistence/";  }  /**   * Returns the JClassLoader.   */  public ClassLoader getTempClassLoader()  {    return _tempLoader;  }    public Class loadTempClass(String name)    throws ClassNotFoundException  {    return Class.forName(name, false, getTempClassLoader());  }  private void copyContainerDefaults(AmberContainer parent)  {    _dataSource = parent._dataSource;    _xaDataSource = parent._xaDataSource;    _readDataSource = parent._readDataSource;    _createDatabaseTables = parent._createDatabaseTables;  }  public void init()  {  }  /**   * Returns the EmbeddableType for an introspected class.   */  public EmbeddableType getEmbeddable(String className)  {    Throwable e = _embeddableExceptionMap.get(className);    if (e != null)      throw new AmberRuntimeException(e);    else if (_exception != null) {      throw new AmberRuntimeException(_exception);    }    return _embeddableMap.get(className);  }  /**   * Returns the EntityType for an introspected class.   */  public EntityType getEntity(String className)  {    Throwable e = _entityExceptionMap.get(className);    if (e != null)      throw new AmberRuntimeException(e);    else if (_exception != null) {      throw new AmberRuntimeException(_exception);    }    return _entityMap.get(className);  }  /**   * Returns the MappedSuperclassType for an introspected class.   */  public MappedSuperclassType getMappedSuperclass(String className)  {    Throwable e = _entityExceptionMap.get(className);    if (e != null)      throw new AmberRuntimeException(e);    else if (_exception != null) {      throw new AmberRuntimeException(_exception);    }    MappedSuperclassType type = _mappedSuperclassMap.get(className);    return type;  }  /**   * Returns the default ListenerType for an introspected class.   */  public ListenerType getDefaultListener(String className)  {    if (true)      return null;        Throwable e = _listenerExceptionMap.get(className);    if (e != null)      throw new AmberRuntimeException(e);    else if (_exception != null) {      throw new AmberRuntimeException(_exception);    }    return _defaultListenerMap.get(className);  }  /**   * Returns the entity ListenerType for an introspected class.   */  public ListenerType getEntityListener(String className)  {    if (true)      return null;        Throwable e = _listenerExceptionMap.get(className);    if (e != null)      throw new AmberRuntimeException(e);    else if (_exception != null) {      throw new AmberRuntimeException(_exception);    }    ArrayList<ListenerType> listenerList;    for (Map.Entry<String, ArrayList<ListenerType>>           entry : _entityListenerMap.entrySet()) {      listenerList = entry.getValue();      if (listenerList == null)        continue;      for (ListenerType listener : listenerList) {        if (className.equals(listener.getBeanClass().getName()))          return listener;      }    }    return null;  }  /**   * Returns the listener for an introspected class.   */  public ListenerType getListener(String className)  {    if (true)      return null;        ListenerType listener = getDefaultListener(className);    if (listener == null)      listener = getEntityListener(className);    return listener;  }  /**   * Returns the entity listeners for an entity.   */  public ArrayList<ListenerType>  getEntityListeners(String entityClassName)  {    return null;        // return _entityListenerMap.get(entityClassName);  }  /**   * Adds an entity for an introspected class.   */  public void addEntityException(String className, Throwable e)  {    _entityExceptionMap.put(className, e);  }  /**   * Adds an entity for an introspected class.   */  public void addException(Throwable e)  {    if (_exception == null) {      _exception = e;      Environment.setConfigException(e);    }  }  public Throwable getConfigException()  {    return _exception;  }  /**   * Adds an embeddable for an introspected class.   */  public void addEmbeddable(String className, EmbeddableType type)  {    _embeddableMap.put(className, type);  }  /**   * Adds an entity for an introspected class.   */  public void addEntity(String className, EntityType type)  {    _entityMap.put(className, type);  }  /**   * Adds a mapped superclass for an introspected class.   */  public void addMappedSuperclass(String className,                                  MappedSuperclassType type)  {    _mappedSuperclassMap.put(className, type);  }  /**   * Adds a default listener.   */  public void addDefaultListener(String className,                                 ListenerType type)  {    _defaultListenerMap.put(className, type);  }  /**   * Adds an entity listener.   */  public void addEntityListener(String entityClassName,                                ListenerType listenerType)  {    ArrayList<ListenerType> listenerList      = _entityListenerMap.get(entityClassName);    if (listenerList == null) {      listenerList = new ArrayList<ListenerType>();      _entityListenerMap.put(entityClassName, listenerList);    }

⌨️ 快捷键说明

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