Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create globally namespaced CONSTANTs during Rails initialization in Tomcat?
    primarykey
    data
    text
    <p>First some background: I've got a Rails 2.2.2 app with all kinds of janky dependencies and customizations that keep me from upgrading past 2.2.2 for the moment. Current plan is to migrate this app to JRuby (1.6.5), deploying it as a war in Tomcat (6.0.23—not yet tied to this particular version FWIW), using warbler (1.3.2) to package it up.</p> <p>I want to keep the JRuby branch of the app from diverging substantially from the vanilla app since I don't know how long it's going to take to tease out and fix all of the issues caused by discrepancies in the runtime environments. So I want to be able to continue to cleanly and easily merge in ongoing work from the vanilla app.</p> <p>A number of customized behaviors in the app can be configured with globally namespaced CONSTANTs set in the <code>environments/*.rb</code> files. But these values can be overridden by using a yaml configuration file or setting a shell environment variable; this is primarily for flexibility during development, where different devs sometimes need to customize various settings. This system has worked well so far, leaving our <code>environments/development.rb</code> file relatively stable and uncluttered while still giving us a great deal of control over customization as we're hacking.</p> <p>I would like to preserve this system of customization in the move to JRuby. So far, I'm using a customized <code>config/web.xml.erb</code> that converts relevant environment variables and settings from the yaml file into <code>&lt;env-entry&gt;</code> fragments, which makes them available during initialization via the <code>java:comp/env</code> JNDI context. In more restricted environments (like staging or production), many of these settings can be locked down by including corresponding <code>&lt;Environment&gt;</code> settings in the container's <code>context.xml</code> file with the <code>override</code> attribute set to <code>false</code>.</p> <p>So far, so good. But now I am struggling to turn the values retrieved from the JNDI context into global namespace CONSTANTs during initialization. Here are excerpts of the relevant bits:</p> <p>config/environments/developer.rb:</p> <pre><code>... envset "FOO", "baz" FOO="fail" unless defined? FOO printf "FOO is now '%s'\n", FOO ... </code></pre> <p>lib/config_helper.rb:</p> <pre><code>1 include Java 2 import javax.naming.InitialContext 3 import javax.naming.NameNotFoundException 4 5 ctx = InitialContext.new 6 7 def envset(value_name, default_value) 8 value = nil 9 begin 10 value = ctx.lookup("java:comp/env/#{value_name}") 11 rescue NameNotFoundException =&gt; e 12 value = default_value 13 end 14 printf "setting %s to '%s'\n", value_name, value 15 eval("%Q[ #{value_name} = '#{value}' ]") 16 end </code></pre> <p>web.xml:</p> <pre><code>... &lt;env-entry&gt; &lt;env-entry-name&gt;FOO&lt;/env-entry-name&gt; &lt;env-entry-value&gt;bar&lt;/env-entry-value&gt; &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt; &lt;/env-entry&gt; ... </code></pre> <p>catalina.out:</p> <pre><code>setting FOO to 'bar' FOO is now 'fail' </code></pre> <p>Where I am expecting this last line to be "FOO is now 'bar'", of course. Line 14 of <code>lib/config_helper.rb</code> seems to be where things are going wrong. Similar code has worked fine in vanilla ruby, but it doesn't work here during initialization in Tomcat.</p> <p>So, how can I create globally namespaced CONSTANTs out of JNDI environment entries during Rails initialization in Tomcat?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload