Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

SNMP does not work in Java Stored Procedure

kirill9292Apr 27 2020 — edited Apr 27 2020

Hello, I am trying to execute Java procedure in Oracle JVM. This procedure sends an SNMP request to the printer to find out its name; it uses the SNMP4j library

import java.io.IOException;

import org.snmp4j.CommunityTarget;

import org.snmp4j.PDU;

import org.snmp4j.Snmp;

import org.snmp4j.Target;

import org.snmp4j.TransportMapping;

import org.snmp4j.event.ResponseEvent;

import org.snmp4j.mp.SnmpConstants;

import org.snmp4j.smi.Address;

import org.snmp4j.smi.GenericAddress;

import org.snmp4j.smi.OID;

import org.snmp4j.smi.OctetString;

import org.snmp4j.smi.VariableBinding;

import org.snmp4j.transport.DefaultUdpTransportMapping;

public class Get_ink_levels_devices {

Snmp snmp = null;

String address = null;

/**

* Constructor

* @param add

*/

public Get_ink_levels_devices(String add)

{

address = add;

}

public static void main(String[] args) throws IOException {

/**

* Port 161 is used for Read and Other operations

* Port 162 is used for the trap generation

*/

Get_ink_levels_devices client = new Get_ink_levels_devices("udp:192.168.115.77/161");

client.start();

/**

* OID - .1.3.6.1.2.1.1.1.0 => SysDec

* OID - .1.3.6.1.2.1.1.5.0 => SysName

*.1.3.6.1.2.1.1.1.0

*.1.3.6.1.2.1.25.3.2.1.3.1

* => MIB explorer will be usefull here, as discussed in previous article

*/

String sysDescr = client.getAsString(new OID("1.3.6.1.2.1.25.3.2.1.3.1"));

System.out.println(sysDescr);

}

private void start() throws IOException {

TransportMapping transport = new DefaultUdpTransportMapping();

snmp = new Snmp(transport);

// Do not forget this line!

transport.listen();

}

/**

* Method which takes a single OID and returns the response from the agent as a String.

* @param oid

* @return

* @throws IOException

*/

public String getAsString(OID oid) throws IOException {

    ResponseEvent event = get(new OID[]{oid});

    if(event.getResponse() != null){

        return event.getResponse().get(0).getVariable().toString();

    } else {

        return "no target";

    }

}

/**

* This method is capable of handling multiple OIDs

* @param oids

* @return

* @throws IOException

*/

public ResponseEvent get(OID oids[]) throws IOException {

PDU pdu = new PDU();

for (OID oid : oids) {

pdu.add(new VariableBinding(oid));

}

pdu.setType(PDU.GET);

ResponseEvent event = snmp.send(pdu, getTarget(), null);

if(event != null) {

return event;

}

throw new RuntimeException("GET timed out");

}

/**

* This method returns a Target, which contains information about

* where the data should be fetched and how.

* @return

*/

private Target getTarget() {

Address targetAddress = GenericAddress.parse(address);

CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString("public"));

target.setAddress(targetAddress);

target.setRetries(2);

target.setTimeout(5000);

target.setVersion(SnmpConstants.version1);

return target;

}

}

Hello, I am trying to execute Java procedure in Oracle JVM. This procedure sends an SNMP request to the printer to find out its name; it uses the SNMP4j library. I use Oracle 19c, I compiled this procedure using javac --release 8, after I loaded the snmp4j-2.8.3.jar library into the database using loadjava and then I loaded the procedure itself. Then I created a procedure in sqlplus CREATE OR REPLACE Procedure Test_ink AS LANGUAGE JAVA NAME 'Get_ink_levels_devices.main (java.lang.String [])'; and set exec permissions dbms_java.grant_permission (grantee => 'ADMIN', permission_type => 'SYS: java.net.SocketPermission', permission_name => '192.168.115.77:161',permission_action =>' accept, connect, listen, resolve ');

and granted GRANT JAVASYSPRIV TO Admin rights;

GRANT JAVAUSERPRIV TO Admin;

GRANT JAVAIDPRIV TO Admin;

I run the procedure this way:

set serveroutput on;

call dbms_java.set_output (1000000);

exec Test_ink;

But it does not return the name of the printer, but returns "no traget", although if I run the program in the console it returns the name. I don’t understand what I’m doing wrong. I didn’t specify the necessary permissions for Java or is there a problem in something else?

Comments
Post Details
Added on Apr 27 2020
0 comments
357 views