cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException on JNDI lookup

Former Member
0 Kudos

Hi!

When I try to lookup the LogicalLockingFactory interface, I receive a ClassCastException.

For the lookup I use the following code:

LogicalLockingFactory lockingFactory = (LogicalLockingFactory) new InitialContext().lookup(
					LogicalLockingFactory.JNDI_NAME);

I run on SAP Netweaver 7.1

Thanks in advance!

Gernot

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi Gernot,

You receive this ClassCastException probably because the class loader which tries to load the class is not application classloader. I suggest change the class where you change the classloader.

So you should change the source like this

ClassLoader oldClassLoader =                                                                   
Thread.currentThread().getContextClassLoader();                                                
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoa                       
der());                                                                                
try {                                                                                
//Perform the lookup here                                                                                
} finally { // this step is very important!                                                    
   Thread.currentThread().setContextClassLoader(oldClassLoader);                                  
}

It is very important to return back the original contextClassLoader when the lookup finishes.

Best regards,

Martin

rosh
Participant
0 Kudos

Hi Martin,

this Classloader-spoofing seems to be quite widespread. I thought there where only 2 people that ever tried a llf-lookup in 7.1

Do you have any info on whether the issue will be fixed? (And how exactly the sample adapter is tested before shipment)

Answers (2)

Answers (2)

Former Member
0 Kudos

I also have the same problem. But mine is a bit different as my application in EJB and i have a Java Program using the EJB .jar as lib file. Previously the EJB is compile in Netweaver 2004 and now i'm compiling the same one in Netweaver 2004s (7.0) and i'm hitting this ClassCastException in my JNDI. This is how i use the JNDI.


Gateway gateway = null;

try{
     Properties env = new Properties();
     env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl") ;
     env.setProperty( javax.naming.Context.PROVIDER_URL, url);
     Context ic = new InitialContext(env);
     GatewayHome home = (GatewayHome)ic.lookup("GATEWAY");
     gateway = home.create();
}
......

I get the ClassCastException in the lookup. It can't find. But when i changed back to the old EJB, it able to work.

rosh
Participant
0 Kudos

In what kind of application are you performing the lookup? An EJB?

Former Member
0 Kudos

I developed an SFTP File Adapter that is running in the Advanced Adapter Engine.

I used the Sample Adapter from SAP as a template for my implementation.

Best regards,

gernot

rosh
Participant
0 Kudos

Hi Gernot,

I've had the same problem and opened a sap support call. I was told that there's class loader issues in the Appserver and got the suggestion to use

>

>ClassLoader oldClassLoader =

>Thread.currentThread().getContextClassLoader();

>Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

>try {

>//[lookup]

>} finally { // this step is very important!

>Thread.currentThread().setContextClassLoader(oldClassLoader);

>}

which did the trick.

NOTE: the above code is to work around an issue in 7.10 classloading in adapter applications. Do not use it in other scenarios