!--a11y-->
Accessing Application, Library or
Service 
The relevant classes of an application or service can be looked up in JNDI. In order the JNDI implementation finds the corresponding object factory, the thread context class loader must be set. The class loader, which is set as thread context class loader, is obtained from the current class.
The setting of the thread context class loader is also needed for calls to the P4 Object Broker like narrow() and the usage of DOM and JAXP.
After the needed operations have been made, the thread context class loader must be set back to the original one. See the example below on how to do this.
If a library is used, the needed classes can be directly imported and used. This means when dealing with libraries, it is not needed to access JNDI nor to deal with the thread context class loader.
This is an example of a portal application, which uses an EJB. Note the handling of the thread context class loader:
import com.sapportals.portal.prt.component.AbstractPortalComponent; import com.sapportals.portal.prt.component.IPortalComponentRequest; import com.sapportals.portal.prt.component.IPortalComponentResponse; import com.sapportals.portal.prt.runtime.PortalRuntime; import com.sap.engine.services.rmi_p4.P4ObjectBroker; import com.sap.engine.frame.core.load.ReferencedLoader; import com.inqmy.examples.ejb.hello.stateless.HelloHome; import com.inqmy.examples.ejb.hello.stateless.Hello; import javax.naming.*;
public class PARInEARPortalApp extends AbstractPortalComponent { public void doContent( IPortalComponentRequest request, IPortalComponentResponse response) { String key = "Hello/Stateless/HelloStatelessBean";
ClassLoader epLoader = Thread.currentThread().getContextClassLoader();
try { Thread.currentThread().setContextClassLoader( getClass().getClassLoader()); Context ctx = new InitialContext(); java.util.Hashtable env = ctx.getEnvironment();
Object obj = ctx.lookup(key);
ClassLoader loader = HelloHome.class.getClassLoader(); HelloHome home = (HelloHome) P4ObjectBroker.init().narrow(obj, HelloHome.class); Hello hello = home.create(); } catch (Throwable e) { PortalRuntime.getLogger().warning(e, ""); e.printStackTrace(); response.write(e.toString()); } finally { Thread.currentThread().setContextClassLoader(epLoader); } } } |
