View Javadoc

1   package nl.toolforge.karma.core.vc.cvsimpl;
2   
3   import nl.toolforge.core.util.file.MyFileUtils;
4   import nl.toolforge.karma.core.KarmaRuntimeException;
5   import nl.toolforge.karma.core.cmd.CommandException;
6   import nl.toolforge.karma.core.cmd.CommandResponse;
7   import nl.toolforge.karma.core.cmd.CommandResponseHandler;
8   import org.apache.tools.ant.BuildException;
9   import org.apache.tools.ant.Project;
10  import org.apache.tools.ant.helper.ProjectHelperImpl;
11  import org.netbeans.lib.cvsclient.command.Command;
12  
13  import java.io.BufferedReader;
14  import java.io.BufferedWriter;
15  import java.io.File;
16  import java.io.FileWriter;
17  import java.io.IOException;
18  import java.io.InputStreamReader;
19  
20  /***
21   * Quick hack to support the CVS <code>ext</code> protocol. This implementation uses the Ant task, not anything
22   * native, unfortunately.
23   *
24   * @author D.A. Smedes
25   * @version $Id: ExtClient.java,v 1.2 2004/11/02 23:57:07 asmedes Exp $
26   */
27  public final class ExtClient {
28  
29    private CommandResponse response = new CommandResponse();
30  
31    public void setHandler(CommandResponseHandler handler) {
32      response.addCommandResponseListener(handler);
33    }
34  
35    private LogParser listener = null;
36  
37    public void runCommand(Command command, File contextDirectory, CVSRepository location) throws CVSException {
38  
39      if (listener == null) {
40        throw new KarmaRuntimeException("No listener registered for this client.");
41      }
42  
43      // todo hmm, this mechanism doesn't integrate with the commandresponse mechanism
44      //
45      listener.setOutputPrintStream(System.out);
46      listener.setMessageOutputLevel(Project.MSG_INFO);
47  
48      // Configure underlying ant to run a command.
49      //
50      Project project = new Project();
51      project.addBuildListener(listener);
52      project.init();
53  
54      // The ping thingie doesn't work through a proxy.
55      //
56  
57  //    if (!Ping.ping(location.getHost(), 22, 3000)) {
58  //      throw new CVSException(LocationException.CONNECTION_EXCEPTION, new Object[]{location.getId()});
59  //    } else {
60  
61      ProjectHelperImpl helper = new ProjectHelperImpl();
62      try {
63        helper.parse(project, getBuildFile("ext-support.xml"));
64      } catch (IOException e) {
65        throw new KarmaRuntimeException("Cannot find ext-support.xml");
66      }
67  
68      project.setProperty("cvsroot", location.getCVSRoot());
69      project.setProperty("command", command.getCVSCommand());
70      project.setProperty("dest", contextDirectory.getPath());
71  
72      try {
73        project.executeTarget("run");
74      } catch (BuildException e) {
75        throw new CVSException(e, CommandException.BUILD_FAILED);
76      }
77  //    }
78    }
79  
80    private final File getBuildFile(String buildFile) throws IOException {
81  
82      File tmp = null;
83  
84      tmp = MyFileUtils.createTempDirectory();
85  
86      ClassLoader loader = this.getClass().getClassLoader();
87  
88      BufferedReader in =
89          new BufferedReader(new InputStreamReader(loader.getResourceAsStream("ant" + File.separator + buildFile)));
90      BufferedWriter out =
91          new BufferedWriter(new FileWriter(new File(tmp, buildFile)));
92  
93      String str;
94      while ((str = in.readLine()) != null) {
95        out.write(str);
96      }
97      out.close();
98      in.close();
99  
100     // Return a temp reference to the file
101     //
102     return new File(tmp, buildFile);
103   }
104 
105   public void addCVSListener(LogParser listener) {
106     this.listener = listener;
107   }
108 }