[jboss-svn-commits] JBL Code SVN: r35769 - in labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api: src/main/java/org/drools/repository and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Oct 27 10:47:16 EDT 2010
Author: kurt.stam at jboss.com
Date: 2010-10-27 10:47:15 -0400 (Wed, 27 Oct 2010)
New Revision: 35769
Added:
labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/util/
labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/util/ClassUtil.java
Modified:
labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/pom.xml
labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/JCRRepositoryConfigurator.java
Log:
BRMS-398, change to not use JSE 6
Modified: labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/pom.xml
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/pom.xml 2010-10-27 14:29:18 UTC (rev 35768)
+++ labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/pom.xml 2010-10-27 14:47:15 UTC (rev 35769)
@@ -15,13 +15,10 @@
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
</dependency>
- <!--
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.8</version>
- </dependency>
- -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
</dependencies>
</project>
Modified: labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/JCRRepositoryConfigurator.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/JCRRepositoryConfigurator.java 2010-10-27 14:29:18 UTC (rev 35768)
+++ labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/JCRRepositoryConfigurator.java 2010-10-27 14:47:15 UTC (rev 35769)
@@ -17,7 +17,6 @@
package org.drools.repository;
import java.util.Properties;
-import java.util.ServiceLoader;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
@@ -25,10 +24,13 @@
import javax.jcr.Session;
import javax.jcr.Workspace;
+import org.drools.repository.util.ClassUtil;
+
+
/**
* This abstract class is required so different JCR implementations can provide their own configuration mechanism.
*
- * * This contains code to initialise the repository using the {@link javax.jcr.RepositoryFactory} interface defined by the JCR 2.0
+ * This contains code to initialise the repository using the {@link javax.jcr.RepositoryFactory} interface defined by the JCR 2.0
* specification. This configurator loads the properties from the {@link PROPERTIES_FILE "/drools_repository.properties"}
* resource, and passes these to the {@link javax.jcr.RepositoryFactory#getRepository(java.util.Map)}.
*
@@ -39,6 +41,7 @@
protected RepositoryFactory factory;
public static final String JCR_IMPL_CLASS = "org.drools.repository.jcr.impl";
+ protected static String defaultJCRImplClass = null;
public static final String REPOSITORY_ROOT_DIRECTORY = "repository.root.directory";
/**
@@ -55,21 +58,31 @@
//Instantiate real repo.
if (jcrImplementationClass==null) {
+ jcrImplementationClass = defaultJCRImplClass;
+ }
+
// Use the JCR 2.0 RepositoryFactory to get a repository using the properties as input ...
- for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
- Repository repo = factory.getRepository(properties);
- if (repo != null) {
- this.factory = factory;
- return repo;
- }
- }
- }
- } catch (RepositoryException re) {
+// We're not yet supporting JSE 1.6, so until then this needs to stay commented out
+// for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
+// Repository repo = factory.getRepository(properties);
+// if (repo != null) {
+// this.factory = factory;
+// return repo;
+// }
+// }
+
+ Class jcrFactory = ClassUtil.forName(jcrImplementationClass, this.getClass());
+ RepositoryFactory factory = (RepositoryFactory) jcrFactory.newInstance();
+ Repository repo = factory.getRepository(properties);
+ this.factory = factory;
+ return repo;
+
+ } catch (Exception re) {
throw new RepositoryException(re);
}
// If here, then we couldn't find a repository factory ...
- String msg = "Unable to find an appropriate JCR 2.0 RepositoryFactory; check the 'drools_repository.properties' configuration file.";
- throw new RepositoryException(msg);
+// String msg = "Unable to find an appropriate JCR 2.0 RepositoryFactory; check the 'drools_repository.properties' configuration file.";
+// throw new RepositoryException(msg);
}
public abstract void registerNodeTypesFromCndFile(String cndFileName, Session session, Workspace workspace) throws RepositoryException;
Added: labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/util/ClassUtil.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/util/ClassUtil.java (rev 0)
+++ labs/jbossrules/soa_branches/BRMS-5.1.1/drools-repo/jcr-api/src/main/java/org/drools/repository/util/ClassUtil.java 2010-10-27 14:47:15 UTC (rev 35769)
@@ -0,0 +1,363 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.drools.repository.util;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Proxy;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility methods to aid in class/resource loading.
+ *
+ * @author kevin
+ */
+public class ClassUtil
+{
+ private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ClassUtil.class);
+
+ /**
+ * Load the specified class.
+ * @param className The name of the class to load.
+ * @param caller The class of the caller.
+ * @return The specified class.
+ * @throws ClassNotFoundException If the class cannot be found.
+ */
+ public static Class forName(final String className, final Class caller)
+ throws ClassNotFoundException
+ {
+ final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader() ;
+ if (threadClassLoader != null)
+ {
+ try
+ {
+ return Class.forName(className, true, threadClassLoader) ;
+ }
+ catch (final ClassNotFoundException cnfe)
+ {
+ if (cnfe.getException() != null)
+ {
+ throw cnfe ;
+ }
+ }
+ }
+
+
+ final ClassLoader classLoader = caller.getClassLoader() ;
+ if (classLoader != null)
+ {
+ try
+ {
+ return Class.forName(className, true, classLoader) ;
+ }
+ catch (final ClassNotFoundException cnfe)
+ {
+ if (cnfe.getException() != null)
+ {
+ throw cnfe ;
+ }
+ }
+ }
+
+ return Class.forName(className, true, ClassLoader.getSystemClassLoader()) ;
+ }
+
+ /**
+ * Resolve a proxy for the specified interfaces.
+ * @param interfaces The interfaces associated with the proxy.
+ * @param caller The class of the caller.
+ * @return The specified proxy class.
+ * @throws ClassNotFoundException If the class cannot be found.
+ */
+ public static Class resolveProxy(final String[] interfaces, final Class caller)
+ throws ClassNotFoundException
+ {
+ final int numInterfaces = (interfaces == null ? 0 : interfaces.length) ;
+ if (numInterfaces == 0)
+ {
+ throw new ClassNotFoundException("Cannot generate proxy with no interfaces") ;
+ }
+
+ final Class[] interfaceClasses = new Class[numInterfaces] ;
+ for(int count = 0 ; count < numInterfaces ; count++)
+ {
+ interfaceClasses[count] = forName(interfaces[count], caller) ;
+ }
+
+ final ClassLoader proxyClassLoader ;
+ final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader() ;
+ if (threadClassLoader != null)
+ {
+ proxyClassLoader = threadClassLoader ;
+ }
+ else
+ {
+ final ClassLoader classLoader = caller.getClassLoader() ;
+ if (classLoader != null)
+ {
+ proxyClassLoader = classLoader ;
+ }
+ else
+ {
+ proxyClassLoader = ClassLoader.getSystemClassLoader() ;
+ }
+ }
+
+ return Proxy.getProxyClass(proxyClassLoader, interfaceClasses) ;
+ }
+
+ /**
+ * Get the specified resource as a stream.
+ * @param resourceName The name of the class to load.
+ * @param caller The class of the caller.
+ * @return The input stream for the resource or null if not found.
+ */
+ public static InputStream getResourceAsStream(final String resourceName, final Class caller)
+ {
+ final String resource ;
+ if (resourceName.startsWith("/"))
+ {
+ resource = resourceName.substring(1) ;
+ }
+ else
+ {
+ final Package callerPackage = caller.getPackage() ;
+ if (callerPackage != null)
+ {
+ resource = callerPackage.getName().replace('.', '/') + '/' + resourceName ;
+ }
+ else
+ {
+ resource = resourceName ;
+ }
+ }
+ final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader() ;
+ if (threadClassLoader != null)
+ {
+ final InputStream is = threadClassLoader.getResourceAsStream(resource) ;
+ if (is != null)
+ {
+ return is ;
+ }
+ }
+
+ final ClassLoader classLoader = caller.getClassLoader() ;
+ if (classLoader != null)
+ {
+ final InputStream is = classLoader.getResourceAsStream(resource) ;
+ if (is != null)
+ {
+ return is ;
+ }
+ }
+
+ return ClassLoader.getSystemResourceAsStream(resource) ;
+ }
+
+ public static URL getResource(final String resourceName, final Class<?> caller)
+ {
+ final String resource ;
+ if (resourceName.startsWith("/"))
+ {
+ resource = resourceName.substring(1) ;
+ }
+ else
+ {
+ final Package callerPackage = caller.getPackage() ;
+ if (callerPackage != null)
+ {
+ resource = callerPackage.getName().replace('.', '/') + '/' + resourceName ;
+ }
+ else
+ {
+ resource = resourceName ;
+ }
+ }
+ final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader() ;
+ if (threadClassLoader != null)
+ {
+ final URL url = threadClassLoader.getResource(resource) ;
+ if (url != null)
+ {
+ return url ;
+ }
+ }
+
+ final ClassLoader classLoader = caller.getClassLoader() ;
+ if (classLoader != null)
+ {
+ final URL url = classLoader.getResource(resource) ;
+ if (url != null)
+ {
+ return url ;
+ }
+ }
+
+ return ClassLoader.getSystemResource(resource) ;
+ }
+
+
+ public static List<URL> getResources(String resourcePath, Class<?> caller) throws IOException {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+ if(resourcePath.startsWith("/")) {
+ resourcePath = resourcePath.substring(1);
+ }
+
+ if (classLoader != null) {
+ return toList(classLoader.getResources(resourcePath));
+ }
+
+ classLoader = caller.getClassLoader();
+ if (classLoader != null) {
+ return toList(classLoader.getResources(resourcePath));
+ }
+
+ return new ArrayList<URL>();
+ }
+
+ private static <T> List<T> toList(Enumeration<T> objects) {
+ List<T> theList = new ArrayList<T>();
+ while(objects.hasMoreElements()) {
+ theList.add(objects.nextElement());
+ }
+ return theList;
+ }
+
+ /**
+ * Get a package name and convert it to a path value, so it can be used
+ * in calls to methods like {@link #getResourceAsStream}.
+ * <p/>
+ * Adds a '/' prefix and converts all '." characters to '/'. Doesn't add a
+ * trailing slash.
+ *
+ * @param packageObj The package.
+ * @return The package path.
+ */
+ public static String getPath(Package packageObj) {
+ return "/" + packageObj.getName().replace('.', '/');
+ }
+
+ public static List<String> getResourceList(String regex, Class caller) {
+ ClasspathResourceFilter filter = new ClasspathResourceFilter(regex);
+ ClassLoader classLoader;
+
+ classLoader = Thread.currentThread().getContextClassLoader();
+ if(classLoader instanceof URLClassLoader) {
+ filter.filter((URLClassLoader) classLoader);
+ }
+ classLoader = caller.getClassLoader();
+ if(classLoader instanceof URLClassLoader) {
+ filter.filter((URLClassLoader) classLoader);
+ }
+
+ return filter.getResourceList();
+ }
+
+ private static class ClasspathResourceFilter {
+
+ private List<String> resourceList = new ArrayList<String>();
+ private Pattern pattern;
+
+ private ClasspathResourceFilter(String regex) {
+ pattern = Pattern.compile(regex);
+ }
+
+ private void filter(URLClassLoader classLoader) {
+ URL[] cpUrls = classLoader.getURLs();
+
+ for (int i = 0; i < cpUrls.length; i++) {
+ try {
+ File file = new File(cpUrls[i].toURI());
+ if(file.isDirectory()) {
+ searchClasspathDirTree(file, "");
+ } else {
+ searchArchive(file);
+ }
+ } catch (URISyntaxException e) {
+ logger.warn("Error searching classpath resource URL '" + cpUrls[i] + "' for resource '" + pattern.pattern() + "': " + e.getMessage());
+ } catch (IOException e) {
+ logger.warn("Error searching classpath resource URL '" + cpUrls[i] + "' for resource '" + pattern.pattern() + "': " + e.getMessage());
+ }
+ }
+ }
+
+ private void searchClasspathDirTree(File rootDir, String subDir) {
+ File currentDir = new File(rootDir, subDir);
+ File[] contents = currentDir.listFiles();
+
+ for(File file: contents) {
+ if(file.isDirectory()) {
+ String subSubDir = subDir + "/" + file.getName();
+ searchClasspathDirTree(rootDir, subSubDir);
+ } else {
+ String resClasspathPath = file.toURI().toString().substring(rootDir.toURI().toString().length() - 1);
+ if(isToBeAdded(resClasspathPath)) {
+ resourceList.add(resClasspathPath);
+ }
+ }
+ }
+ }
+
+ private void searchArchive(File archiveFile) throws IOException {
+ ZipFile zip = new ZipFile(archiveFile);
+ Enumeration<? extends ZipEntry> entries = zip.entries();
+
+ while (entries.hasMoreElements()) {
+ ZipEntry entry = entries.nextElement();
+ String resClasspathPath = "/" + entry.getName();
+ if(isToBeAdded(resClasspathPath)) {
+ resourceList.add(resClasspathPath);
+ }
+ }
+ }
+
+ private boolean isToBeAdded(String resClasspathPath) {
+ if(resourceList.contains(resClasspathPath)) {
+ // Already in the list e.g. same resource in different archives...
+ return false;
+ }
+
+ Matcher matcher = pattern.matcher(resClasspathPath);
+ return matcher.matches();
+ }
+
+ private List<String> getResourceList() {
+ return resourceList;
+ }
+ }
+}
+
More information about the jboss-svn-commits
mailing list