]> matita.cs.unibo.it Git - helm.git/commitdiff
1) New syntax: methods add, reload and remove can now process multiple
authorClaudio Sacerdoti Coen <claudio.sacerdoticoen@unibo.it>
Tue, 6 Nov 2001 18:34:48 +0000 (18:34 +0000)
committerClaudio Sacerdoti Coen <claudio.sacerdoticoen@unibo.it>
Tue, 6 Nov 2001 18:34:48 +0000 (18:34 +0000)
   entries with just one method invocation. NO BACKWARD COMPATIBILITY RETAINED.
2) Serialization and deserialization: when serialization is enabled, every
   time a stylesheet is recompiled it is also serialized on disk. When
   booting, UWOBO automatically reloads every serialized stylesheet.
3) More verbosity: every method gives back to the user and writes in the log
   much more information than before.
4) Methods update and updateAll (whose semantic was unclear) definitely removed.

helm/uwobo/build.xml
helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java
helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java
helm/uwobo/src/it/unibo/cs/helm/uwobo/properties.txt

index de9d59a8764b0ba05d1971d99ad7b9b7e08c4643..abdcdc858d10ce1dfd994447a890e6f7034465fb 100644 (file)
@@ -4,7 +4,8 @@
 
        <property name="Name" value="Uwobo"/>
        <property name="name" value="uwobo"/>
-       <property name="version" value="1.1.14beta"/>
+       <property name="version" value="1.2"/>
+       <property name="serialization_dir" value=""/>
 
        <property file=".${name}.properties" />
        <property file="${user.home}/.${name}.properties" />
@@ -63,6 +64,7 @@
                <filter token="VERSION" value="${version}" />
                <filter token="DATE" value="${TODAY}" />
                <filter token="TIME" value="${TSTAMP}" />
+                <filter token="SERIALIZATION_DIR" value="${serialization_dir}" />
                <copy todir="${build.dir}" overwrite="true" filtering="on">
                        <fileset dir="${src.dir}">
                                <include name="**/properties.txt" />
