Search in sources :

Example 36 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-core by datanucleus.

the class SchemaTool method getNucleusContextForMode.

/**
 * Method to create a NucleusContext for the specified mode of SchemaTool
 * @param mode Mode of operation of SchemaTool
 * @param api Persistence API
 * @param userProps Map containing user provided properties (usually input via a file)
 * @param persistenceUnitName Name of the persistence-unit (if any)
 * @param ddlFile Name of a file to output DDL to
 * @param verbose Verbose mode
 * @param ignoreMetaDataForMissingClasses Whether to ignore metadata for missing classes
 * @return The NucleusContext to use
 * @throws NucleusException Thrown if an error occurs in creating the required NucleusContext
 */
public static StoreNucleusContext getNucleusContextForMode(Mode mode, String api, Map userProps, String persistenceUnitName, String ddlFile, boolean verbose, boolean ignoreMetaDataForMissingClasses) {
    // Extract any properties that affect NucleusContext startup
    Map startupProps = null;
    if (userProps != null) {
        // Possible properties to check for
        for (String startupPropName : AbstractNucleusContext.STARTUP_PROPERTIES) {
            if (userProps.containsKey(startupPropName)) {
                if (startupProps == null) {
                    startupProps = new HashMap();
                }
                startupProps.put(startupPropName, userProps.get(startupPropName));
            }
        }
    }
    // Initialise the context for this API
    PersistenceNucleusContext nucleusCtx = new PersistenceNucleusContextImpl(api, startupProps);
    Configuration propConfig = nucleusCtx.getConfiguration();
    // Generate list of properties for SchemaTool usage
    Map props = new HashMap();
    // Get properties from PersistenceUnit first...
    PersistenceUnitMetaData pumd = null;
    if (persistenceUnitName != null) {
        props.put(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME.toLowerCase(), persistenceUnitName);
        // Extract the persistence-unit metadata
        String filename = nucleusCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_XML_FILENAME);
        boolean validateXML = nucleusCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_VALIDATE);
        boolean supportXMLNamespaces = nucleusCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_NAMESPACE_AWARE);
        ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
        pumd = MetaDataUtils.getMetaDataForPersistenceUnit(nucleusCtx.getPluginManager(), filename, persistenceUnitName, validateXML, supportXMLNamespaces, clr);
        if (pumd != null) {
            // Add the properties for the unit
            if (pumd.getProperties() != null) {
                props.putAll(pumd.getProperties());
            }
        } else {
            throw new NucleusUserException("SchemaTool has been specified to use persistence-unit with name " + persistenceUnitName + " but none was found with that name");
        }
        if (api.equalsIgnoreCase("JPA")) {
            // Don't use JARs when in JavaSE for JPA
            pumd.clearJarFiles();
        }
    }
    // Add/override with user properties
    if (userProps != null) {
        // Properties specified by the user in a file
        for (Object key : userProps.keySet()) {
            String propName = (String) key;
            props.put(propName.toLowerCase(Locale.ENGLISH), userProps.get(propName));
        }
    }
    // Finally add/override with system properties (only support particular ones, and in correct case)
    String[] propNames = { PropertyNames.PROPERTY_CONNECTION_URL, PropertyNames.PROPERTY_CONNECTION_DRIVER_NAME, PropertyNames.PROPERTY_CONNECTION_USER_NAME, PropertyNames.PROPERTY_CONNECTION_PASSWORD, PropertyNames.PROPERTY_MAPPING, "javax.jdo.option.ConnectionURL", "javax.jdo.option.ConnectionDriverName", "javax.jdo.option.ConnectionUserName", "javax.jdo.option.ConnectionPassword", "javax.jdo.option.Mapping", "javax.persistence.jdbc.url", "javax.persistence.jdbc.driver", "javax.persistence.jdbc.user", "javax.persistence.jdbc.password" };
    for (int i = 0; i < propNames.length; i++) {
        if (System.getProperty(propNames[i]) != null) {
            props.put(propNames[i].toLowerCase(Locale.ENGLISH), System.getProperty(propNames[i]));
        }
    }
    // Interferes with usage
    props.put(PropertyNames.PROPERTY_AUTOSTART_MECHANISM.toLowerCase(), "None");
    // Tag on the mandatory props that we must have for each mode
    if (mode == Mode.CREATE) {
        if (ddlFile != null) {
            // the tables must not be created in the DB, so do not validate (DDL is being output to a file)
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "false");
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "false");
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "false");
        }
        // use tables/columns/constraints settings
        props.remove(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase());
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "true");
        }
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "true");
        }
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "true");
        }
        props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
        props.put("datanucleus.rdbms.checkexisttablesorviews", "true");
    } else if (mode == Mode.DELETE) {
        props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
    } else if (mode == Mode.DELETE_CREATE) {
        if (ddlFile != null) {
            // the tables must not be created in the DB, so do not validate (DDL is being output to a file)
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "false");
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "false");
            props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "false");
        }
        // use tables/columns/constraints settings
        props.remove(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase());
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "true");
        }
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "true");
        }
        if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase())) {
            props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "true");
        }
        props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
        props.put("datanucleus.rdbms.checkexisttablesorviews", "true");
    } else if (mode == Mode.VALIDATE) {
        props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase(), "false");
        props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "false");
        props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "false");
        props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "false");
        props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "true");
        props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "true");
        props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "true");
    }
    if (ignoreMetaDataForMissingClasses) {
        props.put(PropertyNames.PROPERTY_METADATA_IGNORE_METADATA_FOR_MISSING_CLASSES, "true");
    }
    // Apply remaining persistence properties
    propConfig.setPersistenceProperties(props);
    if (pumd != null) {
        // Initialise the MetaDataManager with all files/classes for this persistence-unit
        // This is done now that all persistence properties are set (including the persistence-unit props)
        nucleusCtx.getMetaDataManager().loadPersistenceUnit(pumd, null);
    }
    // Initialise the NucleusContext for use
    nucleusCtx.initialise();
    if (verbose) {
        String msg = Localiser.msg("014020");
        LOGGER.info(msg);
        System.out.println(msg);
        // TODO Some persistence properties will be stored against the StoreManager
        Map<String, Object> pmfProps = propConfig.getPersistenceProperties();
        Set<String> keys = pmfProps.keySet();
        List<String> keyNames = new ArrayList<String>(keys);
        Collections.sort(keyNames);
        Iterator keyNamesIter = keyNames.iterator();
        while (keyNamesIter.hasNext()) {
            String key = (String) keyNamesIter.next();
            Object value = pmfProps.get(key);
            boolean display = true;
            if (!key.startsWith("datanucleus")) {
                display = false;
            } else if (key.equals(PropertyNames.PROPERTY_CONNECTION_PASSWORD.toLowerCase())) {
                // Don't show passwords
                display = false;
            } else if (value == null) {
                display = false;
            } else if (value instanceof String && StringUtils.isWhitespace((String) value)) {
                display = false;
            }
            if (display) {
                // Print the property to sysout
                msg = Localiser.msg("014022", key, value);
                LOGGER.info(msg);
                System.out.println(msg);
            }
        }
        System.out.println();
    }
    return nucleusCtx;
}
Also used : Configuration(org.datanucleus.Configuration) HashMap(java.util.HashMap) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandlerTest 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/package1.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 38 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.

