001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.core.config.type.handlers;
019:
020: import org.sape.carbon.core.component.Component;
021: import org.sape.carbon.core.component.ComponentConfiguration;
022: import org.sape.carbon.core.component.Lookup;
023: import org.sape.carbon.core.config.Configuration;
024: import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor;
025: import org.sape.carbon.core.config.type.ComplexConfigurationTypeHandler;
026: import org.sape.carbon.core.config.type.NonCacheableTypeHandler;
027: import org.sape.carbon.core.config.type.TypeConversionException;
028:
029: /**
030: * <P>Parses and formats between Configurations and Components.</P>
031: *
032: * Copyright 2002 Sapient
033: * @since carbon 1.0
034: * @author Douglas Voet, September 2002
035: * @version $Revision: 1.19 $($Author: dvoet $ / $Date: 2003/07/29 19:54:45 $)
036: */
037: public class ComponentTypeHandler implements
038: ComplexConfigurationTypeHandler, NonCacheableTypeHandler {
039:
040: /**
041: * <P>Formats a Component into a Configuration object.</P>
042: *
043: * @param objectValue the Component to be formatted
044: * @return the configuration for the given component
045: * @throws ClassCastException when past an object other than a Component
046: * @throws TypeConversionException if the Component does not have
047: * a <code>ConfigurationInterceptor</code>
048: */
049: public Configuration toConfiguration(Object objectValue)
050: throws TypeConversionException {
051: // assume that objectValue is a component and a ConfigurationInterceptor
052: // is configured
053:
054: try {
055: ConfigurationInterceptor assistantView = (ConfigurationInterceptor) objectValue;
056:
057: return assistantView.getLiveConfiguration();
058:
059: } catch (ClassCastException cce) {
060: throw new TypeConversionException(
061: this .getClass(),
062: "Component ["
063: + ((Component) objectValue)
064: .getComponentName()
065: + "] does not have a ConfigurationInterceptor, "
066: + "one must be configured in its component template "
067: + "in order to set Component types in Configurations",
068: cce);
069: }
070:
071: }
072:
073: /**
074: * <P>Parses a Configuration object into a Component object.</P>
075: *
076: * @param returnType the type of the object that should be returned
077: * @param configuration the config object holding the data to be used in
078: * creating the object.
079: * @return Component specified in the configuration
080: * @throws TypeConversionException when the string does not represent
081: * a valid Component
082: */
083: public Object toObject(Class returnType, Configuration configuration)
084: throws TypeConversionException {
085:
086: Component component = null;
087:
088: if (configuration != null) {
089: component = Lookup.getInstance().fetchComponent(
090: configuration.getConfigurationName());
091:
092: if (!returnType.isAssignableFrom(component.getClass())) {
093: throw new TypeConversionException(
094: this .getClass(),
095: "Component defined by configuraion ["
096: + configuration.getConfigurationName()
097: + "] was incorrect type: expected ["
098: + returnType.getName()
099: + "], actual FunctionalInterface: ["
100: + ((ComponentConfiguration) configuration)
101: .getFunctionalInterface()
102: .getName() + "].");
103: }
104: }
105:
106: return component;
107:
108: }
109:
110: /**
111: * @see org.sape.carbon.core.config.type.ComplexConfigurationTypeHandler#getRequiredConfigurationInterface()
112: */
113: public Class getRequiredConfigurationInterface() {
114: return ComponentConfiguration.class;
115: }
116:
117: }
|