Skip to content

Commit

Permalink
[GORA-72] Made all artifacts proper OSGi bundles. Added ClassLoadingU…
Browse files Browse the repository at this point in the history
…tils which will fallback to thread context classloader if Class.forName fails.

git-svn-id: https://svn.apache.org/repos/asf/incubator/gora/trunk@1227229 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
iocanel committed Jan 4, 2012
1 parent 13ced44 commit 76294f7
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 20 deletions.
7 changes: 6 additions & 1 deletion gora-cassandra/pom.xml
Expand Up @@ -27,10 +27,15 @@
<relativePath>../</relativePath>
</parent>
<artifactId>gora-cassandra</artifactId>
<packaging>jar</packaging>
<packaging>bundle</packaging>

<name>Apache Gora :: Cassandra</name>

<properties>
<osgi.import>*</osgi.import>
<osgi.export>org.apache.gora.cassandra*;version="${project.version}";-noimport:=true</osgi.export>
</properties>

<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
Expand Down
7 changes: 6 additions & 1 deletion gora-core/pom.xml
Expand Up @@ -25,10 +25,15 @@
<relativePath>../</relativePath>
</parent>
<artifactId>gora-core</artifactId>
<packaging>jar</packaging>
<packaging>bundle</packaging>

<name>Apache Gora :: Core</name>

<properties>
<osgi.import>*</osgi.import>
<osgi.export>org.apache.gora*;version="${project.version}";-noimport:=true</osgi.export>
</properties>

<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.gora.util.ClassLoadingUtils;

/**
* Example Hadoop job to count the row of a gora {@link Query}.
Expand Down Expand Up @@ -120,8 +121,8 @@ public int run(String[] args) throws Exception {
return 1;
}

Class<K> keyClass = (Class<K>) Class.forName(args[0]);
Class<T> persistentClass = (Class<T>) Class.forName(args[1]);
Class<K> keyClass = (Class<K>) ClassLoadingUtils.loadClass(args[0]);
Class<T> persistentClass = (Class<T>) ClassLoadingUtils.loadClass(args[1]);

DataStore<K,T> dataStore;
Configuration conf = new Configuration();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.gora.query.Query;
import org.apache.gora.query.Result;
import org.apache.gora.store.DataStore;
import org.apache.gora.util.ClassLoadingUtils;
import org.apache.gora.util.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
Expand Down Expand Up @@ -217,8 +218,7 @@ public void setConf(Configuration conf) {
public void readFields(DataInput in) throws IOException {
String dataStoreClass = Text.readString(in);
try {
dataStore = (DataStore<K, T>) ReflectionUtils.newInstance(
Class.forName(dataStoreClass), conf);
dataStore = (DataStore<K, T>) ReflectionUtils.newInstance(ClassLoadingUtils.loadClass(dataStoreClass), conf);
dataStore.readFields(in);
} catch (ClassNotFoundException ex) {
throw new IOException(ex);
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.logging.LogFactory;
import org.apache.gora.persistency.Persistent;
import org.apache.gora.store.impl.DataStoreBase;
import org.apache.gora.util.ClassLoadingUtils;
import org.apache.gora.util.GoraException;
import org.apache.gora.util.ReflectionUtils;
import org.apache.hadoop.conf.Configurable;
Expand Down Expand Up @@ -159,8 +160,8 @@ public static synchronized DataStore getDataStore(
throws GoraException {

try {
Class k = Class.forName(keyClass);
Class p = Class.forName(persistentClass);
Class k = ClassLoadingUtils.loadClass(keyClass);
Class p = ClassLoadingUtils.loadClass(persistentClass);
return getDataStore(dataStoreClass, k, p, conf);
} catch(GoraException ex) {
throw ex;
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.apache.gora.store.DataStore;
import org.apache.gora.store.DataStoreFactory;
import org.apache.gora.util.AvroUtils;
import org.apache.gora.util.ClassLoadingUtils;
import org.apache.gora.util.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
Expand Down Expand Up @@ -173,8 +174,8 @@ protected Configuration getOrCreateConf() {
@SuppressWarnings("unchecked")
public void readFields(DataInput in) throws IOException {
try {
Class<K> keyClass = (Class<K>) Class.forName(Text.readString(in));
Class<T> persistentClass = (Class<T>)Class.forName(Text.readString(in));
Class<K> keyClass = (Class<K>) ClassLoadingUtils.loadClass(Text.readString(in));
Class<T> persistentClass = (Class<T>)ClassLoadingUtils.loadClass(Text.readString(in));
initialize(keyClass, persistentClass, DataStoreFactory.properties);

} catch (ClassNotFoundException ex) {
Expand Down
@@ -0,0 +1,64 @@
package org.apache.gora.util;

public class ClassLoadingUtils {

private ClassLoadingUtils() {
//Utility Class
}

/**
* Loads a class using the class loader.
* 1. The class loader of the current class is being used.
* 2. The thread context class loader is being used.
* If both approaches fail, returns null.
*
* @param className The name of the class to load.
* @return The class or null if no class loader could load the class.
*/
public static Class<?> loadClass(String className) throws ClassNotFoundException {
return ClassLoadingUtils.loadClass(ClassLoadingUtils.class,className);
}

