All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.oddjob.arooa.beanutils.MagicBeanDescriptorFactory Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package org.oddjob.arooa.beanutils;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.oddjob.arooa.ArooaBeanDescriptor;
import org.oddjob.arooa.ArooaDescriptor;
import org.oddjob.arooa.ArooaType;
import org.oddjob.arooa.ClassResolver;
import org.oddjob.arooa.ElementMappings;
import org.oddjob.arooa.beandocs.MappingsBeanDoc;
import org.oddjob.arooa.beandocs.MappingsContents;
import org.oddjob.arooa.convert.ConversionProvider;
import org.oddjob.arooa.deploy.ArooaDescriptorFactory;
import org.oddjob.arooa.deploy.BeanDescriptorProvider;
import org.oddjob.arooa.deploy.MappingsSwitch;
import org.oddjob.arooa.deploy.annotations.ArooaAttribute;
import org.oddjob.arooa.design.DesignFactory;
import org.oddjob.arooa.design.GenericDesignFactory;
import org.oddjob.arooa.life.ClassLoaderClassResolver;
import org.oddjob.arooa.life.ElementsForIdentifier;
import org.oddjob.arooa.life.InstantiationContext;
import org.oddjob.arooa.parsing.ArooaElement;
import org.oddjob.arooa.reflect.ArooaClass;
import org.oddjob.arooa.reflect.PropertyAccessor;

/**
 * @oddjob.description Define Magic Beans. Magic Beans are beans who's
 * properties can be defined dynamically. Magic beans are useful when
 * you want to collect information in one bean so it can be kept together.
 * 
 * @oddjob.example Creating a magic bean that define some file information.
 * 

* This is an outer Oddjob configuration file that creates the descriptor * that defines properties for a filespec element. * * {@oddjob.xml.resource org/oddjob/arooa/beanutils/MagicBeansExample.xml} * * The nested inner Oddjob configuration uses a list of filespec * magic beans to define information for a For Each job. * * {@oddjob.xml.resource org/oddjob/arooa/beanutils/MagicBeansInner.xml} * * The example will display the following when ran: * *

 * Checking Big File (\files\big)
 * less than 1000000 bytes...
 *
 * Checking Medium File (\files\medium)
 * less than 20000 bytes...
 * 
 * Checking Small File (\files\small)
 * less than 3000 bytes...
 * 
* * @author rob * */ public class MagicBeanDescriptorFactory implements ArooaDescriptorFactory { /** * @oddjob.property * @oddjob.description Definitions for Magic Beans. This will be a * list of {@link MagicBeanDefinition}s. * @oddjob.required. No, but pointless without any definitions. * */ private List definitions = new ArrayList(); /** * @oddjob.property * @oddjob.description The namespace for the magic bean element. * @oddjob.required No. */ private URI namespace; /** * @oddjob.property * @oddjob.description The element prefix. * @oddjob.required No. */ private String prefix; @Override public ArooaDescriptor createDescriptor(ClassLoader classLoader) { Mappings mappings = new Mappings(); Map descriptorProviders = new HashMap(); for (MagicBeanDefinition beanDef : definitions) { ArooaClass arooaClass = beanDef.createMagic(classLoader); BeanDescriptorProvider beanDescriptorProvider = beanDef.createMagicBeanDescriptorProvider(); descriptorProviders.put(arooaClass, beanDescriptorProvider); mappings.put(new ArooaElement(namespace, beanDef.getElement()), arooaClass); } return new Descriptor(mappings, descriptorProviders, classLoader); } class Descriptor implements ArooaDescriptor { private final Mappings mappings; private final Map descriptorProviders; private final ClassLoader classLoader; public Descriptor(Mappings mappings, Map descriptorProviders, ClassLoader classLoader) { this.mappings = mappings; this.descriptorProviders = descriptorProviders; this.classLoader = classLoader; } @Override public ArooaBeanDescriptor getBeanDescriptor( ArooaClass classIdentifier, PropertyAccessor accessor) { BeanDescriptorProvider descriptor = descriptorProviders.get(classIdentifier); if (descriptor == null) { return null; } else { return descriptor.getBeanDescriptor( classIdentifier, accessor); } } @Override public ClassResolver getClassResolver() { return new ClassLoaderClassResolver(classLoader); } @Override public ConversionProvider getConvertletProvider() { return null; } @Override public ElementMappings getElementMappings() { return new MappingsSwitch(null, mappings); } @Override public String getPrefixFor(URI namespace) { if (MagicBeanDescriptorFactory.this.namespace == namespace) { return prefix; } return null; } } public void setDefinitions(int index, MagicBeanDefinition def) { if (def == null) { definitions.remove(index); } else { definitions.add(index, def); } } public MagicBeanDefinition getDefinitions(int index) { return definitions.get(index); } public URI getNamespace() { return namespace; } @ArooaAttribute public void setNamespace(URI namespace) { this.namespace = namespace; } public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } class Mappings implements ElementMappings { private Map mappings = new LinkedHashMap(); @Override public DesignFactory designFor(ArooaElement element, InstantiationContext parentContext) { ArooaClass arooaClass = mappings.get(element); if (arooaClass == null) { return null; } return new GenericDesignFactory(arooaClass); } @Override public ArooaElement[] elementsFor( InstantiationContext propertyContext) { return new ElementsForIdentifier(mappings).elementsFor( propertyContext); } @Override public ArooaClass mappingFor(ArooaElement element, InstantiationContext parentContext) { return mappings.get(element); } void put(ArooaElement element, ArooaClass arooaClass) { mappings.put(element, arooaClass); } @Override public MappingsContents getBeanDoc(ArooaType arooaType) { return new MappingsBeanDoc(mappings); } } @Override public String toString() { return getClass().getSimpleName() + ": num definitions=" + definitions.size(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy