summaryrefslogtreecommitdiffstats
path: root/infrastructure/net.appjet.oui/files.scala
blob: 3df5c1cfac4bbc05e28398b5d0bf693e7ccebe59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.appjet.oui;

import net.appjet.bodylock.{BodyLock, Executable};
import net.appjet.common.util.BetterFile;

import java.io.{File, FileNotFoundException, FileInputStream, IOException, ByteArrayInputStream};
import java.util.concurrent.ConcurrentHashMap;
import java.util.WeakHashMap;

import scala.collection.mutable.{Subscriber, Message, Reset=>SReset};
import scala.collection.jcl.Conversions._;

trait WeakPublisher[A, This <: WeakPublisher[A, This]] { self: This => 
  val subscribers = new WeakHashMap[Subscriber[A, This], Unit];
  
  protected def publish(event: A): Unit = {
    subscribers.synchronized {
      val subsCopy = for (sub <- subscribers.keySet()) yield sub;
      for (sub <- subsCopy) {
        sub.notify(this, event);
      }
    }
  }
  
  def subscribe(sub: Subscriber[A, This]): Unit = {
    subscribers.synchronized {
      subscribers.put(sub, ());
    }
  }
  
  def removeSubscription(sub: Subscriber[A, This]): Unit = {
    subscribers.synchronized {
      subscribers.remove(sub);
    }
  }
}

object Reset extends SReset[Unit];

object FileCache {
  val files = new ConcurrentHashMap[String, CachedFile];
  
  def file(path: String): CachedFile = {
    if (files.containsKey(path)) {
      files.get(path);
    } else {
      val f = new CachedFile(new File(path));
      val oldFile = files.putIfAbsent(path, f);
      if (oldFile != null) {
        oldFile
      } else {
        f
      }
    }
  }
  
  def file(path: String, subscriber: Subscriber[Message[Unit], CachedFile]): CachedFile = {
    val f = file(path);
    f.subscribe(subscriber);
    f;
  }
  
  def testFiles() = {
    val iter = files.values().iterator();
    var filesHaveChanged = false;
    while (iter.hasNext()) {
      if (iter.next().test()) {
        filesHaveChanged = true;
      }
    }
    filesHaveChanged;
  }
}

class CachedFile(f: File) extends WeakPublisher[Message[Unit], CachedFile] {
  var cachedContent: Option[Array[Byte]] = None;
  def content = synchronized { 
    if (cachedContent.isEmpty) {
      cachedContent = Some(BetterFile.getFileBytes(f));
    }
    cachedContent.get;
  }
  def stream = new ByteArrayInputStream(content);

  var cachedExistence: Option[Boolean] = None;
  def exists = synchronized {
    if (cachedExistence.isEmpty) {
      cachedExistence = Some(f.exists());
    }
    cachedExistence.get;
  }
  
  var cachedDirectory: Option[Boolean] = None;
  def isDirectory = synchronized {
    if (cachedDirectory.isEmpty) {
      cachedDirectory = Some(f.isDirectory());
    }
    cachedDirectory.get;
  }

  def underlyingLastModified = f.lastModified;
  var lastModified = underlyingLastModified;
    
  def hasBeenModified = underlyingLastModified != lastModified;
  
  def test() = synchronized {
    if (hasBeenModified) {
      reset;
      true;
    } else {
      false;
    }
  }
  
  def reset = synchronized {
    lastModified = underlyingLastModified;
    cachedContent = None;
    cachedExistence = None;
    cachedDirectory = None;
    publish(Reset);
  }
}

class SpecialJarOrNotFile(root: String, fname: String) extends JarOrNotFile(root, fname) {
  override val classBase = "/net/appjet/ajstdlib/";
  override val fileSep = "/../";

  override def clone(fname: String) = new SpecialJarOrNotFile(root, fname);
}

// A JarOrNotFile that reads from the /mirror directory in the classpath.
class MirroredJarOrNotFile(root: String, fname: String) extends JarOrNotFile(root, fname) {
  override val classBase = "/mirror/";
  override def clone(fname: String) = new MirroredJarOrNotFile(root, fname);
}

class JarVirtualFile(fname: String) extends MirroredJarOrNotFile(config.useVirtualFileRoot, fname);

class JarOrNotFile(root: String, fname: String) extends Subscriber[Message[Unit], CachedFile] with WeakPublisher[Message[Unit], JarOrNotFile] {
  val classBase = "/net/appjet/ajstdlib/modules/";
  val fileSep = "/";
  val isJar = (root == null);
  val streamBase = if (isJar) getClass().getResource((classBase+fname).replaceAll("/+", "/")) else null;
  val file = if (! isJar) FileCache.file(root+fileSep+fname, this) else null;

  def openStream() = {
    if (isJar) streamBase.openStream;
    else file.stream;
  }

  def exists = {
    if (isJar) streamBase != null;
    else file.exists;
  }

  def isDirectory = if (isJar) false else file.isDirectory;

  lazy val streamModified = streamBase.openConnection().getLastModified();
  def lastModified = {
    if (isJar) streamModified;
    else file.lastModified;
  }

  def name = fname;

