001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.mda;
024:
025: import java.io.IOException;
026: import java.util.Map;
027:
028: import biz.hammurapi.config.ConfigurationException;
029: import biz.hammurapi.rules.Rule;
030: import biz.hammurapi.web.HammurapiWebException;
031: import biz.hammurapi.web.eval.EvaluationResult;
032: import biz.hammurapi.web.eval.EvaluatorFactory;
033: import biz.hammurapi.web.eval.ResourceLocator;
034:
035: /**
036: * Base for code generation rules.
037: * This rule looks up resource locator, evaluator factory and channel factory in
038: * rule session attributes. It also contains utility methods to render objects/contexts
039: * using given template to given channel.
040: * @author Pavel
041: *
042: */
043: public class MdaRule extends Rule {
044:
045: private ResourceLocator resourceLocator;
046: private EvaluatorFactory evaluatorFactory;
047: private RootChannel rootChannel;
048:
049: protected RootChannel getRootChannel() {
050: return rootChannel;
051: }
052:
053: protected EvaluatorFactory getEvaluatorFactory() {
054: return evaluatorFactory;
055: }
056:
057: protected ResourceLocator getResourceLocator() {
058: return resourceLocator;
059: }
060:
061: public void start() throws ConfigurationException {
062: super .start();
063: resourceLocator = (ResourceLocator) get("/@resource-locator");
064: if (resourceLocator == null) {
065: throw new ConfigurationException(
066: "Resource locator not found");
067: }
068:
069: evaluatorFactory = (EvaluatorFactory) get("/@evaluator-factory");
070: if (evaluatorFactory == null) {
071: throw new ConfigurationException(
072: "Evaluator factory not found");
073: }
074:
075: rootChannel = (RootChannel) get("/@root-channel");
076: if (rootChannel == null) {
077: throw new ConfigurationException("Root channel not found");
078: }
079:
080: }
081:
082: /**
083: * Renders context to channel writer using template
084: * @param context
085: * @param template
086: * @param channel
087: * @throws HammurapiWebException
088: */
089: protected void render(Map context, String template, String[] path)
090: throws HammurapiWebException {
091: EvaluationResult result = evaluatorFactory.evaluate(template,
092: context, getClass().getClassLoader(), resourceLocator);
093:
094: if (result != null) {
095: try {
096: rootChannel.getWriter(path).write(result.getOutput());
097: } catch (IOException e) {
098: throw new HammurapiWebException(
099: "Output to channel failed: " + e, e);
100: }
101: }
102: }
103:
104: }
|