Skip to content
Permalink
Browse files

Add an admin method "refreshStylesheets" to clear out the style sheet…

…s cache on the server side.

Rfe:#1345
  • Loading branch information
taojing2002 committed Apr 5, 2019
1 parent 92fdd52 commit 9f344a5aacd9c9e1b7cdd3807a4324887555aee8
@@ -72,8 +72,9 @@
*/
public class DBTransform {

static final protected Map<String,Templates> TemplatesMap = new HashMap<>();
static final protected TransformerFactory transformerFactory = TransformerFactory.newInstance();
private static Map<String,Templates> TemplatesMap = new HashMap<>();
private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
private static boolean forceRebuild = false;

/**
* The method that manages the Templates Map instances that will be used to build
@@ -88,11 +89,20 @@
// directly test this class (because it was proving too difficult for me to properly
// configure this class in a test environment (related to configurating PropertyService)
// If you make changes to this method, you will need to duplicate those changes in the test class.
protected static synchronized Transformer getTransformer(String xslSystemId, boolean forceRebuild) throws TransformerConfigurationException {
if (forceRebuild || !TemplatesMap.containsKey(xslSystemId) ) {
protected static synchronized Transformer getTransformer(String xslSystemId) throws TransformerConfigurationException {
//The admin page can reset the value of forceRebuild to true.
if(forceRebuild) {
TemplatesMap = new HashMap<>();
forceRebuild = false;//after clearing the cache, we must reset the value to false in order to use the cache again.
logMetacat.debug("DBTransform.getTransformer - clear the style sheet cache and will reload the style sheets from disk.");
}
if (!TemplatesMap.containsKey(xslSystemId) ) {
logMetacat.debug("DBTransform.getTransformer - Load the style sheets from disk for the id "+xslSystemId);
Templates templates = transformerFactory.newTemplates(new StreamSource(xslSystemId));
TemplatesMap.put(xslSystemId,templates);
}
} else {
logMetacat.debug("DBTransform.getTransformer - Load the style sheets from the cache for the id "+xslSystemId);
}
return TemplatesMap.get(xslSystemId).newTransformer();
}

@@ -201,7 +211,7 @@ protected void doTransform(StreamSource xml,

if (xslSystemId != null) {

transformer = DBTransform.getTransformer(xslSystemId, false); // false means use the existing factory template
transformer = DBTransform.getTransformer(xslSystemId); // false means use the existing factory template

transformer.setParameter("qformat", qformat);
logMetacat.info("DBTransform.doTransform - qformat: " + qformat);
@@ -430,46 +440,13 @@ private String removeDOCTYPE(String in) {
return ret;
}

/**
* the main routine used to test the transform utility.
*
* Usage: java DBTransform
*/
static public void main(String[] args) {

if (args.length > 0)
{
System.err.println("Wrong number of arguments!!!");
System.err.println("USAGE: java DBTransform");
return;
} else {
try {

// Create a test document
StringBuffer testdoc = new StringBuffer();
String encoding = "UTF-8";
testdoc.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
testdoc.append("<dataset_id>DS001</dataset_id>");
testdoc.append("<title>My test doc</title></eml-dataset>");

// Transform the document to the new doctype
Writer w = new OutputStreamWriter(System.out, encoding);
DBTransform dbt = new DBTransform();
dbt.transformXMLDocument(testdoc.toString(),
"-//NCEAS//eml-dataset//EN",
"-//W3C//HTML//EN",
"knb",
w, null, null);

} catch (Exception e) {
System.err.println("EXCEPTION HANDLING REQUIRED");
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}

/**
* Set the field of forceRebuild
* @param forceRebuild
*/
public static void setForceRebuild(boolean forceBuild) {
forceRebuild = forceBuild;
}
// private void dbg(int position) {
// System.err.println("Debug flag: " + position);
// }
@@ -27,6 +27,7 @@
package edu.ucsb.nceas.metacat.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletConfig;
@@ -38,6 +39,7 @@

import org.apache.log4j.Logger;

import edu.ucsb.nceas.metacat.DBTransform;
import edu.ucsb.nceas.metacat.MetaCatServlet;
import edu.ucsb.nceas.metacat.properties.PropertyService;
import edu.ucsb.nceas.metacat.service.SessionService;
@@ -196,6 +198,9 @@ private void handleGetOrPost(HttpServletRequest request,
// process replication config
EZIDAdmin.getInstance().configureEZID(request, response);
return;
} else if (action.equals("refreshStylesheets")) {
clearStylesheetCache(response);
return;
} else {
String errorMessage = "MetacatAdminServlet.handleGetOrPost - Invalid action in configuration request: " + action;
logMetacat.error(errorMessage);
@@ -235,5 +240,20 @@ private void handleGetOrPost(HttpServletRequest request,
logMetacat.error(errorMessage);
processingErrors.add(errorMessage);
}
}
}

/*
* Method to set up the forceBuild true which will clear the style sheet map.
*/
private void clearStylesheetCache(HttpServletResponse response) throws IOException {
Boolean forceRebuild = true;
DBTransform.setForceRebuild(forceRebuild);
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.print("<success>");
out.print("The style sheet cache has been cleared and they will be reload from the disk.");
out.print("</success>");
out.close();

}
}

0 comments on commit 9f344a5

Please sign in to comment.
You can’t perform that action at this time.