JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
FileModelManager.java
Go to the documentation of this file.
1 package jarmos.io;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.MalformedURLException;
10 import java.net.URI;
11 import java.net.URL;
12 import java.net.URLClassLoader;
13 
23 public class FileModelManager extends AModelManager {
24 
25  // public static final String MODEL_DIR = "C:\\Users\\CreaByte\\Documents\\Uni\\Software\\JaRMoS\\JaRMoSModels";
26  public static final String MODEL_DIR = "/home/dwirtz/aghhome/Software/Eclipse/JaRMoS/JaRMoSModels";
27 
28  private String root;
29 
33  public FileModelManager() {
34  this(MODEL_DIR);
35  }
36 
40  public FileModelManager(String root) {
41  super();
42  File r = new File(root);
43  if (!r.exists()) {
44  throw new IllegalArgumentException("Directory does not exist: '" + root + "'");
45  }
46  this.root = root;
47  }
48 
52  @Override
53  public ClassLoader getClassLoader() {
54  try {
55  URL url = new File(root + "/" + getModelDir() + "/" + CLASSES_JARFILE).toURI().toURL();
56  return new URLClassLoader(new URL[] { url }, super.getClassLoader());
57  } catch (MalformedURLException e) {
58  throw new RuntimeException("Creating a file with path '" + root + "/" + getModelDir() + "/"
59  + CLASSES_JARFILE + "' caused a MalformedURLException.", e);
60  }
61  }
62 
63  /*
64  * (non-Javadoc)
65  *
66  * @see kermor.java.io.IModelManager#getInStream(java.lang.String)
67  */
68  @Override
69  protected InputStream getInStreamImpl(String filename) throws FileNotFoundException {
70  return new FileInputStream(getFullModelPath() + filename);
71  }
72 
76  @Override
77  protected String[] getFolderList() throws IOException {
78  // return new File(root + File.separator + getModelDir()).list();
79  return new File(root).list();
80  }
81 
85  @Override
86  public boolean modelFileExists(String filename) {
87  return new File(root + File.separator + getModelDir() + File.separator + filename).exists();
88  }
89 
90  protected String getFullModelPath() {
91  return root + File.separator + getModelDir() + File.separator;
92  }
93 
103  public void writeModelFile(String filename, InputStream in) throws IOException {
104  File file = new File(getFullModelPath() + filename);
105  // Delete if file exists
106  if (file.exists())
107  file.delete();
108  // Create new
109  FileOutputStream out = new FileOutputStream(file);
110  byte[] buffer = new byte[1024];
111  int bytes_read = 0;
112  while ((bytes_read = in.read(buffer)) > 0) {
113  out.write(buffer, 0, bytes_read);
114  }
115  in.close();
116  }
117 
123  public boolean clearCurrentModel() {
124  return deleteDir(new File(getFullModelPath()));
125  }
126 
127  private boolean deleteDir(File dir) {
128  if (dir.isDirectory()) {
129  String[] children = dir.list();
130  for (int i = 0; i < children.length; i++) {
131  boolean success = deleteDir(new File(dir, children[i]));
132  if (!success) {
133  return false;
134  }
135  }
136  }
137  // The directory is now empty so delete it
138  return dir.delete();
139  }
140 
146  public String getRoot() {
147  return root;
148  }
149 
150  @Override
151  public URI getModelURI() {
152  return new File(getFullModelPath()).toURI(); // URI.create("file://" + root + "/" + getModelDir());
153  }
154 
155  @Override
156  protected String getLoadingMessage() {
157  return "Reading models";
158  }
159 
160 }
String getLoadingMessage()
A short message that writes &quot;loading SD models&quot; dependent on the actual instance. ...
String getRoot()
Returns the root folder for the model directories.
Manages models loaded from the file system available via the java.io classes.
static final String CLASSES_JARFILE
The name of the jar file inside a models directory containing .class files in java bytecode...
This class serves as base class for accessing various types of models at different locations...
FileModelManager()
Calls the constructor with the FileModelManager.MODEL_DIR static string as root.
boolean clearCurrentModel()
Removes.
URI getModelURI()
Returns an URI for the current model location/directory.
void writeModelFile(String filename, InputStream in)
Writes the given inputstream to the file specified by filename to the current model directory...
static final String MODEL_DIR
boolean modelFileExists(String filename)
InputStream getInStreamImpl(String filename)
Template method.