Search in sources :

Example 6 with MetaDataParser

use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.

the class MetaDataParserTest method testParseMetaDataURLnullhandler.

public void testParseMetaDataURLnullhandler() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    try {
        parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), null);
        fail("expected JPOXException");
    } catch (NucleusException ex) {
    // expected
    }
}
Also used : NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 7 with MetaDataParser

use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandlerTest method testParseNamespace.

public void testParseNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), "jdo");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 8 with MetaDataParser

use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.

the class PersistenceFileMetaDataHandlerTest method testParseDefaultNamespace.

public void testParseDefaultNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence1.xml"), "persistence");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 9 with MetaDataParser

use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-core by datanucleus.

the class MetaDataUtils method parsePersistenceFiles.

/**
 * Method to parse the available "persistence.xml" files returning the metadata for all found.
 * Searches for all files "META-INF/persistence.xml" in the CLASSPATH of the current thread.
 * @param pluginMgr PluginManager
 * @param persistenceFilename Name of persistence file (if null will use "persistence.xml")
 * @param validate Whether to validate the persistence file
 * @param namespaceAware Whether to support namespaces
 * @param clr ClassLoader resolver
 * @return The metadata for all "persistence.xml" files
 */
public static PersistenceFileMetaData[] parsePersistenceFiles(PluginManager pluginMgr, String persistenceFilename, boolean validate, boolean namespaceAware, ClassLoaderResolver clr) {
    MetaDataParser parser = new MetaDataParser(null, pluginMgr, validate, namespaceAware);
    if (persistenceFilename != null) {
        // User has specified filename for persistence.xml
        try {
            URL fileURL = new URL(persistenceFilename);
            MetaData permd = parser.parseMetaDataURL(fileURL, "persistence");
            return new PersistenceFileMetaData[] { (PersistenceFileMetaData) permd };
        } catch (MalformedURLException mue) {
            // User provided file is not found
            NucleusLogger.METADATA.error("Error reading user-specified persistence.xml file " + persistenceFilename, mue);
            return null;
        }
    }
    Set<MetaData> metadata = new LinkedHashSet<>();
    try {
        // Find all "META-INF/persistence.xml" files in the CLASSPATH of the current thread
        Enumeration files = clr.getResources("META-INF/persistence.xml", Thread.currentThread().getContextClassLoader());
        if (!files.hasMoreElements()) {
            return null;
        }
        for (; files.hasMoreElements(); ) {
            // Parse the "persistence.xml"
            URL fileURL = (URL) files.nextElement();
            MetaData permd = parser.parseMetaDataURL(fileURL, "persistence");
            metadata.add(permd);
        }
    } catch (IOException ioe) {
        // Do nothing
        NucleusLogger.METADATA.warn(StringUtils.getStringFromStackTrace(ioe));
    }
    return metadata.toArray(new PersistenceFileMetaData[metadata.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MalformedURLException(java.net.MalformedURLException) Enumeration(java.util.Enumeration) IOException(java.io.IOException) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) URL(java.net.URL)

Aggregations

MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)9 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)8 NucleusContext (org.datanucleus.NucleusContext)7 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)7 MetaData (org.datanucleus.metadata.MetaData)4 IOException (java.io.IOException)2 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 MetaDataManager (org.datanucleus.metadata.MetaDataManager)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 LinkedHashSet (java.util.LinkedHashSet)1 JDOMetaDataHelper (org.datanucleus.api.jdo.metadata.JDOMetaDataHelper)1