You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2010/12/10 15:10:15 UTC
svn commit: r1044362 - in
/tika/trunk/tika-core/src/main/java/org/apache/tika/config:
LoadErrorHandler.java ServiceLoader.java
Author: jukka
Date: Fri Dec 10 14:10:15 2010
New Revision: 1044362
URL: http://svn.apache.org/viewvc?rev=1044362&view=rev
Log:
TIKA-569: More fault-tolerant loading of parsers and detectors
Add a customizable strategy for handling load errors
Added:
tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java (with props)
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
Added: tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java?rev=1044362&view=auto
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java (added)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java Fri Dec 10 14:10:15 2010
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.config;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public interface LoadErrorHandler {
+
+ void handleLoadError(String classname, Throwable throwable);
+
+ LoadErrorHandler IGNORE = new LoadErrorHandler() {
+ public void handleLoadError(String classname, Throwable throwable) {
+ }
+ };
+
+ LoadErrorHandler WARN = new LoadErrorHandler() {
+ public void handleLoadError(String classname, Throwable throwable) {
+ Logger.getLogger(classname).log(
+ Level.WARNING, "Unable to load " + classname, throwable);
+ }
+ };
+
+ LoadErrorHandler THROW = new LoadErrorHandler() {
+ public void handleLoadError(String classname, Throwable throwable) {
+ throw new RuntimeException("Unable to load " + classname, throwable);
+ }
+ };
+
+}
Propchange: tika/trunk/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java?rev=1044362&r1=1044361&r2=1044362&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java Fri Dec 10 14:10:15 2010
@@ -79,8 +79,15 @@ public class ServiceLoader {
private final ClassLoader loader;
- public ServiceLoader(ClassLoader loader) {
+ private final LoadErrorHandler handler;
+
+ public ServiceLoader(ClassLoader loader, LoadErrorHandler handler) {
this.loader = loader;
+ this.handler = handler;
+ }
+
+ public ServiceLoader(ClassLoader loader) {
+ this(loader, LoadErrorHandler.IGNORE);
}
public ServiceLoader() {
@@ -104,7 +111,11 @@ public class ServiceLoader {
String path = "META-INF/services/" + service.getName();
Enumeration<URL> resources = loader.getResources(path);
for (URL resource : Collections.list(resources)) {
- names.addAll(getServiceClassNames(resource));
+ try {
+ names.addAll(getServiceClassNames(resource));
+ } catch (IOException e) {
+ handler.handleLoadError(service.getName(), e);
+ }
}
} catch (IOException ignore) {
// We couldn't get the list of service resource files
@@ -112,15 +123,12 @@ public class ServiceLoader {
for (String name : names) {
try {
- System.out.println("Loading " + name);
Class<?> klass = loader.loadClass(name);
if (service.isAssignableFrom(klass)) {
providers.add((T) klass.newInstance());
}
- } catch (Throwable ignore) {
- // Catching and ignoring Throwables here since we
- // could be seeing all sorts of weird linking problems
- // from loading a service class
+ } catch (Throwable t) {
+ handler.handleLoadError(name, t);
}
}
}
@@ -132,29 +140,26 @@ public class ServiceLoader {
private static final Pattern WHITESPACE = Pattern.compile("\\s+");
- private Set<String> getServiceClassNames(URL resource) {
+ private Set<String> getServiceClassNames(URL resource)
+ throws IOException {
+ Set<String> names = new HashSet<String>();
+ InputStream stream = resource.openStream();
try {
- Set<String> names = new HashSet<String>();
- InputStream stream = resource.openStream();
- try {
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(stream, "UTF-8"));
- String line = reader.readLine();
- while (line != null) {
- line = COMMENT.matcher(line).replaceFirst("");
- line = WHITESPACE.matcher(line).replaceAll("");
- if (line.length() > 0) {
- names.add(line);
- }
- line = reader.readLine();
+ BufferedReader reader =
+ new BufferedReader(new InputStreamReader(stream, "UTF-8"));
+ String line = reader.readLine();
+ while (line != null) {
+ line = COMMENT.matcher(line).replaceFirst("");
+ line = WHITESPACE.matcher(line).replaceAll("");
+ if (line.length() > 0) {
+ names.add(line);
}
- } finally {
- stream.close();
+ line = reader.readLine();
}
- return names;
- } catch (IOException e) {
- return Collections.emptySet();
+ } finally {
+ stream.close();
}
+ return names;
}
}