001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Janino2ElementInfo.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.engine.exceptions.EngineException;
012: import com.uwyn.rife.engine.exceptions.NotFoundProcessingErrorException;
013: import com.uwyn.rife.engine.exceptions.ParsingErrorException;
014: import com.uwyn.rife.engine.exceptions.ProcessingErrorException;
015: import com.uwyn.rife.resources.ResourceFinder;
016: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
017: import com.uwyn.rife.tools.InnerClassException;
018: import com.uwyn.rife.tools.InputStreamUser;
019: import java.io.InputStream;
020: import java.lang.reflect.InvocationTargetException;
021: import java.net.URL;
022: import org.codehaus.janino.Scanner;
023: import org.codehaus.janino.ScriptEvaluator;
024:
025: class Janino2ElementInfo implements ElementInfoProcessor {
026: Janino2ElementInfo() {
027: }
028:
029: public void processElementInfo(ElementInfoBuilder builder,
030: String declarationName, ResourceFinder resourceFinder)
031: throws EngineException {
032: JaninoProcessor processor = new JaninoProcessor(builder);
033:
034: String processed_path = null;
035: try {
036: // process the element declaration
037: try {
038: processed_path = declarationName;
039: processor.processJanino(processed_path, resourceFinder);
040: } catch (NotFoundProcessingErrorException e) {
041: processed_path = DEFAULT_ELEMENTS_PATH
042: + declarationName;
043: processor.processJanino(processed_path, resourceFinder);
044: }
045: } catch (Exception e) {
046: throw new ProcessingErrorException("element",
047: declarationName, e);
048: }
049:
050: // obtain the modification time
051: if (RifeConfig.Engine.getSiteAutoReload()) {
052: URL resource = resourceFinder.getResource(processed_path);
053: if (null == resource) {
054: throw new NotFoundProcessingErrorException("element",
055: processed_path, null);
056: }
057:
058: try {
059: builder.addResourceModificationTime(new UrlResource(
060: resource, processed_path), resourceFinder
061: .getModificationTime(resource));
062: } catch (ResourceFinderErrorException e) {
063: throw new ProcessingErrorException(
064: "element",
065: declarationName,
066: "Error while retrieving the modification time.",
067: e);
068: }
069: }
070: }
071:
072: private class JaninoProcessor {
073: private ElementInfoBuilder mElementInfoBuilder = null;
074:
075: private JaninoProcessor(ElementInfoBuilder builder) {
076: mElementInfoBuilder = builder;
077: }
078:
079: public synchronized void processJanino(final String janinoPath,
080: ResourceFinder resourceFinder) {
081: if (null == janinoPath)
082: throw new IllegalArgumentException(
083: "janinoPath can't be null.");
084: if (janinoPath.length() == 0)
085: throw new IllegalArgumentException(
086: "janinoPath can't be empty.");
087: if (null == resourceFinder)
088: throw new IllegalArgumentException(
089: "resourceFinder can't be null.");
090:
091: // retrieve a stream towards the janino script
092: ScriptEvaluator evaluator = null;
093: try {
094: evaluator = resourceFinder.useStream(janinoPath,
095: new InputStreamUser() {
096: public ScriptEvaluator useInputStream(
097: InputStream stream)
098: throws InnerClassException {
099: if (null == stream) {
100: throw new NotFoundProcessingErrorException(
101: "element", janinoPath, null);
102: }
103:
104: // parse the janino script and create an evaluator
105: try {
106: return new ScriptEvaluator(
107: new Scanner(janinoPath,
108: stream),
109: Void.TYPE,
110: new String[] { "builder" },
111: new Class[] { ElementInfoBuilder.class },
112: new Class[0], getClass()
113: .getClassLoader());
114: } catch (Throwable e) {
115: throw new ParsingErrorException(
116: "element", janinoPath, e);
117: }
118: }
119: });
120: } catch (ResourceFinderErrorException e) {
121: throw new NotFoundProcessingErrorException("element",
122: janinoPath, e);
123: }
124:
125: // evaluate the script and provide the parameter
126: try {
127: evaluator
128: .evaluate(new Object[] { mElementInfoBuilder });
129: } catch (InvocationTargetException e) {
130: throw new ProcessingErrorException("element",
131: janinoPath, e);
132: }
133: }
134: }
135: }
|