From 69fc0ad6962893fc2df777dcc2dba5ba16b7ef25 Mon Sep 17 00:00:00 2001 From: Jeanette Clark Date: Mon, 8 Apr 2024 15:04:45 -0700 Subject: [PATCH] use try with resources pattern --- .github/workflows/build.yml | 2 +- pom.xml | 6 +- .../edu/ucsb/nceas/mdq/rest/RunsResource.java | 82 +++++++------- .../ucsb/nceas/mdq/rest/SuitesResource.java | 105 +++++++----------- 4 files changed, 84 insertions(+), 111 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 80b21c2..b990cb0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -46,7 +46,7 @@ jobs: docker-publish: name: Docker Build and Publish - if: github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref_name, 'feature') + if: github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref_name, 'bugfix') needs: maven-build runs-on: ubuntu-latest permissions: diff --git a/pom.xml b/pom.xml index 4e80f50..8bbe823 100644 --- a/pom.xml +++ b/pom.xml @@ -6,8 +6,8 @@ UTF-8 metadig - 3.0.0 - 3.0.0 + 3.0.1-SNAPSHOT + 3.0.1-SNAPSHOT 4.0.0 @@ -15,7 +15,7 @@ edu.ucsb.nceas metadig-webapp war - 3.0.0 + 3.0.1-SNAPSHOT metadig-webapp diff --git a/src/main/java/edu/ucsb/nceas/mdq/rest/RunsResource.java b/src/main/java/edu/ucsb/nceas/mdq/rest/RunsResource.java index 771c063..d10a295 100644 --- a/src/main/java/edu/ucsb/nceas/mdq/rest/RunsResource.java +++ b/src/main/java/edu/ucsb/nceas/mdq/rest/RunsResource.java @@ -12,6 +12,7 @@ import edu.ucsb.nceas.mdqengine.exception.MetadigException; import edu.ucsb.nceas.mdqengine.exception.MetadigStoreException; import edu.ucsb.nceas.mdqengine.store.StoreFactory; +import edu.ucsb.nceas.mdqengine.store.DatabaseStore; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -26,71 +27,63 @@ @Path("runs") public class RunsResource { - private Log log = LogFactory.getLog(this.getClass()); + private Log log = LogFactory.getLog(this.getClass()); + + public RunsResource() {} - public RunsResource() { - } - /** - * Method handling HTTP GET requests. The returned object will be sent - * to the client as "text/plain" media type. + * Method handling HTTP GET requests. The returned object will be sent to the client as + * "text/plain" media type. * * @return String that will be returned as a text/plain response. */ -// @GET -// @Produces(MediaType.APPLICATION_JSON) + // @GET + // @Produces(MediaType.APPLICATION_JSON) public String listRuns() { // persist = true causes a database based store to be created by the factory. - boolean persist = true; - MDQStore store = null; - try { - store = StoreFactory.getStore(persist); + Collection runs = null; + try (DatabaseStore store = new DatabaseStore()) { + runs = store.listRuns(); } catch (MetadigException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); - throw(ise); + throw (ise); } - - Collection runs = store.listRuns(); - store.shutdown(); return JsonMarshaller.toJson(runs); } - + @GET @Path("/{suite}/{id : .+}") // Allow for '/' in the metadataId - @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public Response getRun(@PathParam("suite") String suiteId, @PathParam("id") String metadataId, @Context Request r) throws UnsupportedEncodingException, JAXBException { - - boolean persist = true; - MDQStore store = null; - try { - store = StoreFactory.getStore(persist); - } catch (MetadigException e) { - InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); - throw(ise); - } - // Decode just the pid portion of the URL - try { - metadataId = java.net.URLDecoder.decode(metadataId, StandardCharsets.UTF_8.name()); - } catch (UnsupportedEncodingException e) { - // not going to happen - value came from JDK's own StandardCharsets - } + @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) + public Response getRun(@PathParam("suite") String suiteId, @PathParam("id") String metadataId, + @Context Request r) throws UnsupportedEncodingException, JAXBException { Run run = null; - log.debug("Getting run for suiteId: " + suiteId + ", metadataId: " + metadataId); - try { - run = store.getRun(metadataId, suiteId); - } catch (MetadigStoreException e) { + try (DatabaseStore store = new DatabaseStore()) { + + // Decode just the pid portion of the URL + try { + metadataId = java.net.URLDecoder.decode(metadataId, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + // not going to happen - value came from JDK's own StandardCharsets + } + + log.debug("Getting run for suiteId: " + suiteId + ", metadataId: " + metadataId); + try { + run = store.getRun(metadataId, suiteId); + } catch (MetadigStoreException e) { + InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); + throw (ise); + } + } catch (MetadigException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); - throw(ise); - } finally { - store.shutdown(); + throw (ise); } - if(run != null) { + if (run != null) { log.debug("Retrieved run with pid: " + run.getId()); } else { log.info("Run not retrieved for suiteId: " + suiteId + ", metadataId: " + metadataId); - if(run == null) { + if (run == null) { return Response.status(Response.Status.NOT_FOUND).build(); } } @@ -98,7 +91,8 @@ public Response getRun(@PathParam("suite") String suiteId, @PathParam("id") Stri // Get the HTML request 'Accept' header specified media type and return that type String resultString = null; List vs = - Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).build(); + Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE) + .build(); Variant v = r.selectVariant(vs); if (v == null) { return Response.notAcceptable(vs).build(); diff --git a/src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java b/src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java index 573db49..6958220 100644 --- a/src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java +++ b/src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java @@ -16,6 +16,7 @@ import edu.ucsb.nceas.mdqengine.exception.MetadigException; import edu.ucsb.nceas.mdqengine.store.StoreFactory; +import edu.ucsb.nceas.mdqengine.store.DatabaseStore; import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; @@ -66,7 +67,6 @@ public String listSuites() { } Collection suites = store.listSuites(); - store.shutdown(); return JsonMarshaller.toJson(suites); } @@ -84,7 +84,6 @@ public String getSuite(@PathParam("id") String id) throw (ise); } Suite suite = store.getSuite(id); - store.shutdown(); return XmlMarshaller.toXml(suite, true); } @@ -92,26 +91,20 @@ public String getSuite(@PathParam("id") String id) // @Consumes(MediaType.MULTIPART_FORM_DATA) // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21 public boolean createSuite(@FormDataParam("suite") InputStream xml) { - boolean persist = true; - MDQStore store = null; - MDQEngine engine = null; - try { - store = StoreFactory.getStore(persist); - engine = new MDQEngine(); - } catch (MetadigException | IOException | ConfigurationException e) { + + try (DatabaseStore store = new DatabaseStore()) { + Suite suite = null; + try { + suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class); + store.createSuite(suite); + } catch (Exception e) { + log.error(e.getMessage(), e); + return false; + } + } catch (MetadigException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); throw (ise); } - Suite suite = null; - try { - suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class); - store.createSuite(suite); - } catch (Exception e) { - log.error(e.getMessage(), e); - return false; - } finally { - store.shutdown(); - } return true; } @@ -121,26 +114,20 @@ public boolean createSuite(@FormDataParam("suite") InputStream xml) { // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21 public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") InputStream xml) throws JAXBException, IOException { - boolean persist = true; - MDQStore store = null; - MDQEngine engine = null; - try { - store = StoreFactory.getStore(persist); - engine = new MDQEngine(); - } catch (MetadigException | IOException | ConfigurationException e) { + + try (DatabaseStore store = new DatabaseStore()) { + Suite suite = null; + try { + suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class); + store.updateSuite(suite); + } catch (Exception e) { + log.error(e.getMessage(), e); + return false; + } + } catch (MetadigException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); throw (ise); } - Suite suite = null; - try { - suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class); - store.updateSuite(suite); - } catch (Exception e) { - log.error(e.getMessage(), e); - return false; - } finally { - store.shutdown(); - } return true; } @@ -149,19 +136,14 @@ public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") I // @Produces(MediaType.TEXT_PLAIN) // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21 public boolean deleteSuite(@PathParam("id") String id) { - boolean persist = true; - MDQStore store = null; - MDQEngine engine = null; - try { - store = StoreFactory.getStore(persist); - engine = new MDQEngine(); - } catch (MetadigException | IOException | ConfigurationException e) { + + try (DatabaseStore store = new DatabaseStore()) { + Suite suite = store.getSuite(id); + store.deleteSuite(suite); + } catch (MetadigException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); throw (ise); } - Suite suite = store.getSuite(id); - store.deleteSuite(suite); - store.shutdown(); return true; } @@ -179,8 +161,6 @@ public Response run(@PathParam("id") String id, // id is the metadig suite id // ("high", "medium", "low") @Context Request r) throws UnsupportedEncodingException, JAXBException { - boolean persist = true; - MDQStore store = null; MDQEngine engine = null; if (priority == null) @@ -224,26 +204,25 @@ public Response run(@PathParam("id") String id, // id is the metadig suite id // to the processing queue. if (priority.equals("high")) { - try { - store = StoreFactory.getStore(persist); + try (DatabaseStore store = new DatabaseStore()) { engine = new MDQEngine(); + + try { + log.info("Running suite " + id + " for pid " + + sysMeta.getIdentifier().getValue()); + Map params = new HashMap(); + Suite suite = store.getSuite(id); + run = engine.runSuite(suite, input, params, sysMeta); + store.createRun(run); + Dispatcher.getDispatcher("python").close(); + } catch (Exception e) { + log.error(e.getMessage(), e); + return Response.serverError().entity(e).build(); + } } catch (MetadigException | IOException | ConfigurationException e) { InternalServerErrorException ise = new InternalServerErrorException(e.getMessage()); throw (ise); } - try { - log.info("Running suite " + id + " for pid " + sysMeta.getIdentifier().getValue()); - Map params = new HashMap(); - Suite suite = store.getSuite(id); - run = engine.runSuite(suite, input, params, sysMeta); - store.createRun(run); - Dispatcher.getDispatcher("python").close(); - } catch (Exception e) { - log.error(e.getMessage(), e); - return Response.serverError().entity(e).build(); - } finally { - store.shutdown(); - } // determine the format of plot to return List vs = Variant