JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
WebModelManager.java
Go to the documentation of this file.
1 package jarmos.io;
2 
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URI;
7 import java.net.URL;
8 import java.net.URLClassLoader;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Scanner;
12 
24 public class WebModelManager extends AModelManager {
25 
29  public static final String DIRLIST_FILE = "models.txt";
30 
31  private URL rooturl;
32 
36  public WebModelManager(URL rooturl) {
37  super();
38  this.rooturl = rooturl;
39  }
40 
41  public WebModelManager(String url) throws MalformedURLException {
42  this(new URL(url));
43  }
44 
45  // /**
46  // * @return The root web url
47  // */
48  // public String getRootURL() {
49  // return rooturl;
50  // }
51 
55  @Override
56  public ClassLoader getClassLoader() {
57  try {
58  URL url = new URL(rooturl + "/" + getModelDir() + "/" + CLASSES_JARFILE);
59  return new URLClassLoader(new URL[] { url }, super.getClassLoader());
60  } catch (MalformedURLException e) {
61  return super.getClassLoader();
62  }
63  }
64 
65  /*
66  * (non-Javadoc)
67  *
68  * @see kermor.java.io.IModelManager#getInStream(java.lang.String)
69  */
70  @Override
71  protected InputStream getInStreamImpl(String filename) throws IOException {
72  URL u = new URL(rooturl + "/" + getModelDir() + "/" + filename);
73  return u.openStream();
74  }
75 
76  @Override
77  protected String[] getFolderList() throws IOException {
78  URL u = new URL(rooturl + "/" + DIRLIST_FILE);
79  Scanner s = new Scanner(u.openStream());
80  List<String> folders = new ArrayList<String>();
81  while (s.hasNextLine()) {
82  folders.add(s.nextLine());
83  }
84  s.close();
85  if (folders.size() > 0) {
86  return folders.toArray(new String[0]);
87  } else {
88  return new String[0];
89  }
90  }
91 
95  @Override
96  public boolean modelFileExists(String filename) {
97  try {
98  getInStreamImpl(filename).close();
99  return true;
100  } catch (IOException e) {
101  return false;
102  }
103  }
104 
105  @Override
106  public URI getModelURI() {
107  return URI.create(rooturl.toString());
108  }
109 
110  @Override
111  protected String getLoadingMessage() {
112  return "Reading remote models";
113  }
114 
115 }
URI getModelURI()
Returns an URI for the current model location/directory.
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...
String getLoadingMessage()
A short message that writes &quot;loading SD models&quot; dependent on the actual instance. ...
InputStream getInStreamImpl(String filename)
Template method.
static final String DIRLIST_FILE
The file containing the model folders to consider per line.
A model manager reading models from a remote web location.
String[] getFolderList()
Returns the list of all models directories available at the ModelManagers source location.
boolean modelFileExists(String filename)