Skip to content

Commit

Permalink
TIKA-4290 Used try-with-resources for some Closeables. (#1888)
Browse files Browse the repository at this point in the history
* TIKA-4290 Used try-with-resources for some Closeables. It prevents resource leak
  • Loading branch information
dk2k committed Aug 6, 2024
1 parent 5963b6a commit ec4f86f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,14 @@ public Set<MediaType> getSupportedTypes(ParseContext parseContext) {
public void parse(InputStream inputStream, ContentHandler contentHandler, Metadata metadata,
ParseContext parseContext) throws IOException, SAXException, TikaException {
metadata.set(Metadata.CONTENT_TYPE, geoInfoType);
DataStore dataStore = null;
DefaultMetadata defaultMetadata = null;
XHTMLContentHandler xhtmlContentHandler = new XHTMLContentHandler(contentHandler, metadata);

TemporaryResources tmp =
TikaInputStream.isTikaInputStream(inputStream) ? null : new TemporaryResources();
try {
TikaInputStream tikaInputStream = TikaInputStream.get(inputStream, tmp, metadata);
try (TikaInputStream tikaInputStream = TikaInputStream.get(inputStream, tmp, metadata)) {
File file = tikaInputStream.getFile();
dataStore = DataStores.open(file);
defaultMetadata = new DefaultMetadata(dataStore.getMetadata());
if (defaultMetadata != null) {
try (DataStore dataStore = DataStores.open(file)) {
DefaultMetadata defaultMetadata = new DefaultMetadata(dataStore.getMetadata());
extract(xhtmlContentHandler, metadata, defaultMetadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,16 @@ public void initialize(Map<String, Param> params) throws TikaConfigException {
LOG.info("Going to load Inception network...");
long st = System.currentTimeMillis();

KerasModelBuilder builder =
try (KerasModelBuilder builder =
new KerasModel().modelBuilder().modelHdf5Filename(modelWeightsPath)
.enforceTrainingConfig(false);
.enforceTrainingConfig(false)) {

builder.inputShape(new int[]{imgHeight, imgWidth, 3});
KerasModel model = builder.buildModel();
this.graph = model.getComputationGraph();
long time = System.currentTimeMillis() - st;
LOG.info("Loaded the Inception model. Time taken={}ms", time);
builder.inputShape(new int[]{imgHeight, imgWidth, 3});
KerasModel model = builder.buildModel();
this.graph = model.getComputationGraph();
long time = System.currentTimeMillis() - st;
LOG.info("Loaded the Inception model. Time taken={}ms", time);
}
} catch (IOException | InvalidKerasConfigurationException |
UnsupportedKerasConfigurationException e) {
throw new TikaConfigException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,80 +184,81 @@ public Map<String, Set<String>> recognise(String text) {

try {
String url = restHostUrlStr + readRestEndpoint();
Response response =
try (Response response =
WebClient.create(url).accept(MediaType.APPLICATION_JSON)
.post("text=" + text);
int responseCode = response.getStatus();

if (responseCode == 200) {
String result = response.readEntity(String.class);
JSONObject jsonObject = convertToJSONObject(result);
JSONArray measurements = convertToJSONArray(jsonObject, "measurements");
for (Object measurement : measurements) {

StringBuffer measurementString = new StringBuffer();
StringBuffer normalizedMeasurementString = new StringBuffer();

JSONObject quantity = (JSONObject) convertToJSONObject(measurement.toString())
.get("quantity");
if (quantity != null) {
if (quantity.containsKey("rawValue")) {
String measurementNumber =
(String) convertToJSONObject(quantity.toString())
.get("rawValue");
measurementString.append(measurementNumber);
measurementString.append(" ");
measurementNumberSet.add(measurementNumber);
}

if (quantity.containsKey("normalizedQuantity")) {
String normalizedMeasurementNumber =
convertToJSONObject(quantity.toString())
.get("normalizedQuantity").toString();
normalizedMeasurementString.append(normalizedMeasurementNumber);
normalizedMeasurementString.append(" ");
}

if (quantity.containsKey("type")) {
String measurementType =
(String) convertToJSONObject(quantity.toString()).get("type");
measurementTypeSet.add(measurementType);
}

JSONObject jsonObj = (JSONObject) convertToJSONObject(quantity.toString());
if (jsonObj.containsKey("rawUnit")) {
JSONObject rawUnit = (JSONObject) jsonObj.get("rawUnit");
String unitName =
(String) convertToJSONObject(rawUnit.toString()).get("name");
unitSet.add(unitName);
measurementString.append(unitName);
}

if (jsonObj.containsKey("normalizedUnit")) {
JSONObject normalizedUnit = (JSONObject) jsonObj.get("normalizedUnit");
String normalizedUnitName =
(String) convertToJSONObject(normalizedUnit.toString())
.get("name");
normalizedMeasurementString.append(normalizedUnitName);
}

if (!measurementString.toString().equals("")) {
measurementSet.add(measurementString.toString());
.post("text=" + text)) {
int responseCode = response.getStatus();

if (responseCode == 200) {
String result = response.readEntity(String.class);
JSONObject jsonObject = convertToJSONObject(result);
JSONArray measurements = convertToJSONArray(jsonObject, "measurements");
for (Object measurement : measurements) {

StringBuffer measurementString = new StringBuffer();
StringBuffer normalizedMeasurementString = new StringBuffer();

JSONObject quantity = (JSONObject) convertToJSONObject(measurement.toString())
.get("quantity");
if (quantity != null) {
if (quantity.containsKey("rawValue")) {
String measurementNumber =
(String) convertToJSONObject(quantity.toString())
.get("rawValue");
measurementString.append(measurementNumber);
measurementString.append(" ");
measurementNumberSet.add(measurementNumber);
}

if (quantity.containsKey("normalizedQuantity")) {
String normalizedMeasurementNumber =
convertToJSONObject(quantity.toString())
.get("normalizedQuantity").toString();
normalizedMeasurementString.append(normalizedMeasurementNumber);
normalizedMeasurementString.append(" ");
}

if (quantity.containsKey("type")) {
String measurementType =
(String) convertToJSONObject(quantity.toString()).get("type");
measurementTypeSet.add(measurementType);
}

JSONObject jsonObj = (JSONObject) convertToJSONObject(quantity.toString());
if (jsonObj.containsKey("rawUnit")) {
JSONObject rawUnit = (JSONObject) jsonObj.get("rawUnit");
String unitName =
(String) convertToJSONObject(rawUnit.toString()).get("name");
unitSet.add(unitName);
measurementString.append(unitName);
}

if (jsonObj.containsKey("normalizedUnit")) {
JSONObject normalizedUnit = (JSONObject) jsonObj.get("normalizedUnit");
String normalizedUnitName =
(String) convertToJSONObject(normalizedUnit.toString())
.get("name");
normalizedMeasurementString.append(normalizedUnitName);
}

if (!measurementString.toString().equals("")) {
measurementSet.add(measurementString.toString());
}

if (!normalizedMeasurementString.toString().equals("")) {
normalizedMeasurementSet.add(normalizedMeasurementString.toString());
}
}

if (!normalizedMeasurementString.toString().equals("")) {
normalizedMeasurementSet.add(normalizedMeasurementString.toString());
}
}

}

entities.put("MEASUREMENT_NUMBERS", measurementNumberSet);
entities.put("MEASUREMENT_UNITS", unitSet);
entities.put("MEASUREMENTS", measurementSet);
entities.put("NORMALIZED_MEASUREMENTS", normalizedMeasurementSet);
entities.put("MEASUREMENT_TYPES", measurementTypeSet);
entities.put("MEASUREMENT_NUMBERS", measurementNumberSet);
entities.put("MEASUREMENT_UNITS", unitSet);
entities.put("MEASUREMENTS", measurementSet);
entities.put("NORMALIZED_MEASUREMENTS", normalizedMeasurementSet);
entities.put("MEASUREMENT_TYPES", measurementTypeSet);

}
}
} catch (Exception e) {
LOG.info(e.getMessage(), e);
Expand Down

0 comments on commit ec4f86f

Please sign in to comment.