Source code: engine/ModuleLoader.java
1
2 /* **********************************
3 File:main.java
4 Author: Alec Panovici(panovici@elcom.pub.ro)
5 Created on Sat Nov 13 17:13:02 EET 1999
6 Comments: Part of the vIDE Project
7 Copyright 1999 the vIDE Team.
8 ***********************************/
9
10 package engine;
11
12 import java.io.*;
13 import java.util.*;
14
15 /**
16 * This class is used for dynamically loading modules
17 * from the known paths.
18 * It also contains the writeModule methos, which writes
19 * a module in the current working directory.
20 */
21 class ModuleLoader
22 {
23 /**
24 * The paths that will be searched for modules.
25 */
26 public static Vector objPaths;
27
28 /**
29 * Parses the "vide.libdirs" System property and
30 * puts the paths into objPaths.
31 */
32 public static void setLibDirs() {
33 objPaths = new Vector();
34 objPaths.addElement("." + java.io.File.separatorChar);
35 String home = System.getProperty("vide.libdir");
36 if (home != null) {
37 while (!home.equals("")) {
38 int i = home.indexOf(':');
39 if (i == -1) break;
40 objPaths.addElement(home.substring(0, i));
41 home = home.substring(i+1, home.length());
42 }
43 }
44 }
45
46 /**
47 * Resolves names to module descriptions
48 */
49 public static ModuleFactory getModuleByName(String name) {
50 ModuleFactory res = (ModuleFactory) VeriParser.moduleDescriptions.get(name);
51 if (res == null) {
52 xConsole.debug("reading module \"" + name + "\"");
53 res = readModule(name);
54 if (res != null)
55 VeriParser.moduleDescriptions.put(name, res);
56 }
57 return res;
58 }
59
60 /**
61 * Finds a file that holds the given module,
62 * using the paths from objPaths (assumed to end with '/')
63 */
64 public static String getModuleFile(String name) {
65 name += ".vo";
66 for (Enumeration e = objPaths.elements() ; e.hasMoreElements() ; ) {
67 String s = e.nextElement() + name;
68 if((new File(s)).exists()) return s;
69 }
70 return null;
71 }
72
73 /**
74 * Fetches a ModuleDescription from a file.
75 * The paths given in objPaths are searched for a match.
76 * @return null if no filename matches the module name.
77 */
78 public static ModuleFactory readModule(String name) {
79 String fileName = getModuleFile(name);
80 ObjectInputStream oi = null;
81 try {
82 if (fileName != null) {
83 return (ModuleFactory)
84 (oi = new ObjectInputStream(new FileInputStream(fileName))).
85 readObject();
86 }
87 } catch (StreamCorruptedException scex) {
88 throw new Error("error reading module \"" + fileName + "\". Bad file data");
89 } catch (IOException ioex) {
90 throw new Error("IO exception while reading module \"" +
91 fileName + "\": " + ioex);
92 } catch (ClassNotFoundException cex){
93 throw new Error(cex.toString());
94 } finally {
95 try {
96 if (oi != null) oi.close();
97 } catch (IOException ioex) {}
98 }
99 return null;
100 }
101
102 /**
103 * Dumps a moduleDescription to a file together with some magic &
104 * ver info. The file is created into the current directory !
105 */
106 public static void writeModule(ModuleFactory desc) {
107 ObjectOutputStream oo = null;
108 String fileName = desc.name() + ".vo";
109 try {
110 oo = new ObjectOutputStream(new FileOutputStream(fileName));
111 oo.writeObject(desc);
112 } catch (IOException ioex) {
113 throw new Error("IO exception while writing module \"" +
114 fileName + "\": " + ioex);
115 }
116 finally {
117 try {
118 oo.close();
119 } catch (IOException ioex) {}
120 }
121 }
122
123 }