Search in sources :

Example 1 with MetaDataAutoStarter

use of org.datanucleus.store.autostart.MetaDataAutoStarter in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method initialiseAutoStart.

/**
 * Method to initialise the auto-start mechanism, loading up the classes
 * from its store into memory so that we start from what is required to be loaded.
 * @param clr The ClassLoaderResolver
 * @throws DatastoreInitialisationException if an error occurs
 */
protected void initialiseAutoStart(ClassLoaderResolver clr) throws DatastoreInitialisationException {
    String autoStartMechanism = config.getStringProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM);
    String mode = config.getStringProperty(PropertyNames.PROPERTY_AUTOSTART_MODE);
    if ("Classes".equalsIgnoreCase(autoStartMechanism)) {
        starter = new ClassesAutoStarter(storeMgr, clr);
    } else if ("XML".equalsIgnoreCase(autoStartMechanism)) {
        try {
            starter = new XMLAutoStarter(storeMgr, clr);
        } catch (MalformedURLException mue) {
            NucleusLogger.PERSISTENCE.warn("Unable to create XML AutoStarter due to ", mue);
            starter = null;
        }
    } else if ("MetaData".equalsIgnoreCase(autoStartMechanism)) {
        starter = new MetaDataAutoStarter(storeMgr, clr);
    } else {
        // Fallback to the plugin mechanism
        String autoStarterClassName = getPluginManager().getAttributeValueForExtension("org.datanucleus.autostart", "name", autoStartMechanism, "class-name");
        if (autoStarterClassName != null) {
            Class[] argsClass = new Class[] { ClassConstants.STORE_MANAGER, ClassConstants.CLASS_LOADER_RESOLVER };
            Object[] args = new Object[] { storeMgr, clr };
            try {
                starter = (AutoStartMechanism) getPluginManager().createExecutableExtension("org.datanucleus.autostart", "name", autoStartMechanism, "class-name", argsClass, args);
            } catch (Exception e) {
                NucleusLogger.PERSISTENCE.error(StringUtils.getStringFromStackTrace(e));
            }
        }
    }
    if (starter == null) {
        return;
    }
    if (mode.equalsIgnoreCase("None")) {
        starter.setMode(org.datanucleus.store.autostart.AutoStartMechanism.Mode.NONE);
    } else if (mode.equalsIgnoreCase("Checked")) {
        starter.setMode(org.datanucleus.store.autostart.AutoStartMechanism.Mode.CHECKED);
    } else if (mode.equalsIgnoreCase("Quiet")) {
        starter.setMode(org.datanucleus.store.autostart.AutoStartMechanism.Mode.QUIET);
    } else if (mode.equalsIgnoreCase("Ignored")) {
        starter.setMode(org.datanucleus.store.autostart.AutoStartMechanism.Mode.IGNORED);
    }
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("034005", autoStartMechanism));
    }
    boolean illegalState = false;
    try {
        if (!starter.isOpen()) {
            starter.open();
        }
        Collection existingData = starter.getAllClassData();
        if (existingData != null && !existingData.isEmpty()) {
            List classesNeedingAdding = new ArrayList();
            Iterator existingDataIter = existingData.iterator();
            while (existingDataIter.hasNext()) {
                StoreData data = (StoreData) existingDataIter.next();
                if (data.isFCO()) {
                    // Catch classes that don't exist (e.g in use by a different app)
                    Class classFound = null;
                    try {
                        classFound = clr.classForName(data.getName());
                    } catch (ClassNotResolvedException cnre) {
                        if (data.getInterfaceName() != null) {
                            try {
                                getImplementationCreator().newInstance(clr.classForName(data.getInterfaceName()), clr);
                                classFound = clr.classForName(data.getName());
                            } catch (ClassNotResolvedException cnre2) {
                            // Do nothing
                            }
                        }
                    // Thrown if class not found
                    }
                    if (classFound != null) {
                        NucleusLogger.PERSISTENCE.info(Localiser.msg("032003", data.getName()));
                        classesNeedingAdding.add(data.getName());
                        if (data.getMetaData() == null) {
                            // StoreData doesnt have its metadata set yet so load it
                            // This ensures that the MetaDataManager always knows about these classes
                            AbstractClassMetaData acmd = getMetaDataManager().getMetaDataForClass(classFound, clr);
                            if (acmd != null) {
                                data.setMetaData(acmd);
                            } else {
                                String msg = Localiser.msg("034004", data.getName());
                                if (starter.getMode() == AutoStartMechanism.Mode.CHECKED) {
                                    NucleusLogger.PERSISTENCE.error(msg);
                                    throw new DatastoreInitialisationException(msg);
                                } else if (starter.getMode() == AutoStartMechanism.Mode.IGNORED) {
                                    NucleusLogger.PERSISTENCE.warn(msg);
                                } else if (starter.getMode() == AutoStartMechanism.Mode.QUIET) {
                                    NucleusLogger.PERSISTENCE.warn(msg);
                                    NucleusLogger.PERSISTENCE.warn(Localiser.msg("034001", data.getName()));
                                    starter.deleteClass(data.getName());
                                }
                            }
                        }
                    } else {
                        String msg = Localiser.msg("034000", data.getName());
                        if (starter.getMode() == AutoStartMechanism.Mode.CHECKED) {
                            NucleusLogger.PERSISTENCE.error(msg);
                            throw new DatastoreInitialisationException(msg);
                        } else if (starter.getMode() == AutoStartMechanism.Mode.IGNORED) {
                            NucleusLogger.PERSISTENCE.warn(msg);
                        } else if (starter.getMode() == AutoStartMechanism.Mode.QUIET) {
                            NucleusLogger.PERSISTENCE.warn(msg);
                            NucleusLogger.PERSISTENCE.warn(Localiser.msg("034001", data.getName()));
                            starter.deleteClass(data.getName());
                        }
                    }
                }
            }
            String[] classesToLoad = new String[classesNeedingAdding.size()];
            Iterator classesNeedingAddingIter = classesNeedingAdding.iterator();
            int n = 0;
            while (classesNeedingAddingIter.hasNext()) {
                classesToLoad[n++] = (String) classesNeedingAddingIter.next();
            }
            // Load the classes into the StoreManager
            try {
                storeMgr.manageClasses(clr, classesToLoad);
            } catch (Exception e) {
                // Exception while adding so some of the (referenced) classes dont exist
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("034002", e));
                // if an exception happens while loading AutoStart data, them we disable it, since it was unable to load the data from AutoStart. The memory state of AutoStart does
                // not represent the database, and if we don't disable it, we could think that the autostart store is empty, and we would try to insert new entries in
                // the autostart store that are already in there
                illegalState = true;
            // TODO Go back and add classes one-by-one to eliminate the class(es) with the problem
            }
        }
    } finally {
        if (starter.isOpen()) {
            starter.close();
        }
        if (illegalState) {
            NucleusLogger.PERSISTENCE.warn(Localiser.msg("034003"));
            starter = null;
        }
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("034006", autoStartMechanism));
        }
    }
}
Also used : ClassesAutoStarter(org.datanucleus.store.autostart.ClassesAutoStarter) MalformedURLException(java.net.MalformedURLException) XMLAutoStarter(org.datanucleus.store.autostart.XMLAutoStarter) MetaDataAutoStarter(org.datanucleus.store.autostart.MetaDataAutoStarter) ArrayList(java.util.ArrayList) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NamingException(javax.naming.NamingException) NucleusException(org.datanucleus.exceptions.NucleusException) DatastoreInitialisationException(org.datanucleus.exceptions.DatastoreInitialisationException) NucleusTransactionException(org.datanucleus.transaction.NucleusTransactionException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JTASyncRegistryUnavailableException(org.datanucleus.transaction.jta.JTASyncRegistryUnavailableException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) StoreData(org.datanucleus.store.StoreData) DatastoreInitialisationException(org.datanucleus.exceptions.DatastoreInitialisationException) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 NamingException (javax.naming.NamingException)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 DatastoreInitialisationException (org.datanucleus.exceptions.DatastoreInitialisationException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 StoreData (org.datanucleus.store.StoreData)1 ClassesAutoStarter (org.datanucleus.store.autostart.ClassesAutoStarter)1 MetaDataAutoStarter (org.datanucleus.store.autostart.MetaDataAutoStarter)1 XMLAutoStarter (org.datanucleus.store.autostart.XMLAutoStarter)1 NucleusTransactionException (org.datanucleus.transaction.NucleusTransactionException)1 JTASyncRegistryUnavailableException (org.datanucleus.transaction.jta.JTASyncRegistryUnavailableException)1