File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes JNDI lookup in EJBHomeFactory pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JNDI lookup in EJBHomeFactory pattern" Watch "JNDI lookup in EJBHomeFactory pattern" New topic
Author

JNDI lookup in EJBHomeFactory pattern

amit ang
Ranch Hand

Joined: Sep 28, 2001
Posts: 38
Hi Everybody,
Can any on tell me how can u do lookup at EJBHomeFactory class through my client when iam using some app.
It says u can write ur jndi name in the ejb-jar.xml file, Is there any specific tag for the same.
It also says u can pass it as -D.. Parameter in case of standalone app.
THanxs in Adv.
Amit
Sanjiv Pansari
Greenhorn

Joined: Oct 10, 2001
Posts: 4
If you can provide some details of EJBHomeFactory class, I might be able to help you.
-Sanjiv
amit ang
Ranch Hand

Joined: Sep 28, 2001
Posts: 38
This is a Pattern used for not creating unnesscary Home objects. EJBHomeFactory is a factory class which is used to make use of the home object once created instead of creating one for the each client.This class uses the Map for the same.
An EJB client needs to lookup an EJB home object, but multiple lookups of the same home are redundant.
Now here it says:
JNDI environment entries are refactored to external descriptors. Instead of manually placing hardcoded JNDI Factory and URL strings into a properties object and passing this into the InitialContext constructor, these properties should be externalized to the clients deployment descriptor or runtime system properties (web.xml for web apps, ejb-jar.xml for ejb�s, the �D flag for a standalone java client, etc). That way, the InitialContext object can find these properties within the System properties automatically at runtime, simplifying the factory implementation and making it more reusable.

Here i have implemented this pattern for my Entity bean example.But, hardcoded the JNDI Name and the property class is used for getting the context object.
This pattern is available at TheServerSide.com
Amit
Originally posted by Sanjiv Pansari:
If you can provide some details of EJBHomeFactory class, I might be able to help you.
-Sanjiv

Sanjiv Pansari
Greenhorn

Joined: Oct 10, 2001
Posts: 4
My EJBHomeFactory.java
package com.sanjiv.ejb.prototype.factories;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author Sanjiv Pansari
* @version 1.0
*/
public class EJBHomeFactory {
private Map ejbHomes;
private static EJBHomeFactory aFactorySingleton;
Context ctx;
private EJBHomeFactory() throws NamingException {
try {
ctx = getInitialContext();
} catch (Exception e) {
System.out.println("Failed initializing bean access.");
e.printStackTrace();
}
this.ejbHomes = Collections.synchronizedMap(new HashMap());
}
/*
* Returns the singleton instance of the EJBHomeFactory
**/
public static EJBHomeFactory getFactory() throws NamingException {
if (EJBHomeFactory.aFactorySingleton == null ) {
EJBHomeFactory.aFactorySingleton = new EJBHomeFactory();
}
return EJBHomeFactory.aFactorySingleton;
}
/**
* Lookup and cache an EJBHome object
**/
public EJBHome lookUpHome(String jndiName, Class homeClass) throws NamingException {
EJBHome anEJBHome = (EJBHome)this.ejbHomes.get(homeClass);
if (anEJBHome == null) {
//look up jndi name
Object ref = ctx.lookup(jndiName);
anEJBHome = (EJBHome) PortableRemoteObject.narrow(ref, homeClass);
this.ejbHomes.put(homeClass, anEJBHome);
}
return anEJBHome;
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
}
catch(Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
return new InitialContext(properties);
}
}

/*****/
And this is how I do lookup for an EJB

sessionEJBTestHome = (SessionEJBTestHome) EJBHomeFactory.getFactory().lookUpHome("SessionEJBTest",SessionEJBTestHome.class);
I have defined lookUpHome() method to take 2 parameter
1) Jndi name of the bean
2) HomeInterfaceClass
I hope it helps.
This factory class will work lokup only in the WebLogic Application as you can see the getInitialContext() method is specific to weblogic. Do you know how to optimize it so that the properties can be read from a property file?
rgds
Sanjiv
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNDI lookup in EJBHomeFactory pattern
 
Similar Threads
Business Delegate/Session Facade fitting into an MVC application
JNDI LOOKUP
Replacing T3 and JNP transport protocols with HTTP protocol for EJB JNDI Lookup
EJB in jeopardy ?
getting exception