NoInitialContextException in Apache Camel

285 views Asked by At

I have a route that fetches a group of person ids from a database, splits them and does a LDAP lookup for each id. The code below is simplified but I hope you'll get the idea.

Main

public class MainApp {
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        // create mydb and bind it into registry
        DataSource dataSource = setupDataSource();
        main.bind("mydb", dataSource);
        // create ldap and bind it into registry
        InitialLdapContext ldap = setupLDAP();
        main.bind("dir", ldap);
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);
    }

    private static InitialLdapContext setupLDAP() throws NamingException {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        props.setProperty(Context.PROVIDER_URL, "ldap://mycompany:389");
        props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
        props.setProperty(Context.REFERRAL, "ignore");
        props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
        return new InitialLdapContext(props, null);
    }
}

MyRouteBuilder

public class MyRouteBuilder extends RouteBuilder {
    public void configure() throws Exception {
        from("timer:start?period=0&delay=0&repeatCount=1")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("Invoked timer at " + new Date());
            }
        })
        .setBody(constant("select personid from person"))
        .to("jdbc:mydb?outputType=StreamList")
        .split(body())
        .process(new AfterDbProcessor())
        .to("ldap:dir?base=ou=people,dc=mycompany&returnedAttributes=sn,fn")
        .process(new AfterLDAPProcessor());
     }
}

AfterDbProcessor simply takes personid and puts it in the body of the in-message

public class AfterHOTProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        Map<String, Object> row = exchange.getIn().getBody(Map.class);
        Integer personId = (Integer) row.get("henkilo_id");
        exchange.getIn().setBody("(uid=" + personId + ")");
   }

}

Getting ids from database works fine. First lookup into LDAP works also fine, but second will crash with an error message

[l-1) thread #0 - timer://start] TimerConsumer WARN  Error processing exchange.
Exchange[Message: org.apache.camel.component.jdbc.ResultSetIterator@65ce3f76]. 
Caused by:  [javax.naming.NoInitialContextException - Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial]

What happens to InitialLdapContext after first LDAP-lookup? No I need to re-initialize or re-create or what?

0

There are 0 answers