Load path issues inside jar / external app

I’m trying to package some ruby code for running inside spring batch.
I’m
using the spring dynamic language bean feature to load ruby classes from
files on the class path. The problem is these ruby files require other
files via File.dirname(FILE). That’s not working. What’s the best
way
to package up a bunch of jruby code for running inside a java
application,
such that the jruby files can require other jruby files?

Thanks,

Steve

On Mon, Nov 2, 2009 at 4:03 PM, Steve M. [email protected]
wrote:

I’m trying to package some ruby code for running inside spring batch. I’m
using the spring dynamic language bean feature to load ruby classes from
files on the class path. The problem is these ruby files require other
files via File.dirname(FILE). That’s not working. What’s the best way
to package up a bunch of jruby code for running inside a java application,
such that the jruby files can require other jruby files?
Thanks,
Steve

What is the value of FILE? Even with FILE set to a file: URL
of a path inside a jar file, typical Rubyisms like you indicate should
be working. If you can illustrate what is failing, that would help;
there might be a bug. Also indicate what version of JRuby you’re
using. Some of the bugs related to file: URLs and paths have been
fixed in recent releases.

Cheers,
/Nick


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Tue, Nov 3, 2009 at 9:35 AM, Nick S. [email protected]
wrote:

On Mon, Nov 2, 2009 at 4:03 PM, Steve M. [email protected] wrote:

I’m trying to package some ruby code for running inside spring batch. I’m
using the spring dynamic language bean feature to load ruby classes from
files on the class path. The problem is these ruby files require other
files via File.dirname(FILE). That’s not working. What’s the best way
to package up a bunch of jruby code for running inside a java application,
such that the jruby files can require other jruby files?
Thanks,
Steve

Did Spring load Ruby scripts from a classpath? If so, I might have got
an error from the same reason.

What is the value of FILE? Even with FILE set to a file: URL
of a path inside a jar file, typical Rubyisms like you indicate should
be working. If you can illustrate what is failing, that would help;
there might be a bug. Also indicate what version of JRuby you’re
using. Some of the bugs related to file: URLs and paths have been
fixed in recent releases.

I haven’t figured out what’s wrong with that, but FILE doesn’t
work when a script is loaded from a classpath. For example, when I
set all necessary all classapths and load test/test_file.rb from the
classpath, evaluation failed because the error below at the line
“expect = File.read(FILE)[0…100]”:

Loaded suite -
Started
…E…
Finished in 2.153 seconds.

  1. Error:
    test_file_url(TestFile):
    Errno::ENOENT: No such file or directory - File not found - test_file.rb
    test_file.rb:926:in initialize' test_file.rb:926:in test_file_url’

57 tests, 293 assertions, 0 failures, 1 errors

However, when I read test_file.rb from an absolute path, it worked
well. This happens in trunk version as well. If this should work even
when scripts are loaded from classpath, there might be a bug.

-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

One more thing - what’s the state of embedding gems in a jar, where the
gem
includes its own java code in a jar? I’m trying to use the
activerecord-jdbc-adapter inside a spring application where all my ruby
code
and dependent ruby gems are packaged in a jar (or jars). However
activerecord-jdbc can’t load it’s java classes:

