KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > TemplateServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.io.StringWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.alfresco.service.cmr.repository.TemplateException;
26 import org.alfresco.service.cmr.repository.TemplateProcessor;
27 import org.alfresco.service.cmr.repository.TemplateService;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.beans.BeansException;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.ApplicationContextAware;
33
34 /**
35  * @author Kevin Roast
36  */

37 public class TemplateServiceImpl implements TemplateService, ApplicationContextAware
38 {
39     private static Log logger = LogFactory.getLog(TemplateService.class);
40     
41     /** Spring ApplicationContext for bean lookup by ID */
42     private ApplicationContext applicationContext;
43     
44     /** Default Template processor engine to use */
45     private String JavaDoc defaultTemplateEngine;
46     
47     /** Available template engine names to impl class names */
48     private Map JavaDoc<String JavaDoc, String JavaDoc> templateEngines;
49     
50     /** Threadlocal instance for template processor cache */
51     private static ThreadLocal JavaDoc<Map JavaDoc<String JavaDoc, TemplateProcessor>> processors = new ThreadLocal JavaDoc();
52     
53     /**
54      * Set the application context
55      *
56      * @param applicationContext the application context
57      */

58     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
59     {
60         this.applicationContext = applicationContext;
61     }
62     
63     /**
64      * @param defaultTemplateEngine The default Template Engine name to set.
65      */

66     public void setDefaultTemplateEngine(String JavaDoc defaultTemplateEngine)
67     {
68         this.defaultTemplateEngine = defaultTemplateEngine;
69     }
70
71     /**
72      * @param templateEngines The Map of template engine name to impl class name to set.
73      */

74     public void setTemplateEngines(Map JavaDoc<String JavaDoc, String JavaDoc> templateEngines)
75     {
76         this.templateEngines = templateEngines;
77     }
78
79     /**
80      * @see org.alfresco.service.cmr.repository.TemplateService#getTemplateProcessor(java.lang.String)
81      */

82     public TemplateProcessor getTemplateProcessor(String JavaDoc engine)
83     {
84         try
85         {
86             return getTemplateProcessorImpl(engine);
87         }
88         catch (Throwable JavaDoc err)
89         {
90             if (logger.isDebugEnabled())
91                 logger.debug("Unable to load template processor.", err);
92             
93             return null;
94         }
95     }
96
97     /**
98      * @see org.alfresco.service.cmr.repository.TemplateService#processTemplate(java.lang.String, java.lang.String, java.lang.Object, java.io.Writer)
99      */

100     public void processTemplate(String JavaDoc engine, String JavaDoc template, Object JavaDoc model, Writer JavaDoc out)
101         throws TemplateException
102     {
103         try
104         {
105            // execute template processor
106
TemplateProcessor processor = getTemplateProcessorImpl(engine);
107            processor.process(template, model, out);
108         }
109         catch (TemplateException terr)
110         {
111            throw terr;
112         }
113         catch (Throwable JavaDoc err)
114         {
115            throw new TemplateException(err.getMessage(), err);
116         }
117     }
118
119     /**
120      * @see org.alfresco.service.cmr.repository.TemplateService#processTemplate(java.lang.String, java.lang.String, java.lang.Object)
121      */

122     public String JavaDoc processTemplate(String JavaDoc engine, String JavaDoc template, Object JavaDoc model)
123         throws TemplateException
124     {
125         Writer JavaDoc out = new StringWriter JavaDoc(1024);
126         processTemplate(engine, template, model, out);
127         return out.toString();
128     }
129
130     /**
131      * Return the TemplateProcessor implementation for the named template engine
132      *
133      * @param name Template Engine name
134      *
135      * @return TemplateProcessor
136      */

137     private TemplateProcessor getTemplateProcessorImpl(String JavaDoc name)
138     {
139         // use the ThreadLocal map to find the processors instance
140
// create the cache map for this thread if required
141
Map JavaDoc<String JavaDoc, TemplateProcessor> procMap = processors.get();
142         if (procMap == null)
143         {
144             procMap = new HashMap JavaDoc<String JavaDoc, TemplateProcessor>(7, 1.0f);
145             processors.set(procMap);
146         }
147         
148         if (name == null)
149         {
150             name = defaultTemplateEngine;
151         }
152         
153         // find the impl for the named processor
154
TemplateProcessor processor = procMap.get(name);
155         if (processor == null)
156         {
157             String JavaDoc className = templateEngines.get(name);
158             if (className == null)
159             {
160                 throw new AlfrescoRuntimeException("Unable to find configured ClassName for template engine: " + name);
161             }
162             try
163             {
164                 Object JavaDoc obj;
165                 try
166                 {
167                     obj = this.applicationContext.getBean(className);
168                 }
169                 catch (BeansException err)
170                 {
171                     // instantiate the processor class directory if not a Spring bean
172
obj = Class.forName(className).newInstance();
173                 }
174                 
175                 if (obj instanceof TemplateProcessor)
176                 {
177                     processor = (TemplateProcessor)obj;
178                 }
179                 else
180                 {
181                     throw new AlfrescoRuntimeException("Supplied template processors does not implement TemplateProcessor: " + className);
182                 }
183             }
184             catch (ClassNotFoundException JavaDoc err1)
185             {
186                 // if the bean is not a classname, then it may be a spring bean Id
187
throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err1);
188             }
189             catch (IllegalAccessException JavaDoc err2)
190             {
191                 throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err2);
192             }
193             catch (InstantiationException JavaDoc err3)
194             {
195                 throw new AlfrescoRuntimeException("Unable to instantiate class for supplied template processors: " + className, err3);
196             }
197             
198             // cache for later
199
procMap.put(name, processor);
200         }
201         
202         return processor;
203     }
204 }
205
Popular Tags