1/*******************************************************************************
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.releng.util.rss;
12
13import java.io.File;
14import java.io.IOException;
15import java.io.InputStream;
16import java.io.OutputStream;
17
18import org.apache.tools.ant.Project;
19import org.apache.tools.ant.taskdefs.ExecTask;
20import org.apache.tools.ant.types.Commandline.Argument;
21
22/**
23 *
24 *    Helper methods
25 *
26 * @author nickb
27 *
28 */
29public class RSSFeedUtil {
30
31  private static final String SP = " "; //$NON-NLS-1$
32  private static final String CL = ":"; //$NON-NLS-1$
33  public static final String EXPECTED_RESULT = "0"; //$NON-NLS-1$
34
35  public static final String RUN_EXEC_TASK_ERROR = "runExecTask.Error"; //$NON-NLS-1$
36  public static final String RUN_EXEC_TASK_OUTPUT = "runExecTask.Output"; //$NON-NLS-1$
37  public static final String RUN_EXEC_TASK_RESULT = "runExecTask.Result"; //$NON-NLS-1$
38
39  private int debug = 0;
40
41  /**
42   * A buffer.
43   */
44  private static byte[] buffer = new byte [8192];
45
46  public ExecTask runExecTask(String executable, String commandline, String dir)
47  {
48    if (dir==null) {
49      dir = ".";  //$NON-NLS-1$
50    }
51
52    ExecTask exec = new ExecTask();
53    exec.setExecutable(executable);
54    exec.setResolveExecutable(true);
55    exec.setDir((new File(dir)).getAbsoluteFile());
56    Project project = new Project(); project.setName(executable);
57    exec.setProject(project);
58    exec.setFailIfExecutionFails(true);
59    exec.setFailonerror(true);
60    exec.setErrorProperty(RUN_EXEC_TASK_ERROR);
61    exec.setOutputproperty(RUN_EXEC_TASK_OUTPUT);
62    exec.setResultProperty(RUN_EXEC_TASK_RESULT);
63    exec.setLogError(true);
64
65    if (commandline != null || "".equals(commandline)) { //$NON-NLS-1$
66      Argument execArg = exec.createArg();
67      execArg.setLine(commandline);
68    }
69    try
70    {
71      if (debug>0) {
72        System.out.println(Messages.getString("RSSFeedPublisherTask.Execute") + SP + executable + (commandline==null?"":SP + commandline)); //$NON-NLS-1$ //$NON-NLS-2$
73      }
74      exec.execute();
75      handleExecTaskReturn(project);
76    }
77    catch (Exception e)
78    {
79      handleExecTaskReturn(project);
80      System.err.println(Messages.getString("RSSFeedPublisherTask.ForProject") + SP + project.getName() + CL); //$NON-NLS-1$
81      e.printStackTrace();
82    }
83
84    return exec;
85
86  }
87
88  private void handleExecTaskReturn(Project project)
89  {
90    String out = null;
91
92    out = project.getProperty(RUN_EXEC_TASK_RESULT);
93    if (debug>1) {
94      if (!isNullString(out) && !EXPECTED_RESULT.equals(out)) {
95        System.err.println(Messages.getString("RSSFeedPublisherTask.Result") + SP + out); //$NON-NLS-1$
96      }
97    }
98
99    out = project.getProperty(RUN_EXEC_TASK_OUTPUT);
100    if (!isNullString(out)) {
101      System.out.println(out);
102    }
103
104    out = project.getProperty(RUN_EXEC_TASK_ERROR);
105    if (!isNullString(out)) {
106      if (debug>1 && out.equals(Messages.getString("RSSFeedPublisherTask.CVSWarning"))) { //$NON-NLS-1$
107        System.out.println(out);
108      } else if (!out.equals(Messages.getString("RSSFeedPublisherTask.CVSWarning"))) { //$NON-NLS-1$
109        System.err.println(Messages.getString("RSSFeedPublisherTask.Error") + SP + out); //$NON-NLS-1$
110      }
111    }
112  }
113
114  /**
115   * Copies all bytes in the given source stream to the given destination
116   * stream. Neither streams are closed.
117   *
118   * From: org.eclipse.emf/tests/org.eclipse.emf.test.build/src/org/eclipse/emf/test/build/FileTool.java,v 1.2
119   *
120   * @param source
121   *            the given source stream
122   * @param destination
123   *            the given destination stream
124   */
125  public static void transferData(InputStream source, OutputStream destination) throws IOException
126  {
127    int bytesRead = 0;
128    while (bytesRead != -1)
129    {
130      bytesRead = source.read(buffer, 0, buffer.length);
131      if (bytesRead != -1)
132      {
133        destination.write(buffer, 0, bytesRead);
134      }
135    }
136  }
137
138  public static boolean isNullString(String str)
139  {
140    return str==null||"".equals(str); //$NON-NLS-1$
141  }
142
143  public void setDebug(int debug)
144  {
145    this.debug = debug;
146  }
147
148}