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