Package org.jpox.store

Source Code of org.jpox.store.FederationManager

/**********************************************************************
Copyright (c) 2008 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.jpox.store;

import java.io.PrintStream;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import org.jpox.ClassLoaderResolver;
import org.jpox.ManagedConnection;
import org.jpox.OMFContext;
import org.jpox.ObjectManager;
import org.jpox.StateManager;
import org.jpox.api.ApiAdapter;
import org.jpox.exceptions.JPOXException;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.management.runtime.StoreManagerRuntime;
import org.jpox.metadata.AbstractClassMetaData;
import org.jpox.metadata.AbstractMemberMetaData;
import org.jpox.metadata.IdentityStrategy;
import org.jpox.metadata.SequenceMetaData;
import org.jpox.metadata.VersionMetaData;
import org.jpox.plugin.ConfigurationElement;
import org.jpox.plugin.Extension;
import org.jpox.store.poid.PoidManager;
import org.jpox.store.scostore.Store;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;

/**
* Federation Manager orchestrates the persistence in multiple datastores
*/
public class FederationManager implements StoreManager
{
    /** Localisation of messages. */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManager.class.getClassLoader());

    StoreManager storeManager;

    final OMFContext omfContext;

    final ClassLoaderResolver clr;
   
    public FederationManager(ClassLoaderResolver clr, OMFContext omfContext)
    {
        this.omfContext = omfContext;
        this.clr = clr;
        initialiseStoreManager(clr);
    }
   
    /**
     * Gets the context for this ObjectManagerFactory
     * @return Returns the context.
     */
    public OMFContext getOMFContext()
    {
        return omfContext;
    }
   
    /**
     * Method to initialise the StoreManager used by this factory.
     * @param clr ClassLoaderResolver to use for class loading issues
     */
    protected void initialiseStoreManager(ClassLoaderResolver clr)
    {
        StoreManager srm = null;
        Extension[] exts = getOMFContext().getPluginManager().getExtensionPoint("org.jpox.store_manager").getExtensions();

        // Find the StoreManager using the persistence property if specified
        String storeManagerType = getOMFContext().getPersistenceConfiguration().getStringProperty(
            "org.jpox.storeManagerType");
        if (storeManagerType != null)
        {
            for (int e=0; srm == null && e<exts.length; e++)
            {
                ConfigurationElement[] confElm = exts[e].getConfigurationElements();
                for (int c=0; srm == null && c<confElm.length; c++)
                {
                    String storeMgrClassName = confElm[c].getAttribute("class-name");
                    String key = confElm[c].getAttribute("key");
                    if (key.equalsIgnoreCase(storeManagerType))
                    {
                        Class[] ctrArgTypes = new Class[] {ClassLoaderResolver.class, OMFContext.class};
                        Object[] ctrArgs = new Object[] {clr, getOMFContext()};
                        try
                        {
                            srm = (StoreManager) getOMFContext().getPluginManager().createExecutableExtension(
                                "org.jpox.store_manager", "key", storeManagerType, "class-name", ctrArgTypes,
                                ctrArgs);
                        }
                        catch (InvocationTargetException ex)
                        {
                            Throwable t = ex.getTargetException();
                            if (t instanceof RuntimeException)
                            {
                                throw (RuntimeException) t;
                            }
                            else if (t instanceof Error)
                            {
                                throw (Error) t;
                            }
                            else
                            {
                                throw new JPOXException(t.getMessage(), t).setFatal();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new JPOXException(ex.getMessage(), ex).setFatal();
                        }
                        JPOXLogger.JDO.info(">> Found StoreManager " + storeMgrClassName);
                    }
                }
            }
            if (srm == null)
            {
                // No StoreManager of the specified type exists, so check plugins in CLASSPATH
                throw new JPOXUserException(LOCALISER.msg("008004", storeManagerType)).setFatal();
            }
        }

        if (srm == null)
        {
            // Try using the URL of the data source
            String url = getOMFContext().getPersistenceConfiguration().getStringProperty("org.jpox.ConnectionURL");
            if (url != null)
            {
                int idx = url.indexOf(':');
                if (idx > -1)
                {
                    url = url.substring(0, idx);
                }
            }

            for (int e=0; srm == null && e<exts.length; e++)
            {
                ConfigurationElement[] confElm = exts[e].getConfigurationElements();
                for (int c=0; srm == null && c<confElm.length; c++)
                {
                    String urlKey = confElm[c].getAttribute("url-key");
                    if (url == null || urlKey.equalsIgnoreCase(url))
                    {
                        // Either no URL, or url defined so take this StoreManager
                        Class[] ctrArgTypes = new Class[] {ClassLoaderResolver.class, OMFContext.class};
                        Object[] ctrArgs = new Object[] {clr, getOMFContext()};
                        try
                        {
                            srm = (StoreManager) getOMFContext().getPluginManager().createExecutableExtension(
                                "org.jpox.store_manager", "url-key", url, "class-name", ctrArgTypes, ctrArgs);
                        }
                        catch (InvocationTargetException ex)
                        {
                            Throwable t = ex.getTargetException();
                            if (t instanceof RuntimeException)
                            {
                                throw (RuntimeException) t;
                            }
                            else if (t instanceof Error)
                            {
                                throw (Error) t;
                            }
                            else
                            {
                                throw new JPOXException(t.getMessage(), t).setFatal();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new JPOXException(ex.getMessage(), ex).setFatal();
                        }
                    }
                }
            }

            if (srm == null)
            {
                throw new JPOXUserException(LOCALISER.msg("008005", url)).setFatal();
            }
        }
    }

    public StoreManager getStoreManager()
    {
        return storeManager;
    }
   
    public void close()
    {
        storeManager.close();
    }

    public void addClass(String className, ClassLoaderResolver clr)
    {
        storeManager.addClass(className, clr);
    }

    public void addClasses(String[] classes, ClassLoaderResolver clr, Writer writer, boolean completeDdl)
    {
        storeManager.addClasses(classes, clr, writer, completeDdl);
    }

    public void addClasses(String[] classNames, ClassLoaderResolver clr)
    {
        storeManager.addClasses(classNames, clr);       
    }

    public ApiAdapter getApiAdapter()
    {
        return storeManager.getApiAdapter();
    }

    public AutoStartMechanism getAutoStartMechanism()
    {
        return storeManager.getAutoStartMechanism();
    }

    public Store getBackingStoreForField(ClassLoaderResolver clr, AbstractMemberMetaData fmd, Class type)
    {
        return storeManager.getBackingStoreForField(clr, fmd, type);
    }

    public String getClassNameForObjectID(Object id, ClassLoaderResolver clr, ObjectManager om)
    {
        return storeManager.getClassNameForObjectID(id, clr, om);
    }

    public Date getDatastoreDate()
    {
        return storeManager.getDatastoreDate();
    }

    public Extent getExtent(ObjectManager om, Class c, boolean subclasses)
    {
        return storeManager.getExtent(om, c, subclasses);
    }

    public JPOXConnection getJPOXConnection(ObjectManager om)
    {
        return storeManager.getJPOXConnection(om);
    }

    public JPOXSequence getJPOXSequence(ObjectManager om, SequenceMetaData seqmd)
    {
        return storeManager.getJPOXSequence(om, seqmd);
    }

    public StoreSchemaHandler getSchemaHandler()
    {
        return storeManager.getSchemaHandler();
    }

    public StorePersistenceHandler getPersistenceHandler()
    {
        return storeManager.getPersistenceHandler();
    }

    public PoidManager getPoidManager()
    {
        return storeManager.getPoidManager();
    }

    public StoreManagerRuntime getRuntimeManager()
    {
        return storeManager.getRuntimeManager();
    }

    public String getStoreManagerKey()
    {
        return storeManager.getStoreManagerKey();
    }

    public Object getStrategyValue(ObjectManager om, AbstractClassMetaData cmd, int absoluteFieldNumber)
    {
        return storeManager.getStrategyValue(om, cmd, absoluteFieldNumber);
    }

    public HashSet getSubClassesForClass(String className, boolean includeDescendents, ClassLoaderResolver clr)
    {
        return storeManager.getSubClassesForClass(className, includeDescendents, clr);
    }

    public boolean isStrategyDatastoreAttributed(IdentityStrategy identityStrategy, boolean datastoreIdentityField)
    {
        return storeManager.isStrategyDatastoreAttributed(identityStrategy, datastoreIdentityField);
    }

    public String manageClassForIdentity(Object id, ClassLoaderResolver clr)
    {
        return storeManager.manageClassForIdentity(id, clr);
    }

    public boolean managesClass(String className)
    {
        return storeManager.managesClass(className);
    }

    public void notifyObjectIsOutdated(StateManager sm)
    {
        storeManager.notifyObjectIsOutdated(sm);
    }

    public void outputDatastoreInformation(PrintStream ps) throws Exception
    {
        storeManager.outputDatastoreInformation(ps);       
    }

    public void outputSchemaInformation(PrintStream ps) throws Exception
    {
        storeManager.outputSchemaInformation(ps);       
    }

    public void performVersionCheck(StateManager sm, Object versionDatastore, VersionMetaData versionMetaData)
    {
        storeManager.performVersionCheck(sm, versionDatastore, versionMetaData);       
    }

    public void removeAllClasses(ClassLoaderResolver clr)
    {
        storeManager.removeAllClasses(clr);
    }

    public boolean supportsQueryLanguage(String language)
    {
        return storeManager.supportsQueryLanguage(language);
    }

    public boolean supportsValueStrategy(String language)
    {
        return storeManager.supportsValueStrategy(language);
    }

    public Collection getSupportedOptions()
    {
        return storeManager.getSupportedOptions();
    }

    public ManagedConnection getConnection(ObjectManager om)
    {
        return storeManager.getConnection(om);
    }
}
TOP

Related Classes of org.jpox.store.FederationManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.