| orion.Bundle = function(framework, bundleId, location, headers) { |
| this._framework = framework; |
| this._bundleId = bundleId; |
| this._location = location; |
| this._headers = headers || eval("(" + orion.Framework._fetch(location + "/META-INF/manifest.json") + ")"); //naughty |
| this._name = null; |
| this._version = null; |
| this._singleton = false; |
| this._imports = []; |
| this._exports = []; |
| this._resources = {}; |
| this._globals = []; |
| this._state = orion.Bundle.INSTALLED; |
| this._scope = {}; |
| this._bundleContext = null; |
| this._script = null; |
| |
| this._parseName(this._headers.name); |
| this._parseVersion(this._headers.version); |
| this._parseSingleton(this._headers.singleton); |
| this._parseImports(this._headers.imports); |
| this._parseExports(this._headers.exports); |
| this._parseResources(this._headers.resources); |
| this._parsePath(this._headers.path); |
| this._parseGlobals(this._headers.globals); |
| }; |
| |
| orion.Bundle.UNINSTALLED = 1; |
| orion.Bundle.INSTALLED = 2; |
| orion.Bundle.RESOLVED = 4; |
| |
| orion.Bundle.prototype = { |
| getName : function() { |
| return this._name; |
| }, |
| getVersion : function() { |
| return this._version; |
| }, |
| getBundleId : function() { |
| return this._bundleId; |
| }, |
| getLocation : function() { |
| return this._location; |
| }, |
| getHeaders : function() { |
| return this._headers; |
| }, |
| getState : function() { |
| return this._state; |
| }, |
| getEntry : function(path) { |
| if (!path || !path instanceof String || !this._resources[path]) |
| return null; |
| |
| return this.getLocation() + path; |
| }, |
| getResource : function(path) { |
| if (!path || !path instanceof String) |
| return null; |
| |
| var i = 0; |
| var resource; |
| for (i = 0; i < this._imports.length; i++) { |
| var jsImport = this._imports[i]; |
| var jsExport = jsImport.getWiredExport(); |
| if (!jsExport) |
| continue; // optional |
| var name = jsExport.getName(); |
| if (name.charAt(0) != "/") |
| continue; // not a resource |
| if (path.indexOf(name)==0) { |
| resource = jsExport.getExportingBundle().getEntry(path); |
| if (resource) |
| return resource; |
| } |
| } |
| return this.getEntry(path); |
| }, |
| getImports : function() { |
| return this._imports; |
| }, |
| getExports : function() { |
| return this._exports; |
| }, |
| equals : function(other) { |
| if (this === other) |
| return true; |
| |
| if (!other instanceof orion.Bundle) |
| return false; |
| |
| if (this._name !== other._name) |
| return false; |
| if (this._version === null) { |
| if (other._version !== null) |
| return false; |
| } else if (!this._version.equals(other._version)) |
| return false; |
| return true; |
| }, |
| uninstall : function() { |
| this._state = orion.Bundle.UNINSTALLED; |
| }, |
| load : function(name) { |
| return this._framework._loader.load(this, name); |
| }, |
| _isSingleton : function() { |
| return this._singleton; |
| }, |
| _getImports : function() { |
| return this._imports; |
| }, |
| _getExports : function() { |
| return this._exports; |
| }, |
| _resolve : function() { |
| this._framework._loader.resolve(this); |
| this._state = orion.Bundle.RESOLVED; |
| }, |
| _unresolve : function() { |
| this._framework._loader.unresolve(this); |
| if (this._state == orion.Bundle.RESOLVED) |
| this._state = orion.Bundle.INSTALLED; |
| }, |
| _parseName : function(header) { |
| this._name = header; |
| }, |
| _parseSingleton : function(header) { |
| if (typeof header == "boolean") |
| this._singleton = header; |
| }, |
| _parseVersion : function(header) { |
| this._version = orion.Version.parseVersion(header); |
| }, |
| _parseGlobals : function(header) { |
| if (!header) |
| return; |
| |
| for ( var i = 0; i < header.length; i++) { |
| if (header[i]) { |
| this._globals.push(header[i]); |
| } |
| } |
| }, |
| _parseResources : function(header) { |
| if (!header) |
| return; |
| |
| for ( var i = 0; i < header.length; i++) { |
| if (header[i]) { |
| this._resources[header[i]] = true; |
| } |
| } |
| }, |
| _parseExports : function(header) { |
| if (!header) |
| return; |
| |
| for ( var i = 0; i < header.length; i++) { |
| var jsExport = new orion.Export(header[i], this); |
| if (jsExport !== null) |
| this._exports.push(jsExport); |
| } |
| }, |
| _parseImports : function(header) { |
| if (!header) |
| return; |
| |
| for ( var i = 0; i < header.length; i++) { |
| var jsImport = new orion.Import(header[i]); |
| if (jsImport !== null) |
| this._imports.push(jsImport); |
| } |
| }, |
| _parsePath : function(header) { |
| if (!header) { |
| this._script = this._headers.script || ""; |
| return; |
| } |
| var scripts = []; |
| for ( var i = 0; i < header.length; i++) { |
| var path = header[i]; |
| if (path === ".") { |
| scripts[i] = this._headers.script || ""; |
| } else { |
| var scriptURL = this.getResource(path); |
| scripts[i] = orion.Framework._fetch(scriptURL); |
| } |
| } |
| this._script = scripts.join("\n\n"); |
| } |
| }; |