  override def toString() = 
    getClass.getName+": "+hashCode()+"; fname: "+fname+"; streambase: "+streamBase+"; file: "+file+(if (isJar) " from: "+classBase+fname else "");
//   override def equals(o: AnyRef) =
//     o match {
//       case jf: JarOrNotFile => {
//         classBase == jf.classBase &&
//         fileSep == jf.fileSep &&
//         root == jf.root &&
//         fname == jf.fname
//       }
//       case _ => false
//     }
//   override def hashCode() =
//     classBase.hashCode + fileSep.hashCode + root.hashCode + fname.hashCode

  def notify(pub: CachedFile, event: Message[Unit]) = synchronized {
    publish(event);
  }

  def clone(fname: String) = new JarOrNotFile(root, fname);
}

abstract class AutoUpdateFile(val fname: String) extends Subscriber[Message[Unit], JarOrNotFile] {
  def files: Array[JarOrNotFile]; // = config.moduleRoots.map(f => new JarOrNotFile(f, libName));

  def exists = files.exists(_.exists);
  def file = files.find(_.exists).getOrElse(null);
  def fileLastModified = if (exists) file.lastModified else 0L;

  // var lastModified = fileLastModified;
  // var cachedContents: Option[String] = None;

  def fail(): Nothing = {
    throw new FileNotFoundException("No such module: "+fname);
  }

  // def hasBeenModified = {
  //   if (exists) {
  //     val newModTime = try {
  //       fileLastModified
  //     } catch {
  //       case e: NoSuchElementException => fail();
  //       case e: NullPointerException => fail();
  //     }
  //     newModTime > lastModified;
  //   } else {
  //     false;
  //   }
  // }

  // def update() = synchronized {
  //   try {
  //     lastModified = fileLastModified;
  //     val contents = BetterFile.getStreamContents(file.openStream()).replace("\r\n", "\n").replace("\r", "\n");
  //     if (contents == null) {
  //       fail();
  //     }
  //     cachedContents = Some(contents);
  //   } catch {
  //     case e: IOException => {
  //       exceptionlog(e);
  //       e.printStackTrace();
  //       fail();
  //     }
  //   }
  // }
  
  def notify(pub: JarOrNotFile, event: Message[Unit]) {
    event match {
      case Reset => cachedContents = None;
    }
  }
  
  var cachedContents: Option[String] = None;
  def update() = synchronized {
    if (cachedContents.isEmpty) {
      cachedContents = Some(BetterFile.getStreamContents(file.openStream()).replace("\r\n", "\n").replace("\r", "\n"));
    }
  }
  
  def contents = synchronized {
    update();
    cachedContents.get;
  }
  
  override def toString() = "[AutoUpdateFile: "+fname+"]";
}

class FixedDiskResource(srcfile: JarOrNotFile) extends AutoUpdateFile(srcfile.name) {
  lazy val files0 = Array(srcfile);
  files0.foreach(_.subscribe(this));
  
  override def files = files0;
}

abstract class DiskLibrary(fname: String) extends AutoUpdateFile(fname) {
  var cachedExecutable: Option[Executable] = None;

  lazy val classFiles = files.map({ f => 
    val parts = f.name.split("/");
    val pathIfAny = parts.reverse.drop(1).reverse.mkString("/");
    val newFname = 
      if (pathIfAny == "")
        className(f.name);
      else
        pathIfAny+"/"+className(parts.last);
    val newFile = f.clone(newFname+".class");
    newFile.subscribe(this);
    newFile;
  });
  def className(fname: String): String = "JS$"+fname.split("\\.").reverse.drop(1).reverse.mkString(".").replaceAll("[^A-Za-z0-9]", "\\$");
  def className: String = classFile.name.split("\\.").reverse.drop(1).reverse.mkString(".");
  
  override def exists = super.exists || classFiles.exists(_.exists);
  override def file = if (super.exists) super.file else classFile;
  def classFile = classFiles.find(_.exists).getOrElse(null);

//  println("Made DiskLibrary on "+fname+", with classFile: "+classFile);

  def updateExecutable() = synchronized {
    if (classFile == null)
      super.update();
    if (cachedExecutable.isEmpty) {
      try {
        if (classFile != null) {
          cachedExecutable = Some(BodyLock.executableFromBytes(BetterFile.getStreamBytes(classFile.openStream()), className.split("/").last));
        } else {
          cachedExecutable = Some(BodyLock.compileString(contents, "module "+fname, 1));
        }
      } catch {
        case e => { cachedExecutable = None; throw e; }
      }      
    }
  }

  def executable = synchronized {
    updateExecutable();
    cachedExecutable.get
  }

  override def notify(pub: JarOrNotFile, event: Message[Unit]) = synchronized {
    super.notify(pub, event);
    event match {
      case Reset => cachedExecutable = None;
    }
  }

  override def equals(o: Any) = 
    o match {
      case dl: DiskLibrary => {
        getClass.getName == dl.getClass.getName &&
        fname == dl.fname
      }
      case _ => false;
    }
  override def hashCode() =
    getClass.getName.hashCode + fname.hashCode
}

class FixedDiskLibrary(srcfile: JarOrNotFile) extends DiskLibrary(srcfile.name) {
  lazy val files0 = Array(srcfile);
  files0.foreach(_.subscribe(this));

  override def files = files0;
}

class VariableDiskLibrary(libName: String) extends DiskLibrary(libName) {
  lazy val files0 = 
    Array.concat(Array(new MirroredJarOrNotFile(null, libName)),
                 config.moduleRoots.map(f => new JarOrNotFile(f, libName)))
  files0.foreach(_.subscribe(this));

  override def files = files0;
}