All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.autostart.XMLAutoStarter Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.7
Show newest version
/******************************************************************
Copyright (c) 2004 Andy Jefferson 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:
2004 Erik Bengtson - changed to use org.w3c.dom
2004 Andy Jefferson - added table-owner. Changed table to be optional
   ...
*****************************************************************/
package org.datanucleus.store.autostart;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.PropertyNames;
import org.datanucleus.store.StoreData;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.exceptions.DatastoreInitialisationException;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * An auto-starter mechanism storing its definition in an XML file.
 * Is independent of the datastore since it is stored as a file and not in the actual datastore.
 *
 * TODO Add a DataNucleusAutoStart DTD to validate the file automatically.
 * TODO If we have one per PMF, need to guarantee unique naming of file.
 */
public class XMLAutoStarter extends AbstractAutoStartMechanism
{
    protected final URL fileUrl;
    protected Document doc;
    protected Element rootElement;
    String version = null;

    Set autoStartClasses = new HashSet();

    /**
     * Constructor, taking the XML file URL.
     * @param storeMgr The StoreManager managing the store that we are auto-starting.
     * @param clr The ClassLoaderResolver
     * @throws MalformedURLException if an error occurs processing the URL
     */
    public XMLAutoStarter(StoreManager storeMgr, ClassLoaderResolver clr) throws MalformedURLException
    {
        super();

        this.fileUrl = new URL("file:" + 
            storeMgr.getStringProperty(PropertyNames.PROPERTY_AUTOSTART_XMLFILE));

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db;

        try
        {
            db = factory.newDocumentBuilder();
            try
            {
                db.setEntityResolver(new XMLAutoStarterEntityResolver());
                rootElement = db.parse(new InputSource(new InputStreamReader(fileUrl.openStream()))).getDocumentElement();
                doc = rootElement.getOwnerDocument();
            }
            catch (Exception e)
            {
                NucleusLogger.PERSISTENCE.info(Localiser.msg("034201", fileUrl.getFile()));

                // File doesn't exist, so create it
                doc = db.newDocument();
                rootElement = doc.createElement("datanucleus_autostart");
                doc.appendChild(rootElement);

                writeToFile();
            }
        }
        catch (ParserConfigurationException e1)
        {
            NucleusLogger.PERSISTENCE.error(Localiser.msg("034202", fileUrl.getFile(), e1.getMessage()));
        }

        version = storeMgr.getNucleusContext().getPluginManager().getVersionForBundle("org.datanucleus");
    }

    /**
     * Accessor for all auto start data for this starter.
     * @return The class auto start data. Collection of StoreData elements
     * @throws DatastoreInitialisationException If an error occurs in datastore init
     */
    public Collection getAllClassData()
    throws DatastoreInitialisationException
    {
        Collection classes = new HashSet();

        NodeList classElements = rootElement.getElementsByTagName("class");
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy