Easy way to start a standalone JNDI server (and re

2019-02-02 00:53发布

For testing purposes, I'm looking for a simple way to start a standalone JNDI server, and bind my javax.sql.DataSource to "java:/comp/env/jdbc/mydatasource" programmatically.

The server should bind itself to some URL, for example: "java.naming.provider.url=jnp://localhost:1099" (doesn't have to be JNP), so that I can look up my datasource from another process. I don't care about which JNDI server implementation I'll have to use (but I don't want to start a full-blown JavaEE server).

This should be so easy, but to my surprise, I couldn't find any (working) tutorial.

8条回答
乱世女痞
2楼-- · 2019-02-02 01:37

I know I'm late to the party, but I ended up hacking this together like so

InitialContext ctx = new InitialContext();

// check if we have a JNDI binding for "jdbc". If we do not, we are
// running locally (i.e. through JUnit, etc)
boolean isJndiBound = true;
try {
    ctx.lookup("jdbc");
} catch(NameNotFoundException ex) {
    isJndiBound = false;
}

if(!isJndiBound) {
    // Create the "jdbc" sub-context (i.e. the directory)
    ctx.createSubcontext("jdbc");

    //parse the jetty-web.xml file
    Map<String, DataSource> dataSourceProperties = JettyWebParser.parse();

    //add the data sources to the sub-context
    for(String key : dataSourceProperties.keySet()) {
        DataSource ds = dataSourceProperties.get(key);
        ctx.bind(key, ds);
    }
}
查看更多
虎瘦雄心在
3楼-- · 2019-02-02 01:39

I worked on the John´s code and now is working good.

In this version I'm using libs of JBoss5.1.0.GA, see jar list below:

  • jboss-5.1.0.GA\client\jbossall-client.jar
  • jboss-5.1.0.GA\server\minimal\lib\jnpserver.jar
  • jboss-5.1.0.GA\server\minimal\lib\log4j.jar
  • jboss-remote-naming-1.0.1.Final.jar (downloaded from http://search.maven.com)

This is the new code:

import java.net.InetAddress;
import java.util.Hashtable;
import java.util.concurrent.Callable;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.jnp.server.Main;
import org.jnp.server.NamingBeanImpl;

public class StandaloneJNDIServer implements Callable<Object> {

public Object call() throws Exception {

    setup();

    return null;
}

@SuppressWarnings("unchecked")
private void setup() throws Exception {

    //configure the initial factory
    //**in John´s code we did not have this**
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

    //start the naming info bean
    final NamingBeanImpl _naming = new NamingBeanImpl();
    _naming.start();

    //start the jnp serve
    final Main _server = new Main();
    _server.setNamingInfo(_naming);
    _server.setPort(5400);
    _server.setBindAddress(InetAddress.getLocalHost().getHostName());
    _server.start();

    //configure the environment for initial context
    final Hashtable _properties = new Hashtable();
    _properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    _properties.put(Context.PROVIDER_URL,            "jnp://10.10.10.200:5400");

    //bind a name
    final Context _context = new InitialContext(_properties);
    _context.bind("jdbc", "myJDBC");

}

public static void main(String...args){

    try{

        new StandaloneJNDIServer().call();

    }catch(Exception _e){
        _e.printStackTrace();
    }

}
}

To have good logging, use this log4j properties:

log4j.rootLogger=TRACE, A1 
log4j.appender.A1=org.apache.log4j.ConsoleAppender 
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

To consume the Standalone JNDI server, use this client class:

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author fabiojm - Fábio José de Moraes
 *
 */
public class Lookup {

public Lookup(){

}

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    final Hashtable _properties = new Hashtable();

    _properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    _properties.put("java.naming.provider.url",    "jnp://10.10.10.200:5400");

    try{
        final Context _context = new InitialContext(_properties);

        System.out.println(_context);
        System.out.println(_context.lookup("java:comp"));
        System.out.println(_context.lookup("java:jdbc"));

    }catch(Exception _e){
        _e.printStackTrace();
    }
}

}
查看更多
登录 后发表回答