Question

I am working on implementing a JRuby wrapper around the SNMP4J-Agent library, found here:

Website: http://www.snmp4j.org/
JavaDocs: http://www.snmp4j.org/agent/doc/


It's been pretty fun so far, but I am having trouble deciphering the following piece of Java code to implement in JRuby:

server = new DefaultMOServer();
vacmMIB = new VacmMIB(new MOServer[] { server });

The problem is that - as far as I can tell - casting new with MOServer[] (which is an interface) creates an anonymous function that is passed a server object, and I can't seem to find the right way to express that with JRuby. I've included information about the Java classes:


DefaultMOServer is defined as

public class DefaultMOServer implements MOServer {
    public DefaultMOServer() {
       ...
    }
    ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/DefaultMOServer.html


VacmMIB is defined as

public class VacmMIB implements MOGroup, MutableVACM {
    public VacmMIB(MOServer[] server) {
       this.server = server;
       ...
    }
    ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/VacmMIB.html


And finally, MOServer is defined as

public interface MOServer {
   ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/MOServer.html



Here is roughly what I am doing in JRuby:

require 'java'
require 'snmp4jruby'
require 'lib/snmp4j-agent-1.4.3.jar'

module Agent
    include_package 'org.snmp4j.agent'

    module MO
       include_package 'org.snmp4j.agent.mo'

       module SNMP
          include_package 'org.snmp4j.agent.mo.snmp'
       end
    end
end

class SnmpAgent < Agent::BaseAgent

    # Setup the agent
    def init
       ... everything works fine up here ...

       # Server is created early on without issue
       self.server = Agent::DefaultMOServer.new

       # Having trouble here
       _server = Agent::MOServer.new { self.server }
       self.vacmMIB = Agent::MO::SNMP::VacmMIB.new(_server)
    end

end

Running the above code gives me the following error for the line where I set self.vacmMIB = ...:

TypeError: failed to coerce org.jruby.gen.InterfaceImpl792882806 to [Lorg.snmp4j.agent.MOServer;



Any direction on this would be greatly appreciated!

Was it helpful?

Solution

Your issue seems to be that VacmMIB constructor takes array of MOServer as an argument and you're passing an instance of MOServer.

Try this:

_server = Agent::MOServer.new { self.server }
# your code
# self.vacmMIB = Agent::MO::SNMP::VacmMIB.new(_server) 
# updated code
self.vacmMIB = Agent::MO::SNMP::VacmMIB.new([_server]) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top