Last active
September 2, 2018 21:39
-
-
Save StefanHeimberg/0962036ee26d943b3baa to your computer and use it in GitHub Desktop.
java ee 6 lookup utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.stefanheimberg.example.support; | |
import javax.enterprise.context.spi.CreationalContext; | |
import javax.enterprise.inject.spi.Bean; | |
import javax.enterprise.inject.spi.BeanManager; | |
public final class CDIUtil { | |
public static <T> T lookupByName(final String name) { | |
final BeanManager bm = getBeanManager(); | |
final Bean<T> bean = (Bean<T>) bm.getBeans(name).iterator().next(); | |
final CreationalContext<T> ctx = bm.createCreationalContext(bean); | |
return (T) bm.getReference(bean, bean.getClass(), ctx); | |
} | |
public static <T> T lookupByType(final Class<T> type) { | |
final BeanManager bm = getBeanManager(); | |
final Bean<T> bean = (Bean<T>) bm.getBeans(type).iterator().next(); | |
final CreationalContext<T> ctx = bm.createCreationalContext(bean); | |
return (T) bm.getReference(bean, type, ctx); | |
} | |
public static BeanManager getBeanManager() { | |
return JNDIUtil.lookupJndi("java:comp/BeanManager", BeanManager.class); | |
} | |
private CDIUtil() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.stefanheimberg.example.support; | |
public final class EJBUtil { | |
/** | |
* The java:global JNDI namespace is the portable way of finding remote | |
* enterprise beans using JNDI lookups. JNDI addresses are of the following | |
* form: | |
* | |
* java:global[/application name]/module name/enterprise bean | |
* name[/interface name] | |
* | |
* Application name and module name default to the name of the application | |
* and module minus the file extension. Application names are required only | |
* if the application is packaged within an EAR. The interface name is | |
* required only if the enterprise bean implements more than one business | |
* interface. | |
* | |
* @param applicationName | |
* @param moduleName | |
* @param ejbName | |
* @param interfaceName | |
* @param type | |
*/ | |
public static <T> T lookupGlobal(final String applicationName, final String moduleName, final String ejbName, final String interfaceName, final Class<T> type) { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append("java:global"); | |
if (null != applicationName && !applicationName.isEmpty()) { | |
sb.append("/").append(applicationName); | |
} | |
sb.append("/").append(moduleName); | |
sb.append("/").append(ejbName); | |
if (null != interfaceName && !interfaceName.isEmpty()) { | |
sb.append("/").append(interfaceName); | |
} | |
return JNDIUtil.lookupJndi(sb.toString(), type); | |
} | |
/** | |
* The java:module namespace is used to look up local enterprise beans | |
* within the same module. JNDI addresses using the java:module namespace | |
* are of the following form: | |
* | |
* java:module/enterprise bean name[/interface name] | |
* | |
* The interface name is required only if the enterprise bean implements | |
* more than one business interface. | |
* | |
* @param ejbName | |
* @param interfaceName | |
* @param type | |
*/ | |
public static <T> T lookupModule(final String ejbName, final String interfaceName, final Class<T> type) { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append("java:module/").append(ejbName); | |
if (null != interfaceName && !interfaceName.isEmpty()) { | |
sb.append("/").append(interfaceName); | |
} | |
return JNDIUtil.lookupJndi(sb.toString(), type); | |
} | |
/** | |
* The java:app namespace is used to look up local enterprise beans packaged | |
* within the same application. That is, the enterprise bean is packaged | |
* within an EAR file containing multiple Java EE modules. JNDI addresses | |
* using the java:app namespace are of the following form: | |
* | |
* java:app[/module name]/enterprise bean name[/interface name] | |
* | |
* The module name is optional. The interface name is required only if the enterprise bean implements more than one business interface. | |
* | |
* @param moduleName | |
* @param ejbName | |
* @param interfaceName | |
* @param type | |
*/ | |
public static <T> T lookupApp(final String moduleName, final String ejbName, final String interfaceName, final Class<T> type) { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append("java:app"); | |
if (null != moduleName && !moduleName.isEmpty()) { | |
sb.append("/").append(moduleName); | |
} | |
sb.append("/").append(ejbName); | |
if (null != interfaceName && !interfaceName.isEmpty()) { | |
sb.append("/").append(interfaceName); | |
} | |
return JNDIUtil.lookupJndi(sb.toString(), type); | |
} | |
private EJBUtil() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.stefanheimberg.example.support; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
public final class JNDIUtil { | |
private static final Logger LOG = Logger.getLogger(JNDIUtil.class.getName()); | |
public static <T> T lookupJndi(final String jndiName, final Class<T> type) { | |
try { | |
InitialContext initialContext = new InitialContext(); | |
return (T) initialContext.lookup(jndiName); | |
} catch (NamingException e) { | |
LOG.log(Level.SEVERE, "Couldn''t get {0} with jndiName {1}", new Object[]{type.getName(), jndiName}); | |
return null; | |
} | |
} | |
private JNDIUtil() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment