KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ComponentConstructorFactoryImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.service.ClassFactory;
23 import org.apache.tapestry.enhance.EnhancedClassValidator;
24 import org.apache.tapestry.enhance.EnhancementOperationImpl;
25 import org.apache.tapestry.enhance.EnhancementWorker;
26 import org.apache.tapestry.event.ResetEventListener;
27 import org.apache.tapestry.services.ComponentConstructor;
28 import org.apache.tapestry.services.ComponentConstructorFactory;
29 import org.apache.tapestry.spec.IComponentSpecification;
30
31 /**
32  * Implementation of the {@link org.apache.tapestry.services.ComponentConstructorFactory}service
33  * interface.
34  *
35  * @author Howard M. Lewis Ship
36  * @since 4.0
37  */

38 public class ComponentConstructorFactoryImpl implements ComponentConstructorFactory,
39         ResetEventListener
40 {
41     private ClassFactory _classFactory;
42
43     private ClassResolver _classResolver;
44
45     private EnhancedClassValidator _validator;
46
47     private EnhancementWorker _chain;
48
49     /**
50      * Map of {@link org.apache.tapestry.services.ComponentConstructor}keyed on
51      * {@link org.apache.tapestry.spec.IComponentSpecification}.
52      */

53
54     private Map JavaDoc _cachedConstructors = Collections.synchronizedMap(new HashMap JavaDoc());
55
56     public void resetEventDidOccur()
57     {
58         _cachedConstructors.clear();
59     }
60
61     public ComponentConstructor getComponentConstructor(IComponentSpecification specification,
62             String JavaDoc className)
63     {
64         ComponentConstructor result = (ComponentConstructor) _cachedConstructors.get(specification);
65
66         if (result == null)
67         {
68             Class JavaDoc baseClass = _classResolver.findClass(className);
69
70             EnhancementOperationImpl eo = new EnhancementOperationImpl(_classResolver,
71                     specification, baseClass, _classFactory);
72
73             // Invoking on the chain is the same as invoking on every
74
// object in the chain (because method performEnhancement() is type void).
75

76             _chain.performEnhancement(eo, specification);
77
78             result = eo.getConstructor();
79
80             // TODO: This should be optional to work around that IBM JVM bug.
81
// Also, to some degree, it should be passed into EnhancementOperationImpl,
82
// as it generally only needs to be done if a enhanced class
83
// is fabricated.
84

85             _validator.validate(baseClass, result.getComponentClass(), specification);
86
87             _cachedConstructors.put(specification, result);
88         }
89
90         return result;
91     }
92
93     public void setClassFactory(ClassFactory classFactory)
94     {
95         _classFactory = classFactory;
96     }
97
98     public void setClassResolver(ClassResolver classResolver)
99     {
100         _classResolver = classResolver;
101     }
102
103     public void setValidator(EnhancedClassValidator validator)
104     {
105         _validator = validator;
106     }
107
108     public void setChain(EnhancementWorker chain)
109     {
110         _chain = chain;
111     }
112 }
Popular Tags