diff options
| author | Keith Wall <kwall@apache.org> | 2014-12-27 08:55:28 +0000 |
|---|---|---|
| committer | Keith Wall <kwall@apache.org> | 2014-12-27 08:55:28 +0000 |
| commit | f8f58ec7e1e5517ff0a3ecc25248fa636b67a393 (patch) | |
| tree | c294063b6f6b7f1418f7032d49330ead40f6ad00 /qpid/java/broker-core/src | |
| parent | ef98f2d05fabd0cf8dceb5f02394be00345bf30c (diff) | |
| download | qpid-python-f8f58ec7e1e5517ff0a3ecc25248fa636b67a393.tar.gz | |
QPID-6289: [Java Broker] Extend Java Broker model to encapsulate permitted child types
Work of Robert Godfrey <rgodfrey@apache.org>
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1648039 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker-core/src')
6 files changed, 189 insertions, 2 deletions
diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java index 8ca5ff3d6a..d134c43bda 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java @@ -21,8 +21,11 @@ package org.apache.qpid.server.model; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.AbstractCollection; import java.util.Arrays; import java.util.Collection; @@ -176,6 +179,9 @@ public class ConfiguredObjectTypeRegistry private final Map<Class<? extends ConfiguredObject>,Set<Class<? extends ManagedInterface>>> _allManagedInterfaces = Collections.synchronizedMap(new HashMap<Class<? extends ConfiguredObject>, Set<Class<? extends ManagedInterface>>>()); + private final Map<Class<? extends ConfiguredObject>, Map<String, Collection<String>>> _validChildTypes = + Collections.synchronizedMap(new HashMap<Class<? extends ConfiguredObject>, Map<String, Collection<String>>>()); + public ConfiguredObjectTypeRegistry(Iterable<ConfiguredObjectRegistration> configuredObjectRegistrations, Collection<Class<? extends ConfiguredObject>> categoriesRestriction) { @@ -265,6 +271,96 @@ public class ConfiguredObjectTypeRegistry } } + + for(Class<? extends ConfiguredObject> type : types) + { + final ManagedObject annotation = type.getAnnotation(ManagedObject.class); + String validChildren = annotation.validChildTypes(); + if(!"".equals(validChildren)) + { + Method validChildTypesMethod = getValidChildTypesFunction(validChildren, type); + if(validChildTypesMethod != null) + { + try + { + _validChildTypes.put(type, (Map<String, Collection<String>>) validChildTypesMethod.invoke(null)); + } + catch (IllegalAccessException | InvocationTargetException e) + { + throw new IllegalArgumentException("Exception while evaluating valid child types for " + type.getName(), e); + } + } + + } + } + } + + private static Method getValidChildTypesFunction(final String validValue, final Class<? extends ConfiguredObject> clazz) + { + if (validValue.matches("([\\w][\\w\\d_]+\\.)+[\\w][\\w\\d_\\$]*#[\\w\\d_]+\\s*\\(\\s*\\)")) + { + String function = validValue; + try + { + String className = function.split("#")[0].trim(); + String methodName = function.split("#")[1].split("\\(")[0].trim(); + Class<?> validValueCalculatingClass = Class.forName(className); + Method method = validValueCalculatingClass.getMethod(methodName); + if (Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers())) + { + if (Map.class.isAssignableFrom(method.getReturnType())) + { + if (method.getGenericReturnType() instanceof ParameterizedType) + { + Type keyType = + ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0]; + if (keyType == String.class) + { + Type valueType = + ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[1]; + if (valueType instanceof ParameterizedType) + { + ParameterizedType paramType = (ParameterizedType) valueType; + final Type rawType = paramType.getRawType(); + final Type[] args = paramType.getActualTypeArguments(); + if (Collection.class.isAssignableFrom((Class<?>) rawType) + && args.length == 1 + && args[0] == String.class) + { + return method; + } + } + } + } + } + } + + + throw new IllegalArgumentException("The validChildTypes of the class " + + clazz.getSimpleName() + + " has value '" + + validValue + + "' but the method does not meet the requirements - is it public and static"); + + } + catch (ClassNotFoundException | NoSuchMethodException e) + { + throw new IllegalArgumentException("The validChildTypes of the class " + + clazz.getSimpleName() + + " has value '" + + validValue + + "' which looks like it should be a method," + + " but no such method could be used.", e); + } + } + else + { + throw new IllegalArgumentException("The validChildTypes of the class " + + clazz.getSimpleName() + + " has value '" + + validValue + + "' which does not match the required <package>.<class>#<method>() format."); + } } public static Class<? extends ConfiguredObject> getCategory(final Class<?> clazz) @@ -907,4 +1003,18 @@ public class ConfiguredObjectTypeRegistry } } + public Collection<String> getValidChildTypes(Class<? extends ConfiguredObject> type, Class<? extends ConfiguredObject> childType) + { + final Map<String, Collection<String>> allValidChildTypes = _validChildTypes.get(getTypeClass(type)); + if(allValidChildTypes != null) + { + final Collection<String> validTypesForSpecificChild = allValidChildTypes.get(getCategory(childType).getSimpleName()); + return validTypesForSpecificChild == null ? null : Collections.unmodifiableCollection(validTypesForSpecificChild); + } + else + { + return null; + } + } + } diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedObject.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedObject.java index f18869bced..483ddd478d 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedObject.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedObject.java @@ -35,5 +35,6 @@ public @interface ManagedObject boolean creatable() default true; String defaultType() default ""; // in this case the class/interface itself is to be used String type() default ""; + String validChildTypes() default ""; boolean register() default true; } diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/NonStandardVirtualHost.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/NonStandardVirtualHost.java new file mode 100644 index 0000000000..3137ea2766 --- /dev/null +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/NonStandardVirtualHost.java @@ -0,0 +1,29 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.server.virtualhost; + +import org.apache.qpid.server.model.Exchange; +import org.apache.qpid.server.model.Queue; +import org.apache.qpid.server.model.VirtualHost; + +public interface NonStandardVirtualHost<X extends NonStandardVirtualHost<X, Q, E>, Q extends Queue<?>, E extends Exchange<?> > extends VirtualHost<X, Q, E> +{ +} diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/ProvidedStoreVirtualHost.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/ProvidedStoreVirtualHost.java index deb4f1d155..475d8595bf 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/ProvidedStoreVirtualHost.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/ProvidedStoreVirtualHost.java @@ -24,7 +24,10 @@ import org.apache.qpid.server.model.ManagedAttribute; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.store.SizeMonitoringSettings; -public interface ProvidedStoreVirtualHost<X extends ProvidedStoreVirtualHost<X>> extends VirtualHostImpl<X,AMQQueue<?>,ExchangeImpl<?>>, SizeMonitoringSettings +public interface ProvidedStoreVirtualHost<X extends ProvidedStoreVirtualHost<X>> + extends VirtualHostImpl<X,AMQQueue<?>,ExchangeImpl<?>>, + SizeMonitoringSettings, + NonStandardVirtualHost<X,AMQQueue<?>,ExchangeImpl<?>> { @ManagedAttribute(mandatory = true, defaultValue = "0") Long getStoreUnderfullSize(); diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java index f4c0576d79..bcfd0ff951 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -47,14 +48,18 @@ import org.apache.qpid.server.logging.subjects.MessageStoreLogSubject; import org.apache.qpid.server.model.AbstractConfiguredObject; import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.ConfiguredObjectTypeRegistry; import org.apache.qpid.server.model.Exchange; import org.apache.qpid.server.model.LifetimePolicy; import org.apache.qpid.server.model.ManagedAttributeField; +import org.apache.qpid.server.model.ManagedObject; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.StateTransition; import org.apache.qpid.server.model.SystemConfig; import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostNode; +import org.apache.qpid.server.plugin.ConfiguredObjectRegistration; +import org.apache.qpid.server.plugin.QpidServiceLoader; import org.apache.qpid.server.security.access.Operation; import org.apache.qpid.server.security.auth.AuthenticatedPrincipal; import org.apache.qpid.server.store.ConfiguredObjectRecord; @@ -62,6 +67,8 @@ import org.apache.qpid.server.store.ConfiguredObjectRecordConverter; import org.apache.qpid.server.store.ConfiguredObjectRecordImpl; import org.apache.qpid.server.store.DurableConfigurationStore; import org.apache.qpid.server.util.urlstreamhandler.data.Handler; +import org.apache.qpid.server.virtualhost.NonStandardVirtualHost; +import org.apache.qpid.server.virtualhost.ProvidedStoreVirtualHostImpl; public abstract class AbstractVirtualHostNode<X extends AbstractVirtualHostNode<X>> extends AbstractConfiguredObject<X> implements VirtualHostNode<X> { @@ -442,4 +449,34 @@ public abstract class AbstractVirtualHostNode<X extends AbstractVirtualHostNode< return initialConfigReader; } + protected static Collection<String> getSupportedVirtualHostTypes(boolean includeProvided) + { + + final Iterable<ConfiguredObjectRegistration> registrations = + (new QpidServiceLoader()).instancesOf(ConfiguredObjectRegistration.class); + + Set<String> supportedTypes = new HashSet<>(); + + for(ConfiguredObjectRegistration registration : registrations) + { + for(Class<? extends ConfiguredObject> typeClass : registration.getConfiguredObjectClasses()) + { + if(VirtualHost.class.isAssignableFrom(typeClass)) + { + ManagedObject annotation = typeClass.getAnnotation(ManagedObject.class); + + if (annotation.creatable() && annotation.defaultType().equals("") && !NonStandardVirtualHost.class.isAssignableFrom(typeClass)) + { + supportedTypes.add(ConfiguredObjectTypeRegistry.getType(typeClass)); + } + } + } + } + if(includeProvided) + { + supportedTypes.add(ProvidedStoreVirtualHostImpl.VIRTUAL_HOST_TYPE); + } + return Collections.unmodifiableCollection(supportedTypes); + } + } diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/JsonVirtualHostNodeImpl.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/JsonVirtualHostNodeImpl.java index d97d29092c..b8a7b3fa22 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/JsonVirtualHostNodeImpl.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/JsonVirtualHostNodeImpl.java @@ -20,6 +20,8 @@ */ package org.apache.qpid.server.virtualhostnode; +import java.util.Collection; +import java.util.Collections; import java.util.Map; import org.apache.qpid.server.logging.messages.ConfigStoreMessages; @@ -31,7 +33,7 @@ import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.store.DurableConfigurationStore; import org.apache.qpid.server.store.JsonFileConfigStore; -@ManagedObject(type=JsonVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE, category=false) +@ManagedObject(type=JsonVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE, category=false, validChildTypes = "org.apache.qpid.server.virtualhostnode.JsonVirtualHostNodeImpl#getSupportedChildTypes()") public class JsonVirtualHostNodeImpl extends AbstractStandardVirtualHostNode<JsonVirtualHostNodeImpl> implements JsonVirtualHostNode<JsonVirtualHostNodeImpl> { public static final String VIRTUAL_HOST_NODE_TYPE = "JSON"; @@ -68,4 +70,9 @@ public class JsonVirtualHostNodeImpl extends AbstractStandardVirtualHostNode<Jso { return getClass().getSimpleName() + " [id=" + getId() + ", name=" + getName() + ", storePath=" + getStorePath() + "]"; } + + public static Map<String, Collection<String>> getSupportedChildTypes() + { + return Collections.singletonMap(VirtualHost.class.getSimpleName(), getSupportedVirtualHostTypes(false)); + } } |
