KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > type > handlers > ComponentTypeHandler


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

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 /**
31  * <P>Parses and formats between Configurations and Components.</P>
32  *
33  * Copyright 2002 Sapient
34  * @since carbon 1.0
35  * @author Douglas Voet, September 2002
36  * @version $Revision: 1.19 $($Author: dvoet $ / $Date: 2003/07/29 19:54:45 $)
37  */

38 public class ComponentTypeHandler
39     implements ComplexConfigurationTypeHandler, NonCacheableTypeHandler {
40
41     /**
42      * <P>Formats a Component into a Configuration object.</P>
43      *
44      * @param objectValue the Component to be formatted
45      * @return the configuration for the given component
46      * @throws ClassCastException when past an object other than a Component
47      * @throws TypeConversionException if the Component does not have
48      * a <code>ConfigurationInterceptor</code>
49      */

50     public Configuration toConfiguration(Object JavaDoc objectValue)
51         throws TypeConversionException {
52         // assume that objectValue is a component and a ConfigurationInterceptor
53
// is configured
54

55         try {
56             ConfigurationInterceptor assistantView =
57                 (ConfigurationInterceptor) objectValue;
58
59             return assistantView.getLiveConfiguration();
60
61         } catch (ClassCastException JavaDoc 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     /**
75      * <P>Parses a Configuration object into a Component object.</P>
76      *
77      * @param returnType the type of the object that should be returned
78      * @param configuration the config object holding the data to be used in
79      * creating the object.
80      * @return Component specified in the configuration
81      * @throws TypeConversionException when the string does not represent
82      * a valid Component
83      */

84     public Object JavaDoc toObject(Class JavaDoc 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     /**
113      * @see org.sape.carbon.core.config.type.ComplexConfigurationTypeHandler#getRequiredConfigurationInterface()
114      */

115     public Class JavaDoc getRequiredConfigurationInterface() {
116         return ComponentConfiguration.class;
117     }
118
119 }
120
Popular Tags