Created
November 11, 2008 17:31
-
-
Save nicksieger/23909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jruby.ext; | |
import java.util.Arrays; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
import org.jruby.embed.ScriptingContainer; | |
/** | |
* Launch a Ruby script at web application startup. To wire up this | |
* listener, add | |
* <listener-class>jruby.ext.StartupScriptLauncher</listener-class> to your | |
* application's web.xml file. | |
* | |
* @author nicksieger | |
*/ | |
public class StartupScriptLauncher implements ServletContextListener { | |
public void contextInitialized(ServletContextEvent event) { | |
ServletContext servletContext = event.getServletContext(); | |
ScriptingContainer container = new ScriptingContainer(); | |
try { | |
container.setLoadPaths(Arrays.asList(new String[] { | |
// set up any $LOAD_PATH items here | |
servletContext.getRealPath("/WEB-INF"), | |
servletContext.getRealPath("/WEB-INF/lib") | |
})); | |
// setup any global variables | |
container.put("$servlet_context", servletContext); | |
// require e.g., WEB-INF/config/startup.rb | |
container.runScriptlet("require 'config/startup'"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
servletContext.log(ex.getMessage()); | |
} finally { | |
container.terminate(); | |
} | |
} | |
public void contextDestroyed(ServletContextEvent event) { | |
} | |
} |
Hmm. Does the file live in
WEB-INF/classes/jruby/ext/StartupScriptLauncher.class inside the war
file? That's the expected location. If you make a rails_root/classes
directory, you may need to tell Warbler about it if you didn't already
by adding "classes" to the array of directory names in "config.dirs"
inside config/warble.rb.
/Nick
…On Thu, Sep 22, 2011 at 2:15 PM, Chris Weis ***@***.*** wrote:
I put the necessary line into web.xml.erb and added this file into rails_root/classes and included that directory in the war file, but when I try to start the web server, it now complains, that the class jruby.ext.StartupScriptLauncher couldn't be found.
Did I configure something wrong?
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/23909
The wrong directory structure was the first problem, I found that out by looking at a similar java class and its documentation. Now tomcat does not error out anymore but seems to actually find the launcher. It just doesn't seem to launch the corresponding script.
Hmm. Can you try this updated version that uses org.jruby.embed
instead? You can also figure out what scripts are getting loaded by adding
System.setProperty("jruby.debug.loadService", "true");
at the beginning of the contextInitialized
method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, to compile the code I had to put
into line 21, to avoid errors.