001: package build;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.IOException;
007: import java.io.InputStream;
008:
009: public class CompilingLoadlet extends Loadlet {
010:
011: // KRA 28SEP99: This is a bit of a kludge,
012: // CompilingLoadlet's can't operate independently.
013: // Each must contribute to the classpath. This is done in construction.
014: private static String classPath = System
015: .getProperty("java.class.path");
016:
017: private static String getClassPath() {
018: synchronized (CompilingLoadlet.class) {
019: return classPath;
020: }
021: }
022:
023: private static void addClassPath(String cp) {
024: synchronized (CompilingLoadlet.class) {
025: if (classPath == null)
026: classPath = cp;
027: else
028: classPath = classPath
029: + System.getProperty("path.separator") + cp;
030: }
031: }
032:
033: protected String classBase;
034: protected String srcBase;
035:
036: CompilingLoadlet(Loadlet parent, String classBase, String srcBase) {
037: super (parent);
038: this .classBase = classBase;
039: this .srcBase = srcBase;
040: addClassPath(classBase);
041: }
042:
043: /** Rules for deciding how to come up with the byte[] of a class
044: named <tt>name</tt>. **/
045: public byte[] loadClassData(String name) {
046: File src = this .srcFile(name);
047: File bin = this .binFile(name);
048: boolean se = src.exists();
049: boolean be = bin.exists();
050: if (be && se && src.lastModified() > bin.lastModified()) {
051: this .compileClass(name);
052: return toBytes(bin);
053: } else if (be && se)
054: return toBytes(bin);
055: else if (be && !se)
056: return toBytes(bin);
057: else if (!be && se) {
058: this .compileClass(name);
059: return toBytes(bin);
060: } else if (!be && !se
061: && (name.endsWith("_Skel") || name.endsWith("_Stub"))) {
062: this .rmicClass(name.substring(0, name.lastIndexOf('_')));
063: return toBytes(bin);
064: } else
065: return null;
066: }
067:
068: public String toString() {
069: return "new CompilingLoadLet(\"" + this .classBase + "\", \""
070: + this .srcBase + "\")";
071: }
072:
073: public File srcFile(String name) {
074: return toFile(this .srcBase, name, ".java");
075: }
076:
077: public File binFile(String name) {
078: return toFile(this .classBase, name, ".class");
079: }
080:
081: public boolean compileClass(String name) {
082: this .p("Compiling " + name);
083: File file = this .srcFile(name);
084: String[] args = new String[] { "-g", "-deprecation",
085: "-classpath", getClassPath(), "-d", this .classBase,
086: "-sourcepath", this .srcBase, file.toString() };
087: return this .javac(args);
088: }
089:
090: public boolean rmicClass(String name) {
091: this .p("rmicing " + name);
092: String[] args = new String[] { "-classpath", getClassPath(),
093: "-d", this .classBase, "-sourcepath", this .srcBase, name };
094: return this .rmic(args);
095: }
096:
097: private static void p(String arg) {
098: System.out.println(arg);
099: }
100:
101: public static boolean javac(String[] args) {
102: return new sun.tools.javac.Main(System.out, "Build-javac")
103: .compile(args);
104: }
105:
106: public static boolean jar(String[] args) {
107: return new sun.tools.jar.Main(System.out, System.err,
108: "Build-jar").run(args);
109: }
110:
111: public static boolean rmic(String[] args) {
112: return new sun.rmi.rmic.Main(System.out, "Build-rmic")
113: .compile(args);
114: }
115:
116: public static File toFile(String prefix, String name, String suffix) {
117: StringBuffer b = new StringBuffer(prefix.length()
118: + name.length() + suffix.length() + 1);
119: char separator = '/';
120: b.append(prefix);
121: b.append(separator);
122: for (int i = 0; i < name.length(); i++) {
123: char c = name.charAt(i);
124: if (c == '.')
125: b.append(separator);
126: else
127: b.append(c);
128: }
129: b.append(suffix);
130: return new File(b.toString());
131: }
132:
133: public static byte[] toBytes(File f) {
134: if (f.exists()) {
135: FileInputStream s = null;
136: try {
137: s = new FileInputStream(f);
138: int L = (int) f.length();
139: return toBytes(s, new byte[L]);
140: } catch (FileNotFoundException e) {
141: p("toBytes can't open existing file: " + f);
142: e.printStackTrace();
143: return null;
144: } finally {
145: if (s != null)
146: try {
147: s.close();
148: } catch (java.io.IOException e) {
149: e.printStackTrace();
150: }
151: }
152: } else
153: return null;
154: }
155:
156: public static byte[] toBytes(InputStream s, byte[] b) {
157: int L = b.length;
158: int off = 0;
159: int in = L;
160: try {
161: while (L > 0) {
162: in = s.read(b, off, L);
163: off = off + in;
164: L = L - in;
165: }
166: } catch (IOException e) {
167: return null;
168: }
169: return b;
170: }
171: }
|