001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import groovy.lang.GroovyClassLoader;
025:
026: import java.io.BufferedInputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033: import java.net.URL;
034:
035: import org.codehaus.groovy.syntax.SyntaxException;
036:
037: /**
038: * Abstract scripted controller class. All created GUI scripts should subclass
039: * from this class
040: */
041: public abstract class ScriptedController extends Controller {
042: private GroovyClassLoader loader = null;
043:
044: /**
045: * Create a new scripted controller
046: */
047: public ScriptedController() {
048: loader = (GroovyClassLoader) Thread.currentThread()
049: .getContextClassLoader();
050: }
051:
052: /**
053: * Load a groovy script containing another scripted controller
054: * @param scriptFile The script file
055: * @return The instance
056: * @throws GUIExceptions If there were errors while loading/parsing/compiling/instantiating the script
057: */
058: protected ScriptedController loadScript(String scriptFile)
059: throws GUIException {
060: return loadScript(scriptFile, null);
061: }
062:
063: /**
064: * Load a groovy script containing another scripted controller
065: * @param scriptFile The script file
066: * @return The instance
067: * @throws GUIExceptions If there were errors while loading/parsing/compiling/instantiating the script
068: */
069: protected ScriptedController loadScript(String scriptFile,
070: Object parameters[]) throws GUIException {
071: try {
072: InputStream stream = null;
073: File file = new File(scriptFile);
074:
075: if (file.exists()) {
076: stream = new FileInputStream(file);
077: } else {
078: URL url = getClass().getResource(scriptFile);
079: if (url == null)
080: throw new GUIException("Script file [" + scriptFile
081: + "] not found");
082: stream = url.openStream();
083: }
084:
085: if (file.exists()) {
086: stream = new FileInputStream(file);
087: } else {
088: URL url = getClass().getResource(scriptFile);
089: if (url == null)
090: throw new GUIException("Script file [" + scriptFile
091: + "] not found");
092: stream = url.openStream();
093: }
094:
095: Class scriptClass = loader.parseClass(
096: new BufferedInputStream(stream), scriptFile);
097: Object instance = scriptClass.newInstance();
098:
099: if (parameters == null || parameters.length == 0)
100: return (ScriptedController) instance;
101:
102: Method methods[] = scriptClass.getMethods();
103:
104: String className = scriptClass.getName();
105: if (className.indexOf('.') != -1)
106: className = className.substring(className
107: .lastIndexOf('.') + 1, className.length());
108:
109: for (int i = 0; i < methods.length; i++) {
110: Method method = methods[i];
111: if (method.getName().equals(className)
112: && method.getParameterTypes().length == parameters.length) {
113: method.invoke(instance, parameters);
114: return (ScriptedController) instance;
115: }
116: }
117: throw new GUIException(
118: "No matching constructor found while trying to load a script");
119: } catch (IOException e) {
120: throw new GUIException(
121: "I/O Exception while trying to load a script", e);
122: } catch (SyntaxException e) {
123: throw new GUIException(
124: "Syntax error while trying to load a script", e);
125: } catch (IllegalAccessException e) {
126: throw new GUIException(
127: "Access error while trying to load a script", e);
128: } catch (InstantiationException e) {
129: throw new GUIException(
130: "Instantiation error while trying to load a script",
131: e);
132: } catch (InvocationTargetException e) {
133: throw new GUIException(
134: "Exception while trying to instantiate a script", e);
135: } catch (ClassCastException e) {
136: throw new GUIException(
137: "Error while trying to load a script: Does not extend from ScriptedController",
138: e);
139: }
140: }
141:
142: public static ScriptedController launchScript(String scriptFile,
143: Object parameters[]) throws GUIException {
144: /* Initialize the script class loader */
145: Thread.currentThread().setContextClassLoader(
146: new GroovyClassLoader(ScriptedController.class
147: .getClassLoader()));
148:
149: ScriptedController tmp = new ScriptedController() {
150: public void eventOccured(GUIEvent event) {
151: }
152: };
153: return tmp.loadScript(scriptFile, parameters);
154: }
155: }
|