Would like to find out whether we can call Stateless Ejb in class static block to retrieve data from database, the code as captured below ? Thanks.
public class MyCacheManager {
private static final HashMap states = new HashMap();
//Can we inject stateless ejb here ?
public static String getState(String abbrev) {
return (String) states.get(abbrev);
}
static {
// can we call stateless ejb method here to retrieve data from
database ?
//JDBC stuff to load the data
}
}
Updated Comment
Instead of using static block, I have decided to use Singleton class, as understand singleton is suitable to store information, while static block usually used to create common functions.
Due to certain reason, I need to use POJO to implement the Singleton pattern with JDK 6 now, to store the static hashmap, instead of using Singleton EJB. The program call flow and structure as per below, thus would like to get advice whether it is technically feasible, including below queries ? Thanks, Leanne.
1) ServletContextListener class to instantiate below Singleton POJO using MySingleton.getInstance() method ?
2) Singleton Class injected with Session EJB to call DAO to populate the static hashmap using @Inject with CDI beans.xml ?
3) All the EJBs to call the public method of singleton Pojo to access the static hashmap using MySingleton.getInstance().authenticateLogin syntax?
4) What is the different if I use "final class MySingleton" or without final ?
package org.common.authenticate;
public final class MySingleton {
private static MySingleton instance;
private HashMap<String, AuthenInfo> authenInfo;
// Inject EJB to call DAO for retrieving authentication information?
@Inject
private MyAuthenticateEjb myAuthenticateEjb;
private MySingleton() {
// Use above MyAuthenticateEjb to retrieve authenticate info and
// populate the HashMap authenInfo
}
public static MySingleton getInstance()
{
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
public Boolean authenticateLogin(String userNm, String pwd, String
apiName) {
// use above authenInfo HashMap to authenticate whether system is
// authorised to the call API
}
}