001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.beans.admin.main;
034:
035: import com.flexive.shared.EJBLookup;
036: import com.flexive.shared.FxContext;
037: import com.flexive.shared.FxSharedUtils;
038: import com.flexive.shared.security.Role;
039: import groovy.lang.GroovyShell;
040: import groovy.lang.Script;
041: import org.apache.commons.lang.StringUtils;
042:
043: import java.io.PrintWriter;
044: import java.io.StringWriter;
045: import java.util.Formatter;
046:
047: /**
048: * A simple groovy console for testing (war-layer) groovy scripts.
049: *
050: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
051: * @version $Rev: 213 $
052: */
053: public class ScriptConsoleBean {
054: private String code;
055: private long executionTime;
056: private boolean web;
057: private String language;
058:
059: public String getCode() {
060: return code;
061: }
062:
063: public void setCode(String code) {
064: this .code = code;
065: }
066:
067: public long getExecutionTime() {
068: return executionTime;
069: }
070:
071: public void setExecutionTime(long executionTime) {
072: this .executionTime = executionTime;
073: }
074:
075: public boolean isWeb() {
076: return web;
077: }
078:
079: public void setWeb(boolean web) {
080: this .web = web;
081: }
082:
083: public String getLanguage() {
084: return language;
085: }
086:
087: public void setLanguage(String language) {
088: this .language = language;
089: }
090:
091: public Object getResult() {
092: if (StringUtils.isBlank(code)) {
093: return "";
094: }
095: long start = System.currentTimeMillis();
096: try {
097: if (web
098: && FxSharedUtils.isGroovyScript("console."
099: + language)) {
100: if (!FxContext.get().getTicket().isInRole(
101: Role.ScriptExecution))
102: return "No permission to execute scripts!";
103: GroovyShell shell = new GroovyShell();
104: Script script = shell.parse(code);
105: return script.run();
106: } else {
107: return EJBLookup.getScriptingEngine().runScript(
108: "console." + language, null, code).getResult();
109: }
110: } catch (Exception e) {
111: StringWriter writer = new StringWriter();
112: e.printStackTrace(new PrintWriter(writer));
113: final String msg = e.getCause() != null ? e.getCause()
114: .getMessage() : e.getMessage();
115: return new Formatter().format("Exception caught: %s%n%s",
116: msg, writer.getBuffer());
117: } finally {
118: executionTime = System.currentTimeMillis() - start;
119: }
120: }
121: }
|