I had the same problem:
http://www.jboss.com/index.html?module=bb&op=viewtopic&t=82909
To workaround I wrote an horrible class. Have a look to the comment for
checkPersistenceXML.
I think that there is some comment in the class. If that's not clear, I'll try
to explian more precisely my problem. With that hoorible hook I'm able to run
my testcase with maven and eclipse.
With maven ejb3 are not yet in jar files. For me there is a bug persistence.xml
and ejb-jar.xml are not searched in directory, only in jar files. That's why I
need to copy xml in another directory.
The other problem was that with maven maven uses classloader (java.class.path
is not he classpath used). Consequently automatic scan does not works. That's
why I need to declare explicitely jar and directories to scan.
| import java.io.File;
| import java.io.FileReader;
| import java.io.FileWriter;
| import java.io.IOException;
| import java.lang.reflect.Field;
| import java.net.URL;
| import java.net.URLClassLoader;
| import java.util.ArrayList;
| import java.util.Hashtable;
| import java.util.List;
|
| import javax.naming.InitialContext;
| import javax.naming.NamingException;
|
| import junit.framework.TestCase;
|
| import org.apache.commons.logging.Log;
| import org.apache.commons.logging.LogFactory;
| import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
| import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;
|
| import sun.misc.URLClassPath;
|
| /**
| * Tas de boue .......
| *
| *
| */
| public class EmbeddableEJB3TestCase extends TestCase {
| private boolean scanJars = false;
|
| private boolean scanDirs = true;
|
| private final Log LOGGER = LogFactory.getLog(EmbeddableEJB3TestCase.class
| .getName());
|
| private List<File> tempFiles = new ArrayList<File>();
|
| @Override
| protected void setUp() throws Exception {
| EJB3StandaloneBootstrap.boot(null);
| // scanClasspath method scan ejb to deplot using java.class.path.
| // Unfortunately with maven this classpath
| // does not contain the class of the project. Consequently the
abominable
| // hook is to deploy class using the deployer.
| // EJB3StandaloneBootstrap.scanClasspath();
| EJB3StandaloneDeployer deployer = initDeployer();
| deployer.create();
| deployer.start();
|
| // End of the abominable hook
| }
|
| protected EJB3StandaloneDeployer initDeployer() throws Exception {
| EJB3StandaloneDeployer deployer =
EJB3StandaloneBootstrap.createDeployer();
|
| URLClassLoader loader = (URLClassLoader) getClass().getClassLoader();
| List<URL> urls = getURLs(loader);
|
| for (URL url : urls) {
| boolean isUsed = false;
| File file = new File(url.getFile());
| if (file.isDirectory()) {
| if (isScanDirs()) {
| deployer.getDeployDirs().add(url);
| isUsed = true;
| // Another hook
| List<File> files = checkPersistenceXML(deployer, file);
| getTempFiles().addAll(files);
| // END of another hook
| }
| } else if (file.isFile() && file.getName().endsWith(".jar")) {
| if (isScanJars()) {
| deployer.getArchives().add(url);
| isUsed = true;
| }
| }
| if (isUsed) {
| LOGGER.info("Scan " + url + " for deploy in Embeddable EJB3 !");
| } else {
| LOGGER.debug(url + " is not used for deploy in Embeddable EJB3 !");
| }
| }
|
| return deployer;
|
| }
|
| @Override
| protected void tearDown() throws Exception {
| EJB3StandaloneBootstrap.shutdown();
| for (File file : getTempFiles()) {
| file.delete();
| }
| getTempFiles().clear();
| }
|
| // 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);
| }
| }
|
| public InitialContext getInitialContext() {
| Hashtable props = getInitialContextProperties();
| try {
| return new InitialContext(props);
| } catch (NamingException e) {
| fail("Unable to get the initial context !");
| // fail throws an exception, it sould never go there.
| throw new RuntimeException(
| "fail throws an exception, it sould never go there.", e);
| }
| }
|
| private Hashtable getInitialContextProperties() {
| 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;
| }
|
| public boolean isScanDirs() {
| return scanDirs;
| }
|
| public void setScanDirs(boolean scanDirs) {
| this.scanDirs = scanDirs;
| }
|
| public boolean isScanJars() {
| return scanJars;
| }
|
| public void setScanJars(boolean scanJars) {
| this.scanJars = scanJars;
| }
|
| /**
| * Hook: persistence.xml and ejb-jar.xml contains in deployement
dierctories
| * are not used by the standalone ejb3 deployer. Their directory needs to
be
| * added in the list of Jar files. If the directory containing annotated
| * classes is added in jar files list, deployer send a warning.
Consequently a
| * fake directory containig only persistence.xml and ejb-jar.xml has to be
| * created and added to the deployer.
| *
| * That is horrible !!!
| *
| * @param deployer
| * @param file
| *
| * @return List of files copied by this method
| */
| protected List<File> checkPersistenceXML(EJB3StandaloneDeployer deployer,
| File file) throws IOException {
| // Init the list of created file in this method
| List<File> createdFiles = new ArrayList<File>();
|
| final String META_INF = "META-INF/";
| final String PERSISTENCE_XML = META_INF + "persistence.xml";
| final String EJB_JAR_XML = META_INF + "ejb-jar.xml";
| // XML will be added in the temp directory
| List<File> treeFiles = getPathFiles(file);
| String ROOT_DIR = System.getProperty("java.io.tmpdir") + "/ejb3.tests/";
| String TEMP_DIR = ROOT_DIR + getFileName(file);
|
| // Check if xml have to be copied
| File persistence = new File(file, PERSISTENCE_XML);
| File ejb = new File(file, EJB_JAR_XML);
|
| if (persistence.exists() || ejb.exists()) {
| // TempFile are used in order to be automatically deleted when JVM
exits.
| File dirFile = new File(TEMP_DIR);
| if (dirFile.exists()) {
| // If directory contains files or subdirectory throw an exception
| if (!dirFile.delete()) {
| throw new RuntimeException("Danger: Directory " + dirFile
| + " should not exists !!! Delete it manually !");
| }
| }
| dirFile.mkdirs();
| File metainf = new File(dirFile, META_INF);
| metainf.mkdir();
| if (persistence.exists()) {
| createdFiles.add(copyFile(persistence, metainf));
| }
| if (ejb.exists()) {
| createdFiles.add(copyFile(ejb, metainf));
| }
|
| deployer.getArchives().add(dirFile.toURL());
| createdFiles.add(metainf);
| for (File ff : treeFiles) {
| File dir = new File(ROOT_DIR, getFileName(ff));
| createdFiles.add(dir);
| }
| }
| return createdFiles;
| }
|
| protected File copyFile(File src, File targetDir) throws IOException {
| LOGGER.info("Copy " + src + " to " + targetDir);
| FileReader reader = new FileReader(src);
| File dest = new File(targetDir, src.getName());
| dest.createNewFile();
| FileWriter writer = new FileWriter(dest);
| int r = 0;
| while ((r = reader.read()) != -1) {
| writer.write(r);
| }
| writer.flush();
| writer.close();
| reader.close();
| return dest;
| }
|
| protected List<File> getTempFiles() {
| return tempFiles;
| }
|
| protected void setTempFiles(List<File> tempFiles) {
| this.tempFiles = tempFiles;
| }
|
| /**
| * Return the list of files for the path structure
| *
| * @param f
| * @return
| */
| protected List<File> getPathFiles(File f) {
| List<File> files = new ArrayList<File>();
| if (f.isDirectory()) {
| files.add(f);
| }
| File parent = f;
| while (parent != null) {
| parent = parent.getParentFile();
| // Drivers letter is excluded from the list
| if (parent != null && parent.getParentFile() != null) {
| files.add(parent);
| }
| }
| return files;
| }
|
| protected <T> T getService(Class<T> interfac) {
| try {
| InitialContext ctx = getInitialContext();
| T local = (T) ctx.lookup("/" + interfac.getSimpleName() + "/local");
| return local;
| } catch (NamingException e) {
| throw new RuntimeException("Unable to retieve service !", e);
| }
| }
|
| protected String getFileName(File f) {
| return f.getPath().substring(2);
| }
| }
|
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3948141#3948141
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3948141
-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user