/**
* Loads a class using the class loader.
* 1. The class loader of the context class is being used.
* 2. The thread context class loader is being used.
* If both approaches fail, returns null.
*
* @param contextClass The name of a context class to use.
* @param className The name of the class to load
* @return The class or null if no class loader could load the class.
*/
public static Class<?> loadClass(Class<?> contextClass, String className) throws ClassNotFoundException {
Class<?> clazz = null;
if (contextClass.getClassLoader() != null) {
clazz = loadClass(className, contextClass.getClassLoader());
}
if (clazz == null && Thread.currentThread().getContextClassLoader() != null) {
clazz = loadClass(className, Thread.currentThread().getContextClassLoader());
}
if (clazz == null) {
throw new ClassNotFoundException("Failed to load class" + className);
}
return clazz;
}

/**
* Loads a {@link Class} from the specified {@link ClassLoader} without throwing {@ClassNotFoundException}.
*
* @param className
* @param classLoader
* @return
*/
private static Class<?> loadClass(String className, ClassLoader classLoader) {
Class<?> clazz = null;
if (classLoader != null && className != null) {
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
//Ignore and return null
}
}
return clazz;
}
}
6 changes: 3 additions & 3 deletions gora-core/src/main/java/org/apache/gora/util/IOUtils.java
Expand Up @@ -189,7 +189,7 @@ public static<T extends Persistent> byte[] serialize(PersistentDatumWriter<T> da
public static<T> T deserialize(Configuration conf, DataInput in
, T obj , String objClass) throws IOException, ClassNotFoundException {

Class<T> c = (Class<T>) Class.forName(objClass);
Class<T> c = (Class<T>) ClassLoadingUtils.loadClass(objClass);

return deserialize(conf, in, obj, c);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ public static<T> T deserialize(Configuration conf, DataInput in
public static<T> T deserialize(Configuration conf, DataInput in
, T obj) throws IOException, ClassNotFoundException {
String clazz = Text.readString(in);
Class<T> c = (Class<T>)Class.forName(clazz);
Class<T> c = (Class<T>)ClassLoadingUtils.loadClass(clazz);
return deserialize(conf, in, obj, c);
}

Expand Down Expand Up @@ -477,7 +477,7 @@ public static<T> T loadFromConf(Configuration conf, String dataKey)
String classKey = dataKey + "._class";
String className = conf.get(classKey);
try {
T obj = (T) DefaultStringifier.load(conf, dataKey, Class.forName(className));
T obj = (T) DefaultStringifier.load(conf, dataKey, ClassLoadingUtils.loadClass(className));
return obj;
} catch (Exception ex) {
throw new IOException(ex);
Expand Down
Expand Up @@ -87,7 +87,7 @@ public static Object newInstance(String classStr) throws InstantiationException
if(classStr == null) {
throw new IllegalArgumentException("class cannot be null");
}
Class<?> clazz = Class.forName(classStr);
Class<?> clazz = ClassLoadingUtils.loadClass(classStr);
return newInstance(clazz);
}

Expand Down
11 changes: 8 additions & 3 deletions gora-sql/pom.xml
Expand Up @@ -24,10 +24,15 @@
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>gora-sql</artifactId>
<packaging>jar</packaging>
<packaging>bundle</packaging>

<name>Apache Gora :: SQL</name>

<properties>
<osgi.import>*</osgi.import>
<osgi.export>org.apache.gora.sql*;version="${project.version}";-noimport:=true</osgi.export>
</properties>

<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
Expand Down Expand Up @@ -116,10 +121,10 @@
<artifactId>jdom</artifactId>
</dependency>

<dependency>
<!--dependency>
<groupId>com.healthmarketscience.sqlbuilder</groupId>
<artifactId>sqlbuilder</artifactId>
</dependency>
</dependency-->

<!-- Logging Dependencies -->
<dependency>
Expand Down
Expand Up @@ -65,6 +65,7 @@
import org.apache.gora.store.DataStoreFactory;
import org.apache.gora.store.impl.DataStoreBase;
import org.apache.gora.util.AvroUtils;
import org.apache.gora.util.ClassLoadingUtils;
import org.apache.gora.util.IOUtils;
import org.apache.gora.util.StringUtils;
import org.jdom.Document;
Expand Down Expand Up @@ -732,7 +733,7 @@ protected Connection getConnection() throws IOException {
try {
Connection connection = null;

Class.forName(jdbcDriverClass);
ClassLoadingUtils.loadClass(jdbcDriverClass);
if(jdbcUsername == null || jdbcUsername.length() == 0) {
connection = DriverManager.getConnection(jdbcUrl);
} else {
Expand Down
Expand Up @@ -24,6 +24,7 @@

import org.apache.gora.GoraTestDriver;
import org.apache.gora.sql.store.SqlStore;
import org.apache.gora.util.ClassLoadingUtils;
import org.apache.hadoop.util.StringUtils;
import org.hsqldb.Server;

Expand Down Expand Up @@ -99,7 +100,7 @@ public void tearDown() throws Exception {
private Connection createConnection(String driverClassName
, String url) throws Exception {

Class.forName(driverClassName);
ClassLoadingUtils.loadClass(driverClassName);
Connection connection = DriverManager.getConnection(url);
connection.setAutoCommit(false);
return connection;
Expand Down
36 changes: 35 additions & 1 deletion pom.xml
Expand Up @@ -21,7 +21,6 @@
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>9</version>
<relativePath />
</parent>

<groupId>org.apache.gora</groupId>
Expand Down Expand Up @@ -107,6 +106,40 @@
<tagBase>https://svn.apache.org/repos/asf/incubator/gora/tags/</tagBase>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven-bundle-plugin.version}</version>
<extensions>true</extensions>
<inherited>true</inherited>
<configuration>
<instructions>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>${osgi.export}</Export-Package>
<Import-Package>${osgi.import}</Import-Package>
<DynamicImport-Package>${osgi.dynamic.import}</DynamicImport-Package>
<Private-Package>${osgi.private}</Private-Package>
<Require-Bundle>${osgi.bundles}</Require-Bundle>
<Bundle-Activator>${osgi.activator}</Bundle-Activator>
</instructions>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
</supportedProjectTypes>
<unpackBundle>true</unpackBundle>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -153,6 +186,7 @@
<build-helper-maven-plugin.version>1.5</build-helper-maven-plugin.version>
<maven-surfire-plugin.version>2.11</maven-surfire-plugin.version>
<maven-release-plugin.version>2.1</maven-release-plugin.version>
<maven-bundle-plugin.version>2.3.6</maven-bundle-plugin.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit 76294f7

Please sign in to comment.