Author: nick
Date: Thu Jul 4 19:10:57 2013
New Revision: 1499844
URL: http://svn.apache.org/r1499844
Log:
Fix TIKA-1145 - If a specific ClassLoader was given to TikaConfig, have that
used for loading the mimetypes too
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java
tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
URL:
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java?rev=1499844&r1=1499843&r2=1499844&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
Thu Jul 4 19:10:57 2013
@@ -56,8 +56,8 @@ import org.xml.sax.SAXException;
*/
public class TikaConfig {
- private static MimeTypes getDefaultMimeTypes() {
- return MimeTypes.getDefaultMimeTypes();
+ private static MimeTypes getDefaultMimeTypes(ClassLoader loader) {
+ return MimeTypes.getDefaultMimeTypes(loader);
}
private static Detector getDefaultDetector(
@@ -135,7 +135,7 @@ public class TikaConfig {
public TikaConfig(ClassLoader loader)
throws MimeTypeException, IOException {
ServiceLoader serviceLoader = new ServiceLoader(loader);
- this.mimeTypes = getDefaultMimeTypes();
+ this.mimeTypes = getDefaultMimeTypes(loader);
this.detector = getDefaultDetector(mimeTypes, serviceLoader);
this.parser = getDefaultParser(mimeTypes, serviceLoader);
}
@@ -166,7 +166,7 @@ public class TikaConfig {
}
if (config == null) {
- this.mimeTypes = getDefaultMimeTypes();
+ this.mimeTypes =
getDefaultMimeTypes(ServiceLoader.getContextClassLoader());
this.parser = getDefaultParser(mimeTypes, loader);
this.detector = getDefaultDetector(mimeTypes, loader);
} else {
@@ -301,7 +301,7 @@ public class TikaConfig {
if (mtr != null && mtr.hasAttribute("resource")) {
return MimeTypesFactory.create(mtr.getAttribute("resource"));
} else {
- return getDefaultMimeTypes();
+ return getDefaultMimeTypes(null);
}
}
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java
URL:
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java?rev=1499844&r1=1499843&r2=1499844&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java Thu
Jul 4 19:10:57 2013
@@ -486,6 +486,8 @@ public final class MimeTypes implements
}
private static MimeTypes DEFAULT_TYPES = null;
+ private static Map<ClassLoader,MimeTypes>
CLASSLOADER_SPECIFIC_DEFAULT_TYPES =
+ new HashMap<ClassLoader, MimeTypes>();
/**
* Get the default MimeTypes. This includes all the build in
@@ -494,10 +496,25 @@ public final class MimeTypes implements
* @return MimeTypes default type registry
*/
public static synchronized MimeTypes getDefaultMimeTypes() {
- if (DEFAULT_TYPES == null) {
+ return getDefaultMimeTypes(null);
+ }
+ /**
+ * Get the default MimeTypes. This includes all the built-in
+ * media types, and any custom override ones present.
+ *
+ * @param ClassLoader to use, if not the default
+ * @return MimeTypes default type registry
+ */
+ public static synchronized MimeTypes getDefaultMimeTypes(ClassLoader
classLoader) {
+ MimeTypes types = DEFAULT_TYPES;
+ if (classLoader != null) {
+ types = CLASSLOADER_SPECIFIC_DEFAULT_TYPES.get(classLoader);
+ }
+
+ if (types == null) {
try {
- DEFAULT_TYPES = MimeTypesFactory.create(
- "tika-mimetypes.xml", "custom-mimetypes.xml");
+ types = MimeTypesFactory.create(
+ "tika-mimetypes.xml", "custom-mimetypes.xml",
classLoader);
} catch (MimeTypeException e) {
throw new RuntimeException(
"Unable to parse the default media type registry", e);
@@ -505,8 +522,13 @@ public final class MimeTypes implements
throw new RuntimeException(
"Unable to read the default media type registry", e);
}
+
+ if (classLoader == null) {
+ DEFAULT_TYPES = types;
+ } else {
+ CLASSLOADER_SPECIFIC_DEFAULT_TYPES.put(classLoader, types);
+ }
}
- return DEFAULT_TYPES;
+ return types;
}
-
}
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
URL:
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java?rev=1499844&r1=1499843&r2=1499844&view=diff
==============================================================================
---
tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
(original)
+++
tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
Thu Jul 4 19:10:57 2013
@@ -120,7 +120,7 @@ public class MimeTypesFactory {
* Creates and returns a MimeTypes instance. The core mimetypes
* will be loaded from the specified file path, and any custom
* override mimetypes found will loaded afterwards.
- * The file paths will be interpreted by the class loader in
+ * The file paths will be interpreted by the default class loader in
* getResource().
*
* @param coreFilePath The main MimeTypes file to load
@@ -131,15 +131,36 @@ public class MimeTypesFactory {
*/
public static MimeTypes create(String coreFilePath, String
extensionFilePath)
throws IOException, MimeTypeException {
+ return create(coreFilePath, extensionFilePath, null);
+ }
+ /**
+ * Creates and returns a MimeTypes instance. The core mimetypes
+ * will be loaded from the specified file path, and any custom
+ * override mimetypes found will loaded afterwards.
+ * The file paths will be interpreted by the specified class
+ * loader in getResource().
+ *
+ * @param coreFilePath The main MimeTypes file to load
+ * @param extensionFilePath The name of extension MimeType files to load
afterwards
+ *
+ * @throws IOException if the file can not be accessed
+ * @throws MimeTypeException if the type configuration is invalid
+ */
+ public static MimeTypes create(String coreFilePath, String
extensionFilePath,
+ ClassLoader classLoader) throws IOException, MimeTypeException {
+ // If no specific classloader was requested, use our own class's one
+ if (classLoader == null) {
+ classLoader = MimeTypesReader.class.getClassLoader();
+ }
+
// This allows us to replicate class.getResource() when using
// the classloader directly
String classPrefix =
MimeTypesReader.class.getPackage().getName().replace('.', '/') + "/";
- ClassLoader cl = MimeTypesReader.class.getClassLoader();
// Get the core URL, and all the extensions URLs
- URL coreURL = cl.getResource(classPrefix+coreFilePath);
+ URL coreURL = classLoader.getResource(classPrefix+coreFilePath);
List<URL> extensionURLs = Collections.list(
- cl.getResources(classPrefix+extensionFilePath));
+ classLoader.getResources(classPrefix+extensionFilePath));
// Swap that into an Array, and process
List<URL> urls = new ArrayList<URL>();
Modified:
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
URL:
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java?rev=1499844&r1=1499843&r2=1499844&view=diff
==============================================================================
---
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
(original)
+++
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
Thu Jul 4 19:10:57 2013
@@ -17,9 +17,12 @@
package org.apache.tika.config;
import java.net.URL;
+import java.util.List;
+import java.util.Map;
import junit.framework.TestCase;
+import org.apache.tika.ResourceLoggingClassLoader;
import org.apache.tika.exception.TikaException;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.DefaultParser;
@@ -81,4 +84,42 @@ public class TikaConfigTest extends Test
}
}
+ /**
+ * TIKA-1145 If the TikaConfig has a ClassLoader set on it,
+ * that should be used when loading the mimetypes and when
+ * discovering services
+ */
+ public void testClassLoaderUsedEverywhere() throws Exception {
+ ResourceLoggingClassLoader customLoader =
+ new ResourceLoggingClassLoader(getClass().getClassLoader());
+ TikaConfig config;
+
+ // Without a classloader set, normal one will be used
+ config = new TikaConfig();
+ config.getMediaTypeRegistry();
+ config.getParser();
+ assertEquals(0, customLoader.getLoadedResources().size());
+
+ // With a classloader set, resources will come through it
+ config = new TikaConfig(customLoader);
+ config.getMediaTypeRegistry();
+ config.getParser();
+
+ Map<String,List<URL>> resources = customLoader.getLoadedResources();
+ int resourcesCount = resources.size();
+ assertTrue(
+ "Not enough things used the classloader, found only " +
resourcesCount,
+ resourcesCount > 3
+ );
+
+ // Ensure everything that should do, did use it
+ // - Parsers
+
assertNotNull(resources.get("META-INF/services/org.apache.tika.parser.Parser"));
+ // - Detectors
+
assertNotNull(resources.get("META-INF/services/org.apache.tika.detect.Detector"));
+ // - Built-In Mimetypes
+
assertNotNull(resources.get("org/apache/tika/mime/tika-mimetypes.xml"));
+ // - Custom Mimetypes
+
assertNotNull(resources.get("org/apache/tika/mime/custom-mimetypes.xml"));
+ }
}
\ No newline at end of file
Modified:
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
URL:
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java?rev=1499844&r1=1499843&r2=1499844&view=diff
==============================================================================
---
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
(original)
+++
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
Thu Jul 4 19:10:57 2013
@@ -186,5 +186,4 @@ public class MimeTypesReaderTest extends
assertEquals(".ppt",ext);
assertEquals(".ppt",mt.getExtensions().get(0));
}
-
}