001: package org.andromda.core.metafacade;
002:
003: import java.lang.reflect.Constructor;
004:
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import org.andromda.core.common.ClassUtils;
010: import org.andromda.core.common.Introspector;
011: import org.andromda.core.configuration.Namespaces;
012: import org.apache.log4j.Logger;
013:
014: /**
015: * Contains static utility methods for dealing with metafacade instances.
016: *
017: * @author Chad Brandon
018: */
019: final class MetafacadeUtils {
020: /**
021: * Indicates whether or not the mapping properties (present on the mapping, if any) are valid on the
022: * <code>metafacade</code>.
023: *
024: * @param metafacade the metafacade instance on which the properties will be validated.
025: * @param mapping the MetafacadeMapping instance that contains the properties.
026: * @return true/false
027: */
028: static boolean propertiesValid(final MetafacadeBase metafacade,
029: final MetafacadeMapping mapping) {
030: boolean valid = false;
031: final Collection propertyGroups = mapping
032: .getMappingPropertyGroups();
033: if (propertyGroups != null && !propertyGroups.isEmpty()) {
034: try {
035: if (getLogger().isDebugEnabled()) {
036: getLogger()
037: .debug(
038: "evaluating "
039: + propertyGroups.size()
040: + " property groups(s) on metafacade '"
041: + metafacade + "'");
042: }
043: final Introspector introspector = Introspector
044: .instance();
045: for (final Iterator tterator = propertyGroups
046: .iterator(); tterator.hasNext();) {
047: final MetafacadeMapping.PropertyGroup propertyGroup = (MetafacadeMapping.PropertyGroup) tterator
048: .next();
049: for (final Iterator propertyIterator = propertyGroup
050: .getProperties().iterator(); propertyIterator
051: .hasNext();) {
052: final MetafacadeMapping.Property property = (MetafacadeMapping.Property) propertyIterator
053: .next();
054: valid = introspector.containsValidProperty(
055: metafacade, property.getName(),
056: property.getValue());
057: if (getLogger().isDebugEnabled()) {
058: getLogger().debug(
059: "property '" + property.getName()
060: + "', with value '"
061: + property.getValue()
062: + "' on metafacade '"
063: + metafacade
064: + "', evaluated to --> '"
065: + valid + "'");
066: }
067:
068: // - if the property is invalid, we break out
069: // of the loop (since we're evaluating with 'AND')
070: if (!valid) {
071: break;
072: }
073: }
074:
075: // - we break on the first true value since
076: // property groups are evaluated as 'OR'
077: if (valid) {
078: break;
079: }
080: }
081: } catch (final Throwable throwable) {
082: if (getLogger().isDebugEnabled()) {
083: getLogger()
084: .debug(
085: "An error occured while "
086: + "evaluating properties on metafacade '"
087: + metafacade
088: + "', setting valid to 'false'",
089: throwable);
090: }
091: valid = false;
092: }
093: if (getLogger().isDebugEnabled()) {
094: getLogger().debug(
095: "completed evaluating " + propertyGroups.size()
096: + " properties on metafacade '"
097: + metafacade
098: + "' with a result of --> '" + valid
099: + "'");
100: }
101: }
102: return valid;
103: }
104:
105: /**
106: * Constructs a new <code>metafacade</code> from the given
107: * <code>metafacadeClass</code> and <code>mappingObject</code>.
108: *
109: * @param metafacadeClass the metafacade class.
110: * @param mappingObject the object to which the metafacade is mapped.
111: * @return the new metafacade.
112: * @throws Exception if any error occurs during metafacade creation
113: */
114: static MetafacadeBase constructMetafacade(
115: final Class metafacadeClass, final Object mappingObject,
116: final String context) throws Exception {
117: if (getLogger().isDebugEnabled()) {
118: getLogger().debug(
119: "constructing metafacade from class '"
120: + metafacadeClass + "' mapping object '"
121: + mappingObject + "', and context '"
122: + context + "'");
123: }
124: final Constructor constructor = metafacadeClass
125: .getDeclaredConstructors()[0];
126: return (MetafacadeBase) constructor.newInstance(new Object[] {
127: mappingObject, context });
128: }
129:
130: /**
131: * Retrieves the inherited mapping class name for the given <code>mapping</code> by traveling
132: * up the inheritance hiearchy to find the first one that has the mapping class name declared.
133: *
134: * @param mapping the {@link MetafacadeMapping} instance for which we'll retrieve it's mapping class.
135: * @return the name of the mapping class.
136: */
137: public static String getInheritedMappingClassName(
138: final MetafacadeMapping mapping) {
139: final Class metafacadeClass = mapping.getMetafacadeClass();
140: final Collection interfaces = ClassUtils
141: .getAllInterfaces(metafacadeClass);
142: final MetafacadeImpls metafacadeImpls = MetafacadeImpls
143: .instance();
144: final Map mappingInstances = MetafacadeMappings
145: .getAllMetafacadeMappingInstances();
146: String className = null;
147: for (final Iterator iterator = interfaces.iterator(); iterator
148: .hasNext()
149: && className == null;) {
150: final String metafacadeInterface = ((Class) iterator.next())
151: .getName();
152: final Class metafacadeImplClass = metafacadeImpls
153: .getMetafacadeImplClass(metafacadeInterface);
154: className = (String) mappingInstances
155: .get(metafacadeImplClass);
156: }
157: if (className == null) {
158: throw new MetafacadeMappingsException(
159: "No mapping class could be found for '"
160: + metafacadeClass.getName() + "'");
161: }
162: return className;
163: }
164:
165: /**
166: * Indicates whether or not a metafacade model facade is present within the
167: * given namespace
168: *
169: * @param namespace the namespace to check.
170: * @return true/false
171: */
172: public static boolean isMetafacadeModelPresent(
173: final String namespace) {
174: boolean present = false;
175: if (ClassUtils.isClassOfTypePresent(Namespaces.instance()
176: .getResourceRoots(namespace), ModelAccessFacade.class)) {
177: present = true;
178: }
179: return present;
180: }
181:
182: /**
183: * Gets the logger instance which should be used for logging output within this class.
184: *
185: * @return the logger instance.
186: */
187: private static Logger getLogger() {
188: return MetafacadeFactory.getInstance().getLogger();
189: }
190: }
|