View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.runnables;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Iterator;
24  
25  import jgroup.core.ConfigurationException;
26  import jgroup.experiment.PropertyDefinition;
27  import jgroup.experiment.Runnable;
28  import jgroup.experiment.ShellCommand;
29  import jgroup.relacs.config.ExperimentConfig;
30  import jgroup.relacs.config.Host;
31  import jgroup.relacs.config.HostSet;
32  
33  /**
34   * @author Bjarte Svaeren
35   * @author Hein Meling
36   */
37  public class SaveLogs
38    implements Runnable
39  {
40    ////////////////////////////////////////////////////////////////////////////////////////////
41    // Data fields
42    ////////////////////////////////////////////////////////////////////////////////////////////
43  
44    private static final PropertyDefinition[] properties = { 
45          new PropertyDefinition("remote.dir", PropertyDefinition.STRING_TYPE),
46          new PropertyDefinition("save.dir", PropertyDefinition.STRING_TYPE),
47          new PropertyDefinition("variables", PropertyDefinition.STRING_TYPE)
48    };
49  
50  
51    ////////////////////////////////////////////////////////////////////////////////////////////
52    // Methods from Runnable
53    ////////////////////////////////////////////////////////////////////////////////////////////
54  
55    /* (non-Javadoc)
56     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
57     */
58    public void run(ExperimentConfig ec) 
59      throws ConfigurationException
60    {
61      /* Get the remote directory as a String */
62      String remoteDir = ec.getProperty(this, "remote.dir");  	
63      // Get the local log storage for this experiment as a String
64      StringBuilder buf = new StringBuilder(30);
65      buf.append(ec.getProperty(this, "save.dir"));
66      buf.append(File.separator);
67      buf.append(ec.getExperimentName());
68      buf.append(File.separator);
69      buf.append(ec.getProperty(this, "variables"));
70      String repeat = ec.getProperty("repeat", "1");
71      if (repeat != null && repeat.length() > 0) {
72        buf.append(File.separator);
73        buf.append(repeat);
74      }
75      String logStorage = buf.toString();
76  
77      File resultsDir = new File(logStorage);
78      // If the directory does not exist, create it.
79      if (!resultsDir.exists()) {
80        if (!resultsDir.mkdirs()) {
81          throw new ConfigurationException("Could not create results directory: " + resultsDir);
82        }
83      }
84  
85      // get the server hosts
86      HostSet hosts = (HostSet) ec.getServerConfig().getAllHosts().clone();
87      // get the client hosts
88      HostSet clients = ec.getClientConfig().getAllHosts();
89      hosts.addHosts(clients);
90  
91      ThreadGroup threadGroup = new ThreadGroup("SaveLogs");
92      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
93        Host host = (Host) iter.next();
94        String hostName = host.getCanonicalHostName();
95        try {
96          ShellCommand.exec(ec.scp(hostName, remoteDir, logStorage), threadGroup);
97        } catch (IOException e) {
98          e.printStackTrace();
99          System.out.println("Could not save logs from " + hostName);
100       }
101     }
102     ShellCommand.waitFor(threadGroup);  
103   }
104 
105   /* (non-Javadoc)
106    * @see jgroup.experiment.Runnable#getProperties()
107    */
108   public PropertyDefinition[] getProperties() {
109     return properties;
110   }
111 
112 } // END SaveLogs