01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ScriptLoaderJanino.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.engine;
09:
10: import com.uwyn.rife.engine.exceptions.ElementImplementationInstantiationException;
11: import com.uwyn.rife.engine.exceptions.ElementImplementationNotFoundException;
12: import com.uwyn.rife.engine.exceptions.EngineException;
13: import com.uwyn.rife.resources.ResourceFinder;
14: import java.io.InputStream;
15: import java.net.URL;
16: import java.net.URLConnection;
17: import java.util.HashMap;
18: import java.util.Map;
19: import org.codehaus.janino.ClassBodyEvaluator;
20: import org.codehaus.janino.Scanner;
21:
22: class ScriptLoaderJanino {
23: private ResourceFinder mResourceFinder = null;
24: private Map<String, Long> mScriptsModification = new HashMap<String, Long>();
25: private Map<String, Class> mScriptsCache = new HashMap<String, Class>();
26:
27: ScriptLoaderJanino(ResourceFinder resourceFinder) {
28: mResourceFinder = resourceFinder;
29: }
30:
31: ElementAware getInstance(ElementInfo elementInfo)
32: throws EngineException {
33: try {
34: URL resource = mResourceFinder.getResource(elementInfo
35: .getImplementation());
36: long current_modification = -1;
37: if (resource != null) {
38: current_modification = mResourceFinder
39: .getModificationTime(elementInfo
40: .getImplementation());
41: } else {
42: resource = mResourceFinder
43: .getResource(EngineClassLoader.DEFAULT_IMPLEMENTATIONS_PATH
44: + elementInfo.getImplementation());
45: current_modification = mResourceFinder
46: .getModificationTime(EngineClassLoader.DEFAULT_IMPLEMENTATIONS_PATH
47: + elementInfo.getImplementation());
48: }
49: if (null == resource) {
50: throw new ElementImplementationNotFoundException(
51: elementInfo.getDeclarationName(), elementInfo
52: .getImplementation(), null);
53: }
54:
55: String implementation = elementInfo.getImplementation();
56: Long previous_modification = mScriptsModification
57: .get(implementation);
58:
59: Class element_class = null;
60: if (mScriptsCache.containsKey(implementation)
61: && (null == previous_modification || previous_modification
62: .longValue() == current_modification)) {
63: element_class = mScriptsCache.get(implementation);
64: } else {
65: try {
66: URL script_resource = ElementFactory.getScriptUrl(
67: mResourceFinder, elementInfo);
68: URLConnection connection = script_resource
69: .openConnection();
70: connection.setUseCaches(false);
71: InputStream script_stream = connection
72: .getInputStream();
73:
74: element_class = new ClassBodyEvaluator(new Scanner(
75: null, script_stream), Element.class,
76: new Class[0], getClass().getClassLoader())
77: .getClazz();
78: } catch (Throwable e) {
79: throw new ElementImplementationInstantiationException(
80: elementInfo.getDeclarationName(),
81: elementInfo.getImplementation(), e);
82: }
83:
84: if (null == previous_modification
85: || previous_modification.longValue() != current_modification) {
86: mScriptsModification.put(implementation,
87: current_modification);
88: mScriptsCache.put(implementation, element_class);
89: }
90: }
91:
92: return (ElementAware) element_class.newInstance();
93: } catch (Throwable e) {
94: throw new ElementImplementationInstantiationException(
95: elementInfo.getDeclarationName(), elementInfo
96: .getImplementation(), e);
97: }
98: }
99: }
|