001: package nl.hippo.slide.replication;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.InputStream;
007: import java.util.Enumeration;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import org.apache.avalon.framework.configuration.Configuration;
013: import org.apache.avalon.framework.configuration.ConfigurationException;
014: import org.apache.avalon.framework.logger.AbstractLogEnabled;
015: import org.apache.slide.content.NodeRevisionContent;
016: import org.apache.slide.content.NodeRevisionDescriptor;
017: import org.mozilla.javascript.Context;
018: import org.mozilla.javascript.Function;
019: import org.mozilla.javascript.Scriptable;
020:
021: public class JavascriptRepositoryLocation extends AbstractLogEnabled
022: implements ReplicatorLocation {
023:
024: protected File script = null;
025: protected Map params = new HashMap();
026:
027: public void put(String uri, NodeRevisionDescriptor descriptor,
028: NodeRevisionContent content) {
029: InputStream contentStream = null;
030: if (content != null) {
031: contentStream = new ByteArrayInputStream(content
032: .getContentBytes());
033: }
034: callFunctionFromScript("put", uri, contentStream, descriptor
035: .enumerateProperties(), descriptor
036: .enumerateRemovedProperties());
037: }
038:
039: public void mkcol(String uri, NodeRevisionDescriptor descriptor) {
040: callFunctionFromScript("mkcol", uri, null, descriptor
041: .enumerateProperties(), descriptor
042: .enumerateRemovedProperties());
043: }
044:
045: public void delete(String uri) {
046: callFunctionFromScript("remove", uri, null, null, null);
047: }
048:
049: private void callFunctionFromScript(String name, String uri,
050: InputStream content, Enumeration properties,
051: Enumeration removedproperties) {
052: Context cx = Context.enter();
053:
054: try {
055: Scriptable scope = cx.initStandardObjects();
056:
057: scope.put("logger", scope, Context.javaToJS(getLogger()
058: .getChildLogger("js"), scope));
059:
060: Iterator names = params.keySet().iterator();
061: while (names.hasNext()) {
062: String param = (String) names.next();
063: scope.put(param, scope, this .params.get(param));
064: }
065:
066: cx.evaluateReader(scope, new FileReader(script), script
067: .getAbsolutePath(), 1, null);
068:
069: Object function = scope.get(name, scope);
070: if (!(function instanceof Function)) {
071: getLogger().error(
072: "Error executing script! " + name
073: + " has to be a function!");
074: } else {
075:
076: Object args[];
077: if (name.equals("remove")) {
078: args = new Object[] { uri };
079: } else if (name.equals("put")) {
080: args = new Object[] { uri,
081: Context.javaToJS(content, scope),
082: properties, removedproperties };
083: } else {
084: args = new Object[] { uri, properties,
085: removedproperties };
086: }
087:
088: Function func = (Function) function;
089:
090: func.call(cx, scope, scope, args);
091:
092: }
093:
094: } catch (Exception e) {
095:
096: getLogger().error(
097: "Error executing script! Function " + name
098: + " not called!", e);
099:
100: } finally {
101: Context.exit();
102: }
103: }
104:
105: public void configure(Configuration config)
106: throws ConfigurationException {
107: String src = config.getChild("script").getValue();
108:
109: try {
110: script = new File(src);
111:
112: if (!script.exists()) {
113: throw new ConfigurationException(
114: "Script file doesn't exist! " + src);
115: }
116:
117: } catch (Exception e) {
118: throw new ConfigurationException("Can't open script: "
119: + src, e);
120: }
121:
122: Configuration[] params = config.getChildren("parameter");
123: for (int i = 0; i < params.length; i++) {
124:
125: String name = params[i].getAttribute("name");
126: Object value = params[i].getAttribute("value");
127: String type = params[i].getAttribute("type", "string");
128:
129: if ("int".equals(type)) {
130: try {
131: value = new Integer((String) value);
132: } catch (Exception e) {
133: getLogger()
134: .error(
135: "Can't convert '"
136: + value
137: + "' to int! Assuming string type.",
138: e);
139: }
140: } else if ("boolean".equals(type)) {
141: try {
142: value = Boolean.valueOf((String) value);
143: } catch (Exception e) {
144: getLogger()
145: .error(
146: "Can't convert '"
147: + value
148: + "' to boolean! Assuming string type.",
149: e);
150: }
151: }
152:
153: this.params.put(name, value);
154: }
155: }
156: }
|