[jboss-cvs] JBossAS SVN: r102640 - in projects/scanning/trunk: plugins and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Mar 19 17:24:42 EDT 2010
Author: alesj
Date: 2010-03-19 17:24:41 -0400 (Fri, 19 Mar 2010)
New Revision: 102640
Added:
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternFilter.java
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java
projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/WeakClassLoaderHolder.java
Removed:
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/WeakClassLoaderHolder.java
Modified:
projects/scanning/trunk/plugins/pom.xml
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/AbstractElement.java
projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/MutableAnnotationRepository.java
projects/scanning/trunk/pom.xml
Log:
Add Hibernate support - initial impl of HEM's Scanner.
Modified: projects/scanning/trunk/plugins/pom.xml
===================================================================
--- projects/scanning/trunk/plugins/pom.xml 2010-03-19 20:11:57 UTC (rev 102639)
+++ projects/scanning/trunk/plugins/pom.xml 2010-03-19 21:24:41 UTC (rev 102640)
@@ -58,6 +58,10 @@
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ </dependency>
<dependency>
<groupId>junit</groupId>
Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/AbstractElement.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/AbstractElement.java 2010-03-19 20:11:57 UTC (rev 102639)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/AbstractElement.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -27,6 +27,7 @@
import java.lang.reflect.AnnotatedElement;
import org.jboss.scanning.annotations.spi.Element;
+import org.jboss.scanning.plugins.helpers.WeakClassLoaderHolder;
/**
* Abstract annotations element.
Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/MutableAnnotationRepository.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/MutableAnnotationRepository.java 2010-03-19 20:11:57 UTC (rev 102639)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/MutableAnnotationRepository.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -27,7 +27,7 @@
import org.jboss.metadata.spi.signature.Signature;
import org.jboss.scanning.annotations.spi.AnnotationRepository;
-import org.jboss.scanning.spi.ScanningHandle;
+import org.jboss.scanning.plugins.helpers.WeakClassLoaderHolder;
/**
* Mutable annotation repository.
Deleted: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/WeakClassLoaderHolder.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/WeakClassLoaderHolder.java 2010-03-19 20:11:57 UTC (rev 102639)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/WeakClassLoaderHolder.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -1,80 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.scanning.annotations.plugins;
-
-import org.jboss.util.JBossObject;
-
-import java.lang.ref.WeakReference;
-
-/**
- * ClassLoader holder helper.
- *
- * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
- */
-abstract class WeakClassLoaderHolder extends JBossObject
-{
- private transient WeakReference<ClassLoader> clRef;
-
- public WeakClassLoaderHolder(ClassLoader classLoader)
- {
- if (classLoader == null)
- throw new IllegalArgumentException("Null classloader");
-
- clRef = new WeakReference<ClassLoader>(classLoader);
- }
-
- /**
- * Get the classloader from weak ref.
- *
- * @return the classloader
- */
- protected ClassLoader getClassLoader()
- {
- if (clRef == null)
- throw new IllegalArgumentException("Null classloader ref, previously serialized?");
-
- ClassLoader classLoader = clRef.get();
- if (classLoader == null)
- throw new IllegalArgumentException("ClassLoader was already garbage collected.");
-
- return classLoader;
- }
-
- /**
- * Load class from class name.
- *
- * @param className the class name
- * @return loaded class
- */
- protected Class<?> loadClass(String className)
- {
- try
- {
- return Class.forName(className, false, getClassLoader());
- }
- catch (ClassNotFoundException e)
- {
- throw new RuntimeException(e);
- }
- }
-}
Added: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternFilter.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternFilter.java (rev 0)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternFilter.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -0,0 +1,59 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.scanning.hibernate;
+
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.VirtualFileFilter;
+
+/**
+ * Mock work of NativeScanner matching.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class PatternFilter implements VirtualFileFilter
+{
+ private String pattern;
+ private boolean exact = true;
+
+ public PatternFilter(String pattern)
+ {
+ if (pattern == null)
+ throw new IllegalArgumentException("Null pattern");
+
+ if (pattern.startsWith( "**/*" ))
+ {
+ this.pattern = pattern.substring(4);
+ this.exact = false;
+ }
+ else
+ {
+ this.pattern = pattern;
+ }
+ }
+
+ public boolean accepts(VirtualFile file)
+ {
+ String name = file.getName();
+ return exact ? name.equals(pattern) : name.endsWith(pattern);
+ }
+}
\ No newline at end of file
Added: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java (rev 0)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -0,0 +1,255 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.scanning.hibernate;
+
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.*;
+
+import org.jboss.classloading.plugins.vfs.PackageVisitor;
+import org.jboss.classloading.spi.dependency.Module;
+import org.jboss.deployers.spi.deployer.helpers.AttachmentLocator;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.scanning.plugins.helpers.WeakClassLoaderHolder;
+import org.jboss.scanning.spi.ScanningHandle;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VirtualFile;
+
+import org.hibernate.AssertionFailure;
+import org.hibernate.ejb.packaging.NamedInputStream;
+import org.hibernate.ejb.packaging.Scanner;
+
+/**
+ * Hibernate's scanner impl.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ScannerImpl extends WeakClassLoaderHolder implements Scanner, ScanningHandle<ScannerImpl>
+{
+ /** The packages cache */
+ private Map<URL, Set<Package>> packages = new HashMap<URL, Set<Package>>();
+ /** The classes cache */
+ private Map<URL, Map<Class<? extends Annotation>, Set<String>>> classes = new HashMap<URL, Map<Class<? extends Annotation>, Set<String>>>();
+ /** The files cache */
+ private Map<URL, Map<String, Set<NamedInputStream>>> files = new HashMap<URL, Map<String, Set<NamedInputStream>>>();
+
+ /** The deployment unit */
+ private DeploymentUnit unit;
+ /** Do we cache new results */
+ private boolean cacheNewResults;
+
+ private static DeploymentUnit check(DeploymentUnit unit)
+ {
+ if (unit == null)
+ throw new IllegalArgumentException("Null unit");
+ return unit;
+ }
+
+ public ScannerImpl(DeploymentUnit unit)
+ {
+ super(check(unit).getClassLoader());
+ this.unit = unit;
+ }
+
+ public void merge(ScannerImpl subHandle)
+ {
+ }
+
+ public Set<Package> getPackagesInJar(URL jartoScan, Set<Class<? extends Annotation>> annotationsToLookFor)
+ {
+ if (annotationsToLookFor.size() > 0)
+ throw new AssertionFailure("Improper use of NativeScanner: must not filter packages");
+
+ Set<Package> p = packages.get(jartoScan);
+ if (p == null)
+ {
+ Module module = AttachmentLocator.searchAncestors(unit, Module.class);
+ if (module == null)
+ throw new IllegalArgumentException("No such module: " + unit);
+
+ p = new HashSet<Package>();
+
+ VirtualFile root = getFile(jartoScan);
+ Set<String> pckgs = PackageVisitor.determineAllPackages(
+ new VirtualFile[]{root},
+ null,
+ module.getExportAll(),
+ module.getIncluded(),
+ module.getExcluded(),
+ module.getExcludedExport());
+ for (String path : pckgs)
+ {
+ Package pck = loadClass(path + "/package-info").getPackage();
+ p.add(pck);
+ }
+
+ if (cacheNewResults)
+ packages.put(jartoScan, p);
+ }
+ return p;
+ }
+
+ public Set<Class<?>> getClassesInJar(URL jartoScan, Set<Class<? extends Annotation>> annotationsToLookFor)
+ {
+ Set<String> strings = new HashSet<String>();
+ Set<Class<? extends Annotation>> missingCacheAnnotations = new HashSet<Class<? extends Annotation>>();
+ Map<Class<? extends Annotation>, Set<String>> map = classes.get(jartoScan);
+ if (map != null)
+ {
+ for (Class<? extends Annotation> annClass : annotationsToLookFor)
+ {
+ Set<String> s = map.get(annClass);
+ if (s != null)
+ {
+ strings.addAll(s);
+ }
+ else
+ {
+ missingCacheAnnotations.add(annClass);
+ }
+ }
+ }
+ else
+ {
+ missingCacheAnnotations.addAll(annotationsToLookFor);
+ }
+
+ if (missingCacheAnnotations.isEmpty() == false)
+ {
+
+ Map<Class<? extends Annotation>, Set<String>> temp = findClasses(missingCacheAnnotations);
+ if (cacheNewResults)
+ {
+ if (map != null)
+ map.putAll(temp);
+ else
+ classes.put(jartoScan, temp);
+ }
+ for (Set<String> vals : temp.values())
+ strings.addAll(vals);
+ }
+
+ Set<Class<?>> result = new HashSet<Class<?>>();
+ for (String clazz : strings)
+ result.add(loadClass(clazz));
+ return result;
+ }
+
+ private Map<Class<? extends Annotation>, Set<String>> findClasses(Set<Class<? extends Annotation>> annotations)
+ {
+ return Collections.emptyMap(); // TODO
+ }
+
+ public Set<NamedInputStream> getFilesInJar(URL jartoScan, Set<String> filePatterns)
+ {
+ Set<NamedInputStream> result = new HashSet<NamedInputStream>();
+ Map<String, Set<NamedInputStream>> map = files.get(jartoScan);
+ if (map != null)
+ {
+ findFiles(jartoScan, filePatterns, map, result);
+ }
+ else
+ {
+ map = new HashMap<String, Set<NamedInputStream>>();
+ findFiles(jartoScan, filePatterns, map, result);
+ if (cacheNewResults)
+ files.put(jartoScan, map);
+ }
+ return result;
+ }
+
+ private void findFiles(URL jartoScan, Set<String> filePatterns, Map<String, Set<NamedInputStream>> map, Set<NamedInputStream> result)
+ {
+ VirtualFile root = null;
+ for (String pattern : filePatterns)
+ {
+ Set<NamedInputStream> niss = map.get(pattern);
+ if (niss == null)
+ {
+ if (root == null)
+ root = getFile(jartoScan);
+
+ try
+ {
+ List<VirtualFile> children = root.getChildren(new PatternFilter(pattern));
+ niss = toNIS(children);
+ if (cacheNewResults)
+ map.put(pattern, niss);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ result.addAll(niss);
+ }
+ }
+
+ private Set<NamedInputStream> toNIS(Iterable<VirtualFile> files)
+ {
+ try
+ {
+ Set<NamedInputStream> result = new HashSet<NamedInputStream>();
+ for (VirtualFile file : files)
+ {
+ NamedInputStream nis = new NamedInputStream(file.getName(), file.openStream());
+ result.add(nis);
+ }
+ return result;
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Set<NamedInputStream> getFilesInClasspath(URL jartoScan, Set<String> filePatterns)
+ {
+ throw new AssertionFailure("Not implemented");
+ }
+
+ public String getUnqualifiedJarName(URL jarUrl)
+ {
+ VirtualFile file = getFile(jarUrl);
+ return file.getName();
+ }
+
+ private VirtualFile getFile(URL url)
+ {
+ try
+ {
+ return VFS.getChild(url);
+ }
+ catch (URISyntaxException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public void setCacheNewResults(boolean cacheNewResults)
+ {
+ this.cacheNewResults = cacheNewResults;
+ }
+}
Modified: projects/scanning/trunk/pom.xml
===================================================================
--- projects/scanning/trunk/pom.xml 2010-03-19 20:11:57 UTC (rev 102639)
+++ projects/scanning/trunk/pom.xml 2010-03-19 21:24:41 UTC (rev 102640)
@@ -34,6 +34,7 @@
<version.org.jboss.deployers>2.2.0.Alpha4</version.org.jboss.deployers>
<version.javassist>3.11.0.GA</version.javassist>
<version.ant>1.7.1</version.ant>
+ <version.hibernate>3.5.0-CR-2</version.hibernate>
<version.junit>4.4</version.junit>
<version.org.jboss.logging>2.1.1.GA</version.org.jboss.logging>
<version.org.jboss.test>1.1.1.GA</version.org.jboss.test>
@@ -163,6 +164,11 @@
<artifactId>ant</artifactId>
<version>${version.ant}</version>
</dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>${version.hibernate}</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
Copied: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/WeakClassLoaderHolder.java (from rev 102600, projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/annotations/plugins/WeakClassLoaderHolder.java)
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/WeakClassLoaderHolder.java (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/WeakClassLoaderHolder.java 2010-03-19 21:24:41 UTC (rev 102640)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.scanning.plugins.helpers;
+
+import org.jboss.util.JBossObject;
+
+import java.lang.ref.WeakReference;
+
+/**
+ * ClassLoader holder helper.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class WeakClassLoaderHolder extends JBossObject
+{
+ private transient WeakReference<ClassLoader> clRef;
+
+ public WeakClassLoaderHolder(ClassLoader classLoader)
+ {
+ if (classLoader == null)
+ throw new IllegalArgumentException("Null classloader");
+
+ clRef = new WeakReference<ClassLoader>(classLoader);
+ }
+
+ /**
+ * Get the classloader from weak ref.
+ *
+ * @return the classloader
+ */
+ protected ClassLoader getClassLoader()
+ {
+ if (clRef == null)
+ throw new IllegalArgumentException("Null classloader ref, previously serialized?");
+
+ ClassLoader classLoader = clRef.get();
+ if (classLoader == null)
+ throw new IllegalArgumentException("ClassLoader was already garbage collected.");
+
+ return classLoader;
+ }
+
+ /**
+ * Load class from class name.
+ *
+ * @param className the class name
+ * @return loaded class
+ */
+ protected Class<?> loadClass(String className)
+ {
+ try
+ {
+ return Class.forName(className, false, getClassLoader());
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list