Example #1
0
  /**
   * Constructs a new descriptor for the specified class. When describing inheritence, the
   * descriptor of the parent class should be used and only the fields added in this object must be
   * supplied here.
   *
   * @param javaClass The Java type of this class
   * @param fields The fields described for this class
   * @param identity The field of the identity (key) of this class, may be null
   * @param extend The descriptor of the class which this class extends,
   * @param accessMode The access mode for this class (null is shared) or null if this is a
   *     top-level class
   * @throws MappingException The extended descriptor does not match a parent class of this type
   */
  public ClassDescriptorImpl(
      Class javaClass,
      FieldDescriptor[] fields,
      FieldDescriptor identity,
      ClassDescriptor extend,
      AccessMode accessMode)
      throws MappingException {
    if (!Types.isConstructable(javaClass))
      throw new MappingException("mapping.classNotConstructable", javaClass.getName());
    _javaClass = javaClass;
    if (fields == null) throw new IllegalArgumentException("Argument 'fields' is null");
    _fields = (FieldDescriptor[]) fields.clone();

    if (extend != null) {
      if (!extend.getJavaClass().isAssignableFrom(javaClass))
        throw new MappingException(
            "mapping.classDoesNotExtend", _javaClass.getName(), extend.getJavaClass().getName());
      _extends = extend;
      _identity = (identity == null ? _extends.getIdentity() : identity);
    } else {
      _extends = null;
      _identity = identity;
    }
    _accessMode = accessMode;
  }
