KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ClassResolver;
21 import org.apache.tapestry.IEngine;
22 import org.apache.tapestry.services.EngineFactory;
23 import org.apache.tapestry.spec.IApplicationSpecification;
24
25 /**
26  * Standard implementation of {@link org.apache.tapestry.services.EngineFactory} service.
27  * This should do for most purposes, since a major focus of Tapestry 4.0 is to no longer
28  * require subclassing of {@link org.apache.tapestry.engine.BaseEngine}.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class EngineFactoryImpl implements EngineFactory
34 {
35     private IApplicationSpecification _applicationSpecification;
36     private String JavaDoc _defaultEngineClassName;
37     private EngineConstructor _constructor;
38     private ClassResolver _classResolver;
39
40     interface EngineConstructor
41     {
42         IEngine construct();
43     }
44
45
46     // TODO: Create a BaseEngineConstructor that is hardcoded to
47
// instantiate a BaseEngine instance, without using reflection
48
// (for efficiency).
49

50     static class ReflectiveEngineConstructor implements EngineConstructor
51     {
52         private Class JavaDoc _engineClass;
53
54         ReflectiveEngineConstructor(Class JavaDoc engineClass)
55         {
56             _engineClass = engineClass;
57         }
58
59         public IEngine construct()
60         {
61             try
62             {
63                 return (IEngine) _engineClass.newInstance();
64             }
65             catch (Exception JavaDoc ex)
66             {
67                 throw new ApplicationRuntimeException(
68                     ImplMessages.errorInstantiatingEngine(_engineClass, ex),
69                     ex);
70             }
71         }
72     }
73
74     public void initializeService()
75     {
76         String JavaDoc engineClassName = _applicationSpecification.getEngineClassName();
77
78         // TODO: Check in web.xml first.
79

80         if (engineClassName == null)
81             engineClassName = _defaultEngineClassName;
82
83         Class JavaDoc engineClass = _classResolver.findClass(engineClassName);
84
85         _constructor = new ReflectiveEngineConstructor(engineClass);
86     }
87
88     public IEngine constructNewEngineInstance(Locale JavaDoc locale)
89     {
90         IEngine result = _constructor.construct();
91
92         result.setLocale(locale);
93
94         return result;
95     }
96
97     public void setApplicationSpecification(IApplicationSpecification specification)
98     {
99         _applicationSpecification = specification;
100     }
101
102     public void setClassResolver(ClassResolver resolver)
103     {
104         _classResolver = resolver;
105     }
106
107     public void setDefaultEngineClassName(String JavaDoc string)
108     {
109         _defaultEngineClassName = string;
110     }
111
112 }
113
Popular Tags