Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for TIKA-3390 contributed by kamaci #437

Merged
merged 1 commit into from May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -45,8 +45,8 @@ class BatchCommandLineBuilder {

protected static String[] build(String[] args) throws IOException {

Map<String, String> processArgs = new LinkedHashMap<String, String>();
Map<String, String> jvmOpts = new LinkedHashMap<String,String>();
Map<String, String> processArgs = new LinkedHashMap<>();
Map<String, String> jvmOpts = new LinkedHashMap<>();
//take the args, and divide them into process args and options for
//the forked jvm process (i.e. log files, etc)
mapifyArgs(args, processArgs, jvmOpts);
Expand All @@ -72,7 +72,7 @@ protected static String[] build(String[] args) throws IOException {
jvmOpts.put("-Dlog4j.configuration=log4j_batch_process.properties", "");
}
//now build the full command line
List<String> fullCommand = new ArrayList<String>();
List<String> fullCommand = new ArrayList<>();
fullCommand.add("java");
boolean foundHeadlessOption = false;
for (Map.Entry<String, String> e : jvmOpts.entrySet()) {
Expand Down
32 changes: 11 additions & 21 deletions tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java
Expand Up @@ -686,23 +686,15 @@ private void configure() throws TikaException, IOException, SAXException {

private void displayMetModels(){
Class<?>[] modelClasses = Metadata.class.getInterfaces();
Arrays.sort(modelClasses, new Comparator<Class<?>>() {
public int compare(Class<?> o1, Class<?> o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(modelClasses, Comparator.comparing(Class::getName));

for (Class<?> modelClass: modelClasses) {
// we don't care about internal Tika met classes
// if we do, then we can take this conditional out
if (!modelClass.getSimpleName().contains("Tika")) {
System.out.println(modelClass.getSimpleName());
Field[] keyFields = modelClass.getFields();
Arrays.sort(keyFields, new Comparator<Field>() {
public int compare(Field o1, Field o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(keyFields, Comparator.comparing(Field::getName));
for (Field keyField: keyFields) {
System.out.println(" "+keyField.getName());
}
Expand Down Expand Up @@ -784,21 +776,19 @@ private String indent(int indent) {
private Parser[] sortParsers(Map<Parser, Set<MediaType>> parsers) {
// Get a nicely sorted list of the parsers
Parser[] sortedParsers = parsers.keySet().toArray(new Parser[0]);
Arrays.sort(sortedParsers, new Comparator<Parser>() {
public int compare(Parser p1, Parser p2) {
String name1 = p1.getClass().getName();
String name2 = p2.getClass().getName();
return name1.compareTo(name2);
}
Arrays.sort(sortedParsers, (p1, p2) -> {
String name1 = p1.getClass().getName();
String name2 = p2.getClass().getName();
return name1.compareTo(name2);
});
return sortedParsers;
}

private Map<Parser, Set<MediaType>> invertMediaTypeMap(Map<MediaType, Parser> supported) {
Map<Parser,Set<MediaType>> parsers = new HashMap<Parser, Set<MediaType>>();
Map<Parser,Set<MediaType>> parsers = new HashMap<>();
for(Entry<MediaType, Parser> e : supported.entrySet()) {
if (!parsers.containsKey(e.getValue())) {
parsers.put(e.getValue(), new HashSet<MediaType>());
parsers.put(e.getValue(), new HashSet<>());
}
parsers.get(e.getValue()).add(e.getKey());
}
Expand Down Expand Up @@ -839,8 +829,8 @@ private void displaySupportedTypes() {
* @param magicDir Path to the magic directory
*/
private void compareFileMagic(String magicDir) throws Exception {
Set<String> tikaLacking = new TreeSet<String>();
Set<String> tikaNoMagic = new TreeSet<String>();
Set<String> tikaLacking = new TreeSet<>();
Set<String> tikaNoMagic = new TreeSet<>();

// Sanity check
File dir = new File(magicDir);
Expand All @@ -854,7 +844,7 @@ private void compareFileMagic(String magicDir) throws Exception {
}

// Find all the mimetypes in the directory
Set<String> fileMimes = new HashSet<String>();
Set<String> fileMimes = new HashSet<>();
for (File mf : dir.listFiles()) {
if (mf.isFile()) {
BufferedReader r = new BufferedReader(new InputStreamReader(
Expand Down
Expand Up @@ -113,7 +113,7 @@ public Icon getVisualRepresentation(Transferable arg0) {
}

private static List<File> uriToFileList(Object data) {
List<File> list = new ArrayList<File>();
List<File> list = new ArrayList<>();
StringTokenizer st = new StringTokenizer(data.toString(), "\r\n");
while (st.hasMoreTokens())
{
Expand Down
20 changes: 7 additions & 13 deletions tika-app/src/main/java/org/apache/tika/gui/TikaGUI.java
Expand Up @@ -28,7 +28,6 @@
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ProgressMonitorInputStream;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
Expand Down Expand Up @@ -72,7 +71,6 @@
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.DelegatingParser;
import org.apache.tika.parser.DigestingParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
Expand Down Expand Up @@ -123,16 +121,12 @@ public static void main(String[] args) throws Exception {
}
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final TikaConfig finalConfig = config;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TikaGUI(new DigestingParser(
new AutoDetectParser(finalConfig),
new CommonsDigester(MAX_MARK,
CommonsDigester.DigestAlgorithm.MD5,
CommonsDigester.DigestAlgorithm.SHA256)
)).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new TikaGUI(new DigestingParser(
new AutoDetectParser(finalConfig),
new CommonsDigester(MAX_MARK,
CommonsDigester.DigestAlgorithm.MD5,
CommonsDigester.DigestAlgorithm.SHA256)
)).setVisible(true));
}

/**
Expand Down Expand Up @@ -628,7 +622,7 @@ public boolean select(Metadata metadata) {
* parser.
*/
private static class ImageSavingParser extends AbstractParser {
private Map<String, File> wanted = new HashMap<String, File>();
private Map<String, File> wanted = new HashMap<>();
private Parser downstreamParser;
private File tmpDir;

Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.metadata.serialization.JsonMetadataList;
import org.apache.tika.utils.ProcessUtils;

import org.junit.After;
import org.junit.Before;
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.RecursiveParserWrapper;
import org.apache.tika.sax.BasicContentHandlerFactory;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down
24 changes: 10 additions & 14 deletions tika-batch/src/main/java/org/apache/tika/batch/BatchProcess.java
Expand Up @@ -341,13 +341,11 @@ private void startConsumersManager() {
consumersManager.init();
return;
}
Thread timed = new Thread() {
public void run() {
LOG.trace("about to start consumers manager");
consumersManager.init();
LOG.trace("finished starting consumers manager");
}
};
Thread timed = new Thread(() -> {
LOG.trace("about to start consumers manager");
consumersManager.init();
LOG.trace("finished starting consumers manager");
});
//don't allow this thread to keep process alive
timed.setDaemon(true);
timed.start();
Expand All @@ -368,13 +366,11 @@ private void shutdownConsumersManager() {
consumersManager.shutdown();
return;
}
Thread timed = new Thread() {
public void run() {
LOG.trace("starting to shutdown consumers manager");
consumersManager.shutdown();
LOG.trace("finished shutting down consumers manager");
}
};
Thread timed = new Thread(() -> {
LOG.trace("starting to shutdown consumers manager");
consumersManager.shutdown();
LOG.trace("finished shutting down consumers manager");
});
timed.setDaemon(true);
timed.start();
try {
Expand Down
Expand Up @@ -78,20 +78,15 @@ public static void main(String[] args) throws Exception {
final BatchProcessDriverCLI runner = new BatchProcessDriverCLI(args);

//make absolutely certain that the forked process is terminated
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
runner.stop();
}
});
Runtime.getRuntime().addShutdownHook(new Thread(runner::stop));

runner.execute();
System.out.println("FSBatchProcessDriver has gracefully completed");
System.exit(0);
}

private String[] tryToReadMaxRestarts(String[] commandLine) {
List<String> args = new ArrayList<String>();
List<String> args = new ArrayList<>();
for (int i = 0; i < commandLine.length; i++) {
String arg = commandLine[i];
if (arg.equals("-maxRestarts")) {
Expand Down
Expand Up @@ -145,7 +145,7 @@ public BatchProcess build(Node docElement, Map<String, String> incomingRuntimeAt
ArrayBlockingQueue<FileResource> queue = buildQueue(docElement, runtimeAttributes);

NodeList children = docElement.getChildNodes();
Map<String, Node> keyNodes = new HashMap<String, Node>();
Map<String, Node> keyNodes = new HashMap<>();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() != Node.ELEMENT_NODE) {
Expand Down Expand Up @@ -220,7 +220,7 @@ private StatusReporter buildReporter(FileResourceCrawler crawler,

private Map<String, String> setNumConsumersInRuntimeAttributes(Node docElement,
Map<String, String> incomingRuntimeAttributes) {
Map<String, String> runtimeAttributes = new HashMap<String, String>();
Map<String, String> runtimeAttributes = new HashMap<>();

for (Map.Entry<String, String> e : incomingRuntimeAttributes.entrySet()) {
runtimeAttributes.put(e.getKey(), e.getValue());
Expand Down Expand Up @@ -271,7 +271,7 @@ private ArrayBlockingQueue<FileResource> buildQueue(Node docElement,
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
}

return new ArrayBlockingQueue<FileResource>(maxQueueSize);
return new ArrayBlockingQueue<>(maxQueueSize);
}

private ConsumersManager buildConsumersManager(Node node, Map<String, String> runtimeAttributes,
Expand Down
Expand Up @@ -113,7 +113,7 @@ private void execute(String[] args) throws Exception {
System.exit(BatchProcessDriverCLI.PROCESS_NO_RESTART_EXIT_CODE);
}

Map<String, String> mapArgs = new HashMap<String, String>();
Map<String, String> mapArgs = new HashMap<>();
for (Option option : line.getOptions()) {
String v = option.getValue();
if (v == null || v.equals("")) {
Expand Down
Expand Up @@ -120,7 +120,7 @@ public ConsumersManager build(Node node, Map<String, String> runtimeAttributes,
config = TikaConfig.getDefaultConfig();
}

List<FileResourceConsumer> consumers = new LinkedList<FileResourceConsumer>();
List<FileResourceConsumer> consumers = new LinkedList<>();
int numConsumers = BatchProcessBuilder.getNumConsumers(runtimeAttributes);

NodeList nodeList = node.getChildNodes();
Expand All @@ -131,12 +131,16 @@ public ConsumersManager build(Node node, Map<String, String> runtimeAttributes,
for (int i = 0; i < nodeList.getLength(); i++) {
Node child = nodeList.item(i);
String cn = child.getNodeName();
if (cn.equals("parser")) {
parserFactoryNode = child;
} else if (cn.equals("contenthandler")) {
contentHandlerFactoryNode = child;
} else if (cn.equals("outputstream")) {
outputStreamFactoryNode = child;
switch (cn) {
case "parser":
parserFactoryNode = child;
break;
case "contenthandler":
contentHandlerFactoryNode = child;
break;
case "outputstream":
outputStreamFactoryNode = child;
break;
}
}

Expand Down
Expand Up @@ -29,12 +29,8 @@ public static <T> T buildClass(Class<T> iface, String className) {
return (T) clazz.newInstance();
}
throw new IllegalArgumentException(
iface.toString() + " is not assignable from " + className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
iface + " is not assignable from " + className);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}

Expand Down
Expand Up @@ -33,7 +33,7 @@ public class XMLDOMUtil {
* @return map of attributes
*/
public static Map<String, String> mapifyAttrs(Node node, Map<String, String> overwrite) {
Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new HashMap<>();
NamedNodeMap nnMap = node.getAttributes();
for (int i = 0; i < nnMap.getLength(); i++) {
Node attr = nnMap.item(i);
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void testEmbeddedWithNPE() throws Exception {
final Metadata metadata = new Metadata();
metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, "embedded_with_npe.xml");

ArrayBlockingQueue<FileResource> queue = new ArrayBlockingQueue<FileResource>(2);
ArrayBlockingQueue<FileResource> queue = new ArrayBlockingQueue<>(2);
queue.add(new FileResource() {

@Override
Expand Down Expand Up @@ -102,7 +102,7 @@ public void testEmbeddedThenNPE() throws Exception {
final Metadata metadata = new Metadata();
metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, "embedded_then_npe.xml");

ArrayBlockingQueue<FileResource> queue = new ArrayBlockingQueue<FileResource>(2);
ArrayBlockingQueue<FileResource> queue = new ArrayBlockingQueue<>(2);
queue.add(new FileResource() {

@Override
Expand Down Expand Up @@ -145,7 +145,7 @@ public InputStream openInputStream() throws IOException {


private class MockOSFactory implements OutputStreamFactory {
List<ByteArrayOutputStream> streams = new ArrayList<ByteArrayOutputStream>();
List<ByteArrayOutputStream> streams = new ArrayList<>();

@Override
public OutputStream getOutputStream(Metadata metadata) throws IOException {
Expand Down
Expand Up @@ -205,7 +205,7 @@ public void testBundleDetectors() throws Exception {

// Get the raw detectors list from the traditional service loading mechanism
DefaultDetector detector = new DefaultDetector();
Set<String> rawDetectors = new HashSet<String>();
Set<String> rawDetectors = new HashSet<>();
for (Detector d : detector.getDetectors()) {
if (d instanceof DefaultDetector) {
for (Detector dChild : ((DefaultDetector) d).getDetectors()) {
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.apache.tika.concurrent;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand All @@ -30,13 +29,7 @@ public class SimpleThreadPoolExecutor extends ThreadPoolExecutor implements
ConfigurableThreadPoolExecutor {

public SimpleThreadPoolExecutor() {
super(1, 2, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
new ThreadFactory() {

@Override
public Thread newThread(Runnable r) {
return new Thread(r, "Tika Executor Thread");
}
});
super(1, 2, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
r -> new Thread(r, "Tika Executor Thread"));
}
}
2 changes: 1 addition & 1 deletion tika-core/src/main/java/org/apache/tika/config/Param.java
Expand Up @@ -130,7 +130,7 @@ public static <T> Param<T> load(Node node) throws TikaConfigException {
value = valueAttr;
}

Param<T> ret = new Param<T>();
Param<T> ret = new Param<>();
ret.name = nameAttr.getTextContent();
if (typeAttr != null) {
ret.setTypeString(typeAttr.getTextContent());
Expand Down