001: package sisc.ser;
002:
003: import java.net.URL;
004: import java.net.MalformedURLException;
005: import java.io.*;
006: import java.util.*;
007: import sisc.util.Util;
008: import sisc.data.Expression;
009: import sisc.data.Symbol;
010: import sisc.interpreter.AppContext;
011:
012: public class LibraryManager extends Util {
013:
014: class LoadableLibrary {
015: URL path;
016: Library handle;
017:
018: public LoadableLibrary(URL path) {
019: this .path = path;
020: }
021:
022: public LoadableLibrary(Library l) {
023: this .handle = l;
024: }
025:
026: public void open() throws IOException {
027: if (handle == null) {
028: SeekableDataInputStream sis;
029: if (path.getProtocol().equals("file")) {
030: sis = new SeekableDataInputStream(
031: new BufferedRandomAccessInputStream(
032: new File(new File(path.getPath()),
033: path.getFile())
034: .getCanonicalPath(), "r",
035: 8, 2048));
036: } else {
037: sis = new SeekableDataInputStream(
038: new MemoryRandomAccessInputStream(path
039: .openStream()));
040: }
041: try {
042: handle = Library.load(ctx, sis);
043: } catch (ClassNotFoundException cnf) {
044: cnf.printStackTrace();
045: }
046: }
047: }
048:
049: public Library getLibrary() throws IOException {
050: open();
051: return handle;
052: }
053: }
054:
055: protected Map loadedLibraries;
056: AppContext ctx;
057:
058: public LibraryManager(AppContext ctx) {
059: this .ctx = ctx;
060: loadedLibraries = new HashMap();
061: }
062:
063: public Expression getExpression(Symbol name) throws IOException {
064: for (Iterator i = loadedLibraries.values().iterator(); i
065: .hasNext();) {
066: LoadableLibrary ll = (LoadableLibrary) i.next();
067: try {
068: return ll.getLibrary().getLocalExpression(name);
069: } catch (FileNotFoundException fnf) {
070: }
071: }
072: throw new FileNotFoundException(liMessage(SISCB,
073: "namedlibbindingnotanywhere", name.toString()));
074: }
075:
076: /**
077: * Returns the reference to a binding in the active libraries, or null
078: * if the provided expression isn't an entry point in any library.
079: */
080: public LibraryBinding lookupBinding(Expression e)
081: throws IOException {
082: for (Iterator i = loadedLibraries.entrySet().iterator(); i
083: .hasNext();) {
084: Map.Entry entry = (Map.Entry) i.next();
085: String name = (String) entry.getKey();
086: LoadableLibrary ll = (LoadableLibrary) entry.getValue();
087: int eid = ll.getLibrary().reverseLookup(e);
088: if (eid > -1) {
089: return new LibraryBinding(name, eid);
090: }
091: }
092: return null;
093: }
094:
095: public void addLibrary(Library l) {
096: loadedLibraries.put(l.getName(), new LoadableLibrary(l));
097: }
098:
099: public void addLibrary(String name, URL l) {
100: loadedLibraries.put(name, new LoadableLibrary(l));
101: }
102:
103: public boolean loadLibrary(String name) {
104: //Should eventually utilize a load path
105: try {
106: URL u = new URL(name);
107: addLibrary(name, u);
108: return true;
109: } catch (MalformedURLException mfu) {
110: mfu.printStackTrace();
111: }
112: return false;
113: }
114:
115: /**
116: * Returns an expression from an external library
117: */
118: public Expression resolveBinding(LibraryBinding lb)
119: throws IOException {
120: LoadableLibrary ll = (LoadableLibrary) loadedLibraries
121: .get(lb.name);
122: return ll.getLibrary().getExpression(lb.epid);
123: }
124: }
125:
126: /*
127: * The contents of this file are subject to the Mozilla Public
128: * License Version 1.1 (the "License"); you may not use this file
129: * except in compliance with the License. You may obtain a copy of
130: * the License at http://www.mozilla.org/MPL/
131: *
132: * Software distributed under the License is distributed on an "AS
133: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
134: * implied. See the License for the specific language governing
135: * rights and limitations under the License.
136: *
137: * The Original Code is the Second Interpreter of Scheme Code (SISC).
138: *
139: * The Initial Developer of the Original Code is Scott G. Miller.
140: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
141: * Scott G. Miller. All Rights Reserved.
142: *
143: * Contributor(s):
144: * Matthias Radestock
145: *
146: * Alternatively, the contents of this file may be used under the
147: * terms of the GNU General Public License Version 2 or later (the
148: * "GPL"), in which case the provisions of the GPL are applicable
149: * instead of those above. If you wish to allow use of your
150: * version of this file only under the terms of the GPL and not to
151: * allow others to use your version of this file under the MPL,
152: * indicate your decision by deleting the provisions above and
153: * replace them with the notice and other provisions required by
154: * the GPL. If you do not delete the provisions above, a recipient
155: * may use your version of this file under either the MPL or the
156: * GPL.
157: */
|