// javadoc inherited
public ResourceDescriptor createDescriptor(
String resourceType,Parameters configParams, ParameterNames names,
final long initialTimeToLive) {
DefaultResourceDescriptor result = null;
int hash = DefaultResourceDescriptor.computeDBHash(
resourceType, configParams);
PersistenceManager pm = getPersistenceManager();
pm.getFetchPlan().setMaxFetchDepth(-1);
pm.getFetchPlan().setFetchSize(FetchPlan.FETCH_SIZE_GREEDY);
pm.setDetachAllOnCommit(true);
final Transaction tx = pm.currentTransaction();
try {
tx.begin();
// run the query
final Query query = pm.newQuery(DefaultResourceDescriptor.class,
"hash == hashcode");
query.declareParameters("int hashcode");
List hashMatches = (List) query.execute(new Integer(hash));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found " + hashMatches.size() +
" matches for hashcode " + hash);
}
Iterator it = hashMatches.iterator();
boolean finished = false;
while (it.hasNext() && !finished) {
DefaultResourceDescriptor gi = (DefaultResourceDescriptor) it.next();
// here we check that the important bits of the retrieved entry
// are equal to the requested information
if (resourceType.equals(gi.getResourceType()) &&
gi.getInputParameters().equals(
gi.getInputParameters())) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found exact match for hash " + hash);
}
result = gi;
finished = true;
}
}
// there is a minor concurrency issue here where more then one
// ResourceDefinition objects could be created for the same
// resource. It is a small hole and causes an inefficiency in the
// amount of data stored but does not break anything.
if (null == result) {
result = new DefaultResourceDescriptor(resourceType,
(DefaultParameters) configParams,
(DefaultParameterNames) names);
result.computeDBHash();
result.setExternalID(ExternalIDGenerator.getNextID());
result.setTimeToLive(initialTimeToLive);