View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.functions;
23  
24  import org.millscript.commons.vfs.VFile;
25  import org.millscript.commons.vfs.alerts.VEntryNotFoundAlert;
26  import org.millscript.millscript.alert.Alerts;
27  import org.millscript.millscript.vm.Machine;
28  
29  import java.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  import java.util.zip.GZIPInputStream;
33  import java.util.zip.GZIPOutputStream;
34  
35  /**
36   * This class implements the MillScript <code>cacheFile</code> function.
37   */
38  public class CacheFunction extends Function {
39  
40      /**
41       * @see org.millscript.millscript.functions.Function#apply(org.millscript.millscript.vm.Machine, int)
42       */
43      @Override
44      public void apply( final Machine mc, final int nargs ) {
45          if ( nargs == 1 ) {
46              final String name = mc.popString();
47              try {
48                  VFile i = mc.getConfig().getWebsiteCacheFolder().getVFile( name );
49                  GZIPInputStream gis = new GZIPInputStream( i.getInputStream() );
50                  ObjectInputStream ois = new ObjectInputStream( gis );
51                  mc.pushObject( ois.readObject() );
52              } catch ( IOException e ) {
53                  throw(
54                      Alerts.eval(
55                          "Problems opening cache file",
56                          null
57                      ).
58                      culprit( "cache file", name ).
59                      culprit( "detail", e.getMessage() ).
60                      mishap()
61                  );
62              } catch ( ClassNotFoundException e ) {
63                  throw(
64                      Alerts.eval(
65                          "Object with unsupported class in cache file",
66                          "Cache files are version dependent"
67                      ).
68                      culprit( "cache file", name ).
69                      culprit( "message", e.getMessage() ).
70                      mishap()
71                  );
72              }
73          } else if ( nargs == 2 ) {
74              final Object defaultValue = mc.popObject();
75              final String name = mc.popString();
76              try {
77                  VFile i = mc.getConfig().getWebsiteCacheFolder().getVFile( name );
78                  GZIPInputStream gis = new GZIPInputStream( i.getInputStream() );
79                  ObjectInputStream ois = new ObjectInputStream( gis );
80                  mc.pushObject( ois.readObject() );
81              } catch ( VEntryNotFoundAlert ex ) {
82                  // Ooops, the file doesn't exist
83                  mc.pushObject( defaultValue );
84              } catch ( IOException e ) {
85                  // Ooops, there's a problem, so return the default value
86                  mc.pushObject( defaultValue );
87              } catch ( ClassNotFoundException e ) {
88                  throw(
89                      Alerts.eval(
90                          "Object with unsupported class in cache file",
91                          "Cache files are version dependent"
92                      ).
93                      culprit( "cache file", name ).
94                      culprit( "message", e.getMessage() ).
95                      mishap()
96                  );
97              }
98          } else {
99              checkNargsGT( mc, 1, nargs );
100         }
101     }
102 
103     /**
104      * @see org.millscript.millscript.functions.Function#applyUpdater(org.millscript.millscript.vm.Machine, int, int)
105      */
106     @Override
107     public void applyUpdater( final Machine mc, final int nargs, final int unargs ) {
108         checkUNargs( mc, 1, nargs, 1, unargs );
109         Object obj = mc.popObject();
110         String name = mc.popString();
111         try {
112             VFile o = mc.getConfig().getWebsiteCacheFolder().getVFile( name );
113             GZIPOutputStream gos =  new GZIPOutputStream( o.getOutputStream() );
114             ObjectOutputStream oos = new ObjectOutputStream( gos );
115             oos.writeObject( obj );
116             oos.flush();
117             oos.close();
118         } catch ( IOException e ) {
119             throw(
120                 Alerts.eval(
121                     "Problem opening cache file",
122                     "Trying to save an object in the cache"
123                 ).
124                 culprit( "cache file", name ).
125                 culprit( "save object", obj ).
126                 culprit( "exception type", e.getClass().getName() ).
127                 culprit( "exception", e.getMessage() ).
128                 mishap()
129             );
130         }
131     }
132 
133 }