1 17 18 package org.sape.carbon.core.config.type.handlers; 19 20 import org.sape.carbon.core.component.Component; 21 import org.sape.carbon.core.component.ComponentConfiguration; 22 import org.sape.carbon.core.component.Lookup; 23 import org.sape.carbon.core.config.Configuration; 24 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor; 25 import org.sape.carbon.core.config.type.ComplexConfigurationTypeHandler; 26 import org.sape.carbon.core.config.type.NonCacheableTypeHandler; 27 import org.sape.carbon.core.config.type.TypeConversionException; 28 29 30 38 public class ComponentTypeHandler 39 implements ComplexConfigurationTypeHandler, NonCacheableTypeHandler { 40 41 50 public Configuration toConfiguration(Object objectValue) 51 throws TypeConversionException { 52 55 try { 56 ConfigurationInterceptor assistantView = 57 (ConfigurationInterceptor) objectValue; 58 59 return assistantView.getLiveConfiguration(); 60 61 } catch (ClassCastException cce) { 62 throw new TypeConversionException( 63 this.getClass(), 64 "Component [" 65 + ((Component) objectValue).getComponentName() 66 + "] does not have a ConfigurationInterceptor, " 67 + "one must be configured in its component template " 68 + "in order to set Component types in Configurations", 69 cce); 70 } 71 72 } 73 74 84 public Object toObject(Class returnType, Configuration configuration) 85 throws TypeConversionException { 86 87 Component component = null; 88 89 if (configuration != null) { 90 component = 91 Lookup.getInstance().fetchComponent( 92 configuration.getConfigurationName()); 93 94 if (!returnType.isAssignableFrom(component.getClass())) { 95 throw new TypeConversionException( 96 this.getClass(), 97 "Component defined by configuraion [" 98 + configuration.getConfigurationName() 99 + "] was incorrect type: expected [" 100 + returnType.getName() 101 + "], actual FunctionalInterface: [" 102 + ((ComponentConfiguration) configuration) 103 .getFunctionalInterface() 104 .getName() 105 + "]."); 106 } 107 } 108 109 return component; 110 111 } 112 115 public Class getRequiredConfigurationInterface() { 116 return ComponentConfiguration.class; 117 } 118 119 } 120 | Popular Tags |