KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > storage > lib > filestorage > FileStorageProxyImpl


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 */

24
25 package org.objectweb.clif.storage.lib.filestorage;
26
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import org.objectweb.clif.storage.api.*;
38 import org.objectweb.clif.supervisor.api.ClifException;
39
40
41 /**
42  * Proxy part of a file-based storage system.
43  *
44  * @author Julien Buret
45  * @author Nicolas Droze
46  * @author Bruno Dillenseger
47  */

48 public class FileStorageProxyImpl
49     implements
50         StorageWrite,
51         StorageProxyAdmin
52 {
53     static public final String JavaDoc CSV_SEPARATOR = ";";
54     static public final String JavaDoc CSV_EXTENSION= ".csv";
55     static public final String JavaDoc nodePropFilename = "server.prop";
56     static public final String JavaDoc baseTestDirname = "report";
57
58
59     protected String JavaDoc bladeId;
60     protected long dateOrigin;
61     private Map JavaDoc writers = new HashMap JavaDoc();
62     private String JavaDoc currentDir = null;
63
64
65     public FileStorageProxyImpl()
66     {
67     }
68
69
70     ////////////////////////////
71
// interface StorageWrite //
72
////////////////////////////
73

74
75     /**
76      * Writes a blade event in the storage system.
77      * @param event The event to write
78      */

79     public synchronized void write(BladeEvent event)
80     {
81         BufferedWriter JavaDoc wrt = (BufferedWriter JavaDoc)writers.get(event.getTypeLabel());
82         if (wrt == null)
83         {
84             String JavaDoc filename = currentDir
85                 + File.separator
86                 + event.getTypeLabel();
87             try
88             {
89                 wrt = new BufferedWriter JavaDoc(new FileWriter JavaDoc(filename));
90                 writers.put(event.getTypeLabel(), wrt);
91             }
92             catch (IOException JavaDoc ex)
93             {
94                 throw new RuntimeException JavaDoc("Storage proxy can't create file " + filename, ex);
95             }
96         }
97         try
98         {
99             wrt.write(event.toString(dateOrigin, CSV_SEPARATOR));
100             wrt.newLine();
101         }
102         catch (Exception JavaDoc e)
103         {
104             e.printStackTrace(System.err);
105         }
106     }
107
108
109     /////////////////////////////////
110
// interface StorageProxyAdmin //
111
/////////////////////////////////
112

113
114     /**
115      * Initializes the storage directories for the new test
116      */

117     public void newTest(Serializable JavaDoc testId)
118         throws ClifException
119     {
120         closeTest();
121         dateOrigin = System.currentTimeMillis();
122         currentDir = baseTestDirname + File.separator + testId + File.separator + bladeId;
123         File JavaDoc baseDir = new File JavaDoc(currentDir);
124         int i = 0;
125         while (baseDir.exists())
126         {
127             currentDir = baseTestDirname + File.separator + testId + "-" + ++i + File.separator + bladeId;
128             baseDir = new File JavaDoc(currentDir);
129         }
130         try
131         {
132             while (!baseDir.exists())
133             {
134                 baseDir.mkdirs();
135             }
136         }
137         catch(Exception JavaDoc ex)
138         {
139             throw new ClifException("Could not create " + currentDir + " directory.", ex);
140         }
141         try
142         {
143             OutputStream JavaDoc out = new FileOutputStream JavaDoc(new File JavaDoc(currentDir, nodePropFilename));
144             System.getProperties().store(out, "System properties for blade " + bladeId);
145             out.close();
146         }
147         catch (IOException JavaDoc ex)
148         {
149             throw new ClifException(
150                 "Could not write file " + currentDir + File.separator + nodePropFilename,
151                 ex);
152         }
153     }
154
155
156     /**
157      * Terminates the file storage system by closing every file stream.
158      */

159     public void closeTest()
160     {
161         try
162         {
163             Iterator JavaDoc iter = writers.values().iterator();
164             while (iter.hasNext())
165             {
166                 ((BufferedWriter JavaDoc)iter.next()).close();
167                 iter.remove();
168             }
169         }
170         catch (Exception JavaDoc e)
171         {
172             e.printStackTrace(System.err);
173         }
174     }
175
176
177     /**
178      * @return The identifier of the blade containing the storage proxy component
179      */

180     public String JavaDoc getBladeId()
181     {
182         return bladeId;
183     }
184
185
186     /**
187      * Initializes the storage system
188      */

189     public void init(String JavaDoc bladeId)
190     {
191         this.bladeId = bladeId;
192     }
193
194
195     /**
196      * Initializes a new collect for a given test
197      * @param testId the test identifier whose data must be collected
198      * @return a FileStorageCollectKey object identifying this collect, or null if the test identifier
199      * does not designate a valid test
200      * @see FileStorageCollectKey
201      */

202     public CollectKey initCollect(Serializable JavaDoc testId)
203     {
204         closeTest();
205         FileStorageCollect collect = FileStorageCollect.newCollect(new File JavaDoc(currentDir));
206         if (collect != null)
207         {
208             return collect.getKey();
209         }
210         return null;
211     }
212
213
214     public long getCollectSize(CollectKey key)
215     {
216         return FileStorageCollect.getSize(key);
217     }
218
219
220     public Serializable JavaDoc collect(CollectKey key)
221     {
222         return FileStorageCollect.collect(key);
223     }
224
225
226     public void closeCollect(CollectKey key)
227     {
228         FileStorageCollect.close(key);
229     }
230 }
231
Popular Tags