Package com.sun.jdo.api.persistence.model

Examples of com.sun.jdo.api.persistence.model.Model


     * <code>pcClass</code>.
     * @param pcClass The persistence capable class.
     * @return A new instance of ClassDesc.
     */
    static ClassDesc newInstance(Class pcClass) {
        Model model = Model.RUNTIME;
        String className = pcClass.getName();
        ClassLoader classLoader = pcClass.getClassLoader();
        ClassDesc rc = null;

        try {
            MappingClassElement mdConfig =
               model.getMappingClass(className, classLoader);

            // Validate the model information for this class.
            validateModel(model, className, classLoader);

            rc = new ClassDesc(mdConfig, pcClass);
View Full Code Here


            }
        }

        // Notify others to cleanup
        TypeTable.removeInstance(classLoader);
        Model model = Model.RUNTIME;
        model.removeResourcesFromCaches(classLoader);

    }
View Full Code Here

   * field and relationship tests to be performed.
   */
  private Collection getFieldsValidationList ()
  {
    ArrayList list = new ArrayList();
    Model model = getModel();
    String className = getClassName();
    PersistenceClassElement persistenceClass =
      getPersistenceClass(className);

    if (persistenceClass != null)
    {
      PersistenceFieldElement[] fields = persistenceClass.getFields();
      int i, count = ((fields != null) ? fields.length : 0);
      Iterator iterator =
        getMappingClass(className).getFields().iterator();

      for (i = 0; i < count; i++)
      {
        PersistenceFieldElement field = fields[i];

        list.add(createFieldExistenceComponent(field));

        // even though this is really the validation step, we
        // only want to add the others if the field exists
        if (model.hasField(className, field.getName()))
        {
          list.add(createFieldPersistenceComponent(field));
          list.add(createFieldPersistenceTypeComponent(field));
          list.add(createFieldConsistencyComponent(field));

          if (isLegalRelationship(field))
          {
            RelationshipElement rel = (RelationshipElement)field;

            /* user modifiable collection class not yet supported
            list.add(createCollectionClassComponent(rel));*/
            list.add(createElementClassComponent(rel));
            list.add(createRelatedClassMatchesComponent(rel));
          }
        }
      }

      while (iterator.hasNext())
      {
        MappingFieldElement field =
          (MappingFieldElement)iterator.next();
        String fieldName = field.getName();

        // only check this if it is not in the jdo model
        if (persistenceClass.getField(fieldName) == null)
        {
          list.add(createFieldExistenceComponent(field));

          // even though this is really the validation step, we
          // only want to add the others if the field exists
          if (model.hasField(className, fieldName))
            list.add(createFieldConsistencyComponent(field));
        }

        if (!isRelationship(field))
          list.add(createColumnOverlapComponent(field));
View Full Code Here

  {
    return new ValidationComponent ()
    {
      public void validate () throws ModelValidationException
      {
        Model model = getModel();
 
        if ((className != null) &&
           model.hasClass(className, getClassLoader()))
        {
          String key = null;

          if (!isPersistent(className))
            key = "util.validation.class_not_persistence_capable";//NOI18N
          else if (!model.isPersistenceCapableAllowed(className))
            key = "util.validation.class_not_allowed";//NOI18N

          if (key != null)
          {
            throw constructClassException(
View Full Code Here

        String className = getClassName();
        String fieldName = field.getName();

        if (isCollection(className, fieldName))
        {
          Model model = getModel();
          String collectionClass = field.getCollectionClass();
          String fieldType = model.getFieldType(className, fieldName);
          boolean missingCollectionClass =
            StringHelper.isEmpty(collectionClass);

          if (!missingCollectionClass &&
            !model.getSupportedCollectionClasses(fieldType).
            contains(collectionClass))
          {
            throw constructFieldException(fieldName,
              "util.validation.collection_class_invalid");//NOI18N
          }
View Full Code Here

  {
    return new ValidationComponent ()
    {
      public void validate () throws ModelValidationException
      {
        Model model = getModel();
        RelationshipElement inverse =
          field.getInverseRelationship(model);
        RelationshipElement inverseInverse = ((inverse != null) ?
          inverse.getInverseRelationship(model) : null);

        if ((inverse != null) &&
          (!field.equals(inverseInverse) || (inverseInverse == null)))
        {
          String fieldName = field.getName();

          throw new ModelValidationException(
            model.getField(getClassName(), fieldName),
            I18NHelper.getMessage(getMessages(),
            "util.validation.inverse_field_invalid", //NOI18N
            new Object[]{fieldName, inverse.getName()}));
        }
      }
View Full Code Here

        String inverseName =
          field.getInverseRelationshipName();

        if (!StringHelper.isEmpty(inverseName))
        {
          Model model = getModel();
          RelationshipElement inverse =
            field.getInverseRelationship(model);

          if (inverse == null) // no such field in that related class
          {
            String relatedClass = getRelatedClass(field);
            String fieldName = field.getName();
            String key = ((relatedClass != null) ?
              "util.validation.related_class_mismatch" : //NOI18N
              "util.validation.related_class_not_found");//NOI18N
            Object[] args = ((relatedClass != null) ?
              new Object[]{fieldName, inverseName, relatedClass}
              : new Object[]{fieldName, inverseName});
             
            throw new ModelValidationException(
              model.getField(getClassName(), fieldName),
              I18NHelper.getMessage(getMessages(), key, args));
          }
        }
      }
    };
View Full Code Here

  {
    return new ValidationComponent ()
    {
      public void validate () throws ModelValidationException
      {
        Model model = getModel();
        RelationshipElement inverse =
          field.getInverseRelationship(model);

        if ((inverse != null) && !isInverseMapping(field, inverse))
        {
          String fieldName = field.getName();

          throw new ModelValidationException(
            model.getField(getClassName(), fieldName),
            I18NHelper.getMessage(getMessages(),
            "util.validation.inverse_mapping_mismatch", //NOI18N
            new Object[]{fieldName, inverse.getName()}));
        }
      }
View Full Code Here

      /** Helper method validating the key class itself:
       * public, serializable, static.
       */
      private void validateClass () throws ModelValidationException
      {
        Model model = getModel();
        int modifiers = model.getModifiersForClass(keyClassName);
        boolean hasKeyClassName = !StringHelper.isEmpty(keyClassName);
        boolean isInnerClass =
          (hasKeyClassName && (keyClassName.indexOf('$') != -1));
        String pcClassName = getClassName();

        // check for key class existence
        if (keyClass == null)
        {
          throw new ModelValidationException(
            ModelValidationException.WARNING,
            model.getClass(pcClassName),
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_missing", //NOI18N
            keyClassName, pcClassName));
        }

        // check for public class modifier
        if (!Modifier.isPublic(modifiers))
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_public", //NOI18N
            keyClassName, pcClassName));
        }
       
        // check for Serializable
        /* This check is disabled because of Boston backward
           compatibility. In Boston there was no requirement for a
           key class being serializable, thus key classes from pc
           classes mapped with boston are not serializable.

        if (!model.implementsInterface(keyClass,
          "java.io.Serializable")) //NOI18N
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_serializable", //NOI18N
            keyClassName, pcClassName));
        }
        */

        // if inner class it must be static
        if (isInnerClass && !Modifier.isStatic(modifiers))
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_static", //NOI18N
            keyClassName, pcClassName));
        }
      }

      /** Helper method validating the fields of the key class.
       */
      private void validateFields () throws ModelValidationException
      {
        String pcClassName = getClassName();
        Model model = getModel();
        // check for valid typed public non-static fields
        List keyClassFieldNames = model.getAllFields(keyClassName);
        Map keyFields = getKeyFields();

        for (Iterator i = keyClassFieldNames.iterator(); i.hasNext();)
        {
          String keyClassFieldName = (String)i.next();
          Object keyClassField =
            getKeyClassField(keyClassName, keyClassFieldName);
          int keyClassFieldModifiers =
            model.getModifiers(keyClassField);
          String keyClassFieldType = model.getType(keyClassField);
          Object keyField = keyFields.get(keyClassFieldName);

          if (Modifier.isStatic(keyClassFieldModifiers))
            // we are not interested in static fields
            continue;

          if (!model.isValidKeyType(keyClassName, keyClassFieldName))
          {
            throw new ModelValidationException(keyClassField,
              I18NHelper.getMessage(getMessages(),
              "util.validation.key_field_type_invalid", //NOI18N
              keyClassFieldName, keyClassName));
          }
         
          if (!Modifier.isPublic(keyClassFieldModifiers))
          {
            throw new ModelValidationException(keyClassField,
              I18NHelper.getMessage(getMessages(),
              "util.validation.key_field_public", //NOI18N
              keyClassFieldName, keyClassName));
          }

          if (keyField == null)
            continue;
         
          if (!keyClassFieldType.equals(model.getType(keyField)))
          {
            throw new ModelValidationException(keyClassField,
              I18NHelper.getMessage(getMessages(),
              "util.validation.key_field_type_mismatch", //NOI18N
              keyClassFieldName, keyClassName, pcClassName));
          }

          // remove handled keyField from the list of keyFields
          keyFields.remove(keyClassFieldName);
        }

        // check whether there are any unhandled key fields
        if (!keyFields.isEmpty())
        {
          Object pcClass = model.getClass(pcClassName);
          String fieldNames = StringHelper.arrayToSeparatedList(
            new ArrayList(keyFields.keySet()));

          throw new ModelValidationException(pcClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_field_missing", //NOI18N
            pcClassName, keyClassName, fieldNames));
        }
      }

      /** Helper method validating the key class constructors.
       */
      private void validateConstructor () throws ModelValidationException
      {
        // no constructor or no arg constructor
        Model model = getModel();
        boolean hasConstr = model.hasConstructor(keyClassName);
        Object noArgConstr =
          model.getConstructor(keyClassName, Model.NO_ARGS);
        int modifiers = model.getModifiers(noArgConstr);

        if (hasConstr &&
          ((noArgConstr == null) || !Modifier.isPublic(modifiers)))
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_constructor", //NOI18N
            keyClassName, getClassName()));
        }
      }

      /** Helper method validating the key class methods.
       */
      private void validateMethods () throws ModelValidationException
      {
        Model model = getModel();
        Object equalsMethod = getNonObjectMethod(keyClassName,
          "equals", Model.EQUALS_ARGS); //NOI18N
        Object hashCodeMethod = getNonObjectMethod(keyClassName,
          "hashCode", Model.NO_ARGS); //NOI18N

        // check equals method
        if (!matchesMethod(equalsMethod, Modifier.PUBLIC,
          0, "boolean")) //NOI18N
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_equals", //NOI18N
            keyClassName, getClassName()));
        }
       
        // check hashCode method
        if (!matchesMethod(hashCodeMethod, Modifier.PUBLIC,
          0, "int")) //NOI18N
        {
          throw new ModelValidationException(keyClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_hashcode", //NOI18N
            keyClassName, getClassName()));
        }
      }
     
      /** Helper method validating the name of the key class.
       */
      private String validateKeyClassName (String keyClassName)
        throws ModelValidationException
      {
        String pcClassName = getClassName();
        Model model = getModel();
        boolean hasKeyClassName = !StringHelper.isEmpty(keyClassName);
        boolean hasPrefix;
        String nameSuffix;
        boolean isOIDNameSuffix;

        // check for existence of key class name
        if (!hasKeyClassName)
        {
          throw new ModelValidationException(
            ModelValidationException.WARNING,
            model.getClass(pcClassName),
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_unset", //NOI18N
            pcClassName));
        }

        keyClassName = keyClassName.trim();
        hasPrefix = keyClassName.startsWith(pcClassName);
        nameSuffix = (hasPrefix ?
           keyClassName.substring(pcClassName.length()) : keyClassName);
        isOIDNameSuffix =
          (nameSuffix.equalsIgnoreCase(".OID") || // NOI18N
           nameSuffix.equalsIgnoreCase("$OID")); // NOI18N

        if (!hasPrefix ||
          (!nameSuffix.equalsIgnoreCase("Key") &&   // NOI18N
           !isOIDNameSuffix))
        {
          Object pcClass = getModel().getClass(pcClassName);
          throw new ModelValidationException(pcClass,
            I18NHelper.getMessage(getMessages(),
            "util.validation.key_class_invalid", //NOI18N
            keyClassName, pcClassName));
        }
        if (isOIDNameSuffix)
        {
          StringBuffer buf = new StringBuffer(keyClassName);
          buf.setCharAt(keyClassName.length() - 4, '$');
          return buf.toString()
        }
        return keyClassName;
      }

      // helper method which returns a field object from the
      // given class or one of its superclasses
      private Object getKeyClassField (String keyClassName,
        String keyClassFieldName)
      {
        Model model = getModel();
        Object keyClassField =
          model.getField(keyClassName, keyClassFieldName);

        if (keyClassField == null// this is an inherited field
        {
          keyClassField = model.getInheritedField(
            keyClassName, keyClassFieldName);
        }

        return keyClassField;
      }

      /** Helper method returning the key fields of the pc class as a map.
       */
      private Map getKeyFields ()
      {
        Model model = getModel();
        String pcClassName = getClassName();
        PersistenceClassElement pce =
          model.getPersistenceClass(pcClassName);
        PersistenceFieldElement[] fields = pce.getFields();
        Map keyFields = new HashMap();

        if (fields != null)
        {
          for (int i = 0; i < fields.length; i++)
          {
            PersistenceFieldElement pfe = fields[i];
            if (pfe.isKey())
            {
              String name = pfe.getName();
              keyFields.put(name,
                model.getField(pcClassName, name));
            }
          }
        }
       
        return keyFields;
      }
      // helper method which returns a method object from the
      // given class or one of its superclasses provided it
      // is not java.lang.Object
      private Object getNonObjectMethod (String className,
        String methodName, String[] argTypeNames)
      {
        Model model = getModel();
        Object method =
          model.getMethod(className, methodName, argTypeNames);

        if (method == null// look for an inherited method
        {
          method = model.getInheritedMethod(
            className, methodName, argTypeNames);

          if ((method != null) && model.getDeclaringClass(method).
            equals("java.lang.Object"))    // NOI18N
          {
            method = null;
          }
        }
View Full Code Here

  {
    return new ValidationComponent ()
    {
      public void validate () throws ModelValidationException
      {
        Model model = getModel();
        Object pcClass = null;

        if (className == null)
          return;
        pcClass = model.getClass(className);
        if (pcClass == null)
          return;

        if (model.implementsInterface(pcClass, "java.io.Serializable")) //NOI18N
        {
          // check readObject method
          Object readMethod = model.getMethod(className,
            "readObject", Model.READ_OBJECT_ARGS); //NOI18N

          if (!matchesMethod(readMethod, Modifier.PRIVATE,
            Modifier.SYNCHRONIZED, "void")) // NOI18N
          {
            throw new ModelValidationException(pcClass,
              I18NHelper.getMessage(getMessages(),
              "util.validation.class_readobject", //NOI18N
              className));
          }
         
          // check writeObject method
          Object writeMethod = model.getMethod(className,
            "writeObject", Model.WRITE_OBJECT_ARGS); //NOI18N

          if (!matchesMethod(writeMethod, Modifier.PRIVATE,
            Modifier.SYNCHRONIZED, "void")) // NOI18N
          {
View Full Code Here

TOP

Related Classes of com.sun.jdo.api.persistence.model.Model

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.