static final AttributeDefinition[] ATTRIBUTES = new AttributeDefinition[] { MODE, STOP_TIMEOUT, LOCKING };
private final boolean allowRuntimeOnlyRegistration;
static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder parent) {
ResourceTransformationDescriptionBuilder builder = parent.addChildResource(PATH);
List<OperationTransformer> addOperationTransformers = new LinkedList<>();
List<OperationTransformer> removeOperationTransformers = new LinkedList<>();
Map<String, OperationTransformer> readAttributeTransformers = new HashMap<>();
Map<String, OperationTransformer> writeAttributeTransformers = new HashMap<>();
Map<String, OperationTransformer> undefineAttributeTransformers = new HashMap<>();
if (InfinispanModel.VERSION_3_0_0.requiresTransformation(version)) {
// Convert BATCH -> NONE, and include write-attribute:name=batching
OperationTransformer addTransformer = new OperationTransformer() {
@Override
public TransformedOperation transformOperation(TransformationContext context, PathAddress address, ModelNode operation) throws OperationFailedException {
if (operation.hasDefined(MODE.getName())) {
ModelNode mode = operation.get(MODE.getName());
if ((mode.getType() == ModelType.STRING) && (TransactionMode.valueOf(mode.asString()) == TransactionMode.BATCH)) {
mode.set(TransactionMode.NONE.name());
ModelNode writeBatchingOperation = OperationFactory.createWriteAttributeOperation(cacheAddress(address), CacheResourceDefinition.BATCHING.getName(), new ModelNode(true));
return new TransformedOperation(OperationFactory.createCompositeOperation(writeBatchingOperation, operation), OperationResultTransformer.ORIGINAL_RESULT);
}
}
return new TransformedOperation(operation, OperationResultTransformer.ORIGINAL_RESULT);
}
};
addOperationTransformers.add(addTransformer);
// Additionally include undefine-attribute:name=batching
OperationTransformer removeTransformer = new OperationTransformer() {
@Override
public TransformedOperation transformOperation(TransformationContext context, PathAddress address, ModelNode operation) throws OperationFailedException {
ModelNode undefineBatchingOperation = OperationFactory.createUndefineAttributeOperation(cacheAddress(address), CacheResourceDefinition.BATCHING.getName());
return new TransformedOperation(OperationFactory.createCompositeOperation(undefineBatchingOperation, operation), OperationResultTransformer.ORIGINAL_RESULT);
}
};
removeOperationTransformers.add(removeTransformer);
// If read-attribute:name=batching is true, return BATCH, otherwise use result from read-attribute:name=mode
OperationTransformer readAttributeTransformer = new OperationTransformer() {
@Override
public TransformedOperation transformOperation(TransformationContext context, PathAddress address, ModelNode operation) throws OperationFailedException {
ModelNode readBatchingOperation = OperationFactory.createReadAttributeOperation(cacheAddress(address), CacheResourceDefinition.BATCHING.getName());
OperationResultTransformer resultTransformer = new OperationResultTransformer() {
@Override
public ModelNode transformResult(ModelNode result) {
ModelNode readBatchingResult = result.get(0);
return readBatchingResult.asBoolean() ? new ModelNode(TransactionMode.BATCH.name()) : result.get(1);
}
};
return new TransformedOperation(OperationFactory.createCompositeOperation(readBatchingOperation, operation), resultTransformer);
}
};
readAttributeTransformers.put(MODE.getName(), readAttributeTransformer);
// Convert BATCH -> NONE, and include write-attribute:name=batching
OperationTransformer writeAttributeTransformer = new OperationTransformer() {
@Override
public TransformedOperation transformOperation(TransformationContext context, PathAddress address, ModelNode operation) throws OperationFailedException {
ModelNode mode = operation.hasDefined(ModelDescriptionConstants.VALUE) ? operation.get(ModelDescriptionConstants.VALUE) : null;
boolean batching = (mode != null) && (mode.getType() == ModelType.STRING) ? (TransactionMode.valueOf(mode.asString()) == TransactionMode.BATCH) : false;
if (batching) {
mode.set(TransactionMode.NONE.name());
}
ModelNode writeBatchingOperation = OperationFactory.createWriteAttributeOperation(cacheAddress(address), CacheResourceDefinition.BATCHING.getName(), new ModelNode(batching));
return new TransformedOperation(OperationFactory.createCompositeOperation(writeBatchingOperation, operation), OperationResultTransformer.ORIGINAL_RESULT);
}
};
writeAttributeTransformers.put(MODE.getName(), writeAttributeTransformer);
undefineAttributeTransformers.put(MODE.getName(), writeAttributeTransformer);
// Convert BATCH -> NONE
AttributeConverter modeConverter = new AttributeConverter() {
@Override
public void convertOperationParameter(PathAddress address, String name, ModelNode value, ModelNode operation, TransformationContext context) {
// Operation transformation is handled via custom operation transformers
}
@Override
public void convertResourceAttribute(PathAddress address, String name, ModelNode value, TransformationContext context) {
if (value.isDefined() && (value.getType() == ModelType.STRING)) {
TransactionMode mode = TransactionMode.valueOf(value.asString());
if (mode == TransactionMode.BATCH) {
value.set(TransactionMode.NONE.name());
}
}
}
};
builder.getAttributeBuilder()
.addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, MODE)
.setValueConverter(modeConverter, MODE)
.end();
}
if (InfinispanModel.VERSION_1_4_0.requiresTransformation(version)) {
builder.getAttributeBuilder().addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, MODE, STOP_TIMEOUT, LOCKING);
}
buildOperationTransformation(builder, ModelDescriptionConstants.ADD, addOperationTransformers);
buildOperationTransformation(builder, ModelDescriptionConstants.REMOVE, removeOperationTransformers);
buildOperationTransformation(builder, ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION, readAttributeTransformers);