baseconfigintrospector.java

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

JAVA
2,100
字号
/* * 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 Rodrigo Westrupp */package com.caucho.amber.cfg;import com.caucho.amber.entity.Listener;import com.caucho.amber.field.*;import com.caucho.amber.idgen.IdGenerator;import com.caucho.amber.manager.AmberPersistenceUnit;import com.caucho.amber.table.AmberColumn;import com.caucho.amber.table.ForeignColumn;import com.caucho.amber.table.LinkColumns;import com.caucho.amber.table.AmberTable;import com.caucho.amber.type.*;import com.caucho.bytecode.*;import com.caucho.config.ConfigException;import com.caucho.jdbc.JdbcMetaData;import com.caucho.util.L10N;import java.lang.annotation.Annotation;import java.lang.reflect.AccessibleObject;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.Type;import javax.persistence.*;import javax.persistence.EmbeddedId;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.logging.Level;import java.util.logging.Logger;/** * Base concrete introspector for orm.xml and annotations. */public class BaseConfigIntrospector extends AbstractConfigIntrospector {  private static final Logger log    = Logger.getLogger(BaseConfigIntrospector.class.getName());  private static final L10N L = new L10N(BaseConfigIntrospector.class);  private static final Class []_annTypes = new Class[] {    Basic.class, javax.persistence.Column.class, javax.persistence.Id.class,    ElementCollection.class, EmbeddedId.class,    ManyToOne.class, OneToMany.class, OneToOne.class, ManyToMany.class,    Version.class, Transient.class  };  final AmberConfigManager _configManager;  final AmberPersistenceUnit _persistenceUnit;  ArrayList<Completion> _linkCompletions = new ArrayList<Completion>();  ArrayList<Completion> _depCompletions = new ArrayList<Completion>();  HashMap<String, EmbeddableConfig> _embeddableConfigMap    = new HashMap<String, EmbeddableConfig>();  ArrayList<EntityMappingsConfig> _entityMappingsList;  // HashMap<String, EntityConfig> _entityConfigMap  //   = new HashMap<String, EntityConfig>();  //  // HashMap<String, MappedSuperclassConfig> _mappedSuperclassConfigMap  //   = new HashMap<String, MappedSuperclassConfig>();  /**   * Creates the introspector.   */  public BaseConfigIntrospector(AmberConfigManager manager)  {    _configManager = manager;    _persistenceUnit = manager.getPersistenceUnit();  }  /**   * Sets the entity mappings list.   */  public void setEntityMappingsList(ArrayList<EntityMappingsConfig> entityMappingsList)  {    _entityMappingsList = entityMappingsList;  }  /**   * Returns the entity config for a class name.   */  public EntityConfig getEntityConfig(String className)  {    // jpa/0r41    if (_entityMappingsList == null)      return null;    // jpa/0s2l: mapping-file.    HashMap<String, EntityConfig> entityMap;    EntityConfig entityConfig;    for (EntityMappingsConfig entityMappings : _entityMappingsList) {      entityMap = entityMappings.getEntityMap();      if (entityMap != null) {        entityConfig = entityMap.get(className);        if (entityConfig != null)          return entityConfig;      }    }    return null;  }  /**   * Returns the mapped superclass config for a class name.   */  public MappedSuperclassConfig getMappedSuperclassConfig(String className)  {    if (_entityMappingsList == null)      return null;    HashMap<String, MappedSuperclassConfig> superclassMap;    MappedSuperclassConfig superclassConfig;    for (EntityMappingsConfig entityMappings : _entityMappingsList) {      superclassMap = entityMappings.getMappedSuperclassMap();      if (superclassMap != null) {        superclassConfig = superclassMap.get(className);        if (superclassConfig != null)          return superclassConfig;      }    }    return null;  }  /**   * Initializes the persistence unit meta data:   * default listeners and so on.   */  public void initMetaData(ArrayList<EntityMappingsConfig> entityMappingsList,                           AmberPersistenceUnit persistenceUnit)    throws ConfigException  {    PersistenceUnitMetaDataConfig metaData = null;    for (EntityMappingsConfig entityMappings : entityMappingsList) {      metaData = entityMappings.getPersistenceUnitMetaData();      // It is undefined if this element occurs in multiple mapping      // files within the same persistence unit.      if (metaData != null)        break;    }    if (metaData == null)      return;    PersistenceUnitDefaultsConfig defaults;    defaults = metaData.getPersistenceUnitDefaults();    if (defaults == null)      return;    EntityListenersConfig entityListeners;    entityListeners = defaults.getEntityListeners();    if (entityListeners == null)      return;    ArrayList<EntityListenerConfig> listeners;    listeners = entityListeners.getEntityListeners();    for (EntityListenerConfig listener : listeners)      introspectDefaultListener(listener, persistenceUnit);  }  public void introspectDefaultListener(EntityListenerConfig listener,                                        AmberPersistenceUnit persistenceUnit)    throws ConfigException  {    String className = listener.getClassName();    Class type = persistenceUnit.loadTempClass(className);    if (type == null)      throw new ConfigException(L.l("'{0}' is an unknown type for <entity-listener> in orm.xml",                                    className));    ListenerType listenerType = persistenceUnit.addDefaultListener(type);    introspectListener(type, listenerType);  }  public void introspectEntityListeners(Class type,                                        EntityType entityType,                                        AmberPersistenceUnit persistenceUnit)    throws ConfigException  {    getInternalEntityListenersConfig(type, _annotationCfg);    EntityListeners entityListenersAnn = (EntityListeners) _annotationCfg.getAnnotation();    EntityListenersConfig entityListenersCfg      = _annotationCfg.getEntityListenersConfig();    Class listeners[] = null;/*    // XML mapping takes higher priority than annotations.    if (entityListenersCfg != null)      listeners = entityListenersCfg.getEntityListeners().toArray();    else if (entityListenersAnn != null)      listeners = entityListenersAnn.value();    else      return;*/    String entityTypeName = entityType.getBeanClass().getName();    for (int i = 0; listeners != null && i < listeners.length; i++) {      Class cl = null;      // Introspects annotation or xml.      if (listeners[i] instanceof Class)        cl = (Class) listeners[i];      else {        /*        EntityListenerConfig listenerConfig          = (EntityListenerConfig) listeners[i];        String className = listenerConfig.getClassName();        cl = persistenceUnit.loadTempClass(className);        if (cl == null)          throw new ConfigException(L.l("'{0}' is an unknown type for <entity-listener> in orm.xml",                                        className));         */      }      if (persistenceUnit.getDefaultListener(cl.getName()) != null)        continue;      introspectEntityListener(cl,                               persistenceUnit,                               entityType,                               entityTypeName);    }  }  public void introspectEntityListener(Class type,                                       AmberPersistenceUnit persistenceUnit,                                       EntityType sourceType,                                       String sourceClassName)    throws ConfigException  {    if (type == null) {      throw new ConfigException(L.l("'{0}' is an unknown type for @EntityListeners annotated at class '{1}'",                                    type.getName(),                                    sourceClassName));    }    Class parentClass = type.getSuperclass();    if (parentClass == null) {      // java.lang.Object      return;    }    /*    else {      // XXX: entity listener super-classes in a hierarchy might      // not be annotated as entity listeners but they might have      // @PreXxx or @PostXxx annotated methods. On the other hand,      // needs to filter regular classes out.      introspectEntityListener(parentClass, persistenceUnit,                               sourceType, sourceClassName);    }    */    // jpa/0r42    ListenerType listenerType      = persistenceUnit.getEntityListener(type.getName());    ListenerType newListenerType      = persistenceUnit.addEntityListener(sourceClassName, type);    if (listenerType == null) {      listenerType = newListenerType;      introspectListener(type, listenerType);    }    sourceType.addListener(listenerType);  }  public void introspectListener(Class type,                                 ListenerType listenerType)    throws ConfigException  {    listenerType.setInstanceClassName(listenerType.getName() + "__ResinExt");    for (Method method : type.getDeclaredMethods()) {      introspectCallbacks(listenerType, method);    }  }  /**   * Introspects the callbacks.   */  public void introspectCallbacks(Class type,                                  EntityType entityType)    throws ConfigException  {    getInternalExcludeDefaultListenersConfig(type, _annotationCfg);    if (! _annotationCfg.isNull())      entityType.setExcludeDefaultListeners(true);    getInternalExcludeSuperclassListenersConfig(type, _annotationCfg);    if (! _annotationCfg.isNull())      entityType.setExcludeSuperclassListeners(true);    for (Method method : type.getDeclaredMethods()) {      introspectCallbacks(entityType, method);    }  }  /**   * Introspects the callbacks.   */  public void introspectCallbacks(AbstractEnhancedType type,                                  Method method)    throws ConfigException  {    Class []param = method.getParameterTypes();    String methodName = method.getName();    Class jClass = type.getBeanClass();    boolean isListener = type instanceof ListenerType;    int n = ListenerType.CALLBACK_CLASS.length;    for (int i = 1; i < n; i++) {      getInternalCallbackConfig(i, jClass, method, methodName,				_annotationCfg);      if (! _annotationCfg.isNull()) {        validateCallback(ListenerType.CALLBACK_CLASS[i].getName(),                         method, isListener);        type.addCallback(i, method);      }    }  }  /**   * Introspects named queries.   */  void introspectNamedQueries(Class type, String typeName)  {    // jpa/0y0-    getInternalNamedQueryConfig(type, _annotationCfg);    NamedQuery namedQueryAnn = (NamedQuery) _annotationCfg.getAnnotation();    NamedQueryConfig namedQueryConfig = _annotationCfg.getNamedQueryConfig();    // getInternalNamedQueriesConfig(type);    NamedQueries namedQueriesAnn = (NamedQueries) type.getAnnotation(NamedQueries.class);    // NamedQueriesConfig namedQueriesConfig = _annotationCfg.getNamedQueriesConfig();    if ((namedQueryAnn == null) && (namedQueriesAnn == null))      return;    NamedQuery namedQueryArray[];    if ((namedQueryAnn != null) && (namedQueriesAnn != null)) {      throw new ConfigException(L.l("{0} may not have both @NamedQuery and @NamedQueries",                                    typeName));    }    else if (namedQueriesAnn != null) {      namedQueryArray = namedQueriesAnn.value();    }    else {      namedQueryArray = new NamedQuery[] { namedQueryAnn };    }    for (int i=0; i < namedQueryArray.length; i++) {      namedQueryAnn = namedQueryArray[i];      _persistenceUnit.addNamedQuery(namedQueryAnn.name(),                                     namedQueryAnn.query());    }

⌨️ 快捷键说明

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