Search in sources :

Example 6 with Model

use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.

the class ConfigCacheImpl method notifyApplicationUnloaded.

/**
 * @inheritDoc
 */
public void notifyApplicationUnloaded(ClassLoader classLoader) {
    boolean debug = logger.isLoggable(Logger.FINEST);
    // classLoader.
    synchronized (this) {
        if (debug) {
            Object[] items = new Object[] { "classLoaderToClassType", classLoaderToClassType.size() };
            // NOI18N
            logger.finest("sqlstore.model.configcacheimpl.size_before", items);
        }
        List pcClasses = (List) classLoaderToClassType.get(classLoader);
        if (pcClasses != null) {
            if (debug) {
                Object[] items = new Object[] { "classConfigs", classConfigs.size() };
                // NOI18N
                logger.finest("sqlstore.model.configcacheimpl.size_before", items);
                items = new Object[] { "oidClassToClassType", oidClassToClassType.size() };
                // NOI18N
                logger.finest("sqlstore.model.configcacheimpl.size_before", items);
            }
            Iterator it = pcClasses.iterator();
            while (it.hasNext()) {
                Class classType = (Class) it.next();
                ClassDesc config = (ClassDesc) classConfigs.remove(classType);
                Class oidClass = config.getOidClass();
                oidClassToClassType.remove(oidClass);
                if (config.hasVersionConsistency() && vcCache != null) {
                    vcCache.removePCType(classType);
                }
            }
            if (debug) {
                Object[] items = new Object[] { "classConfigs", classConfigs.size() };
                // NOI18N
                logger.finest("sqlstore.model.configcacheimpl.size_after", items);
                items = new Object[] { "oidClassToClassType", oidClassToClassType.size() };
                // NOI18N
                logger.finest("sqlstore.model.configcacheimpl.size_after", items);
            }
            // Data about this classLoader is no longer needed.
            // Remove it from cache.
            classLoaderToClassType.remove(classLoader);
            if (debug) {
                Object[] items = new Object[] { "classLoaderToClassType", classLoaderToClassType.size() };
                // NOI18N
                logger.finest("sqlstore.model.configcacheimpl.size_after", items);
            }
        }
    }
    // Notify others to cleanup
    TypeTable.removeInstance(classLoader);
    Model model = Model.RUNTIME;
    model.removeResourcesFromCaches(classLoader);
}
Also used : Model(com.sun.jdo.api.persistence.model.Model)

Example 7 with Model

use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.

the class ClassDesc method newInstance.

/**
 * Creates a new instance of <code>ClassDesc</code> for the given
 * <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);
    } catch (JDOException e) {
        throw e;
    } catch (IllegalArgumentException e) {
        throw new JDOFatalUserException(I18NHelper.getMessage(messages, "core.configuration.loadfailed.class", className), // NOI18N
        e);
    } catch (Exception e) {
        throw new JDOFatalInternalException(I18NHelper.getMessage(messages, "core.configuration.loadfailed.class", className), // NOI18N
        e);
    }
    return rc;
}
Also used : Model(com.sun.jdo.api.persistence.model.Model) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement)

Example 8 with Model

use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.

the class ModelValidator method createRelatedClassMatchesComponent.

/**
 * Create a validation component which can check whether the inverse of
 * the relationship belongs to the related class (type or element class
 * depending on cardinality).
 * @param field the relationship whose inverse relationship is being checked
 * @return the validation component
 */
protected ValidationComponent createRelatedClassMatchesComponent(final RelationshipElement field) {
    return new ValidationComponent() {

        public void validate() throws ModelValidationException {
            String inverseName = field.getInverseRelationshipName();
            if (!StringHelper.isEmpty(inverseName)) {
                Model model = getModel();
                RelationshipElement inverse = field.getInverseRelationship(model);
                if (// no such field in that related class
                inverse == null) {
                    String relatedClass = getRelatedClass(field);
                    String fieldName = field.getName();
                    String key = ((relatedClass != null) ? // NOI18N
                    "util.validation.related_class_mismatch" : // NOI18N
                    "util.validation.related_class_not_found");
                    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));
                }
            }
        }
    };
}
Also used : Model(com.sun.jdo.api.persistence.model.Model)

Example 9 with Model

use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.

the class ModelValidator method createSerializableClassComponent.

/**
 * Create a validation component which can check that the persistence
 * capable class implement methods readObject and writeObject, if the class
 * implements the intreface java.io.Serializable
 * @param className the class whose methods are checked
 * @return the validation component
 */
protected ValidationComponent createSerializableClassComponent(final String className) {
    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 (// NOI18N
            model.implementsInterface(pcClass, "java.io.Serializable")) {
                // check readObject method
                Object readMethod = model.getMethod(className, "readObject", // NOI18N
                Model.getReadObjectArgs());
                if (!matchesMethod(readMethod, Modifier.PRIVATE, Modifier.SYNCHRONIZED, // NOI18N
                "void")) {
                    throw new ModelValidationException(pcClass, I18NHelper.getMessage(getMessages(), // NOI18N
                    "util.validation.class_readobject", className));
                }
                // check writeObject method
                Object writeMethod = model.getMethod(className, "writeObject", // NOI18N
                Model.getWriteObjectArgs());
                if (!matchesMethod(writeMethod, Modifier.PRIVATE, Modifier.SYNCHRONIZED, // NOI18N
                "void")) {
                    throw new ModelValidationException(pcClass, I18NHelper.getMessage(getMessages(), // NOI18N
                    "util.validation.class_writeobject", className));
                }
            }
        }
    };
}
Also used : Model(com.sun.jdo.api.persistence.model.Model)

