[jboss-user] [EJB 3.0] - Re: Embedded EJB3 and .scanClasspath()
jc7442
do-not-reply at jboss.com
Thu Sep 28 05:46:41 EDT 2006
I have the same problem using maven. It uses several classpath. To solve this I have implemented a custom class embeddableejb3tools.EmbeddableEJB3Container(). Before starting the container I declare at least one session bean class. It will deploy all the classes deploy in the jar of this classes or in its directory. Following an example on how to use.
| EJB3Container container = new embeddableejb3tools.EmbeddableEJB3Container();
| container.getAccepted().add(
| container.getArchiveURL(MyService.class));
| container.startup();
| container.scan();
| container.shutdown();
|
The code: 3 classes: an interface for the container, an impl for the EJB3 embeddable and an exception. It can probably be improved. If you do improvement please keep me inform.
| package embeddableejb3tools;
|
| import java.util.HashSet;
|
| import javax.naming.InitialContext;
| import javax.naming.NamingException;
|
| public interface EJB3Container {
|
| /**
| * Startup the embedd EJB3 container
| *
| * @throws Exception
| */
| public abstract void startup() throws Exception;
| public void scan() throws Exception;
|
| /**
| * Shutdown the embedded EJB3 container
| *
| */
| public abstract void shutdown();
|
| public abstract String getArchiveURL(Class clazz)
| throws InitializationException;
|
| public abstract InitialContext getInitialContext() throws NamingException;
|
| public abstract boolean accept(String file);
|
| public abstract HashSet<String> getAccepted();
|
| public abstract void setAccepted(HashSet<String> accepted);
|
| public abstract HashSet<String> getIgnored();
|
| public abstract void setIgnored(HashSet<String> ignored);
|
| }
|
| package embeddableejb3tools;
|
| import java.io.File;
| import java.lang.reflect.Field;
| import java.net.URL;
| import java.net.URLClassLoader;
| import java.util.ArrayList;
| import java.util.HashSet;
| import java.util.Hashtable;
| import java.util.Iterator;
| import java.util.List;
|
| import javax.naming.InitialContext;
| import javax.naming.NamingException;
|
| import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
|
| import sun.misc.URLClassPath;
|
| /**
| *
| * @author walmetjc
| *
| */
| public class EmbeddableEJB3Container implements EJB3Container {
| private HashSet<String> accepted = new HashSet<String>();
|
| private HashSet<String> ignored = new HashSet<String>();
|
| private String originalProperty;
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#startup()
| */
| public void startup() throws Exception {
| initProperty();
| EJB3StandaloneBootstrap.boot(null);
| }
| public void scan() throws Exception {
| EJB3StandaloneBootstrap.scanClasspath();
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#shutdown()
| */
| public void shutdown() {
| EJB3StandaloneBootstrap.shutdown();
| finalizeProperty();
| }
|
| protected void initProperty() {
| originalProperty = System.getProperty("java.class.path");
| System.getProperties().put("java.class.path", getPath());
| }
|
|
| protected void finalizeProperty() {
| System.getProperties().put("java.class.path", originalProperty);
|
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#getArchiveURL(java.lang.Class)
| */
| public String getArchiveURL(Class clazz) throws InitializationException {
| try {
| String className = "/" + clazz.getCanonicalName().replace('.', '/')
| + ".class";
| URL url = getClass().getResource(className);
| String urlName = url.getFile();
| int i = urlName.indexOf('!');
| if (i != -1) {
| String jarName = urlName.substring(0, i);
| URL jarUrl = new URL(jarName);
| return jarUrl.getFile();
| } else {
| int j = urlName.indexOf(className);
| String jarName = urlName.substring(0, j + 1);
| return jarName;
| }
|
| } catch (Exception e) {
| throw new InitializationException(
| "Unable to retrieve the archive containing " + clazz, e);
| }
| }
|
| /*
| * (non-Javadoc)
| *
| * @see .embeddableejb3tools.EJB3Container#getInitialContext()
| */
| public InitialContext getInitialContext() throws NamingException {
| Hashtable props = getInitialContextProperties();
| return new InitialContext(props);
| }
|
| private Hashtable getInitialContextProperties() {
| java.util.Hashtable<String, String> props = new Hashtable<String, String>();
| props.put("java.naming.factory.initial",
| "org.jnp.interfaces.LocalOnlyContextFactory");
| props.put("java.naming.factory.url.pkgs",
| "org.jboss.naming:org.jnp.interfaces");
| return props;
| }
|
| // In JDK1.5, it seems to be impossible to get the urls from the classloader
| // using the API.
| protected List<URL> getURLs(URLClassLoader loader) {
| try {
| Field ucp = URLClassLoader.class.getDeclaredField("ucp");
| ucp.setAccessible(true);
| URLClassPath path = (URLClassPath) ucp.get(loader);
| URL[] urls = path.getURLs();
| // Usage of a list instead of a Set in order not to change the order of
| // the classpath
| List<URL> list = new ArrayList<URL>();
| for (URL url : urls) {
| if (!list.contains(url)) {
| list.add(url);
| }
| }
| return list;
| } catch (Exception e) {
| throw new RuntimeException("Unable to get urls from classloader !", e);
| }
| }
|
| protected String getPath() {
| List<URL> urls = getURLs((URLClassLoader) getClass().getClassLoader());
| Iterator<URL> iter = urls.iterator();
| String prop = "";// iter.next().getFile();
| while (iter.hasNext()) {
| String file = iter.next().getFile();
| if (accept(file)) {
| prop = prop + File.pathSeparator + file;
| }
| }
| return prop;
| }
|
| /*
| * (non-Javadoc)
| *
| * @see .embeddableejb3tools.EJB3Container#accept(java.lang.String)
| */
| public boolean accept(String file) {
| for (String pattern : getAccepted()) {
| if (file.matches(pattern)) {
| return true;
| }
| }
| for (String pattern : getIgnored()) {
| if (file.matches(pattern)) {
| return false;
| }
| }
| return false;
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#getAccepted()
| */
| public HashSet<String> getAccepted() {
| return accepted;
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#setAccepted(java.util.HashSet)
| */
| public void setAccepted(HashSet<String> accepted) {
| this.accepted = accepted;
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#getIgnored()
| */
| public HashSet<String> getIgnored() {
| return ignored;
| }
|
| /*
| * (non-Javadoc)
| *
| * @see embeddableejb3tools.EJB3Container#setIgnored(java.util.HashSet)
| */
| public void setIgnored(HashSet<String> ignored) {
| this.ignored = ignored;
| }
| }
|
| package embeddableejb3tools;
|
| public class InitializationException extends RuntimeException {
|
| /**
| *
| */
| private static final long serialVersionUID = 5277895415289316574L;
|
| public InitializationException() {
| super();
| }
|
| public InitializationException(String message, Throwable cause) {
| super(message, cause);
| }
|
| public InitializationException(String message) {
| super(message);
| }
|
| public InitializationException(Throwable cause) {
| super(cause);
| }
|
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3974813#3974813
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3974813
More information about the jboss-user
mailing list