public final Collection<Class<? extends ConfiguredObject>> getDescendantCategories(Class<? extends ConfiguredObject> parent) { Set<Class<? extends ConfiguredObject>> allDescendants = new HashSet<>(); for(Class<? extends ConfiguredObject> clazz : getChildTypes(parent)) { if(allDescendants.add(clazz)) { allDescendants.addAll(getDescendantCategories(clazz)); } } return allDescendants; }
private boolean isBrokerType(Class<? extends ConfiguredObject> category) { return Broker.class.isAssignableFrom(category) || BrokerLogInclusionRule.class.isAssignableFrom(category) || VirtualHostAlias.class.isAssignableFrom(category) || ( !VirtualHostNode.class.isAssignableFrom(category) && getModel().getChildTypes(Broker.class).contains(category)); }
private boolean hasDescendant(final Class<? extends ConfiguredObject> candidate, final Class<? extends ConfiguredObject> descendantClass) { int oldSize = 0; Set<Class<? extends ConfiguredObject>> allDescendants = new HashSet<>(getChildTypes(candidate)); while(allDescendants.size() > oldSize) { oldSize = allDescendants.size(); Set<Class<? extends ConfiguredObject>> prev = new HashSet<>(allDescendants); for(Class<? extends ConfiguredObject> clazz : prev) { allDescendants.addAll(getChildTypes(clazz)); } if(allDescendants.contains(descendantClass)) { break; } } return allDescendants.contains(descendantClass); }
private void populateChildTypeMaps() { if(!(_children.isEmpty() && _childrenById.isEmpty() && _childrenByName.isEmpty())) { throw new IllegalStateException("Cannot update the child type maps on a class with pre-existing child types"); } for (Class<? extends ConfiguredObject> childClass : getModel().getChildTypes(getCategoryClass())) { _children.put(childClass, new CopyOnWriteArrayList<ConfiguredObject<?>>()); _childrenById.put(childClass, new ConcurrentHashMap<UUID, ConfiguredObject<?>>()); _childrenByName.put(childClass, new ConcurrentHashMap<String, ConfiguredObject<?>>()); } }
private static Map<String,Class<? extends ConfiguredObject>> generateClassNameMap(final Model model, final Class<? extends ConfiguredObject> clazz) { Map<String,Class<? extends ConfiguredObject>>map = new HashMap<String, Class<? extends ConfiguredObject>>(); if(clazz != null) { map.put(clazz.getSimpleName(), clazz); Collection<Class<? extends ConfiguredObject>> childClasses = model.getChildTypes(clazz); if (childClasses != null) { for (Class<? extends ConfiguredObject> childClass : childClasses) { map.putAll(generateClassNameMap(model, childClass)); } } } return map; }
private void addManagedHierarchies(Class<? extends ConfiguredObject> category, final Class<? extends ConfiguredObject> rootCategory) { if (!_hierarchies.containsKey(category)) { _hierarchies.put(category, calculateHierarchy(category, rootCategory)); for (Class<? extends ConfiguredObject> childClass : _model.getChildTypes(category)) { addManagedHierarchies(childClass, rootCategory); } } }
private void applyRecursively(final ConfiguredObject<?> object) { Collection<Class<? extends ConfiguredObject>> childTypes = object.getModel().getChildTypes(object.getCategoryClass()); childTypes.forEach(childClass -> { Collection<? extends ConfiguredObject> children = object.getChildren(childClass); if (!children.isEmpty()) { children.forEach(this::processChild); } }); processAssociations(object); }
public static boolean isSpecialization(final Model model, final Model specialization, final Class<? extends ConfiguredObject> specializationPoint) { if(model.getSupportedCategories().contains(specializationPoint) && specialization.getSupportedCategories().containsAll(model.getSupportedCategories()) && model.getChildTypes(specializationPoint).isEmpty()) { final Collection<Class<? extends ConfiguredObject>> modelSupportedCategories = new ArrayList<>(model.getSupportedCategories()); modelSupportedCategories.remove(specializationPoint); for(Class<? extends ConfiguredObject> category : modelSupportedCategories) { if(!model.getChildTypes(category).equals(specialization.getChildTypes(category))) { return false; } } return true; } else { return false; } } }
private Map<String, Collection<String>> getValidChildTypes(final Class<? extends ConfiguredObject> type, final Model model) { Map<String, Collection<String>> validChildTypes = new HashMap<>(); for (Class<? extends ConfiguredObject> childType : model.getChildTypes(ConfiguredObjectTypeRegistry.getCategory( type))) { Collection<String> validValues = model.getTypeRegistry().getValidChildTypes(type, childType); if (validValues != null) { validChildTypes.put(childType.getSimpleName(), validValues); } } return validChildTypes; }
<X extends ConfiguredObject<X>> Collection<X> getAllDescendants(final ConfiguredObject ancestor, final Class<? extends ConfiguredObject> ancestorClass, final Class<X> clazz) { Set<X> descendants = new HashSet<X>(); for(Class<? extends ConfiguredObject> childClass : getChildTypes(ancestorClass)) { Collection<? extends ConfiguredObject> children = ancestor.getChildren(childClass); if(childClass == clazz) { if(children != null) { descendants.addAll((Collection<X>)children); } } else { if(children != null) { for(ConfiguredObject child : children) { descendants.addAll(getAllDescendants(child, childClass, clazz)); } } } } return descendants; }
protected final void updateModel(Model model) { if(this instanceof DynamicModel && _children.isEmpty() && _model.getChildTypes(getCategoryClass()).isEmpty() && Model.isSpecialization(_model, model, getCategoryClass())) { _model = model; populateChildTypeMaps(); } else { throw new IllegalStateException("Cannot change the model of a class which does not implement DynamicModel, or has defined child types"); } }
private Map<String, Object> build(final Class<? extends ConfiguredObject> type, final UUID id, Map<UUID, Map<String, SortedSet<ConfiguredObjectRecord>>> childMap) { ConfiguredObjectRecord record = _objectsById.get(id); Map<String,Object> map = new LinkedHashMap<>(); map.put("id", id); map.putAll(record.getAttributes()); List<Class<? extends ConfiguredObject>> childClasses = new ArrayList<>(_parent.getModel().getChildTypes(type)); Collections.sort(childClasses, CATEGORY_CLASS_COMPARATOR); final Map<String, SortedSet<ConfiguredObjectRecord>> allChildren = childMap.get(id); if(allChildren != null && !allChildren.isEmpty()) { for(Map.Entry<String, SortedSet<ConfiguredObjectRecord>> entry : allChildren.entrySet()) { String singularName = entry.getKey().toLowerCase(); String attrName = singularName + (singularName.endsWith("s") ? "es" : "s"); final SortedSet<ConfiguredObjectRecord> sortedChildren = entry.getValue(); List<Map<String,Object>> entities = new ArrayList<>(); for(ConfiguredObjectRecord childRecord : sortedChildren) { entities.add(build(_classNameMapping.get(entry.getKey()), childRecord.getId(), childMap)); } if(!entities.isEmpty()) { map.put(attrName,entities); } } } return map; }
@Override public final Container<?> getContainer() { final Collection<Class<? extends ConfiguredObject>> containerTypes = getModel().getChildTypes(SystemConfig.class); Class containerClass = null; for(Class<? extends ConfiguredObject> clazz : containerTypes) { if(Container.class.isAssignableFrom(clazz)) { if(containerClass == null) { containerClass = clazz; } else { throw new IllegalArgumentException("Model has more than one child Container class beneath SystemConfig"); } } } if(containerClass == null) { throw new IllegalArgumentException("Model has no child Container class beneath SystemConfig"); } return getContainer(containerClass); }
private void checkReferencesOnDelete(final ConfiguredObject<?> referrer, final ConfiguredObject<?> referee) { if (!managesChildren(referee)) { getModel().getChildTypes(referee.getCategoryClass()) .forEach(childClass -> referee.getChildren(childClass) .forEach(child -> checkReferencesOnDelete(referrer, child))); } checkReferences(referrer, referee); }
private List<ConfiguredObject<?>> getChildrenInState(final ConfiguredObject<?> configuredObject, final State state, final DescendantScope descendantScope) { List<ConfiguredObject<?>> foundChildren = new ArrayList<>(); Class<? extends ConfiguredObject> categoryClass = configuredObject.getCategoryClass(); for (final Class<? extends ConfiguredObject> childClass : getModel().getChildTypes(categoryClass)) { final Collection<? extends ConfiguredObject> children = configuredObject.getChildren(childClass); for (final ConfiguredObject<?> child : children) { if (child.getState() == state) { foundChildren.add(child); } if (descendantScope == DescendantScope.ALL) { foundChildren.addAll(getChildrenInState(child, state, descendantScope)); } } } return foundChildren; }
protected void applyToChildren(Action<ConfiguredObject<?>> action) { for (Class<? extends ConfiguredObject> childClass : getModel().getChildTypes(getCategoryClass())) { Collection<? extends ConfiguredObject> children = getChildren(childClass); if (children != null) { for (ConfiguredObject<?> child : children) { action.performAction(child); } } } }
private void applyRecursively(final ConfiguredObject<?> object, final RecursiveAction<ConfiguredObject<?>> action, final HashSet<ConfiguredObject<?>> visited) { if(!visited.contains(object)) { visited.add(object); action.performAction(object); if (action.applyToChildren(object)) { for (Class<? extends ConfiguredObject> childClass : object.getModel().getChildTypes(object.getCategoryClass())) { Collection<? extends ConfiguredObject> children = object.getChildren(childClass); if (children != null) { for (ConfiguredObject<?> child : children) { applyRecursively(child, action, visited); } } } } } }
getModel().getChildTypes(referrer.getCategoryClass()) .forEach(childClass -> referrer.getChildren(childClass) .stream()
private Map<String, Object> generateStructure(ConfiguredObject<?> object, Class<? extends ConfiguredObject> clazz, final boolean includeAssociated) { Map<String, Object> structure = new LinkedHashMap<>(); structure.put("id", object.getId()); structure.put("name", object.getName()); for(Class<? extends ConfiguredObject> childClass : object.getModel().getChildTypes(clazz)) { addChildrenToStructure(structure, childClass, object.getChildren(childClass)); } if(includeAssociated) { ConfiguredObjectFinder finder = getConfiguredObjectFinder(object); for(Class<? extends ConfiguredObject> childClass : finder.getAssociatedChildCategories()) { addChildrenToStructure(structure, childClass, finder.getAssociatedChildren(childClass)); } } return structure; }
@Override public void childAdded(ConfiguredObject<?> object, ConfiguredObject<?> child) { if (!object.managesChildStorage()) { if(object.isDurable() && child.isDurable()) { Model model = child.getModel(); Class<? extends ConfiguredObject> parentType = model.getParentType(child.getCategoryClass()); if(parentType.equals(object.getCategoryClass())) { child.addChangeListener(this); _store.update(true, child.asObjectRecord()); Class<? extends ConfiguredObject> categoryClass = child.getCategoryClass(); Collection<Class<? extends ConfiguredObject>> childTypes = model.getChildTypes(categoryClass); for (Class<? extends ConfiguredObject> childClass : childTypes) { for (ConfiguredObject<?> grandchild : child.getChildren(childClass)) { childAdded(child, grandchild); } } } } } }