JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
CachingModelManager.java
Go to the documentation of this file.
1 package jarmos.io;
2 
5 import jarmos.ModelType;
6 import jarmos.Parameters;
8 
9 import java.io.File;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URI;
14 import java.util.List;
15 
30 public class CachingModelManager extends AModelManager {
31 
32  private FileModelManager dest;
33 
34  private boolean overwriteFlag;
35 
36  private AModelManager source;
37 
45  this(source, dest, false);
46  }
47 
58  public CachingModelManager(AModelManager source, FileModelManager dest, boolean overwriteFlag) {
59  this.source = source;
60  this.dest = dest;
61  this.overwriteFlag = overwriteFlag;
62  }
63 
64  @Override
66  dest.addMessageHandler(h);
67  }
68 
69  private void cacheFile(String filename) throws IOException {
70  if (overwriteFlag || !dest.modelFileExists(filename)) {
71  dest.writeModelFile(filename, source.getInStream(filename));
72  }
73  }
74 
75  private void cacheModelFiles() throws IOException, ModelManagerException {
76  // Cache the model info file.
77  // Existence of the tag guarantess the existence of the info html file.
78  if (source.xmlTagExists("model.description.infohtml")) {
79  String infohtml = source.getModelXMLTagValue("model.description.infohtml");
80  if (!source.modelFileExists(infohtml)) {
81  throw new ModelManagerException("Inconsistent model state: XML tag for infohtml exists but the file "
82  + infohtml + " could not be found in the model directory.");
83  }
84  cacheFile(infohtml);
85  }
86  if (source.xmlTagExists("model.description.image")) {
87  String image = source.getModelXMLTagValue("model.description.image");
88  if (!source.modelFileExists(image)) {
89  throw new ModelManagerException(
90  "Inconsistent model state: XML tag for a model image exists but the file " + image
91  + " could not be found in the model directory.");
92  }
93  cacheFile(image);
94  }
95  cacheFile("dexclasses.jar");
96  cacheFile("classes.jar");
97  }
98 
99  @Override
100  public ClassLoader getClassLoader() {
101  return dest.getClassLoader();
102  }
103 
104  /*
105  * (non-Javadoc)
106  *
107  * @see jarmos.io.AModelManager#getFolderList()
108  */
109  @Override
110  protected String[] getFolderList() throws IOException {
111  return source.getFolderList();
112  }
113 
114  /*
115  * (non-Javadoc)
116  *
117  * @see jarmos.io.AModelManager#getInStreamImpl(java.lang.String)
118  */
119  @Override
120  protected InputStream getInStreamImpl(String filename) throws IOException {
121  // Write file to the destination if not already exists
122  if (overwriteFlag || !dest.modelFileExists(filename)) {
123  dest.writeModelFile(filename, source.getInStream(filename));
124  }
125  // Return stream from destination
126  return dest.getInStream(filename);
127  }
128 
129  @Override
131  return dest.getMathObjReader();
132  }
133 
134  @Override
135  public List<ModelDescriptor> getModelDescriptors(IProgressReporter pr) throws ModelManagerException {
136  // Only the source knows all models..
137  return source.getModelDescriptors(pr);
138  }
139 
140  @Override
141  public String getModelDir() {
142  return dest.getModelDir();
143  }
144 
145  @Override
147  return dest.getModelType();
148  }
149 
150  @Override
151  public URI getModelURI() {
152  return dest.getModelURI();
153  }
154 
155  @Override
156  public String getModelXMLAttribute(String attrib_name) {
157  return dest.getModelXMLAttribute(attrib_name);
158  }
159 
160  @Override
161  public String getModelXMLAttribute(String attrib_name, String tagname) {
162  return dest.getModelXMLAttribute(attrib_name, tagname);
163  }
164 
165  @Override
166  public String getModelXMLTagValue(String tagname) {
167  return dest.getModelXMLTagValue(tagname);
168  }
169 
170  @Override
171  public String getModelXMLTagValue(String tagname, String default_value) {
172  return dest.getModelXMLTagValue(tagname, default_value);
173  }
174 
175  @Override
177  return dest.getParameters();
178  }
179 
180  @Override
181  public boolean isValidModelDir(String dir) {
182  // Have to check at the source
183  return source.isValidModelDir(dir);
184  }
185 
186  /*
187  * (non-Javadoc)
188  *
189  * @see jarmos.io.AModelManager#modelFileExists(java.lang.String)
190  */
191  @Override
192  public boolean modelFileExists(String filename) {
193  return dest.modelFileExists(filename) || source.modelFileExists(filename);
194  }
195 
196  @Override
198  dest.removeMessageHandler(h);
199  }
200 
201  @Override
202  protected void sendMessage(String msg) {
203  dest.sendMessage(msg);
204  }
205 
206  @Override
207  public void useModel(String dir) throws ModelManagerException {
208  source.useModel(dir);
209  File destdir = new File(dest.getRoot() + File.separator + dir);
210  if (!destdir.isDirectory()) {
211  if (!destdir.mkdir())
212  throw new ModelManagerException("Could not create directory " + dir + " in " + dest.getRoot());
213  }
214  File modelxml = new File(destdir, "model.xml");
215  if (overwriteFlag || !modelxml.exists()) {
216  try {
217  FileOutputStream out = new FileOutputStream(modelxml);
218  byte[] buffer = new byte[1024];
219  int bytes_read = 0;
220  InputStream in = source.getInStream("model.xml");
221  while ((bytes_read = in.read(buffer)) > 0) {
222  out.write(buffer, 0, bytes_read);
223  }
224  in.close();
225  } catch (IOException e) {
226  throw new ModelManagerException("Error caching model.xml", e);
227  }
228  }
229  dest.useModel(dir);
230  try {
231  cacheModelFiles();
232  } catch (IOException e) {
233  throw new ModelManagerException("Error caching model files.", e);
234  }
235  }
236 
237  public boolean deleteCachedFiles() {
238  return dest.clearCurrentModel();
239  }
240 
241  @Override
242  public boolean xmlTagExists(String tagname) {
243  return dest.xmlTagExists(tagname);
244  }
245 
246  @Override
247  protected String getLoadingMessage() {
248  return source.getLoadingMessage();
249  }
250 
251 }
String getModelXMLAttribute(String attrib_name, String tagname)
An interface for classes capable of sending messages.
boolean modelFileExists(String filename)
Returns whether the specified file exists in the current model folder.
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
String[] getFolderList()
Returns the list of all models directories available at the ModelManagers source location.
void addMessageHandler(IMessageHandler h)
abstract boolean modelFileExists(String filename)
Returns whether the specified file exists in the current model folder.
String getRoot()
Returns the root folder for the model directories.
Manages models loaded from the file system available via the java.io classes.
Known model types within the JaRMoSBase project.
Definition: ModelType.java:9
This class serves as base class for accessing various types of models at different locations...
A class for model parameters.
Definition: Parameters.java:17
String getModelXMLTagValue(String tagname, String default_value)
Represents a short description of a model managed by a model manager.
CachingModelManager(AModelManager source, FileModelManager dest, boolean overwriteFlag)
Creates a new caching model manager.
String getModelXMLTagValue(String tagname)
URI getModelURI()
Returns an URI for the current model location/directory.
InputStream getInStreamImpl(String filename)
Template method.
String getModelXMLAttribute(String attrib_name)
void removeMessageHandler(IMessageHandler h)
CachingModelManager(AModelManager source, FileModelManager dest)
Creates a new caching model manager with overwriteFlag set to false.
A wrapper class that takes any AModelManager as source and a FileModelManager as target.
boolean xmlTagExists(String tagname)
List< ModelDescriptor > getModelDescriptors(IProgressReporter pr)
boolean xmlTagExists(String tagname)
Checks if a specified tag exists inside the current models model.xml file.
String getLoadingMessage()
A short message that writes &quot;loading SD models&quot; dependent on the actual instance. ...
boolean modelFileExists(String filename)
Simple interface for progress reporting.