1 |
package appl.data; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.Serializable; |
5 |
|
6 |
import org.apache.log4j.LogManager; |
7 |
import org.apache.log4j.Logger; |
8 |
|
9 |
import appl.util.GeneralUtil; |
10 |
|
11 |
/** |
12 |
* Loads an object on demand with the help of an <code>DataLoader</code>. The |
13 |
* unload() method unloads a serializable {@link Object} into a temporary folder |
14 |
* on the hard disk. Unloading can be disabled via the |
15 |
* {@link #setUnloading(boolean)} method. |
16 |
* |
17 |
* @see DataLoader |
18 |
* @author Dominik Appl |
19 |
*/ |
20 |
public class LateLoadingProxy implements DataProxy, Serializable { |
21 |
|
22 |
private File unloadFile; |
23 |
|
24 |
protected transient final Logger LOG = LogManager.getLogger(this.getClass() |
25 |
.getName()); |
26 |
|
27 |
/** Object loaded? */ |
28 |
protected boolean loaded = false; |
29 |
|
30 |
protected transient DataLoader dataLoader; |
31 |
|
32 |
/** |
33 |
* The loader which constructs the object |
34 |
*/ |
35 |
protected transient DataLoader intialDataLoader; |
36 |
|
37 |
protected Object baseObject; |
38 |
|
39 |
private boolean unloadingEnabled; |
40 |
|
41 |
/** |
42 |
* Creates a new instance with the specified data loader |
43 |
* |
44 |
* @param loader |
45 |
* the load method of the {@link DataLoader} is called for |
46 |
* loading |
47 |
*/ |
48 |
public LateLoadingProxy(DataLoader loader) { |
49 |
if (loader == null) { |
50 |
throw new UnsupportedOperationException( |
51 |
"Error: the dataloader may not be null"); |
52 |
} |
53 |
setUnloadDir("Temp"); |
54 |
this.intialDataLoader = loader; |
55 |
this.dataLoader = loader; |
56 |
} |
57 |
|
58 |
private LateLoadingProxy() { |
59 |
}; |
60 |
|
61 |
/** |
62 |
* returns the File, in which the object is/will be unloaded |
63 |
*/ |
64 |
protected File getUnloadFile() { |
65 |
return unloadFile; |
66 |
} |
67 |
|
68 |
/** |
69 |
* Loads the data into memory (if not already loaded).<br> |
70 |
* This method is thread-safe |
71 |
* |
72 |
* @throws LoadingException |
73 |
*/ |
74 |
public synchronized void loadData() throws LoadingException { |
75 |
if (loaded) |
76 |
return; |
77 |
try { |
78 |
if (dataLoader == null) |
79 |
throw new UnsupportedOperationException( |
80 |
"A dataloader was found to be null!"); |
81 |
baseObject = dataLoader.load(); |
82 |
if (LOG.isDebugEnabled()) |
83 |
LOG.debug("Successfully loaded " + baseObject + " with " |
84 |
+ dataLoader.getLoadInfo()); |
85 |
loaded = true; |
86 |
|
87 |
} catch (LoadingException e) { |
88 |
throw e; |
89 |
} |
90 |
} |
91 |
|
92 |
/*************************************************************************** |
93 |
* If no Data is loaded or unloading is disabled this method does nothing, |
94 |
* else it saves the Data into a temporary File. <br> |
95 |
* Warning: Should be Thread-Safe with loadData, but was not tested |
96 |
**************************************************************************/ |
97 |
public synchronized void unloadData() { |
98 |
// only do something if the Data is loaded and unloading is enabled: |
99 |
if (!loaded) |
100 |
return; |
101 |
if (!unloadingEnabled) |
102 |
return; |
103 |
// there is no way to save the Grid to a File, when its not serializible |
104 |
if (!(baseObject instanceof java.io.Serializable)) { |
105 |
LOG.warn("Could not unload the Data because the class " |
106 |
+ baseObject.getClass().getName() + "is not serializable"); |
107 |
return; |
108 |
} |
109 |
|
110 |
// unload the Data |
111 |
try { |
112 |
GeneralUtil.SerializeToFile(baseObject, unloadFile); |
113 |
loaded = false; |
114 |
baseObject = null; |
115 |
dataLoader = new SerializedDataLoader(unloadFile, true); |
116 |
LOG.debug("Successfully unloaded Grid into File: " |
117 |
+ unloadFile.getPath()); |
118 |
} catch (Exception e) { |
119 |
LOG.error("Error while trying to unload a grid into file '" |
120 |
+ unloadFile.getAbsolutePath() + "'"); |
121 |
e.printStackTrace(); |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
/** |
127 |
* This class supports late loading, of course. |
128 |
* |
129 |
* @see appl.data.LateLoadable#unloadData() |
130 |
* @return true; |
131 |
* @see appl.data.LateLoadable#isLateLoadable() |
132 |
*/ |
133 |
public boolean isLateLoadable() { |
134 |
return true; |
135 |
} |
136 |
|
137 |
/** |
138 |
* returns the baseObject |
139 |
* |
140 |
* @see appl.data.DataProxy#getProxiedObject() |
141 |
*/ |
142 |
public Object getProxiedObject() { |
143 |
try { |
144 |
loadData(); |
145 |
} catch (LoadingException e) { |
146 |
System.err.println("Error while loading Griddata ... "); |
147 |
e.printStackTrace(); |
148 |
} |
149 |
return baseObject; |
150 |
} |
151 |
|
152 |
/** returns true, if the baseObject is loaded */ |
153 |
public boolean isLoaded() { |
154 |
return loaded; |
155 |
} |
156 |
|
157 |
public void setUnloadDir(String newUnloadDirectory) { |
158 |
unloadFile = new File(newUnloadDirectory + File.separatorChar |
159 |
+ this.toString() + ".tmp"); |
160 |
// the unload file will be deleted after the JVM shuts down (only after |
161 |
// "normal" shutdown) |
162 |
unloadFile.deleteOnExit(); |
163 |
} |
164 |
|
165 |
/** |
166 |
* Set to false, if you want to disable the unloading in a temporary file |
167 |
*/ |
168 |
public void setUnloading(boolean unloadingEnabled) { |
169 |
this.unloadingEnabled = unloadingEnabled; |
170 |
} |
171 |
|
172 |
/** |
173 |
* @return true, if unloading is enabled |
174 |
*/ |
175 |
public boolean isUnloadingEnabled() { |
176 |
return unloadingEnabled; |
177 |
} |
178 |
} |