amberpersistenceunit.java

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

JAVA
1,802
字号
  }  /**   * Updates global entity priorities for flushing.   */  public void updateFlushPriority()  {    ArrayList<EntityType> updatingEntities      = new ArrayList<EntityType>();    try {      HashMap<String,AmberType> typeMap = _typeManager.getTypeMap();      Collection<AmberType> types = typeMap.values();      Iterator it = types.iterator();      while (it.hasNext()) {        AmberType type = (AmberType) it.next();        if (type instanceof EntityType) {          EntityType entityType = (EntityType) type;          if (updatingEntities.contains(entityType))            continue;          updatingEntities.add(entityType);          entityType.updateFlushPriority(updatingEntities);        }      }    } finally {      updatingEntities = null;    }  }  /**   * Creates a type.   */  public AmberType createType(String typeName)    throws ConfigException  {    AmberType type = _typeManager.get(typeName);    if (type != null)      return type;    Class cl = loadTempClass(typeName);    if (cl == null)      throw new ConfigException(L.l("'{0}' is an unknown type", typeName));    return createType(cl);  }  /**   * Creates a type.   */  public AmberType createType(Class javaType)    throws ConfigException  {    AmberType type = _typeManager.create(javaType);    if (type != null)      return type;    return createEntity(javaType);  }  /**   * Sets the generator.   */  public AmberGenerator getGenerator()  {    if (_generator != null)      return _generator;    /*      else if (_enhancer != null)      return _enhancer;    */    else {      _generator = _amberContainer.getGenerator();      return _generator;    }  }  /**   * Returns the FALSE SQL literal, i.e., either "false" or "0".   */  public String getFalseLiteral()  {    return getMetaData().getFalseLiteral();  }  /**   * Returns true if POSITION SQL function is allowed.   */  public boolean hasPositionFunction()  {    return getMetaData().supportsPositionFunction();  }  /**   * Returns true if generated keys are allowed.   */  public boolean hasReturnGeneratedKeys()  {    return getMetaData().supportsGetGeneratedKeys();  }  /**   * Sets the entity mappings config.   */  public void setEntityMappingsList(ArrayList<EntityMappingsConfig> entityMappingsList)  {    //_entityMappingsList = entityMappingsList;    //_entityIntrospector.setEntityMappingsList(_entityMappingsList);    //_mappedSuperIntrospector.setEntityMappingsList(_entityMappingsList);  }  /**   * Initialize the resource.   */  public void init()    throws ConfigException, IOException  {    initLoaders();    /*    if (_entityMappingsList != null) {      BaseConfigIntrospector introspector = new BaseConfigIntrospector();      introspector.initMetaData(_entityMappingsList, this);    }    */    if (_dataSource == null)      return;    /*    try {      Connection conn = _dataSource.getConnection();      try {        DatabaseMetaData metaData = conn.getMetaData();        try {          _supportsGetGeneratedKeys = metaData.supportsGetGeneratedKeys();        } catch (Throwable e) {        }      } finally {        conn.close();      }    } catch (SQLException e) {      throw new ConfigException(e);    }    */  }  /**   * Initialize the resource.   */  public void initLoaders()    throws ConfigException, IOException  {    // getEnvManager().initLoaders();  }  public void initEntityHomes()    throws AmberRuntimeException, ConfigException  {    ArrayList<AmberEntityHome> homeList;        synchronized (this) {      if (_isInit)        return;      _isInit = true;      homeList = new ArrayList<AmberEntityHome>(_lazyHomeInit);      _lazyHomeInit.clear();    }    if (_jtaDataSourceName != null && _jtaDataSource == null)      _jtaDataSource = (DataSource) Jndi.lookup(_jtaDataSourceName);    if (_nonJtaDataSourceName != null && _nonJtaDataSource == null)      _nonJtaDataSource = (DataSource) Jndi.lookup(_nonJtaDataSourceName);    initTables();    // for QA consistency    Collections.sort(homeList);    for (AmberEntityHome home : homeList) {      home.init();    }  }  /**   * Configure lazy.   */  public void initTables()    throws ConfigException  {    for (IdGenerator gen : _tableGenMap.values())      gen.start();        while (_lazyTable.size() > 0) {      AmberTable table = _lazyTable.remove(0);      if (getDataSource() == null)        throw new ConfigException(L.l("{0}: No configured data-source found.",				      this));      if (getCreateDatabaseTables())        table.createDatabaseTable(this);      if (getValidateDatabaseTables())        table.validateDatabaseTable(this);    }  }  /**   * Returns the cache connection.   */  public CacheConnection getCacheConnection()  {    // cache connection cannot be reused (#998)    CacheConnection cacheConnection = new CacheConnection(this);    // ejb/0a0b - avoid dangling connections.    cacheConnection.register();    return cacheConnection;  }  /**   * Returns the cache connection.   */  public AmberConnection createAmberConnection(boolean isExtended)  {    return new AmberConnection(this, isExtended);  }  /**   * Returns the thread's amber connection.   */  public AmberConnection getThreadConnection(boolean isExtended)  {    AmberConnection aConn = _threadConnection.get();    if (aConn == null) {      aConn = new AmberConnection(this, isExtended);      aConn.initThreadConnection();      _threadConnection.set(aConn);    }    return aConn;  }  /**   * Unset the thread's amber connection.   */  public void removeThreadConnection()  {    _threadConnection.set(null);  }  /**   * Returns an EntityHome.   */  public AmberEntityHome getHome(Class cl)  {    return getEntityHome(cl.getName());  }  /**   * Returns the query cache.   */  public AbstractQuery getQueryParseCache(String sql)  {    return _queryParseCache.get(sql);  }  /**   * Returns the query cache.   */  public void putQueryParseCache(String sql, AbstractQuery query)  {    _queryParseCache.put(sql, query);  }    /**   * Returns the query result.   */  public ResultSetCacheChunk getQueryChunk(QueryCacheKey key)  {    SoftReference<ResultSetCacheChunk> ref = _queryCache.get(key);    if (ref == null)      return null;    else {      ResultSetCacheChunk chunk = ref.get();      if (chunk != null && chunk.isValid())        return chunk;      else        return null;    }  }  /**   * Returns the query meta data.   */  public ResultSetMetaData getQueryMetaData(QueryCacheKey key)  {    SoftReference<ResultSetMetaData> ref = _queryCacheMetaData.get(key);    if (ref == null)      return null;    else      return ref.get();  }  /**   * Applies persistence unit default and entity listeners   * for @PreXxx, @PostXxx callbacks.   */  protected void callListeners(int callbackIndex,                               Entity entity)  {    // ejb/0g22    if (! isJPA())      return;    String className = entity.getClass().getName();    EntityType entityType = (EntityType) _typeManager.get(className);    if (! entityType.getExcludeDefaultListeners()) {      for (ListenerType listenerType : _defaultListeners) {        for (Method m : listenerType.getCallbacks(callbackIndex)) {          Listener listener = (Listener) listenerType.getInstance();          listener.__caucho_callback(callbackIndex, entity);        }      }    }    ArrayList<ListenerType> listeners;    listeners = _amberContainer.getEntityListeners(className);    if (listeners == null)      return;    for (ListenerType listenerType : listeners) {      if ((! entityType.getExcludeDefaultListeners()) &&          _defaultListeners.contains(listenerType))        continue;      for (Method m : listenerType.getCallbacks(callbackIndex)) {        Listener listener = (Listener) listenerType.getInstance();        listener.__caucho_callback(callbackIndex, entity);      }    }  }  /**   * Sets the query result.   */  public void putQueryChunk(QueryCacheKey key, ResultSetCacheChunk chunk)  {    _queryCache.put(key, new SoftReference<ResultSetCacheChunk>(chunk));  }  /**   * Sets the query meta data.   */  public void putQueryMetaData(QueryCacheKey key, ResultSetMetaData metaData)  {    _queryCacheMetaData.put(key, new SoftReference<ResultSetMetaData>(metaData));  }  /**   * Returns the entity item.   */  public EntityItem getEntityItem(String homeName, Object key)    throws AmberException  {    AmberEntityHome home = getEntityHome(homeName);    // XXX: needs refactoring    throw new IllegalStateException("XXXX:");    // return home.findEntityItem(getCacheConnection(), key, false);  }  /**   * Returns the entity with the given key.   */  public EntityItem getEntity(EntityType rootType, Object key)  {    SoftReference<EntityItem> ref;    synchronized (_entityKey) {      _entityKey.init(rootType.getInstanceClass(), key);      ref = _entityCache.get(_entityKey);    }    if (ref != null)      return ref.get();    else      return null;  }  /**   * Returns the entity with the given key.   */  public EntityItem getEntity(EntityKey entityKey)  {    SoftReference<EntityItem> ref;    ref = _entityCache.get(entityKey);    if (ref != null)      return ref.get();    else      return null;  }  /**   * Sets the entity result.   */  public EntityItem putEntity(EntityType rootType,                              Object key,                              EntityItem entity)  {    if (entity == null)      throw new IllegalStateException(L.l("Null entity item cannot be added to the persistence unit cache"));    SoftReference<EntityItem> ref = new SoftReference<EntityItem>(entity);    EntityKey entityKey = new EntityKey(rootType.getInstanceClass(), key);    // can't use putIfNew because the SoftReference might be empty, i.e.    // not "new" but in need of replacement    ref = _entityCache.put(entityKey, ref);    return entity;  }  /**   * Sets the entity result.   */  public EntityItem putEntity(Class cl,                              Object key,                              EntityItem entity)  {    if (entity == null)      throw new IllegalStateException(L.l("Null entity item cannot be added to the persistence unit cache"));    SoftReference<EntityItem> ref = new SoftReference<EntityItem>(entity);    EntityKey entityKey = new EntityKey(cl, key);    // can't use putIfNew because the SoftReference might be empty, i.e.    // not "new" but in need of replacement    ref = _entityCache.put(entityKey, ref);    return entity;  }  /**   * Remove the entity result.   */  public EntityItem removeEntity(EntityType rootType, Object key)  {    SoftReference<EntityItem> ref;    synchronized (_entityKey) {      _entityKey.init(rootType.getInstanceClass(), key);      ref = _entityCache.remove(_entityKey);    }    if (ref != null)      return ref.get();    else      return null;  }  /**   * Updates the cache item after commit.   */  public void updateCacheItem(EntityType rootType,			      Object key,			      EntityItem cacheItem)  {    if (cacheItem == null)      throw new IllegalStateException(L.l("Null entity item cannot be used to update the persistence unit cache"));    SoftReference<EntityItem> ref;    synchronized (_entityKey) {      _entityKey.init(rootType.getInstanceClass(), key);      // jpa/0q00      ref = new SoftReference<EntityItem>(cacheItem);      EntityKey entityKey = new EntityKey(rootType.getInstanceClass(), key);      // ejb/0628, ejb/06d0      _entityCache.put(entityKey, ref);    }  }  /**   * Completions affecting the cache.   */  public void complete(ArrayList<AmberCompletion> completions)  {    int size = completions.size();    if (size == 0)      return;    synchronized (_entityCache) {      Iterator<LruCache.Entry<EntityKey,SoftReference<EntityItem>>> iter;      iter = _entityCache.iterator();      while (iter.hasNext()) {        LruCache.Entry<EntityKey,SoftReference<EntityItem>> entry;        entry = iter.next();        EntityKey key = entry.getKey();        SoftReference<EntityItem> valueRef = entry.getValue();        EntityItem value = valueRef.get();        if (value == null)          continue;	AmberEntityHome entityHome = value.getEntityHome();        EntityType entityRoot = entityHome.getEntityType();        Object entityKey = key.getKey();        for (int i = 0; i < size; i++) {          if (completions.get(i).complete(entityRoot, entityKey, value)) {            // XXX: delete          }        }      }    }    synchronized (_queryCache) {      Iterator<SoftReference<ResultSetCacheChunk>> iter;      iter = _queryCache.values();      while (iter.hasNext()) {        SoftReference<ResultSetCacheChunk> ref = iter.next();        ResultSetCacheChunk chunk = ref.get();        if (chunk != null) {          for (int i = 0; i < size; i++) {            if (completions.get(i).complete(chunk)) {              // XXX: delete            }          }        }      }    }  }  /**   * destroys the manager.   */  public void destroy()  {    _typeManager = null;    _queryCache = null;    _entityCache = null;  }  /**   * New Version of getCreateTableSQL which returns   * the SQL for the table with the given SQL type   * but takes sqlType, length, precision, and scale.   */  public String getCreateColumnSQL(int sqlType, int length, int precision, int scale) {    return getMetaData().getCreateColumnSQL(sqlType, length, precision, scale);  }  @Override  public String toString()  {    return getClass().getSimpleName() + "[" + _name + "]";  }}

⌨️ 快捷键说明

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