You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2017/09/03 14:23:28 UTC

Re: Camel LDAP component NoInitialContextException on second poll

Hi

Yeah its a bit unfortunatey how JNDI works in this matter where it
mutate the context so after using it once its kinda fucked.

That is why if you use spring xml you can declare the bean with
prototype scope, so a new context is created each time. This is a bit
harder to do in java to have it create a new instance per lookup. So I
have logged a ticket to make this easier.

https://issues.apache.org/jira/browse/CAMEL-11743

On Mon, Aug 28, 2017 at 10:06 AM, Pakarinen, Jari
<ja...@orion.fi> wrote:
> Hi,
>
> Camel 2.19.2
>
> Running on localhost by itself, no container. Querying LDAP for user details with a timer. First query works ok (shown on line 5 of the log), second query and all subsequent queries after that give a "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" error.
>
> I've checked that the properties object I give to the route is used on the first poll, but on the second poll it is replaced by the system properties object which does not contain the properties I've set up when starting the route. Am I missing something obvious here ?
>
> Main:
>
> public class LocalTest {
>
>     public static void main(String[] args) {
>         InitialLdapContext context = null;
>         Properties props = new Properties();
>         props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
>         props.setProperty(Context.PROVIDER_URL, "ldap://xxxxxxxxx.zzz:389");
>         props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
>         props.setProperty(Context.REFERRAL, "ignore");
>         props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
>         props.setProperty(Context.SECURITY_PRINCIPAL, "user@domain");
>         props.setProperty(Context.SECURITY_CREDENTIALS, "password");
>         try {
>             context = new InitialLdapContext(props, null);
>         } catch (NamingException e) {
>             e.printStackTrace();
>         }
>         Main main = new Main();
>         main.bind("myldap", context);
>         main.addRouteBuilder(new MyRouteBuilder());
>         try {
>             main.run();
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>     }
> }
>
> Route:
>
> public class MyRouteBuilder extends RouteBuilder {
>     @Override
>     public void configure() throws Exception {
>         from("timer:determine?period=30000")
>                 .transform().simple("(&(objectClass=user) (sAMAccountName=MY_USER_NAME))")
>                 .log("Query body: ${body}")
>                 // Tee ldap query
>                 .to("ldap:myldap?base=ou=XXXXXX,dc=YYYYYY,dc=ZZZ")
>                 // loki
>                 .log("Received body: ${body}")
>         .end();
>     }
> }
>
> Log snip:
>
> 2017-08-28 10:46:27,268 [main           ] INFO  DefaultCamelContext            - Total 1 routes, of which 1 are started.
> 2017-08-28 10:46:27,269 [main           ] INFO  DefaultCamelContext            - Apache Camel 2.19.2 (CamelContext: camel-1) started in 0.369 seconds
> 2017-08-28 10:46:28,289 [mer://determine] INFO  route1                         - Query body: (&(objectClass=user) (sAMAccountName=MY_USER_NAME))
> 2017-08-28 10:46:28,291 [mer://determine] DEBUG SendProcessor                  - >>>> ldap://myldap?base=ou%3DXXXXXXX%2Cdc%3DXXXXXXXnet%2Cdc%3DZZZ Exchange[ID-XXXXXXXXXXX-59010-1503906386453-0-2]
> 2017-08-28 10:46:29,241 [mer://determine] INFO  route1                         - Received body: [CN=Pakarinen Jari,OU=ZZZ etc.
> 2017-08-28 10:46:58,270 [mer://determine] INFO  route1                         - Query body: (&(objectClass=user) (sAMAccountName=MY_USER_NAME))
> 2017-08-28 10:46:58,270 [mer://determine] DEBUG SendProcessor                  - >>>> ldap://myldap?base=ou%3DXXXXXXX%2Cdc%3DXXXXXXXnet%2Cdc%3DZZZ Exchange[ID-XXXXXXXXXXX-59010-1503906386453-0-4]
> 2017-08-28 10:46:58,271 [mer://determine] DEBUG DefaultErrorHandler            - Failed delivery for (MessageId: ID-WL109160-59010-1503906386453-0-3 on ExchangeId: ID-XXXXXXXXXXX-59010-1503906386453-0-4). On delivery attempt: 0 caught: 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
> 2017-08-28 10:46:58,279 [mer://determine] ERROR DefaultErrorHandler            - Failed delivery for (MessageId: ID-WL109160-59010-1503906386453-0-3 on ExchangeId: ID-WL109160-59010-1503906386453-0-4). Exhausted after delivery attempt: 1 caught: 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
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
> [route1            ] [route1            ] [timer://some_timer?period=30000                                                ] [         7]
> [route1            ] [transform1        ] [transform[simple{(&(objectClass=user) (sAMAccountName=MY_USER_NAME))}]              ] [         0]
> [route1            ] [log1              ] [log                                                                           ] [         0]
> [route1            ] [to1               ] [ldap:myldap?base=ou=XXXXXX,dc=YYYYYY,dc=ZZZ                                  ] [         2]
>
> Stacktrace
> ---------------------------------------------------------------------------------------------------------------------------------------
> 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
>         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
>         at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
>         at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
>         at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:106)
>         at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:267)
>         at org.apache.camel.component.ldap.LdapProducer.simpleSearch(LdapProducer.java:102)
>         at org.apache.camel.component.ldap.LdapProducer.process(LdapProducer.java:71)
>         at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
>         at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
>         at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
>         at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:541)
>         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
>         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198)
>         at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197)
>         at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79)
>         at java.util.TimerThread.mainLoop(Timer.java:555)
>         at java.util.TimerThread.run(Timer.java:505)
>
>
> --
> Jari
>
>



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

RE: Camel LDAP component NoInitialContextException on second poll

Posted by "Pakarinen, Jari" <ja...@orion.fi>.
Hi,

I just tried with 2.23.1 and the problem is the same. Is there something I should do differently ? The ticket you mentioned has been closed as fixed but there is no explanation what was changed. 

--
Jari


-----Original Message-----
From: Claus Ibsen <cl...@gmail.com> 
Sent: sunnuntai 3. syyskuuta 2017 17.23
To: users@camel.apache.org
Subject: Re: Camel LDAP component NoInitialContextException on second poll

Hi

Yeah its a bit unfortunatey how JNDI works in this matter where it mutate the context so after using it once its kinda fucked.

That is why if you use spring xml you can declare the bean with prototype scope, so a new context is created each time. This is a bit harder to do in java to have it create a new instance per lookup. So I have logged a ticket to make this easier.

https://issues.apache.org/jira/browse/CAMEL-11743

On Mon, Aug 28, 2017 at 10:06 AM, Pakarinen, Jari <ja...@orion.fi> wrote:
> Hi,
>
> Camel 2.19.2
>
> Running on localhost by itself, no container. Querying LDAP for user details with a timer. First query works ok (shown on line 5 of the log), second query and all subsequent queries after that give a "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" error.
>
> I've checked that the properties object I give to the route is used on the first poll, but on the second poll it is replaced by the system properties object which does not contain the properties I've set up when starting the route. Am I missing something obvious here ?
>
> Main:
>
> public class LocalTest {
>
>     public static void main(String[] args) {
>         InitialLdapContext context = null;
>         Properties props = new Properties();
>         props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
>         props.setProperty(Context.PROVIDER_URL, "ldap://xxxxxxxxx.zzz:389");
>         props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
>         props.setProperty(Context.REFERRAL, "ignore");
>         props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
>         props.setProperty(Context.SECURITY_PRINCIPAL, "user@domain");
>         props.setProperty(Context.SECURITY_CREDENTIALS, "password");
>         try {
>             context = new InitialLdapContext(props, null);
>         } catch (NamingException e) {
>             e.printStackTrace();
>         }
>         Main main = new Main();
>         main.bind("myldap", context);
>         main.addRouteBuilder(new MyRouteBuilder());
>         try {
>             main.run();
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>     }
> }
>
> Route:
>
> public class MyRouteBuilder extends RouteBuilder {
>     @Override
>     public void configure() throws Exception {
>         from("timer:determine?period=30000")
>                 .transform().simple("(&(objectClass=user) (sAMAccountName=MY_USER_NAME))")
>                 .log("Query body: ${body}")
>                 // Tee ldap query
>                 .to("ldap:myldap?base=ou=XXXXXX,dc=YYYYYY,dc=ZZZ")
>                 // loki
>                 .log("Received body: ${body}")
>         .end();
>     }
> }
>
> Log snip:
>
> 2017-08-28 10:46:27,268 [main           ] INFO  DefaultCamelContext            - Total 1 routes, of which 1 are started.
> 2017-08-28 10:46:27,269 [main           ] INFO  DefaultCamelContext            - Apache Camel 2.19.2 (CamelContext: camel-1) started in 0.369 seconds
> 2017-08-28 10:46:28,289 [mer://determine] INFO  route1                         - Query body: (&(objectClass=user) (sAMAccountName=MY_USER_NAME))
> 2017-08-28 10:46:28,291 [mer://determine] DEBUG SendProcessor                  - >>>> ldap://myldap?base=ou%3DXXXXXXX%2Cdc%3DXXXXXXXnet%2Cdc%3DZZZ Exchange[ID-XXXXXXXXXXX-59010-1503906386453-0-2]
> 2017-08-28 10:46:29,241 [mer://determine] INFO  route1                         - Received body: [CN=Pakarinen Jari,OU=ZZZ etc.
> 2017-08-28 10:46:58,270 [mer://determine] INFO  route1                         - Query body: (&(objectClass=user) (sAMAccountName=MY_USER_NAME))
> 2017-08-28 10:46:58,270 [mer://determine] DEBUG SendProcessor                  - >>>> ldap://myldap?base=ou%3DXXXXXXX%2Cdc%3DXXXXXXXnet%2Cdc%3DZZZ Exchange[ID-XXXXXXXXXXX-59010-1503906386453-0-4]
> 2017-08-28 10:46:58,271 [mer://determine] DEBUG DefaultErrorHandler            - Failed delivery for (MessageId: ID-WL109160-59010-1503906386453-0-3 on ExchangeId: ID-XXXXXXXXXXX-59010-1503906386453-0-4). On delivery attempt: 0 caught: 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
> 2017-08-28 10:46:58,279 [mer://determine] ERROR DefaultErrorHandler            - Failed delivery for (MessageId: ID-WL109160-59010-1503906386453-0-3 on ExchangeId: ID-WL109160-59010-1503906386453-0-4). Exhausted after delivery attempt: 1 caught: 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
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
> [route1            ] [route1            ] [timer://some_timer?period=30000                                                ] [         7]
> [route1            ] [transform1        ] [transform[simple{(&(objectClass=user) (sAMAccountName=MY_USER_NAME))}]              ] [         0]
> [route1            ] [log1              ] [log                                                                           ] [         0]
> [route1            ] [to1               ] [ldap:myldap?base=ou=XXXXXX,dc=YYYYYY,dc=ZZZ                                  ] [         2]
>
> Stacktrace
> ----------------------------------------------------------------------
> -----------------------------------------------------------------
> 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
>         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
>         at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
>         at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
>         at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:106)
>         at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:267)
>         at org.apache.camel.component.ldap.LdapProducer.simpleSearch(LdapProducer.java:102)
>         at org.apache.camel.component.ldap.LdapProducer.process(LdapProducer.java:71)
>         at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
>         at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
>         at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
>         at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:541)
>         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
>         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198)
>         at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197)
>         at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79)
>         at java.util.TimerThread.mainLoop(Timer.java:555)
>         at java.util.TimerThread.run(Timer.java:505)
>
>
> --
> Jari
>
>



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2