Skip to content

Commit

Permalink
Merge pull request #16 from NASA-PDS/i14_to_main
Browse files Browse the repository at this point in the history
Fix memory leaks with Solr client connections for Solr 9
  • Loading branch information
jordanpadams authored Oct 19, 2023
2 parents c17184c + e4a5058 commit 4e6bd41
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 80 deletions.
115 changes: 81 additions & 34 deletions src/main/java/gov/nasa/pds/dsview/registry/PDS3Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@

package gov.nasa.pds.dsview.registry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.logging.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Map;
import java.util.Collection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.logging.Logger;

/**
* This class is used by the PDS data set view web interface to retrieve values
* for building the search parameter pull-down lists.
Expand All @@ -47,18 +48,20 @@
public class PDS3Search {

private static Logger log = Logger.getLogger(PDS3Search.class.getName());
static String solrServerUrl = "http://pdsdev.jpl.nasa.gov:8080/search-service/";

static String solrServerUrl = "http://pdsdev.jpl.nasa.gov:8080/search-service/";
public static final String DOI_SERVER_URL = "https://pds.nasa.gov/api/doi/0.2/dois";
/**
* Constructor.
*/
public PDS3Search(String url) {
this.solrServerUrl = url;
solrServerUrl = url;
}

public SolrDocumentList getDataSetList() throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -93,10 +96,16 @@ public SolrDocumentList getDataSetList() throws SolrServerException, IOException
}
}
return solrResults;
} finally {
solr.close();
}
}

public SolrDocument getDataSet(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -142,10 +151,16 @@ public SolrDocument getDataSet(String identifier) throws SolrServerException, IO
doc = dsDocs.get(returnIdx);
}
return doc;
} finally {
solr.close();
}
}

public SolrDocument getMission(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -177,13 +192,18 @@ public SolrDocument getMission(String identifier) throws SolrServerException, IO
}
instDocs.add(doc);
}
//if (idx>1)
// doc = instDocs.get(0);
return doc;

} finally {
solr.close();
}
}

public SolrDocument getInstHost(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -216,13 +236,18 @@ public SolrDocument getInstHost(String identifier) throws SolrServerException, I
}
instDocs.add(doc);
}
//if (idx>1)
// doc = instDocs.get(0);
return doc;
return doc;

} finally {
solr.close();
}
}

public List<SolrDocument> getInst(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -255,10 +280,17 @@ public List<SolrDocument> getInst(String identifier) throws SolrServerException,
instDocs.add(doc);
}
return instDocs;

} finally {
solr.close();
}
}

public SolrDocument getInst(String instId, String instHostId) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -289,10 +321,17 @@ public SolrDocument getInst(String instId, String instHostId) throws SolrServerE
}
}
return doc;

} finally {
solr.close();
}
}

public SolrDocument getTarget(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -326,10 +365,17 @@ public SolrDocument getTarget(String identifier) throws SolrServerException, IOE
if (idx>1)
doc = instDocs.get(0);
return doc;

} finally {
solr.close();
}
}

public SolrDocument getResource(String identifier) throws SolrServerException, IOException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrServerUrl).build();
HttpSolrClient solr = null;

try {
solr = new HttpSolrClient.Builder(solrServerUrl).build();

ModifiableSolrParams params = new ModifiableSolrParams();

Expand Down Expand Up @@ -360,20 +406,21 @@ public SolrDocument getResource(String identifier) throws SolrServerException, I
}
}
return doc;

} finally {
solr.close();
}
}

public List<String> getValues(SolrDocument doc, String key) {
Collection<Object> values = doc.getFieldValues(key);

if (values==null || values.size()==0) {
//log.info("key = " + key + " values = " + values);
return null;
}

List<String> results = new ArrayList<String>();
for (Object obj: values) {

//log.info("------------values = " + values.toString());
if (obj instanceof java.util.Date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Expand Down
Loading

0 comments on commit 4e6bd41

Please sign in to comment.