KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.net.Socket JavaDoc;
32 import org.objectweb.clif.storage.api.StorageAdmin;
33 import org.objectweb.clif.storage.api.StorageProxyAdmin;
34 import org.objectweb.clif.storage.api.CollectKey;
35 import org.objectweb.clif.console.lib.TestPlanWriter;
36 import org.objectweb.fractal.api.control.BindingController;
37
38 /**
39  * Part of the storage.
40  * This part is deployed in the console
41  *
42  * @author Julien Buret
43  * @author Nicolas Droze
44  * @author Bruno Dillenseger
45  */

46 public class ConsoleFileStorageImpl
47     implements StorageAdmin, BindingController
48 {
49     static public final String JavaDoc testDirbase = "report";
50     static public final String JavaDoc testplanExt = ".testplan";
51
52
53     protected Map JavaDoc distributedStorage = new Hashtable JavaDoc();
54     protected BufferedWriter test = null;
55     protected String JavaDoc testDirname = null;
56     protected Serializable testId = null;
57
58
59     /////////////////////////////////
60
// interface BindingController //
61
/////////////////////////////////
62

63
64     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
65     {
66         if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
67         {
68             return distributedStorage.get(clientItfName);
69         }
70         return null;
71     }
72
73
74     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
75     {
76         if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
77         {
78             distributedStorage.put(clientItfName, serverItf);
79         }
80     }
81
82
83     public void unbindFc(String JavaDoc clientItfName)
84     {
85         if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
86         {
87             distributedStorage.remove(clientItfName);
88         }
89     }
90
91
92     public String JavaDoc[] listFc()
93     {
94         return (String JavaDoc[])distributedStorage.keySet().toArray(new String JavaDoc[distributedStorage.size()]);
95     }
96
97
98     ////////////////////////////
99
// interface StorageAdmin //
100
////////////////////////////
101

102
103     /**
104      * Inform the storage system the beginning of a new test
105      * @param testPlan Map of scenario deployment definitions, indexed by a unique identifier
106      * @see org.objectweb.clif.deploy.DeployDefinition
107      */

108     public void newTest(
109         Serializable testId,
110         Map JavaDoc testPlan)
111     {
112         this.testId = testId;
113         testDirname = testDirbase + File.separator + testId;
114         try
115         {
116             File baseDir = new File(testDirname);
117             int i = 0;
118             while (! baseDir.mkdirs())
119             {
120                 if (baseDir.exists())
121                 {
122                     testDirname = testDirbase + File.separator + testId + "-" + ++i;
123                     baseDir = new File(testDirname);
124                 }
125                 else
126                 {
127                     throw new IOException("Could not create " + testDirname + " directory.");
128                 }
129             }
130             TestPlanWriter.write2prop(
131                 new FileOutputStream(testDirname + ".testplan"),
132                 testPlan);
133         }
134         catch (Exception JavaDoc e)
135         {
136             e.printStackTrace(System.err);
137         }
138     }
139
140
141     public void collect()
142     {
143         Iterator JavaDoc list = distributedStorage.values().iterator();
144         StorageProxyAdmin store;
145         byte[] buffer = new byte[FileStorageCollect.BLOCK_SIZE];
146         try
147         {
148             while (list.hasNext())
149             {
150                 store = (StorageProxyAdmin)list.next();
151                 File directory = new File(testDirname + File.separator + store.getBladeId());
152                 if (directory.mkdirs())
153                 {
154                     CollectKey key = store.initCollect(testId);
155                     if (key != null)
156                     {
157                         FileStorageCollectStep step;
158                         Socket JavaDoc sock;
159                         while ((step = (FileStorageCollectStep) store.collect(key)) != null)
160                         {
161                             File outputFile = new File(directory, step.getFilename());
162                             if (!outputFile.exists())
163                             {
164                                 sock = new Socket JavaDoc();
165                                 sock.connect(step.getSocketAddress(), 0);
166                                 InputStream in = sock.getInputStream();
167                                 OutputStream out = new FileOutputStream(outputFile);
168                                 int n = in.read(buffer);
169                                 while (n != -1)
170                                 {
171                                     out.write(buffer, 0, n);
172                                     n = in.read(buffer);
173                                 }
174                                 in.close();
175                                 out.close();
176                                 sock.close();
177                             }
178                         }
179                     }
180                 }
181             }
182         }
183         catch (Exception JavaDoc ex)
184         {
185             throw new RuntimeException JavaDoc("Could not collect data", ex);
186         }
187     }
188
189
190     /**
191      * Terminate the storage system
192      */

193     public void terminate()
194     {
195     }
196 }
197
Popular Tags