Example #2
0
  protected ClassDescriptor createDescriptor(ClassMapping clsMap) throws MappingException {
    ClassDescriptor clsDesc;
    FieldDescriptor[] fields;
    Vector jdoFields;

    // If no LDAP information for class, ignore it. DAX only
    // supports DAX class descriptors.
    if (clsMap.getMapTo() == null || clsMap.getMapTo().getLdapOc() == null) return NoDescriptor;

    // See if we have a compiled descriptor.
    clsDesc = loadClassDescriptor(clsMap.getName());
    if (clsDesc != null && clsDesc instanceof DAXClassDescriptor) return clsDesc;

    // Use super class to create class descriptor. Field descriptors will be
    // generated only for supported fields, see createFieldDesc later on.
    // This class may only extend a DAX class, otherwise no mapping will be
    // found for the parent.
    clsDesc = super.createDescriptor(clsMap);

    // DAX descriptor must include an identity field, the identity field
    // is either a field, or a container field containing only DAX fields.
    // If the identity field is not a JDO field, it will be cleaned later
    // on (we need the descriptor for relations mapping).
    if (clsDesc.getIdentity() == null)
      throw new MappingException("mapping.noIdentity", clsDesc.getJavaClass().getName());
    /*
    if ( clsDesc.getIdentity() instanceof ContainerFieldDesc ) {
        FieldDescriptor[] idFields;

        idFields = ( (ContainerFieldDesc) clsDesc.getIdentity() ).getFields();
        for ( int i = 0 ; i < idFields.length ; ++i )
            if ( ! ( idFields[ i ] instanceof DAXFieldDesc ) )
                throw new MappingException( "dax.identityNotDAX", idFields[ i ] );
    }
    */

    return new DAXClassDescriptor(clsDesc, clsMap.getMapTo().getLdapOc(), null);
  }
 /**
  * Walks through all fields of a descriptor and resolves relation {@link ClassDescriptor}s by
  * using mapping information or, if not present, resolution by file to support generated {@link
  * ClassDescriptor}s. Resolved {@link ClassDescriptor}s will be set as a field's descriptor.
  *
  * @param clsDesc The ClassDescriptor in focus.
  */
 protected void resolveRelations(final ClassDescriptor clsDesc) {
   FieldDescriptor[] fields = clsDesc.getFields();
   for (int i = 0; i < fields.length; ++i) {
     FieldDescriptor field = fields[i];
     ClassDescriptor desc = getDescriptor(field.getFieldType().getName());
     // Resolve ClassDescriptor from the file system as well.
     if (desc == null && !field.getFieldType().isPrimitive()) {
       ClassResolutionByFile resolutionCommand = new ClassResolutionByFile();
       resolutionCommand.addNature(ClassLoaderNature.class.getName());
       ClassLoaderNature clNature = new ClassLoaderNature(resolutionCommand);
       clNature.setClassLoader(getClassLoader());
       desc = resolutionCommand.resolve(field.getFieldType());
       ((FieldDescriptorImpl) field).setClassDescriptor(desc);
     }
     if ((desc != null) && (field instanceof FieldDescriptorImpl)) {
       ((FieldDescriptorImpl) field).setClassDescriptor(desc);
     }
   }
 }
  protected ClassDescriptor createClassDescriptor(final ClassMapping classMapping)
      throws MappingException {
    // If there is no SQL information for class, ignore it.
    if ((classMapping.getMapTo() == null) || (classMapping.getMapTo().getTable() == null)) {
      LOG.info(Messages.format("mapping.ignoringMapping", classMapping.getName()));
      return null;
    }

    // Create the class descriptor, and register the JDO nature with it.
    ClassDescriptorImpl clsDesc = new ClassDescriptorImpl();
    clsDesc.addNature(ClassDescriptorJDONature.class.getName());
    ClassDescriptorJDONature jdoNature = new ClassDescriptorJDONature(clsDesc);

    // Obtain the Java class.
    Class<?> javaClass = resolveType(classMapping.getName());
    if (!Types.isConstructable(javaClass, true)) {
      throw new MappingException("mapping.classNotConstructable", javaClass.getName());
    }
    clsDesc.setJavaClass(javaClass);

    // If this class extends another class, we need to obtain the extended
    // class and make sure this class indeed extends it.
    ClassDescriptor extDesc = getExtended(classMapping, javaClass);
    if (extDesc != null) {
      if (!(extDesc.hasNature(ClassDescriptorJDONature.class.getName()))) {
        throw new IllegalArgumentException("Extended class does not have a JDO descriptor");
      }

      new ClassDescriptorJDONature(extDesc).addExtended(clsDesc);
    }
    clsDesc.setExtends(extDesc);

    // If this class depends on another class, obtain the class it depends upon.
    clsDesc.setDepends(getDepended(classMapping, javaClass));

    // Create all field descriptors.
    FieldDescriptorImpl[] allFields = createFieldDescriptors(classMapping, javaClass);

    // Make sure there are no two fields with the same name.
    checkFieldNameDuplicates(allFields, javaClass);

    // Set class descriptor containing the field
    for (int i = 0; i < allFields.length; i++) {
      allFields[i].setContainingClassDescriptor(clsDesc);
    }

    // Identify identity and normal fields. Note that order must be preserved.
    List<FieldDescriptor> fieldList = new ArrayList<FieldDescriptor>(allFields.length);
    List<FieldDescriptor> idList = new ArrayList<FieldDescriptor>();
    if (extDesc == null) {
      // Sort fields into 2 lists based on identity definition of field.
      for (int i = 0; i < allFields.length; i++) {
        if (!allFields[i].isIdentity()) {
          fieldList.add(allFields[i]);
        } else {
          idList.add(allFields[i]);
        }
      }

      if (idList.size() == 0) {
        // Found no identities based on identity definition of field.
        // Try to find identities based on identity definition on class.
        String[] idNames = classMapping.getIdentity();
        if ((idNames == null) || (idNames.length == 0)) {
          // There are also no identity definitions on class.
          throw new MappingException("mapping.noIdentity", javaClass.getName());
        }

        FieldDescriptor identity;
        for (int i = 0; i < idNames.length; i++) {
          identity = findIdentityByName(fieldList, idNames[i], javaClass);
          if (identity != null) {
            idList.add(identity);
          } else {
            throw new MappingException("mapping.identityMissing", idNames[i], javaClass.getName());
          }
        }
      }
    } else {
      // Add all fields of extending class to field list.
      for (int i = 0; i < allFields.length; i++) {
        fieldList.add(allFields[i]);
      }

      // Add all identities of extended class to identity list.
      FieldDescriptor[] extIds = ((ClassDescriptorImpl) extDesc).getIdentities();
      for (int i = 0; i < extIds.length; i++) {
        idList.add(extIds[i]);
      }

      // Search redefined identities in extending class.
      FieldDescriptor identity;
      for (int i = 0; i < idList.size(); i++) {
        String idName = (idList.get(i)).getFieldName();
        identity = findIdentityByName(fieldList, idName, javaClass);
        if (identity != null) {
          idList.set(i, identity);
        }
      }
    }

    // Set identities on class descriptor.
    FieldDescriptor[] ids = new FieldDescriptor[idList.size()];
    clsDesc.setIdentities(idList.toArray(ids));

    // Set fields on class descriptor.
    FieldDescriptor[] fields = new FieldDescriptor[fieldList.size()];
    clsDesc.setFields(fieldList.toArray(fields));

    jdoNature.setTableName(classMapping.getMapTo().getTable());

    // Set the field name used for object modification checks.
    jdoNature.setVersionField(classMapping.getVersion());

    extractAndSetAccessMode(jdoNature, classMapping);
    extractAndAddCacheParams(jdoNature, classMapping, javaClass);
    extractAndAddNamedQueries(jdoNature, classMapping);
    extractAndAddNamedNativeQueries(jdoNature, classMapping);
    extractAndSetKeyGeneratorDescriptor(jdoNature, classMapping.getKeyGenerator());

    return clsDesc;
  }