Announcement Announcement Module
Collapse
No announcement yet.
Getting ClassCastException even when I am not using custom DirObjectFactory. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting ClassCastException even when I am not using custom DirObjectFactory.

    I have configuration something like this,
    Code:
    <bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource">
    
    <property name="urls" value="${ldapconnection.urls}" />
    
    <property name="base" value="${ldapconnection.base}" />
    
    <property name="userDn" value="${ldapconnection.userDn}" />
    
    <property name="password" value="${ldapconnection.password}" />
    
    <property name="pooled" value="false"/>
    
    <property name="baseEnvironmentProperties">
    
    <map>
    
    <entry key="com.sun.jndi.ldap.connect.timeout" value="${ldapconnection.timeout}" />
    
    </map>
    
    </property>
    
    </bean>
    
    <bean id="dirContextValidator"
    
    class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" />
    
    <bean id="contextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource">
    
    <property name="contextSource" ref="contextSourceTarget" />
    
    <property name="dirContextValidator" ref="dirContextValidator" />
    
    <property name="testOnBorrow" value="${ldappool.testOnBorrow}" />
    
    <property name="testWhileIdle" value="${ldappool.testWhileIdle}" />
    
    <property name="minIdle" value="${ldappool.minIdle}" />
    
    <property name="maxIdle" value="${ldappool.maxIdle}" />
    
    <property name="maxActive" value="${ldappool.maxActive}" />
    
    <property name="maxWait" value="${ldappool.maxWait}" />
    
    <property name="whenExhaustedAction" value="${ldappool.whenExhaustedAction}" />
    
    <property name="timeBetweenEvictionRunsMillis" value="${ldappool.timeBetweenEvictionRunsMillis}" />
    
    <property name="minEvictableIdleTimeMillis" value="${ldappool.minEvictableIdleTimeMillis}" />
    
    <property name="numTestsPerEvictionRun" value="${ldappool.numTestsPerEvictionRun}" />
    
    </bean>
    
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
    
    <constructor-arg ref="contextSource" />
    
    </bean>
    
    <bean id="tripletContextMapper" class="com.anand.MyContextMapper">
    
    <property name="name" value="name" />
    
    </bean>
    
    <bean id="ldapSearch" class="com.anand.LDAPSearch">
    
    <property name="searchDn" value="${search.Dn}" />
    
    <property name="tripletContextMapper" ref="tripletContextMapper" />
    
    </bean>
    
    </beans>
    From above, You can see that, I have not configured any DirObjectFactory but still getting ClassCastException as shown below,

    1|localhost|MyAdapter|2009-06-16 17:54:49|ldap search error: : null
    java.lang.ClassCastException
    at org.springframework.ldap.core.support.AbstractCont extMapper.mapFromContext(AbstractContextMapper.jav a:43)
    at org.springframework.ldap.core.LdapTemplate$14.exec uteWithContext(LdapTemplate.java:869)
    at org.springframework.ldap.core.LdapTemplate.execute WithContext(LdapTemplate.java:784)
    at org.springframework.ldap.core.LdapTemplate.execute ReadOnly(LdapTemplate.java:771)
    at org.springframework.ldap.core.LdapTemplate.lookup( LdapTemplate.java:865)


    I am running this in a startup servlet inside Welogic8.2

    Even after upgrading the spring-ldap jar to 1.3 still getting the same exception... Any idea?? wat could be the problem????

  • #2
    Forgot to mention,

    When i run my sample code in a separate helloworld program, it runs successfully. But when i deply this client program as servlet inside the weblogic container, it fails ....

    Comment


    • #3
      A previous post reports similar problems when running inside an application server. It appears that the problem is caused by class loading issues; in particular it appears that you will need to load the Spring Application in the same (or a parent of the) ClassLoader that the executing thread is using, or else this will not work.

      Comment


      • #4
        Hi..

        The issue mentioned in the previous post is quite different from what I am facing..
        In previous post he is getting following exception...

        java.lang.ClassCastException: com.sun.jndi.ldap.LdapCtx cannot be cast to org.springframework.ldap.core.DirContextAdapter

        Looks like it is related to DefaultDirO bjectFactory but I am not using any DirObjFactory. I am using my TriplexContextMapper.

        Comment


        • #5
          You're not reading it properly. What is causing ANY ContextMapper to work is the DefaultDirObjectFactory, which takes care of the translation of an ordinary DirContext (which is what is returned by Java LDAP) to a DirContextAdapter instance.

          So your issue is very much related to the DefaultDirObjectFactory.

          Comment


          • #6
            Okkie got it...

            Conclusion is that....

            Both classloaders of ( the one which creates context source object and the one which looksup ) should be same?????

            My scenario is something like this,

            I am creating ldapHelper object which creates contextSource, ldapTemplate etc in a startup servlet and putting this ldapHelper inside some shared hashmap. This servlet will be in a different web application. After that, I am getting the ldaphelper object from hashmap in a different application ( ear ), and trying to lookup through ldaphelper object then i endup with is exception...


            If I am not wrong, the reason is due to ClassLoader mismatch.... right???

            Any work around for the same?????

            Comment


            • #7
              Hi..

              Found a work around for this,


              Just change the classloader of the executing thread to the classloader of the the class which loaded the contextsource, ldaptemplate etc...
              Code:
              ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
                          Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
              
              Object obj =  ldapTemplate.lookup(dn, ldapTemplate);
              
              Thread.currentThread().setContextClassLoader(originalClassLoader);

              Regards
              Anand

              Comment

              Working...
              X