the class PersistenceFileMetaDataHandlerTest 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/persistence2.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 39 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.

the class MetaDataManagerTest method testLocationsForClass.

/**
 * Test of the valid locations for a specified class.
 */
public void testLocationsForClass() {
    Map startupProps = new HashMap<>();
    startupProps.put(JDOPropertyNames.PROPERTY_METADATA_XML_JDO_1_0, "true");
    JDOMetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", startupProps));
    // Try typical JDO class name
    String className = "org.jpox.samples.store.Product";
    List locations = mgr.getValidMetaDataLocationsForClass("jdo", null, className);
    assertTrue("Locations returned from MetaData Manager was null!", locations != null);
    List validLocations = new ArrayList();
    validLocations.add("/META-INF/package.jdo");
    validLocations.add("/WEB-INF/package.jdo");
    validLocations.add("/package.jdo");
    validLocations.add("/org.jdo");
    validLocations.add("/org/package.jdo");
    validLocations.add("/org/jpox.jdo");
    validLocations.add("/org/jpox/package.jdo");
    validLocations.add("/org/jpox/samples.jdo");
    validLocations.add("/org/jpox/samples/package.jdo");
    validLocations.add("/org/jpox/samples/store.jdo");
    validLocations.add("/org/jpox/samples/store/package.jdo");
    validLocations.add("/org/jpox/samples/store/Product.jdo");
    checkLocations(className, locations, validLocations);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) List(java.util.List) ArrayList(java.util.ArrayList) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 40 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method readResolve.

/**
 * Control deserialisation of the PMF where we have a singleton (in pmfByName).
 * @return The PMF
 * @throws InvalidObjectException if an error occurs
 */
private Object readResolve() throws InvalidObjectException {
    JDOPersistenceManagerFactory pmf = null;
    if (pmfByName != null) {
        String name = (String) deserialisationProps.get(PropertyNames.PROPERTY_PMF_NAME);
        if (name == null) {
            name = (String) deserialisationProps.get(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME);
        }
        // Return singleton if present to save reinitialisation
        pmf = pmfByName.get(name);
        if (pmf != null) {
            return pmf;
        }
    }
    // Use deserialised object, so need to initialise it
    configurable = true;
    if (pmCache == null) {
        pmCache = Collections.newSetFromMap(new ConcurrentHashMap());
    }
    nucleusContext = new PersistenceNucleusContextImpl("JDO", deserialisationProps);
    PersistenceUnitMetaData pumd = null;
    if (getPersistenceUnitName() != null) {
        // Load the metadata for the persistence-unit
        String filename = nucleusContext.getConfiguration().getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_XML_FILENAME);
        boolean validateXML = nucleusContext.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_VALIDATE);
        boolean supportXMLNamespaces = nucleusContext.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_NAMESPACE_AWARE);
        ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
        pumd = MetaDataUtils.getMetaDataForPersistenceUnit(nucleusContext.getPluginManager(), filename, getPersistenceUnitName(), validateXML, supportXMLNamespaces, clr);
    }
    initialiseMetaData(pumd);
    processLifecycleListenersFromProperties(deserialisationProps);
    freezeConfiguration();
    deserialisationProps = null;
    return this;
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData)

Aggregations

PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)86 MetaDataManager (org.datanucleus.metadata.MetaDataManager)69 NucleusContext (org.datanucleus.NucleusContext)59 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)56 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 ClassMetaData (org.datanucleus.metadata.ClassMetaData)51 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)40 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)35 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)32 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)29 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)18 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)7 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)7 PackageMetaData (org.datanucleus.metadata.PackageMetaData)6 FetchPlan (org.datanucleus.FetchPlan)5 FetchPlanForClass (org.datanucleus.FetchPlanForClass)5 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)5 ArrayList (java.util.ArrayList)4