Skip to content

Commit

Permalink
fix merge conflicts pr#446, clean up code, and add debug loggin
Browse files Browse the repository at this point in the history
  • Loading branch information
tballison committed Jan 27, 2022
1 parent 0439d00 commit aca45d1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
6 changes: 4 additions & 2 deletions tika-bundles/tika-bundle-standard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
poi-scratchpad|
poi-ooxml|
poi-ooxml-lite|
log4j-core|
log4j-api|
commons-math3|
curvesapi|
Expand Down Expand Up @@ -210,6 +211,9 @@
!org.junit.*,
!junit.*,
org.apache.tika.fork,
org.slf4j,
org.slf4j.event,
org.slf4j.helpers,
android.util;resolution:=optional,
com.apple.eawt;resolution:=optional,
com.github.luben.zstd;resolution:=optional,
Expand Down Expand Up @@ -311,8 +315,6 @@
org.jaxen.util;resolution:=optional,
org.openxmlformats.schemas.officeDocument.x2006.math;resolution:=optional,
org.openxmlformats.schemas.schemaLibrary.x2006.main;resolution:=optional,
org.slf4j;resolution:=optional,
org.slf4j.helpers;resolution:=optional,
org.w3c.dom;resolution:=optional,
org.w3c.dom.svg;resolution:=optional,
org.xml.sax;resolution:=optional,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public Option[] configuration() throws IOException, URISyntaxException, ClassNot
bundle(new File(base, "tika-core.jar").toURI().toURL().toString()),
//I couldn't find a way to get the build of bundle to work via imports
//for this one
mavenBundle("commons-io", "commons-io", "2.8.0"),
mavenBundle("org.apache.logging.log4j", "log4j-core", "2.15.0"),
mavenBundle("org.apache.logging.log4j", "log4j-api", "2.15.0"),
mavenBundle("commons-io", "commons-io", "2.11.0"),
mavenBundle("org.apache.logging.log4j", "log4j-core", "2.17.1"),
mavenBundle("org.apache.logging.log4j", "log4j-api", "2.17.1"),
mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.8.5"),
mavenBundle("org.ops4j.pax.logging", "pax-logging-service", "1.8.5"),
junitBundles(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ServiceLoader {
* source of service instances in the {@link #loadServiceProviders(Class)}
* method.
*/
private static final Map<Object, RankedService> services = new HashMap<>();
private static final Map<Object, RankedService> SERVICES = new HashMap<>();
private static final Pattern COMMENT = Pattern.compile("#.*");
private static final Pattern WHITESPACE = Pattern.compile("\\s+");
/**
Expand Down Expand Up @@ -121,14 +121,14 @@ public static void setContextClassLoader(ClassLoader loader) {
}

static void addService(Object reference, Object service, int rank) {
synchronized (services) {
services.put(reference, new RankedService(service, rank));
synchronized (SERVICES) {
SERVICES.put(reference, new RankedService(service, rank));
}
}

static Object removeService(Object reference) {
synchronized (services) {
return services.remove(reference);
synchronized (SERVICES) {
return SERVICES.remove(reference);
}
}

Expand Down Expand Up @@ -262,8 +262,8 @@ public <T> List<T> loadServiceProviders(Class<T> iface) {
@SuppressWarnings("unchecked")
public <T> List<T> loadDynamicServiceProviders(Class<T> iface) {
if (dynamic) {
synchronized (services) {
List<RankedService> list = new ArrayList<>(services.values());
synchronized (SERVICES) {
List<RankedService> list = new ArrayList<>(SERVICES.values());
Collections.sort(list);

List<T> providers = new ArrayList<>(list.size());
Expand All @@ -275,7 +275,7 @@ public <T> List<T> loadDynamicServiceProviders(Class<T> iface) {
return providers;
}
} else {
return new ArrayList<>(0);
return Collections.EMPTY_LIST;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ private static List<Detector> getDefaultDetectors(MimeTypes types, ServiceLoader

@Override
public List<Detector> getDetectors() {
if (loader != null) {
if (loader != null && loader.isDynamic()) {
List<Detector> detectors = loader.loadDynamicServiceProviders(Detector.class);
detectors.addAll(super.getDetectors());
return detectors;
if (detectors.size() > 0) {
detectors.addAll(super.getDetectors());
return detectors;
} else {
return super.getDetectors();
}
} else {
return super.getDetectors();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CloseShieldInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.tika.config.Field;
import org.apache.tika.config.LoadErrorHandler;
Expand All @@ -56,6 +58,8 @@ public class DefaultZipContainerDetector implements Detector {
*/
private static final long serialVersionUID = 2891763938430295453L;

private static final Logger LOG = LoggerFactory.getLogger(DefaultZipContainerDetector.class);

static {
TIFF_SIGNATURES[0] = new byte[]{'M', 'M', 0x00, 0x2a};
TIFF_SIGNATURES[1] = new byte[]{'I', 'I', 0x2a, 0x00};
Expand All @@ -73,7 +77,7 @@ public class DefaultZipContainerDetector implements Detector {

public DefaultZipContainerDetector() {
this(new ServiceLoader(DefaultZipContainerDetector.class.getClassLoader(),
LoadErrorHandler.WARN, true));
LoadErrorHandler.WARN, false));
}

public DefaultZipContainerDetector(ServiceLoader loader) {
Expand Down Expand Up @@ -200,6 +204,10 @@ private MediaType detectZipFormatOnFile(TikaInputStream tis) {
for (ZipContainerDetector zipDetector : getDetectors()) {
MediaType type = zipDetector.detect(zip, tis);
if (type != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} detected {}", zipDetector.getClass(),
type.toString());
}
//e.g. if OPCPackage has already been set
//don't overwrite it with the zip
if (tis.getOpenContainer() == null) {
Expand All @@ -208,6 +216,10 @@ private MediaType detectZipFormatOnFile(TikaInputStream tis) {
tis.addCloseableResource(zip);
}
return type;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} detected null", zipDetector.getClass());
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -283,10 +295,16 @@ private MediaType finalDetect(StreamingDetectContext detectContext) {
}

private List<ZipContainerDetector> getDetectors() {
if (loader != null) {
List<ZipContainerDetector> zipDetectors = new ArrayList<>(staticZipDetectors);
zipDetectors.addAll(loader.loadDynamicServiceProviders(ZipContainerDetector.class));
return zipDetectors;
if (loader != null && loader.isDynamic()) {
List<ZipContainerDetector> dynamicDetectors =
loader.loadDynamicServiceProviders(ZipContainerDetector.class);
if (dynamicDetectors.size() > 0) {
List<ZipContainerDetector> zipDetectors = new ArrayList<>(staticZipDetectors);
zipDetectors.addAll(dynamicDetectors);
return zipDetectors;
} else {
return staticZipDetectors;
}
}
return staticZipDetectors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.commons.io.IOUtils;

import org.apache.tika.config.LoadErrorHandler;
import org.apache.tika.config.ServiceLoader;
import org.apache.tika.io.LookaheadInputStream;
import org.apache.tika.metadata.Metadata;
Expand All @@ -43,15 +44,17 @@ public class StreamingZipContainerDetector extends DefaultZipContainerDetector {


public StreamingZipContainerDetector() {
this(new ServiceLoader(StreamingZipContainerDetector.class.getClassLoader(), true));
this(new ServiceLoader(StreamingZipContainerDetector.class.getClassLoader(),
LoadErrorHandler.WARN, false));
}

public StreamingZipContainerDetector(ServiceLoader loader) {
super(loader);
}

public StreamingZipContainerDetector(List<ZipContainerDetector> zipDetectors) {
//TODO: OPCBased needs to be last!!!
//in initial development, OPCBased needed to be last.
//this should have been fixed.
super(zipDetectors);
}

Expand Down

0 comments on commit aca45d1

Please sign in to comment.