1 /*
  2  * RhinoLoader.js - loader implementation for Rhino-based apps.
  3  *
  4  * Copyright © 2015, JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 importPackage(java.io);
 21 
 22 var ilib = require("./ilib.js");
 23 var path = require("./Path.js");
 24 var Loader = require("./Loader.js");
 25 
 26 /**
 27  * @class
 28  * An implementation of the Loader class for Rhino.
 29  *
 30  * @private
 31  * @constructor
 32  */
 33 var RhinoLoader = function() {
 34     //console.log("new RhinoLoader instance called with " + fr);
 35 
 36     this.parent.call(this, ilib);
 37 
 38     this.root = module.resolve("..") || environment["user.dir"];
 39     this.root = this.root.replace("file://", "");
 40     // console.log("RhinoLoader using root: " + this.root);
 41 
 42     if (this.root[this.root.length-1] === '/') {
 43         this.root = this.root.substring(0, this.root.length-1);
 44     }
 45 
 46     this.includePath.push(path.join(this.root, "resources"));     // always check the application's resources dir first
 47 
 48     // then a standard locale dir of a built version of ilib from npm
 49     this._exists(path.join(this.root, "locale"), "localeinfo.json");
 50 
 51     // try the standard install directories
 52     // this won't work under a web server because /usr is out
 53     // of the context that the web app is allowed to access
 54     //this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json");
 55 
 56     // ... else fall back to see if we're in a check-out dir of ilib
 57     // this._exists(path.join(this.root, "data", "locale"), "localeinfo.json");
 58 
 59     //console.log("RhinoLoader: include path is now " + JSON.stringify(this.includePath));
 60 };
 61 
 62 RhinoLoader.prototype = new Loader();
 63 RhinoLoader.prototype.parent = Loader;
 64 RhinoLoader.prototype.constructor = RhinoLoader;
 65 
 66 RhinoLoader.prototype._loadFile = function (pathname, sync, cb) {
 67     // ignore sync flag -- always load synchronously
 68     // console.log("RhinoLoader._loadFile: attempting to load " + pathname);
 69     var text = "";
 70     var reader;
 71     try {
 72         reader = new BufferedReader(new InputStreamReader(new FileInputStream(pathname), "utf-8"));
 73         var tmp;
 74         while ((tmp = reader.readLine()) !== null) {
 75             text += tmp + '\n';
 76         }
 77     } catch (e) {
 78         // ignore
 79         text = undefined;
 80     } finally {
 81         if (reader) {
 82             try {
 83                 reader.close();
 84             } catch (e2) {}
 85         }
 86         cb && typeof(cb) === 'function' && cb(text);
 87     }
 88     return text;
 89 };
 90 
 91 RhinoLoader.prototype._exists = function(dir, file) {
 92     var fullpath = path.normalize(path.join(dir, file));
 93     // console.log("RhinoLoader._exists: checking for the existence of " + fullpath);
 94     try {
 95         var f = new File(fullpath);
 96         if (f.exists() && f.canRead()) {
 97             // console.log("RhinoLoader._exists: found");
 98             this.includePath.push(dir);
 99         }
100     } catch (e) {
101         // ignore errors -- that means we have a permission problem and shouldn't add
102         // the dir to the include path anyways
103         console.log(e);
104     }
105 };
106 
107 module.exports = RhinoLoader;