Search in sources :

Example 1 with XMLInputStream

use of org.netbeans.modules.dbschema.migration.archiver.XMLInputStream in project Payara by payara.

the class Model method getMappingClass.

/**
 * Returns the MappingClassElement created for the specified class name.
 * This method looks up the class in the internal cache. If not present
 * it loads the corresponding xml file containing the mapping information.
 * @param className the fully qualified name of the mapping class
 * @param classLoader the class loader used to find mapping information
 * @return the MappingClassElement for className,
 * <code>null</code> if an error occurs or none exists
 * @see MappingClassElementImpl#forName
 */
public MappingClassElement getMappingClass(String className, ClassLoader classLoader) {
    // using the same variable _classes (e.g. updateKeyForClass).
    synchronized (this._classes) {
        MappingClassElement mappingClass = (MappingClassElement) _classes.get(className);
        if (mappingClass == null) {
            // check whether the class is known to be non PC
            if (_nonPCClasses.contains(className))
                return null;
            try {
                InputStream stream = getInputStreamForResource(className, classLoader, getResourceNameWithExtension(className));
                if (stream != null) {
                    // return null without updating either cache
                    if (stream.available() > 0) {
                        XMLInputStream xmlInput = new XMLInputStream(stream, getClass().getClassLoader());
                        mappingClass = (MappingClassElement) xmlInput.readObject();
                        xmlInput.close();
                        // postUnarchive performs version number checking
                        // and possible format conversions
                        mappingClass.postUnarchive();
                        // can't call updateKeyForClass here there are cases
                        // when the mapping class name doesn't match the
                        // classname (such as copy/paste, move etc.)
                        _classes.put(className, mappingClass);
                        // update the modified flags for the mapping and
                        // persistence classes since the xml archiver uses
                        // all the set methods
                        mappingClass.setModified(false);
                        getPersistenceClass(mappingClass).setModified(false);
                    }
                } else {
                    // stream is null, mapping file does not exist =>
                    // class is not PC, so store the class name in the
                    // set of classes known to be non PC
                    _nonPCClasses.add(className);
                }
            } catch (ModelException e) {
                // MBO: print reason to logger
                LogHelperModel.getLogger().log(Logger.WARNING, e.getMessage());
                return null;
            } catch (Exception e) {
                // MBO: print reason to logger
                LogHelperModel.getLogger().log(Logger.WARNING, I18NHelper.getMessage(getMessages(), "file.cannot_read", className, // NOI18N
                e.toString()));
            }
        // will return null
        }
        return mappingClass;
    }
}
Also used : XMLInputStream(org.netbeans.modules.dbschema.migration.archiver.XMLInputStream) XMLInputStream(org.netbeans.modules.dbschema.migration.archiver.XMLInputStream) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement)

Aggregations

MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)1 XMLInputStream (org.netbeans.modules.dbschema.migration.archiver.XMLInputStream)1