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: Janino2Site.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 Janino2Site implements SiteProcessor {
026: Janino2Site() {
027: }
028:
029: public void processSite(SiteBuilder 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 site declaration
037: try {
038: processed_path = declarationName;
039: processor.processJanino(processed_path, resourceFinder);
040: } catch (NotFoundProcessingErrorException e) {
041: processed_path = DEFAULT_SITES_PATH + declarationName;
042: processor.processJanino(processed_path, resourceFinder);
043: }
044: } catch (Exception e) {
045: throw new ProcessingErrorException("site", declarationName,
046: e);
047: }
048:
049: // obtain the modification time
050: if (RifeConfig.Engine.getSiteAutoReload()) {
051: URL resource = resourceFinder.getResource(processed_path);
052: if (null == resource) {
053: throw new NotFoundProcessingErrorException("site",
054: processed_path, null);
055: }
056:
057: try {
058: builder.addResourceModificationTime(new UrlResource(
059: resource, processed_path), resourceFinder
060: .getModificationTime(resource));
061: } catch (ResourceFinderErrorException e) {
062: throw new ProcessingErrorException(
063: "site",
064: declarationName,
065: "Error while retrieving the modification time.",
066: e);
067: }
068: }
069: }
070:
071: private class JaninoProcessor {
072: private SiteBuilder mSiteBuilder = null;
073:
074: private JaninoProcessor(SiteBuilder builder) {
075: mSiteBuilder = builder;
076: }
077:
078: public synchronized void processJanino(final String janinoPath,
079: ResourceFinder resourceFinder) {
080: if (null == janinoPath)
081: throw new IllegalArgumentException(
082: "janinoPath can't be null.");
083: if (janinoPath.length() == 0)
084: throw new IllegalArgumentException(
085: "janinoPath can't be empty.");
086: if (null == resourceFinder)
087: throw new IllegalArgumentException(
088: "resourceFinder can't be null.");
089:
090: // retrieve a stream towards the janino script
091: ScriptEvaluator evaluator = null;
092: try {
093: evaluator = resourceFinder.useStream(janinoPath,
094: new InputStreamUser() {
095: public ScriptEvaluator useInputStream(
096: InputStream stream)
097: throws InnerClassException {
098: if (null == stream) {
099: throw new NotFoundProcessingErrorException(
100: "site", janinoPath, null);
101: }
102:
103: // parse the janino script and create an evaluator
104: try {
105: return new ScriptEvaluator(
106: new Scanner(janinoPath,
107: stream),
108: Void.TYPE,
109: new String[] { "builder" },
110: new Class[] { SiteBuilder.class },
111: new Class[0], getClass()
112: .getClassLoader());
113: } catch (Throwable e) {
114: throw new ParsingErrorException(
115: "site", janinoPath, e);
116: }
117: }
118: });
119: } catch (ResourceFinderErrorException e) {
120: throw new NotFoundProcessingErrorException("site",
121: janinoPath, e);
122: }
123:
124: // evaluate the script and provide the parameter
125: try {
126: evaluator.evaluate(new Object[] { mSiteBuilder });
127: } catch (InvocationTargetException e) {
128: throw new ProcessingErrorException("site", janinoPath,
129: e);
130: }
131: }
132: }
133: }
|