KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > interceptor > ConfigurationInterceptorFactory


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.interceptor;
19
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.sape.carbon.core.component.ComponentConfiguration;
27 import org.sape.carbon.core.component.FunctionalInterface;
28 import org.sape.carbon.core.component.proxy.ComponentProxyInvocationHandler;
29 import org.sape.carbon.core.component.proxy.Decorator;
30 import org.sape.carbon.core.component.proxy.DecoratorFactory;
31 import org.sape.carbon.core.component.proxy.Interceptor;
32 import org.sape.carbon.core.config.Config;
33 import org.sape.carbon.core.config.Configuration;
34 import org.sape.carbon.core.config.InvalidConfigurationException;
35
36 /**
37  * <P>This factory builds
38  * {@link org.sape.carbon.core.config.interceptor.ConfigurationInterceptor}s that
39  * manage configurations for Carbon Components.
40  * </P>
41  *
42  * Copyright 2002 Sapient
43  * @since carbon 1.0
44  * @author Greg Hinkle, January 2002
45  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/10/27 19:54:41 $)
46  */

47 public class ConfigurationInterceptorFactory implements DecoratorFactory {
48
49     /**
50      * <p>Constructs a component interceptor. The parameters of this method
51      * encompass all the information known about a component at the time
52      * it is constructed.</p>
53      *
54      * @param functionalImplementation The functional implementation of the
55      * component. This is supplied for cases where the interceptor needs to
56      * access the functional implementation object directly.
57      * @param componentConfiguration configuartion of the component which being
58      * built
59      * @param proxyInvocationHandler the invocation handler object that backs
60      * the component that this interceptor is being built for
61      * @param decoratorConfiguration a custom configuration object for the
62      * interceptor
63      * @return The <CODE>Decorator</CODE> that was built by this factory
64      */

65     public Decorator getInstance(
66         FunctionalInterface functionalImplementation,
67         ComponentConfiguration componentConfiguration,
68         ComponentProxyInvocationHandler proxyInvocationHandler,
69         Configuration decoratorConfiguration) {
70
71         try {
72             ConfigurationInterceptorConfiguration config;
73             if (decoratorConfiguration == null) {
74                 config = (ConfigurationInterceptorConfiguration)
75                     Config.getInstance().createConfiguration(
76                     ConfigurationInterceptorConfiguration.class);
77             } else {
78                 config = (ConfigurationInterceptorConfiguration)
79                     decoratorConfiguration;
80             }
81
82             ConfigurationInterceptor configurationInterceptor =
83                 new DefaultConfigurationInterceptor(
84                     componentConfiguration,
85                     proxyInvocationHandler,
86                     config);
87
88             List JavaDoc interfaces = new ArrayList JavaDoc();
89
90             interfaces.addAll(
91                 Arrays.asList(
92                     ((Decorator) configurationInterceptor)
93                         .getExposedInterfaces()));
94
95             // Must be recognizable as an Interceptor after being built or it won't
96
// be added to the invocation chain.
97
interfaces.add(Interceptor.class);
98
99             Decorator decoratorProxy =
100                 (Decorator) Proxy.newProxyInstance(
101                     this.getClass().getClassLoader(),
102                     (Class JavaDoc[]) interfaces.toArray(new Class JavaDoc[interfaces.size()]),
103                     (InvocationHandler JavaDoc) configurationInterceptor);
104
105             return decoratorProxy;
106         } catch (ClassCastException JavaDoc cce) {
107             throw new InvalidConfigurationException(
108                 this.getClass(),
109                 decoratorConfiguration.getConfigurationName(),
110                 "ConfigurationInterface",
111                 "Configuration must be of type "
112                     + ConfigurationInterceptorConfiguration.class.getName(),
113                 cce);
114         }
115
116     }
117 }
118
Popular Tags