• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JNDI lookup in EJBHomeFactory pattern

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can provide some details of EJBHomeFactory class, I might be able to help you.
-Sanjiv
 
amit ang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic