use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManager method getSequence.
// ------------------------------------- Sequence Management --------------------------------------
/**
* Method to retrieve a sequence by name. As per JDO2 spec section 12.14.
* If the named sequence is not known, throws a JDOUserException.
* @param sequenceName Fully qualified name of the sequence
* @return The sequence
*/
public Sequence getSequence(String sequenceName) {
assertIsOpen();
SequenceMetaData seqmd = ec.getMetaDataManager().getMetaDataForSequence(ec.getClassLoaderResolver(), sequenceName);
if (seqmd == null) {
throw new JDOUserException(Localiser.msg("017000", sequenceName));
}
Sequence seq = null;
if (seqmd.getFactoryClass() != null) {
// User has specified a factory class
seq = pmf.getSequenceForFactoryClass(seqmd.getFactoryClass());
if (seq == null) {
// Create a new instance of the factory class and obtain the Sequence
Class factory = ec.getClassLoaderResolver().classForName(seqmd.getFactoryClass());
if (factory == null) {
throw new JDOUserException(Localiser.msg("017001", sequenceName, seqmd.getFactoryClass()));
}
Class[] argTypes = null;
Object[] arguments = null;
if (seqmd.getStrategy() != null) {
argTypes = new Class[2];
argTypes[0] = String.class;
argTypes[1] = String.class;
arguments = new Object[2];
arguments[0] = seqmd.getName();
arguments[1] = seqmd.getStrategy().toString();
} else {
argTypes = new Class[1];
argTypes[0] = String.class;
arguments = new Object[1];
arguments[0] = seqmd.getName();
}
Method newInstanceMethod;
try {
// Obtain the sequence from the static "newInstance(...)" method
newInstanceMethod = factory.getMethod("newInstance", argTypes);
seq = (Sequence) newInstanceMethod.invoke(null, arguments);
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("017002", seqmd.getFactoryClass(), e.getMessage()));
}
// Register the sequence with the PMF
pmf.addSequenceForFactoryClass(seqmd.getFactoryClass(), seq);
}
} else {
NucleusSequence nucSeq = ec.getStoreManager().getNucleusSequence(ec, seqmd);
seq = new JDOSequence(nucSeq);
}
return seq;
}
use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManager method deletePersistentAll.
/**
* JDO method to delete a collection of objects from the datastore.
* Throws a JDOUserException if objects could not be deleted.
* @param pcs The objects
* @throws JDOUserException Thrown if one (or more) object cannot be deleted
*/
public void deletePersistentAll(Collection pcs) {
assertIsOpen();
assertWritable();
try {
ec.deleteObjects(pcs.toArray());
} catch (NucleusUserException nue) {
Throwable[] failures = nue.getNestedExceptions();
throw new JDOUserException(Localiser.msg("010040"), failures);
}
}
use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method processLifecycleListenersFromProperties.
/**
* Convenience method to extract lifecycle listeners that are specified by way of persistence properties.
* @param props Persistence props.
*/
protected void processLifecycleListenersFromProperties(Map props) {
if (props != null) {
// Process any lifecycle listeners defined in persistent properties
Iterator<Map.Entry> propsIter = props.entrySet().iterator();
while (propsIter.hasNext()) {
Map.Entry entry = propsIter.next();
String key = (String) entry.getKey();
if (key.startsWith(Constants.PROPERTY_INSTANCE_LIFECYCLE_LISTENER)) {
String listenerClsName = key.substring(45);
String listenerClasses = (String) entry.getValue();
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
Class listenerCls = null;
try {
listenerCls = clr.classForName(listenerClsName);
} catch (ClassNotResolvedException cnre) {
throw new JDOUserException(Localiser.msg("012022", listenerClsName));
}
InstanceLifecycleListener listener = null;
// Find method getInstance()
Method method = ClassUtils.getMethodForClass(listenerCls, "getInstance", null);
if (method != null) {
// Create instance via getInstance()
try {
listener = (InstanceLifecycleListener) method.invoke(null);
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("012021", listenerClsName), e);
}
} else {
// Try default constructor
try {
listener = (InstanceLifecycleListener) listenerCls.newInstance();
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("012020", listenerClsName), e);
}
}
Class[] classes = null;
if (!StringUtils.isWhitespace(listenerClasses)) {
String[] classNames = StringUtils.split(listenerClasses, ",");
classes = new Class[classNames.length];
for (int i = 0; i < classNames.length; i++) {
classes[i] = clr.classForName(classNames[i]);
}
}
addInstanceLifecycleListener(listener, classes);
}
}
}
}
use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method setServerTimeZoneID.
/**
* Mutator for the timezone id of the datastore server.
* If not set assumes that it is running in the same timezone as this JVM.
* @param id Timezone Id to use
*/
public void setServerTimeZoneID(String id) {
assertConfigurable();
boolean validated = new CorePropertyValidator().validate(PropertyNames.PROPERTY_SERVER_TIMEZONE_ID, id);
if (validated) {
getConfiguration().setProperty(PropertyNames.PROPERTY_SERVER_TIMEZONE_ID, id);
} else {
throw new JDOUserException("Invalid TimeZone ID specified");
}
}
use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.
the class JDOReplicationManager method replicate.
/**
* Method to perform the replication for all objects of the specified class names.
* @param classNames Classes to replicate
*/
public void replicate(String... classNames) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(classNames)));
}
// Check if classes are detachable
NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
for (int i = 0; i < classNames.length; i++) {
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(classNames[i], clr);
if (!cmd.isDetachable()) {
throw new JDOUserException("Class " + classNames[i] + " is not detachable so cannot replicate");
}
}
Object[] detachedObjects = null;
// Detach from datastore 1
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
}
PersistenceManager pm1 = pmfSource.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
pm1.getFetchPlan().setMaxFetchDepth(-1);
}
try {
tx1.begin();
clr = ((JDOPersistenceManager) pm1).getExecutionContext().getClassLoaderResolver();
ArrayList objects = new ArrayList();
for (int i = 0; i < classNames.length; i++) {
Class cls = clr.classForName(classNames[i]);
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(cls, clr);
if (!cmd.isEmbeddedOnly()) {
Extent ex = pm1.getExtent(cls);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
objects.add(iter.next());
}
}
}
Collection detachedColl = pm1.detachCopyAll(objects);
detachedObjects = detachedColl.toArray();
tx1.commit();
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
replicateInTarget(detachedObjects);
}
Aggregations