KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > sun > Javac


1 // $Id: Javac.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 /*
4  * Copyright 1999 by Olivier Refalo
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package edu.neu.ccs.jmk.sun;
22
23 import sun.tools.javac.*;
24 import edu.neu.ccs.jmk.Operator;
25 import edu.neu.ccs.jmk.CommandFailedException;
26 import java.io.*;
27
28 /**
29  * Javac as an Operator.
30  * <pre>
31  * javac = "edu.neu.ccs.jmk.sun.Javac";
32  *
33  * "all":;
34  * {
35  * forname javac "-O" "source.java";
36  * }
37  * </pre>
38  */

39 public final class Javac
40 implements Operator, Runnable JavaDoc
41 {
42     // if the static compiler is not used during 30s
43
// in a row, we clean the reference and call the GC
44
private static final long CLEANER_DELAY=1000*30;
45     private static Thread JavaDoc cleaner_=null;
46     private static Main ji_=null;
47     private static long launchdate_=0;
48
49     public String JavaDoc getName()
50     {
51         return("Javac");
52     }
53
54     public void run()
55     {
56         while ( System.currentTimeMillis()-launchdate_<CLEANER_DELAY )
57         {
58             try
59             {
60                 Thread.sleep(CLEANER_DELAY+1000);
61             }
62             catch ( Exception JavaDoc e )
63             {
64                 System.err.println(e.toString());
65             }
66         }
67
68         // clean up resources
69
synchronized(this)
70         {
71             ji_=null;
72             cleaner_=null;
73             System.gc();
74         }
75     }
76
77     public void exec(String JavaDoc[] _args, PrintWriter _out)
78     throws CommandFailedException
79     {
80         if ( _args.length == 0 )
81             throw new CommandFailedException("No args to " + getName());
82         try
83         {
84             // make sure the cleaning thread doesn't destroy the resource
85
// while we use it.
86
synchronized(this)
87             {
88                 if ( ji_==null )
89                 {
90                     ji_ = new Main(new OutputStreamToPrintWriter(_out), "javac");
91                 }
92
93                 launchdate_=System.currentTimeMillis();
94                 if ( cleaner_==null )
95                 {
96                     cleaner_=new Thread JavaDoc(this);
97                     cleaner_.start();
98                 }
99
100                 if ( ji_.compile(_args) == false )
101                     throw new CommandFailedException(getName() + " failed");
102             }
103         }
104         catch ( CommandFailedException ex1 )
105         {
106             throw ex1;
107         }
108         catch ( Exception JavaDoc ex2 )
109         {
110             throw new CommandFailedException(getName() + " threw an exception -> "+ex2.toString());
111         }
112     }
113 }
114
Popular Tags