private Configurable getComponentFromAnnotation(String name, S4Component s4Component) { Configurable configurable; Class<? extends Configurable> defClass = s4Component.defaultClass(); if (defClass.equals(Configurable.class) && s4Component.mandatory()) { throw new InternalConfigurationException( getInstanceName(), name, "mandatory property is not set!"); } if (Modifier.isAbstract(defClass.getModifiers()) && s4Component.mandatory()) throw new InternalConfigurationException( getInstanceName(), name, defClass.getName() + " is abstract!"); // because we're forced to use the default type, make sure that it // is set if (defClass.equals(Configurable.class)) { if (s4Component.mandatory()) { throw new InternalConfigurationException( getInstanceName(), name, instanceName + ": no default class defined for " + name); } else { return null; } } configurable = ConfigurationManager.getInstance(defClass); if (configurable == null) { throw new InternalConfigurationException( getInstanceName(), name, "instantiation of referenenced configurable failed"); } return configurable; }
/** * Gets a list of components associated with the given parameter name * * @param <T> parent component * @param name the parameter name * @param tclass the class of the list elements * @return the component associated with the name * @throws PropertyException if the component does not exist or is of the wrong type. */ public <T> List<T> getComponentList(String name, Class<T> tclass) throws InternalConfigurationException { getProperty(name, S4ComponentList.class); List<?> components = (List<?>) propValues.get(name); assert registeredProperties.get(name).getAnnotation() instanceof S4ComponentList; S4ComponentList annotation = (S4ComponentList) registeredProperties.get(name).getAnnotation(); // no components names are available and no component list was yet // loaded therefore load the default list of components from the // annotation if (components == null) { List<Class<? extends Configurable>> defClasses = Arrays.asList(annotation.defaultList()); // if (annotation.mandatory() && defClasses.isEmpty()) // throw new InternalConfigurationException(getInstanceName(), name, // "mandatory property is not set!"); List<Configurable> defaultComponents = new ArrayList<Configurable>(); for (Class<? extends Configurable> defClass : defClasses) { defaultComponents.add(ConfigurationManager.getInstance(defClass)); } propValues.put(name, defaultComponents); } else if (!components.isEmpty() && !(components.get(0) instanceof Configurable)) { List<Configurable> resolvedComponents = new ArrayList<Configurable>(); for (Object componentName : components) { Configurable configurable = cm.lookup((String) componentName); if (configurable != null) { resolvedComponents.add(configurable); } else if (!annotation.beTolerant()) { throw new InternalConfigurationException( name, (String) componentName, "lookup of list-element '" + componentName + "' failed!"); } } propValues.put(name, resolvedComponents); } List<?> values = (List<?>) propValues.get(name); ArrayList<T> result = new ArrayList<T>(); for (Object obj : values) { if (tclass.isInstance(obj)) { result.add(tclass.cast(obj)); } else { throw new InternalConfigurationException( getInstanceName(), name, "Not all elements have required type " + tclass + " Found one of type " + obj.getClass()); } } return result; }