generatorNameKeyInManager = cmd.getBaseAbstractClassMetaData().getFullClassName();
}
}
// Try to find the generator from the ValueGenerationManager if we already have it managed
ValueGenerator generator = null;
synchronized (this)
{
// This block is synchronised since we don't want to let any other thread through until
// the generator is created, in case it is the same as the next request
generator = getValueGenerationManager().getValueGenerator(generatorNameKeyInManager);
if (generator == null)
{
if (generatorName == null)
{
// No available value-generator for the specified strategy for this datastore
throw new NucleusUserException(LOCALISER.msg("038004", strategy));
}
// Set up the default properties available for all value generators
Properties props = getPropertiesForGenerator(cmd, absoluteFieldNumber, ec, sequenceMetaData,
tableGeneratorMetaData);
Class cls = null;
if (elem != null)
{
cls = nucleusContext.getPluginManager().loadClass(elem.getExtension().getPlugin().getSymbolicName(),
elem.getAttribute("class-name"));
}
if (cls == null)
{
throw new NucleusException("Cannot create Value Generator for strategy " + generatorName);
}
// Create the new ValueGenerator since the first time required (registers it with
// the ValueGenerationManager too)
generator = getValueGenerationManager().createValueGenerator(generatorNameKeyInManager, cls, props, this, null);
}
}
// Get the next value from the generator
Object oid = getStrategyValueForGenerator(generator, ec);
if (mmd != null)
{
// replace the value of the id, but before convert the value to the field type if needed
try
{
Object convertedValue = TypeConversionHelper.convertTo(oid, mmd.getType());
if (convertedValue == null)
{
throw new NucleusException(LOCALISER.msg("038003", mmd.getFullFieldName(), oid)).setFatal();
}
oid = convertedValue;
}
catch (NumberFormatException nfe)
{
throw new NucleusUserException("Value strategy created value="+oid+" type="+oid.getClass().getName() +
" but field is of type "+mmd.getTypeName() + ". Use a different strategy or change the type of the field " + mmd.getFullFieldName());
}
}
if (NucleusLogger.VALUEGENERATION.isDebugEnabled())
{
NucleusLogger.VALUEGENERATION.debug(LOCALISER.msg("038002", fieldName, strategy, generator.getClass().getName(), oid));
}
return oid;
}