Example 10 with Model

use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.

the class ModelValidator method createKeyClassComponent.

/**
 * Create a validation component which can check whether the key class
 * of the persistence capable class is valid. This includes:
 * <ul>
 * <li> The key class must be public.
 * <li> The key class must implement Serializable.
 * <li> If the key class is an inner class, it must be static.
 * <li> The key class must have a public constructor, which might be
 * the default constructor or a no-arg constructor.
 * <li> The field types of all non-static fields in the key class must be
 * of valid types.
 * <li> All serializable non-static fields in the key class must be public.
 * <li> The names of the non-static fields in the key class must include the
 * names of the primary key fields in the JDO class, and the types of the
 * common fields must be identical
 * <li> The key class must redefine equals and hashCode.
 * </ul>
 */
protected ValidationComponent createKeyClassComponent(final String className) {
    return new ValidationComponent() {

        /**
         * The class element of the key class
         */
        private Object keyClass;

        /**
         * The fully qualified name of the key class
         */
        private String keyClassName;

        public void validate() throws ModelValidationException {
            // checks the key class name
            keyClassName = validateKeyClassName(className);
            // initilialize keyClass field
            keyClass = getModel().getClass(keyClassName, getClassLoader());
            validateClass();
            validateConstructor();
            validateFields();
            validateMethods();
        }

        /**
         * 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(), // NOI18N
                "util.validation.key_class_missing", keyClassName, pcClassName));
            }
            // check for public class modifier
            if (!Modifier.isPublic(modifiers)) {
                throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
                "util.validation.key_class_public", keyClassName, pcClassName));
            }
            // if inner class it must be static
            if (isInnerClass && !Modifier.isStatic(modifiers)) {
                throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
                "util.validation.key_class_static", 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(), // NOI18N
                    "util.validation.key_field_type_invalid", keyClassFieldName, keyClassName));
                }
                if (!Modifier.isPublic(keyClassFieldModifiers)) {
                    throw new ModelValidationException(keyClassField, I18NHelper.getMessage(getMessages(), // NOI18N
                    "util.validation.key_field_public", keyClassFieldName, keyClassName));
                }
                if (keyField == null)
                    continue;
                if (!keyClassFieldType.equals(model.getType(keyField))) {
                    throw new ModelValidationException(keyClassField, I18NHelper.getMessage(getMessages(), // NOI18N
                    "util.validation.key_field_type_mismatch", 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(), // NOI18N
                "util.validation.key_field_missing", 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(), // NOI18N
                "util.validation.key_class_constructor", keyClassName, getClassName()));
            }
        }

        /**
         * Helper method validating the key class methods.
         */
        private void validateMethods() throws ModelValidationException {
            Model model = getModel();
            Object equalsMethod = getNonObjectMethod(keyClassName, "equals", // NOI18N
            Model.getEqualsArgs());
            Object hashCodeMethod = getNonObjectMethod(keyClassName, "hashCode", // NOI18N
            Model.NO_ARGS);
            // check equals method
            if (!matchesMethod(equalsMethod, Modifier.PUBLIC, 0, // NOI18N
            "boolean")) {
                throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
                "util.validation.key_class_equals", keyClassName, getClassName()));
            }
            // check hashCode method
            if (!matchesMethod(hashCodeMethod, Modifier.PUBLIC, 0, // NOI18N
            "int")) {
                throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
                "util.validation.key_class_hashcode", 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(), // NOI18N
                "util.validation.key_class_unset", pcClassName));
            }
            keyClassName = keyClassName.trim();
            hasPrefix = keyClassName.startsWith(pcClassName);
            nameSuffix = (hasPrefix ? keyClassName.substring(pcClassName.length()) : keyClassName);
            isOIDNameSuffix = (// NOI18N
            nameSuffix.equalsIgnoreCase(".OID") || // NOI18N
            nameSuffix.equalsIgnoreCase("$OID"));
            if (!hasPrefix || (// NOI18N
            !nameSuffix.equalsIgnoreCase("Key") && !isOIDNameSuffix)) {
                Object pcClass = getModel().getClass(pcClassName);
                throw new ModelValidationException(pcClass, I18NHelper.getMessage(getMessages(), // NOI18N
                "util.validation.key_class_invalid", 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 (// this is an inherited field
            keyClassField == null) {
                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 (// look for an inherited method
            method == null) {
                method = model.getInheritedMethod(className, methodName, argTypeNames);
                if ((method != null) && model.getDeclaringClass(method).equals(// NOI18N
                "java.lang.Object")) {
                    method = null;
                }
            }
            return method;
        }
    };
}
Also used : Model(com.sun.jdo.api.persistence.model.Model)

Aggregations

Model (com.sun.jdo.api.persistence.model.Model)11 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)1