1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
83 mc.pushObject( defaultValue );
84 } catch ( IOException e ) {
85
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 }