index cd2f2fa30a2a05251d599ae687bab383a7be623c..9b33b662977fc37cb9c9e5d915641396c9dbb6f5 100644 (file)
@@ -37,7 +37,7 @@ import org.xml.sax.helpers.*;
 */
 
 public class Server {
-       private static class Style {
+       private static class Style implements Serializable {
                public String fileName;
                public long lastModified;
                public Templates stylesheet;
@@ -48,6 +48,7 @@ public class Server {
        public static final String VERSION;
        public static final String DATE;
        public static final String TIME;
+       public static final String SERIALIZATION_DIR;
 
        static {
                Properties props = new Properties();
@@ -63,17 +64,71 @@ public class Server {
                VERSION = props.getProperty("VERSION");
                DATE = props.getProperty("DATE");
                TIME = props.getProperty("TIME");
+                SERIALIZATION_DIR = props.getProperty("SERIALIZATION_DIR");
        }
 
+        Server() {
+           if(!SERIALIZATION_DIR.equals("")) {
+              log("Looking for serialized stylesheets");
+              File [] serialized = new File(SERIALIZATION_DIR).listFiles();
+              if (serialized == null) {
+                 log("Serialized stylesheets directory \"" + SERIALIZATION_DIR +
+                  "\" not found");
+              } else {
+                 for (int i = 0; i < serialized.length ; i++) {
+                    File filename = serialized[i];
+                    String key = filename.getName();
+                    log("Found serialized stylesheet " + key);
+                    log("Reloading serialized stylesheet \"" + filename + "\"... ");
+                    FileInputStream istream;
+                    try {
+                       istream = new FileInputStream(filename);
+                       ObjectInputStream p = new ObjectInputStream(istream);
+                       Style style = (Style)p.readObject();
+                       istream.close();
+                       hashMap.put(key, style);
+                    } catch (Exception e) {log(e.toString());};
+                 }
+                 log("Serialized stylesheets loaded!");
+              }
+           } else
+              log("Stylesheet serialization is off. Set the property SERIALIZATION_DIR to a non-empty value to turn it on.");
+          
+        }
+
        private final HashMap hashMap = new HashMap();
        private static int logCounter = 0;
        
-       private final Templates compileStylesheet(String xsl_file) throws TransformerConfigurationException, SAXException, IOException {
+       private final String compileStylesheet(Style style, String key) throws TransformerConfigurationException, SAXException, IOException {
                StreamSource streamsource =
-                       new StreamSource(new URL(xsl_file).openStream());
-               streamsource.setSystemId(xsl_file);
-               return ((SAXTransformerFactory)TransformerFactory.newInstance())
-                       .newTemplates(streamsource);
+                       new StreamSource(new URL(style.fileName).openStream());
+               streamsource.setSystemId(style.fileName);
+                Templates templates =
+                   ((SAXTransformerFactory)TransformerFactory.newInstance())
+                      .newTemplates(streamsource);
+                style.stylesheet = templates;
+                File serializationFile = new File(SERIALIZATION_DIR,key);
+                String res = "";
+                if(!SERIALIZATION_DIR.equals("")) {
+                   try {
+                      FileOutputStream ostream =
+                         new FileOutputStream(serializationFile);
+                      ObjectOutputStream p = new ObjectOutputStream(ostream);
+                      p.writeObject(style);
+                      p.flush();
+                      ostream.close();
+                      res =
+                       "Stylesheet serialized in \"" + serializationFile + "\"";
+                     log(res);
+                   } catch (FileNotFoundException e) {
+                      res = "Warning: Stylesheet not " +
+                         "serialized. Error opening " + "file \"" +
+                         serializationFile + "\"";
+                     log(res);
+                      res = htmlOfWarning(res);
+                   }
+                }
+                return res;
        }
 
        private String getContentType(Templates templates) {
@@ -136,43 +191,81 @@ public class Server {
                System.err.println(SERVERNAME + "[" + logCounter++ + "]: " + msg);
        }
 
-       public void add(String filename, String key) throws TransformerConfigurationException, SAXException, IOException {
+       public String add(String filename, String key) throws TransformerConfigurationException, SAXException, IOException {
+               log("processing stylesheet \"" + filename + "\" using key " + key + "... ");
                if (hashMap.containsKey(key)) {
-                       log("there is already a stylesheet with keyword \"" + key + "\" (aborted)");
-                       return;
+                   Style style = (Style)hashMap.get(key);
+                   if (style.fileName.equals(filename)) {
+                      String msg =
+                         "Warning: Stylesheet already loaded. Reloading...";
+                      log(msg);
+                      String msg2 = reload(key);
+                      return htmlOfWarning(msg) +
+                         (msg2.equals("") ? "" : "<br />" + msg2);
+                   } else {
+                      String res =
+                         "Error: There is already a stylesheet with keyword \""+
+                          key + "\" (aborted)";
+                      log(res);
+                      return htmlOfError(res);
+                   }
                }
 
                Style style = new Style();
                style.fileName = filename;
                style.lastModified = new File(filename).lastModified();
-               log("processing stylesheet \"" + filename + "\"... ");
-               style.stylesheet = compileStylesheet(filename);
-               log("done!");
+               String res = compileStylesheet(style,key);
+                log("done!");
 
                hashMap.put(key, style);
+                return res;
        }
 
-       public void removeAll() 
-        {
-               String key;
-               Style style;
+       public ArrayList removeAll() throws TransformerConfigurationException, SAXException, IOException {
+                ArrayList res = new ArrayList();
+                String log = "";
                Iterator i = hashMap.keySet().iterator();
                while (i.hasNext()) {
-                       key = (String)i.next();
-                       style = (Style)hashMap.get(key);
-                       log("removing \"" + key + " (" + style.fileName + ")");
-               }
-               hashMap.clear();
+                        String key = (String)i.next();
+                       Style style = (Style)hashMap.get(key);
+                        String msg =
+                         "removing \"" + key + " (" + style.fileName + ")";
+                        res.add(msg);
+                        log(msg);
+                        msg = partialRemove(key,false);
+                        if(!msg.equals(""))
+                           res.add(msg);
+                }
+                hashMap.clear();
+                return res;
        }
 
-       public void remove(String key) {
+       private String partialRemove(String key, boolean removeFromHashTable) {
+                String res = "";
                Style style = (Style)hashMap.get(key);
                if (style != null) {
                        log("removing \"" + key + " (" + style.fileName + ")");
-                       hashMap.remove(key);
+                       if (removeFromHashTable)
+                           hashMap.remove(key);
+                        if(!SERIALIZATION_DIR.equals("")) {
+                           File to_delete = new File(SERIALIZATION_DIR,key);
+                           if (!to_delete.delete()) {
+                              res = "Warning: Serialized stylesheet \"" +
+                                 to_delete + "\" to remove not found";
+                              log(res);
+                              res = htmlOfWarning(res);
+                           }
+                        }
                } else {
-                       log("error, stylesheet \"" + key + "\" not loaded");
+                       res = "Error: stylesheet \"" + key + "\" not loaded";
+                        log(res);
+                        res = htmlOfError(res);
                }
+                return res;
+       }
+
+       public String remove(String key) {
+           return partialRemove(key,true);
        }
 
        public String getContentType(String key) {
@@ -180,7 +273,7 @@ public class Server {
                if (style != null) {
                        return getContentType(style.stylesheet);
                } else {
-                       log("error, stylesheet \"" + key + "\" not loaded");
+                       log("Error, stylesheet \"" + key + "\" not loaded");
                        return null;
                }
        }
@@ -213,7 +306,7 @@ public class Server {
                        return applyStylesheet(style.stylesheet, keys[idx].params, applyRec(keys, idx - 1, saxOutput));
                }
        }
-//FileNotFoundException
+
        public void apply(String inFile, OutputStream outputStream, Key[] keys, Properties userProperties)
        throws IOException, TransformerConfigurationException, SAXException, Exception
        {
@@ -233,7 +326,7 @@ public class Server {
                        if (keys.length > 0) {
                                Style style = (Style) hashMap.get(rkeys[0].name);
                                if (style == null) {
-                                       log("error, stylesheet \"" + rkeys[0].name + "\" not loaded");
+                                       log("Error, stylesheet \"" + rkeys[0].name + "\" not loaded");
                                        return;
                                }
                                outputProperties = style.stylesheet.getOutputProperties();
@@ -266,44 +359,46 @@ public class Server {
 //             }
        }
 
-       public void reloadAll() throws TransformerConfigurationException, SAXException, IOException {
+       public ArrayList reloadAll() throws TransformerConfigurationException, SAXException, IOException {
+                ArrayList res = new ArrayList();
                Iterator i = hashMap.keySet().iterator();
-               while (i.hasNext())
-                       reload((String)i.next());
+               while (i.hasNext()) {
+                        String key = (String)i.next();
+                       Style style = (Style)hashMap.get(key);
+                        String msg =
+                         "reloading \"" + key + " (" + style.fileName + ")";
+                        res.add(msg);
+                        log(msg);
+                        msg = reload(key);
+                        if(!msg.equals(""))
+                           res.add(msg);
+                }
+                return res;
        }
 
-       public void reload(String key) throws TransformerConfigurationException, SAXException, IOException {
+       public String reload(String key) throws TransformerConfigurationException, SAXException, IOException {
+                String res = "";
                Style style = (Style)hashMap.get(key);
-                if (style != null) {
-                       log("reloading \"" + key + "\"... ");
-                       style.stylesheet = compileStylesheet(style.fileName);
-                       style.lastModified = new File(style.fileName).lastModified();
-                       log("done!");
+               if (style != null) {
+                  log("reloading \"" + key + "\"... ");
+                  style.lastModified = new File(style.fileName).lastModified();
+                   res= compileStylesheet(style,key);
+                   log("done!");
                } else {
-                       log("error, stylesheet \"" + key + "\" not loaded");
+                       res = "Error: stylesheet \"" + key + "\" not loaded";
+                        log(res);
+                        res = htmlOfError(res);
                }
+                return res;
        }
 
-       public void updateAll() throws TransformerConfigurationException, SAXException, IOException {
-               Iterator i = hashMap.keySet().iterator();
-               while (i.hasNext())
-                       update((String)i.next());
-       }
+        private String htmlOfWarning(String message)
+        {
+           return "<b style=\"color: maroon\">" + message+"</b>";
+        }
 
-       public void update(String key) throws TransformerConfigurationException, SAXException, IOException {
-               Style style = (Style)hashMap.get(key);
-                if (style != null) {
-                       log("updating \"" + key + "\"... ");
-                       File styleFile = new File(style.fileName);
-                       if (styleFile.lastModified() > style.lastModified) {
-                               style.stylesheet = compileStylesheet(style.fileName);
-                               style.lastModified = styleFile.lastModified();
-                               log("done!");
-                       } else {
-                               log("\"" + key + "\" is up to date");
-                       }
-               } else {
-                       log("error, stylesheet \"" + key + "\" not loaded");
-               }
-       }
+        private String htmlOfError(String message)
+        {
+           return "<b style=\"color: red\">" + message+"</b>";
+        }
 }
index 3b4766250a42298964bd9612dcd00399aaf23fa4..7158bc6464a12d113d09d117f3b4e205d12d20bd 100644 (file)
@@ -7,24 +7,24 @@ import javax.servlet.http.*;
 import javax.xml.transform.*;
 import org.xml.sax.*;
 
+// WARNING: next comment is obsolete!!!!!
 /*
 *
 * usage:
-*   http://aristotele/helm/servlet/uwobo/help
-*   http://aristotele/helm/servlet/uwobo/add?xsluri=&key=
-*   http://aristotele/helm/servlet/uwobo/remove[?key=]
-*   http://aristotele/helm/servlet/uwobo/list
-*   http://aristotele/helm/servlet/uwobo/reload[?key=]
-*   http://aristotele/helm/servlet/uwobo/update[?key=]
-*   http://aristotele/helm/servlet/uwobo/apply?xmluri=&keys=[&param.=]*
+*   http://hostname/helm/servlet/uwobo/help
+*   http://hostname/helm/servlet/uwobo/add?bind=key,stylesheet[&bind=key,stylesheet]*
+*   http://hostname/helm/servlet/uwobo/remove[?keys=key_1,...,key_n]
+*   http://hostname/helm/servlet/uwobo/list
+*   http://hostname/helm/servlet/uwobo/reload[?keys=key_1,...,key_n]
+*   http://hostname/helm/servlet/uwobo/apply?xmluri=xmldata&keys=key_1,...,key_n[&param.name=value]*[&param.key.name=value]*[&prop.name=[value]]*
 *
 * example:
-*   http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl&key=foo1
-*   http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl&key=foo2
-*   http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&key=foo1&key=foo2
+*   http://aristotele/helm/servlet/uwobo/add?bind=foo1,file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl
+*   http://aristotele/helm/servlet/uwobo/add?bind=foo2,file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl
+*   http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&keys=foo1,foo2
 *
 * installation notes (Tomcat):
-*      replace parser.jar and jaxp.jar from /lib with xerces.jar
+*      replace parser.jar and jaxp.jar from /lib with xerces.jar and add xalan.jar
 *
 *      add in conf/server.xml
 *              <Context path="/helm" 
@@ -37,8 +37,6 @@ import org.xml.sax.*;
 *      add in uriworkermap.properties
 *              /helm/*=ajp12
 *
-* bugs:
-*      directory base stylesheet inclusi
 *
 * @author The HELM team
 */
@@ -46,11 +44,10 @@ public class Servlet extends HttpServlet {
    
    public static final String[] usage = {
       "http://<i>hostname</i>/helm/servlet/uwobo/help",
-      "http://<i>hostname</i>/helm/servlet/uwobo/add?key=<i>key</i>,<i>stylesheet</i>[&key=<i>key</i>,<i>stylesheet</i>]*",
+      "http://<i>hostname</i>/helm/servlet/uwobo/add?bind=<i>key</i>,<i>stylesheet</i>[&bind=<i>key</i>,<i>stylesheet</i>]*",
       "http://<i>hostname</i>/helm/servlet/uwobo/remove[?keys=<i>key_1,...,key_n</i>]",
       "http://<i>hostname</i>/helm/servlet/uwobo/list",
       "http://<i>hostname</i>/helm/servlet/uwobo/reload[?keys=<i>key_1,...,key_n</i>]",
-      "http://<i>hostname</i>/helm/servlet/uwobo/update[?keys=<i>key_1,...,key_n</i>]",
       "http://<i>hostname</i>/helm/servlet/uwobo/apply?xmluri=<i>xmldata</i>&keys=<i>key_1,...,key_n</i>[&param.<i>name</i>=<i>value</i>]*[&param.<i>key</i>.<i>name</i>=<i>value</i>]*[&prop.<i>name</i>=[<i>value</i>]]*"
    };
    public static final String help;
@@ -97,7 +94,7 @@ public class Servlet extends HttpServlet {
    throws IOException
    {  
       resp.setContentType("text/html");
-      out.println("<html><body bgcolor=\"#ffffff\"><h1>Uwobo servlet</h1>");
+      out.println("<html><body bgcolor=\"#ffffff\"><h1>" + server.PACKAGE + " servlet</h1>");
    }
    
    private void html_close(ServletOutputStream out)
@@ -109,7 +106,7 @@ public class Servlet extends HttpServlet {
 
    private String msg_out(String message)
    {
-      return message+"<br>";
+      return message+"<br />";
    }
    
    private String exc_out(String message, Exception e)
@@ -121,13 +118,14 @@ public class Servlet extends HttpServlet {
       }
       String local = e.getLocalizedMessage();
       local = local.substring(local.lastIndexOf(':')+1);
-      return "<b>"+message+": "+local+"</b><br>";
+      return "<b>"+message+": "+local+"</b><br />";
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
       ServletOutputStream out = response.getOutputStream();
+      String log = "";
       
       response.setHeader("Cache-Control", "no-cache");
       response.setHeader("Pragma", "no-cache");
@@ -140,16 +138,18 @@ public class Servlet extends HttpServlet {
            sendError(response, out, "unknown command", help); return;
         } 
         if (cmd.equals("/add")) {
-           final String[] xslkey = request.getParameterValues("key");
+           final String[] xslkey = request.getParameterValues("bind");
            if (xslkey == null) {
               sendError(response, out, "bad parameters", usage[1]); return;
            } 
            html_open(response, out);
            for (int i = 0; i < xslkey.length; i++) {
               final String data[] = split2(xslkey[i], ",");
-              out.println(msg_out("adding key "+data[0]+" ("+data[1]+")"));
+              out.println(msg_out("adding stylesheet "+data[0]+" ("+data[1]+")"));
               try {
-                 server.add(data[1], data[0]);
+                 log = server.add(data[1], data[0]);
+                  if(!log.equals(""))
+                     out.println(msg_out(log));
                } catch (TransformerConfigurationException tce) {
                   out.println(exc_out("stylesheet error", tce));
                } catch (Exception e) {
@@ -236,10 +236,14 @@ public class Servlet extends HttpServlet {
            for (int i = 0; i < data.length; i++) { 
               if (data[i] == null) {
                  out.println(msg_out("removing all keys"));
-                 server.removeAll();
+                  ArrayList res;
+                 res = server.removeAll();
+                  Iterator j = res.iterator();
+                  while (j.hasNext())
+                     out.println(msg_out((String)j.next()));
               } else {
                  out.println(msg_out("removing key "+data[i]));
-                 server.remove(data[i]);
+                 out.println(msg_out(server.remove(data[i])));
               }
            }
            html_close(out); return;
@@ -261,31 +265,14 @@ public class Servlet extends HttpServlet {
               try {
                  if (data[i] == null) {
                     out.println(msg_out("reloading all keys"));
-                    server.reloadAll();
+                     ArrayList res;
+                    res = server.reloadAll();
+                     Iterator j = res.iterator();
+                     while(j.hasNext())
+                        out.println(msg_out((String)j.next()));
                  } else {
                     out.println(msg_out("reloading key "+data[i]));
-                    server.reload(data[i]);
-                 }
-              } catch (TransformerConfigurationException tce) {
-                  out.println(exc_out("stylesheet error", tce));
-               } catch (Exception e) {
-                 out.println(exc_out(null, e));
-               }
-           }
-           html_close(out); return;
-        }
-         if (cmd.equals("/update")) {
-           final String key = request.getParameter("keys");
-            final String [] data = split(key, ",");
-           html_open(response, out);
-           for (int i = 0; i < data.length; i++) {
-              try {
-                 if (data[i] == null) {
-                    out.println(msg_out("updating all keys")); 
-                    server.updateAll();
-                 } else {
-                    out.println(msg_out("updating key "+data[i]));
-                    server.update(data[i]);
+                    out.println(msg_out(server.reload(data[i])));
                  }
               } catch (TransformerConfigurationException tce) {
                   out.println(exc_out("stylesheet error", tce));
@@ -297,14 +284,21 @@ public class Servlet extends HttpServlet {
         }
         if (cmd.equals("/help")) {
            html_open(response, out);
-           out.println("<h2>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
+           out.println("<h2>Version "+server.VERSION+"</h1>");
            out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
+            if(Server.SERIALIZATION_DIR.equals("")) {
+               out.println("<p><b>Stylesheet serialization is off.</b>");
+               out.println("(To turn it on, choose a non-empty value for the " +
+                  "SERIALIZATION_DIR property.)</p>");
+            } else
+               out.println("<p><b>Serialized stylesheed are stored in &quot;" +
+                  Server.SERIALIZATION_DIR + "&quot;.</b></p>");
            out.println("<p>usage:</p>"+help+"</body></html>");
            html_close(out); return;
         }
         sendError(response, out, "unknown command", help); return;
       } catch (Exception e) {
-        sendError(response, out, exc_out(null, e),""); // FG: non dovrebbe servire mai
+        sendError(response, out, exc_out(null, e),"");
         return;
       }
    }
index eb286113204c836a433f2e2e02f760131998bb67..debd517734cbd606b2c9cc6be05a983fc3fe2654 100644 (file)
@@ -2,3 +2,4 @@ PACKAGE=@PACKAGE@
 VERSION=@VERSION@
 DATE=@DATE@
 TIME=@TIME@
+SERIALIZATION_DIR=@SERIALIZATION_DIR@