`class_eval’: cannot load Java class jdbc_adapter.JdbcConnectionFactory
(NameError)

I’m not surprised it’s not working, but I’m not sure what the best
solution is. Should I just copy jdbc_adapter_internal.jar (used
internally
by the activerecord-jdbc gem) out from under the activerecord-jdbc
directory
to someplace in my classpath?

Steve

Yes I had Spring load the scripts from a classpath. I’ve worked around
the
issue by removing the File.dirname(FILE) bits from my code. Instead
I
specify from the root of the classpath: 'require “path/to/ruby/file”
where
the directory “path” is on the root of the classpath. I can do this
because
it’s all my code and not that many files to modify.

I’m still playing around with it; will update if I find anything out.

Steve

On Wed, Nov 4, 2009 at 11:07 AM, Steve M. [email protected]
wrote:

to someplace in my classpath?
I think better would be to un-jar the contents of the jar into another
jar. There’s also the problem that the jdbc adapter code does a
“require ‘jdbc_adapter/jdbc_adapter_internal’” which means that you
might have to put an empty 'jdbc_adapter/jdbc_adapter_internal.rb"
file inside your jar as well.

The relevant JIRA is http://jira.codehaus.org/browse/JRUBY-3299. If we
can’t find a solution to jars-in-jars, we might need to build a gem
re-packaging solution that takes care of the above for you.

/Nick

I’m still playing around with it; will update if I find anything out.

from
Did Spring load Ruby scripts from a classpath? If so, I might have got
I haven’t figured out what’s wrong with that, but FILE doesn’t

  1. Error:

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks for the help. That got me further but I’ve hit another snag that
has
me really confused.

Background: I’m running ruby code that uses activerecord against MsSql
inside a jar that is loaded inside a spring application. I’m using a
spring
dynamic language jruby bean, which kicks everything off. I’ve removed
the
jdbc_adapter_internal.jar from within the jdbc_adapter gem and replaced
it
with an empty jdbc_adapter_internal.rb. My ruby code and gems (minus
jars
in the gems) are jarred up. This is all running inside tomcat in a WAR,
and
I put jdbc_adapter_internal.jar and my_ruby_code_with_gem.jar under
WEB-INF/lib in the war.

Problem: I’m getting this when my jruby code tries to establish a
connection to the db:

org.springframework.scripting.jruby.JRubyScriptUtils$JRubyExecutionException:
uninitialized constant
ActiveRecord::ConnectionAdapters::MssqlJdbcConnection; nested exception
is
org.jruby.exceptions.RaiseException: uninitialized constant
ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
at
org.springframework.scripting.jruby.JRubyScriptUtils$RubyObjectInvocationHandler.invoke(JRubyScriptUtils.java:184)
……

Caused by: org.jruby.exceptions.RaiseException: uninitialized constant
ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
at
Kernel.raise(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activesupport/lib/active_support/dependencies.rb:442)
at
ActiveSupport::Dependencies.load_missing_constant(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activesupport/lib/active_support/dependencies.rb:77)
at
ActiveSupport::Dependencies::ModuleConstMissing.const_missing_with_dependencies(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord-jdbc-adapter-0.9.2/lib/jdbc_adapter/jdbc_mssql.rb:35)
at
#Class:01x45192510.jdbc_connection_class(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:462)
at
ActiveRecord::ConnectionAdapters::JdbcAdapter.jdbc_connection_class(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:452)
at
ActiveRecord::ConnectionAdapters::JdbcAdapter.initialize(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:49)
at
JdbcSpec::ActiveRecordExtensions.jdbc_connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:223)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.new_connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:245)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.checkout_new_connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:188)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.checkout(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:184)
at
Kernel.loop(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:184)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.checkout(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/jruby-complete-1.3.1.jar!/monitor.rb:191)
at
MonitorMixin.mon_synchronize(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:183)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.checkout(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:98)
at
ActiveRecord::ConnectionAdapters::ConnectionPool.connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:326)
at
ActiveRecord::ConnectionAdapters::ConnectionHandler.retrieve_connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:121)
at
#Class:01x322a0b7b.retrieve_connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:113)
at
#Class:01x322a0b7b.connection(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/base.rb:2934)
at
#Class:01x322a0b7b.quoted_table_name(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/base.rb:1626)
at
#Class:01x322a0b7b.construct_finder_sql(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/base.rb:1490)
at
#Class:01x322a0b7b.find_every(file:/Users/steve/src/apache-tomcat-6.0.20/webapps/mercybatch/WEB-INF/lib/remittanceAdvice-1.0.jar!/vendor/gems/activerecord/lib/active_record/base.rb:589)
at #Class:01x322a0b7b.find(:38)
at RemittanceAdvice::RaItemReader.read(:1)
at (unknown).(unknown)(:1)

I know warbler and jruby-rack gets AR/jdbc adapter and ms sql to work in
a
WAR, but that isn’t really an option for me right now; I’m trying to
squeeze
in some existing jruby stuff into an existing spring app built by maven.
So
I guess I’m going to have to figure out what jruby-rack and warbler do
to
make this stuff work.

Any help would be greatly appreciated!!!

Steve

I figured out how we can get things work.

I haven’t figured out what’s wrong with that, but FILE doesn’t
work when a script is loaded from a classpath. For example, when I
set all necessary all classapths and load test/test_file.rb from the
classpath, evaluation failed because the error below at the line
“expect = File.read(FILE)[0…100]”:

When a Ruby code is loaded from a classpath, a current directory is
not set correctly. So, if we set the feasible current directory and
look at the adjusted current directory, we can make thinks work fine.

In case of Embed Core, we have the method to set the current
directory. For example:

ScriptingContainer container = new ScriptingContainer();
container.getProvider().getRubyInstanceConfig().setCurrentDirectory(System.getProperty("user.dir")+"/src/ruby");

Setting user.dir system property works, too. Probably, on Spring, you
need to use the user.dir system property. (I’m not sure how to do
that)
Also, we need to use File.expand_path so that the adjusted current
directory is referred. For example:

File.join(File.expand_path(File.dirname(__FILE__)), "abc.rb")

Then, we can get a file path we want.
See,
yokolet's notelets: JRuby Embed (Red Bridge) Gotchas: __FILE__,
for further detail.

-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email