上面三篇文章已经介绍了EJB3中JNDI调用SessionBean的初步解决方案,该部分主要是该解决方案中用到的源代码。
JNDIUtil.java
Java代码
- public class JNDIUtil {
-
- private static Log log = LogFactory.getLog(JNDIUtil.class);
-
- @SuppressWarnings("unchecked")
- public static Object lookup(Class clazz) throws NamingException {
- ComponentInfo c = getAnnotation(clazz);
- String componentName = "jndi";
- if (c != null) {
- componentName = c.name();
- }
- InputStream is = clazz.getClassLoader().getResourceAsStream(
- "jndi/" + componentName + ".properties");
- Properties p = new Properties();
- try {
- if (is != null) {
- p.load(is);
- is.close();
- } else {
- log
- .debug("没有查找到相应的配置jndi的属性文件,系统将采用默认的基于jboss的本地配置!\n"
- + "如果采取其他的配置,请在classes目录下建立jndi目录并配置相应的属性文件,默认的属性文件为jndi.properties");
- getDefaultProp(p);
- }
- } catch (IOException e) {
- e.printStackTrace();
- getDefaultProp(p);
- }
- InitialContext ctx = new InitialContext(p);
- JNDIFinder finder = JNDIFactory.getFinder(p.getProperty("server"));
- return finder.lookup(ctx, clazz);
- }
-
- private static void getDefaultProp(Properties p) {
- p.put(Context.INITIAL_CONTEXT_FACTORY,
- "org.jnp.interfaces.NamingContextFactory");
- p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jpn.interfaces");
- p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
- }
-
- @SuppressWarnings("unchecked")
- private static ComponentInfo getAnnotation(Class clazz) {
- if (clazz != null) {
- ComponentInfo c = (ComponentInfo) clazz
- .getAnnotation(ComponentInfo.class);
- if (c != null) {
- return c;
- } else {
- Class[] clas = clazz.getInterfaces();
- ComponentInfo cc = null;
- for (Class cla : clas) {
- cc = getAnnotation(cla);
- if (cc != null) {
- break;
- }
- }
- return cc;
- }
- } else {
- return null;
- }
- }
-
- }
JNDIFactory.java
Java代码
- public class JNDIFactory {
- private static Log log = LogFactory.getLog(JNDIFactory.class);
- public static JNDIFinder getFinder(String server) {
- if (JBOSS_SERVER.equals(server)) {
- log.info("当前应用为jboss的lookup");
- return new JbossJNDIFinderImpl();
- } else if (WEBLOGIC_SERVER.equals(server)) {
- log.info("当前应用为weblogic的lookup");
- return new WeblogicJNDIFinderImpl();
- }
- return new JbossJNDIFinderImpl();
- }
- private final static String JBOSS_SERVER = "jboss";
- private final static String WEBLOGIC_SERVER = "weblogic";
- }
JNDIFinder.java
Java代码
- public interface JNDIFinder {
- public Object lookup(InitialContext ctx, Class clazz)
- throws NamingException;
- }
JbossJNDIFinderImpl.java
Java代码
- public class JbossJNDIFinderImpl implements JNDIFinder {
- public Object lookup(InitialContext ctx, Class clazz)
- throws NamingException {
- String s = "remote";
- Local local = (Local) clazz.getAnnotation(Local.class);
- if (local != null) {
- s = "local";
- }
- return ctx.lookup(clazz.getSimpleName() + "/" + s);
- }
-
- }
WeblogicJNDIFinderImpl.java
Java代码
- public class WeblogicJNDIFinderImpl implements JNDIFinder {
- public Object lookup(InitialContext ctx, Class clazz)
- throws NamingException {
- return ctx.lookup(clazz.getSimpleName() + "#" + clazz.getName());
- }
- }
ComponentInfo.java
Java代码
- @Target( { TYPE })
- @Retention(RetentionPolicy.RUNTIME)
- public @interface ComponentInfo {
- String name();
- String description() default "";
- }
该组件全局的接口:DefaultCoreService.java
Java代码
- @ComponentInfo(name = "oecp-core", description = "oecp平台核心组件,提供了整个组件平台运行的环境和相关通用功能")
- public interface DefaultCoreService {
-
- }