By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

accessing CMP from Session Bean

P: n/a
In my project I have to access in one business method of session bean
usually more then 10 CMP Beans. I would like to have some utils classes,
which could get me right instance of CMP or create one. Instead of making
the same thing again and again. My proposition looks like:

1) Class for taking home interface:
public final class JNDILookup {
private static Context initialContext;
private static HashMap homeInterfaces;
private static Context getInitialContext() throws NamingException {
if (initialContext == null) {
Context context = new InitialContext();
initialContext = (Context) context.lookup("local:");
}
return initialContext;
}

private static HashMap getHomeInterfaces() {
if (homeInterfaces == null) {
homeInterfaces = new HashMap();
}
return homeInterfaces;
}

public static EJBLocalHome getLocalHome(String beanName) throws
Exception {
HashMap map = getHomeInterfaces();
EJBLocalHome localHome = (EJBLocalHome) map.get(beanName);
if (localHome == null) {
try {
localHome = (EJBLocalHome)
getInitialContext().lookup(beanName);
map.put(beanName,localHome);
} catch (NamingException e) {
e.printStackTrace();
throw new Exception("Naming exception: " + beanName);
}
}//end if
return localHome;
}
}

2) Bean ames are define in interface as a static final fields

3) For each CMP bean I would like to have a Util class based on the tamplate

public class CMP_NAMEUtil extends CMPUtil {
private static CMP_NAMELocalHome localHome;
private static CMP_NAMELocalHome getLocalHome() throws Exception {
if (localHome == null) {
localHome = (CMP_NAMELocalHome)
JNDILookup.getLocalHome(JNDIName.CMP_NAME);
}
return localHome;
}
public static CMP_NAMELocal create(String someParam1, Integer
otherParam2) throws Exception {
BigDecimal key =
getPKGeneratorLocal().getNextPKValue(TableName.CMP _NAME);
try {
return getLocalHome().create(key, someParam1, otherParam2);
} catch (CreateException e1) {
throw new Exception(e1.toString());
}
}
public static CMP_NAMELocal getBean(BigDecimal key) throws Exception {
try {
return getLocalHome().findByPrimaryKey(new CMP_NAMEKey(key));
} catch (FinderException e) {
e.printStackTrace();
throw new NoSuchObjectException(e.toString());
}
}
}

3) and based class has method to generate session bean responisble for
generating primary key (based on some standard pattern)

public abstract class CMPUtil {
protected static PKGeneratorLocal getPKGeneratorLocal() throws
MetaConnException {
PKGeneratorLocal pkLocal;
PKGeneratorLocalHome pkLocalHome = (PKGeneratorLocalHome)
JNDILookup.getLocalHome(JNDIName.PKGenerator);
try {
pkLocal = pkLocalHome.create();
} catch (CreateException e) {
throw new Exception("Cannot create session bean");
}
return pkLocal;
}
}

Could anyone says something about this code. I use a static methods, but I
think that's method getLocalHome() can cause some threads problem, but I
don't know if critical. How do you think, can I use such classes in session
bean or should I do it directly within the session bean? How do you solve
this problem. Do you know any online resource, books?
Thanks in advance
Borek

Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.