/**********************************************************************
Copyright (c) 2003 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 Andy Jefferson - added type, version
2004 Andy Jefferson - changed to use StoreData. Added owner
...
**********************************************************************/
package org.jpox.store.rdbms;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.jpox.ClassLoaderResolver;
import org.jpox.exceptions.JPOXException;
import org.jpox.jdo.JDOPersistenceManagerFactory;
import org.jpox.store.AbstractAutoStartMechanism;
import org.jpox.store.StoreData;
import org.jpox.store.StoreManager;
import org.jpox.store.exceptions.DatastoreInitialisationException;
import org.jpox.store.mapped.MappedStoreData;
import org.jpox.util.Localiser;
/**
* Implementation of an Auto-Start Mechanism for JPOX. This implementation
* stores the classes supported in a table in the datastore. It is initialised
* and read at startup of JPOX, and is continually updated during the lifetime
* of the calling application.
*
* @version $Revision: 1.28 $
*/
public class SchemaAutoStarter2 extends AbstractAutoStartMechanism
{
private static final Localiser LOCALISER_RDBMS = Localiser.getInstance("org.jpox.store.rdbms.Localisation",
RDBMSManager.class.getClassLoader());
protected SchemaTable2 schemaTable = null;
protected RDBMSManager storeMgr = null;
PersistenceManagerFactory pmf;
PersistenceManager pm;
/**
* Constructor.
* @param store_mgr The RDBMSManager managing the store that we are
* auto-starting.
* @param clr The ClassLoaderResolver
**/
public SchemaAutoStarter2(StoreManager store_mgr, ClassLoaderResolver clr)
{
super();
storeMgr = (RDBMSManager)store_mgr;
storeMgr.addClass(org.jpox.store.rdbms.SchemaTable2.class.getName(), clr);
Map props = new HashMap();
props.putAll(storeMgr.getOMFContext().getPersistenceConfiguration().getOptions());
props.put("org.jpox.autoStartMechanism", "None");
pmf = JDOPersistenceManagerFactory.getPersistenceManagerFactory(props);
}
/**
* Accessor for the data for the classes supported.
* @return Collection of classes supported (StoreData). Collection of StoreData elements
* @throws DatastoreInitialisationException
**/
public Collection getAllClassData()
throws DatastoreInitialisationException
{
HashSet schema_data = new HashSet();
Iterator it = pm.getExtent(org.jpox.store.rdbms.SchemaTable2.class).iterator();
while (it.hasNext())
{
org.jpox.store.rdbms.SchemaTable2 st = (org.jpox.store.rdbms.SchemaTable2) it.next();
StoreData data = new MappedStoreData(st.getClassName(), st.getTableName(),
st.getOwner().equals("1") ? true : false,
st.getType().equalsIgnoreCase("FCO") ? MappedStoreData.FCO_TYPE : MappedStoreData.SCO_TYPE,
st.getInterfaceName());
schema_data.add(data);
}
return schema_data;
}
/**
* Assert that the mechanism is open from writings
*/
private void assertIsOpen()
{
if (pm==null)
{
throw new JPOXException(LOCALISER_RDBMS.msg("049008")).setFatal();
}
}
/**
* Assert that the mechanism is closed from writings
*/
private void assertIsClosed()
{
if (pm!=null)
{
throw new JPOXException(LOCALISER_RDBMS.msg("049009")).setFatal();
}
}
/**
* Starts a transaction for writing (add/delete) classes to the auto start
* mechanism
*/
public void open()
{
assertIsClosed();
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
}
/**
* Closes a transaction for writing (add/delete) classes to the auto start
* mechanism
*/
public void close()
{
assertIsOpen();
pm.currentTransaction().commit();
pm.close();
pm = null;
}
/**
* Whether it's open for writing (add/delete) classes to the auto start
* mechanism
* @return whether this is open for writing
*/
public boolean isOpen()
{
return pm != null;
}
/**
* Method to add a class to the supported list.
* @param data Data for the class to add.
**/
public void addClass(StoreData data)
{
assertIsOpen();
if( hasClass(data.getName()))
{
return;
}
MappedStoreData tableData = (MappedStoreData)data; // We only support TableStoreData
org.jpox.store.rdbms.SchemaTable2 st = new org.jpox.store.rdbms.SchemaTable2();
st.setClassName(tableData.getName());
st.setInterfaceName(tableData.getInterfaceName());
st.setOwner(tableData.isTableOwner()? "1" : "0");
st.setTableName(tableData.getTableName());
st.setType(data.isFCO() ? "FCO" : "SCO");
// TODO Sort out version
st.setVersion("JPOX");
pm.makePersistent(st);
}
/**
* Method to verify the a class is already stored in the table.
* @param class_name The class
* @return if the SchemaTable already has the class
**/
public boolean hasClass(String class_name)
{
assertIsOpen();
return ((Collection)pm.newQuery(org.jpox.store.rdbms.SchemaTable2.class,"className == :name").execute(class_name)).size()>0;
}
/**
* Method to drop support for a class.
* @param class_name The class
**/
public void deleteClass(String class_name)
{
assertIsOpen();
pm.newQuery(org.jpox.store.rdbms.SchemaTable2.class,"className == :name").deletePersistentAll(new Object[] {class_name});
}
/**
* Method to drop support for all current classes.
**/
public void deleteAllClasses()
{
assertIsOpen();
pm.newQuery(org.jpox.store.rdbms.SchemaTable2.class).deletePersistentAll();
}
/**
* Utility to output the storage description for this mechanism.
* @return The storage description
**/
public String getStorageDescription()
{
return LOCALISER_RDBMS.msg("049007", this.schemaTable.toString());
}
}