1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.scripting.ruby;
16
17 import org.figure8.join.core.ParameterDefinition;
18 import org.figure8.join.core.InvalidParameterException;
19 import org.figure8.join.services.scripting.ScriptLauncher;
20 import org.figure8.join.services.scripting.ScriptException;
21 import org.figure8.join.util.LogUtil;
22
23 import org.apache.commons.logging.Log;
24
25
26
27
28 import java.util.List;
29 import java.util.Iterator;
30 import java.util.ArrayList;
31 import java.util.Properties;
32 /**
33 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
34 * @version $Revision$
35 */
36 public class RubyScriptLauncher extends ScriptLauncher{
37
38
39
40 /** Get a commons logger. */
41 private static final Log log = LogUtil.getLog(RubyScriptLauncher.class);
42
43 /** Name of configurable parameter denoting the path to Ruby libraries */
44 public static final String LIBRARY_PATH_PARAM = "libraryPath";
45
46 /** List of {@link org.figure8.join.core.ParameterDefinition}s supported by this launcher */
47 protected static List parameters = new ArrayList();
48
49 /** ParameterDefinition representation of parameter denoting the target to execute. */
50 protected static final ParameterDefinition targetParam = new ParameterDefinition(LIBRARY_PATH_PARAM,
51 "A ; separated list of Ruby library paths that should be loaded before running script",
52 "c:/ruby/lib/mylib.rb;c:/myproject/lib/mylib2.rb", false);
53
54
55
56
57 /** The path of Ruby libraries to load */
58 private String libraryPath = null;
59
60
61
62
63 /** Creates a new instance of RubyScriptLauncher */
64 public RubyScriptLauncher(){
65 }
66
67 /**
68 * Creates a new instance of RubyScriptLauncher with script path
69 * @param scriptPath the path of script to later execute
70 * @throws InvalidParameterException if path cannot be accessed
71 */
72 public RubyScriptLauncher(String scriptPath) throws InvalidParameterException{
73 super(scriptPath);
74 }
75
76
77
78
79 /** @return The path of Ruby libraries to load */
80 public String getLibraryPath(){
81 return libraryPath;
82 }
83 /** @param libraryPath The path of Ruby libraries to load */
84 public void setLibraryPath(String libraryPath){
85 this.libraryPath = libraryPath;
86 }
87
88
89
90
91 /**
92 * Run the Ruby script file identified by <code>scriptPath</code> inner
93 * field. Properties passed as param are injected into the Ruby runtime
94 * as arguments. Script can directly use them using the $property
95 * form.
96 * @param properties The runtime properties to inject into execution environment
97 * @throws ScriptException if something wrong occurs during script parsing, evaluation, ...
98 */
99 public void runScript(Properties properties) throws ScriptException{
100 try{
101
102 Iterator keys = properties.keySet().iterator();
103 while (keys != null && keys.hasNext()){
104 String key = (String)keys.next();
105 String value = properties.getProperty(key);
106 }
107 }
108 catch (Throwable t){
109
110 }
111 finally{
112
113 if (getLogOutputStream() != null){
114 try {getLogOutputStream().close();}
115 catch (Exception e){
116 log.warn("Exception while closing the log output stream: " + e.getMessage());
117 }
118 }
119 }
120 }
121
122
123
124
125 /**
126 * Get this object parameters definitions as a list
127 * @return A list of {@code ParameterDefinition} objects
128 */
129 public List getParameterDefinitionsAsList(){
130
131 if (parameters.isEmpty()){
132 parameters.addAll(super.getParameterDefinitionsAsList());
133 parameters.add(libraryPath);
134 }
135 return parameters;
136 }
137
138 /**
139 * Set the value of a parameter using its nama
140 * @param parameterName The name of parameter so set value for
141 * @param parameterValue The value of the paramater
142 * @throws InvalidParameterException if this parameter is not supported by this object
143 */
144 public void setParameter(String parameterName, String parameterValue) throws InvalidParameterException{
145 try{
146
147 super.setParameter(parameterName, parameterValue);
148 }
149 catch (InvalidParameterException ipe){
150
151 if (LIBRARY_PATH_PARAM.equals(parameterName))
152 setLibraryPath(parameterValue);
153 else{
154 log.warn("The parameter '" + parameterName + "' is not supported by RubyScriptLauncher");
155 throw new InvalidParameterException("The parameter '" + parameterName + "' is not supported by RubyScriptLauncher");
156 }
157 }
158 }
159 }