[jboss-jira] [JBoss JIRA] (AS7-3587) Create a AttributeDefinition validator for validating JGroups protocol types
Richard Achmatowicz (JIRA)
jira-events at lists.jboss.org
Thu Feb 2 11:48:48 EST 2012
[ https://issues.jboss.org/browse/AS7-3587?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12663806#comment-12663806 ]
Richard Achmatowicz commented on AS7-3587:
------------------------------------------
The validator can be easily created if we set up an enum type Protocol listing the valid protocol types. But the list of JGroups protocols changes often, so we want a validator which can create the list of protocol names dynamically.
Here is a first attempt which uses code from org.jgroups.util.Util to attempt to read the list of valid protocols:
{noformat}
public class ProtocolTypeValidator extends ModelTypeValidator implements AllowedValuesValidator {
private final Set<String> allowedValues;
private final List<ModelNode> nodeValues;
public ProtocolTypeValidator(final boolean nullable) {
// (type, nullable, allowExpressions)
super(ModelType.STRING, nullable, false);
// rather than get list of protocols from static enum, ask JGroups for most current list
Set<Class<org.jgroups.stack.Protocol>> classes = null ;
try {
classes = findClassesAssignableFrom("org.jgroups.protocols", org.jgroups.stack.Protocol.class) ;
classes.addAll(findClassesAssignableFrom("org.jgroups.protocols.pbcast", org.jgroups.stack.Protocol.class)) ;
}
catch(Exception e) {
// handle exception
System.out.println("exception occurred: " + e.toString()) ;
}
Set<String> validProtocolNames = new HashSet<String>();
for (Class<org.jgroups.stack.Protocol> clazz : classes) {
validProtocolNames.add(clazz.getSimpleName());
}
allowedValues = validProtocolNames ;
nodeValues = new ArrayList<ModelNode>(allowedValues.size());
for (String protocol : allowedValues) {
nodeValues.add(new ModelNode().set(protocol.toString()));
}
}
@Override
public void validateParameter(final String parameterName, final ModelNode value) throws OperationFailedException {
super.validateParameter(parameterName, value);
if (value.isDefined()) {
final String protocol = value.asString();
if (protocol == null || !allowedValues.contains(protocol)) {
// throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidTargetName(allowedValues)));
throw new OperationFailedException(new ModelNode().set("invalid JGroups protocol layer type specified: " + protocol));
}
}
}
@Override
public List<ModelNode> getAllowedValues() {
return nodeValues;
}
private static <T> Set<Class<T>> findClassesAssignableFrom(String packageName, Class<T> assignableFrom)
throws IOException, ClassNotFoundException {
ClassLoader loader = org.jgroups.stack.Protocol.class.getClassLoader();
Set<Class<T>> classes = new HashSet<Class<T>>();
String path = packageName.replace('.', '/');
URL resource = loader.getResource(path);
if (resource != null) {
String filePath = resource.getFile();
System.out.println("resource file path = " + filePath);
if (filePath != null && new File(filePath).isDirectory()) {
for (String file : new File(filePath).list()) {
System.out.println("resource file name = " + file);
if (file.endsWith(".class")) {
String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
Class<T> clazz =(Class<T>)org.jgroups.stack.Protocol.class.getClassLoader().loadClass(name);
if (assignableFrom.isAssignableFrom(clazz))
classes.add(clazz);
}
}
}
}
return classes;
}
{noformat}
Problem is that the JGroups classes being searches are not in a directory but in a jar:
resource file path = file:/home/nrla/.m2/repository/org/jgroups/jgroups/3.0.4.Final/jgroups-3.0.4.Final.jar!/org/jgroups/protocols
resource file path = file:/home/nrla/.m2/repository/org/jgroups/jgroups/3.0.4.Final/jgroups-3.0.4.Final.jar!/org/jgroups/protocols/pbcast
Will have to find a way to safely search through the list of classes. Paul suggested looking at JBoss Modules. As I don't have time to investigate further, have to come back to this at a later date.
> Create a AttributeDefinition validator for validating JGroups protocol types
> ----------------------------------------------------------------------------
>
> Key: AS7-3587
> URL: https://issues.jboss.org/browse/AS7-3587
> Project: Application Server 7
> Issue Type: Feature Request
> Components: Clustering
> Reporter: Richard Achmatowicz
> Assignee: Richard Achmatowicz
>
> The management operation
> /subsystem=jgroups/stack=udp:add-protocol(type=X, socket-binding=Y, properties={Z})
> should have a validator for the JGroups protocol type which will allow the CLI user to see the current list of possible JGroups protocols to include in a stack.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.jboss.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the jboss-jira
mailing list