KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > client > InitDB


1 /*
2  * RUBiS
3  * Copyright (C) 2002, 2003, 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet, Julie Marguerite
22  * Contributor(s):
23  */

24  package edu.rice.rubis.client;
25
26 import java.io.File JavaDoc;
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.lang.Thread JavaDoc;
30 import java.lang.reflect.Array JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Random JavaDoc;
33
34 /**
35  * This program initializes the RUBiS database according to the rubis.properties file
36  * found in the classpath.
37  *
38  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
39  * @version 1.0
40  */

41
42 public class InitDB
43 {
44   private URLGenerator urlGen = null;
45   private Random JavaDoc rand = new Random JavaDoc();
46   private RUBiSProperties rubis = null;
47   private int[] itemsPerCategory;
48
49
50   /**
51    * Creates a new <code>InitDB</code> instance.
52    *
53    */

54   public InitDB()
55   {
56     rubis = new RUBiSProperties();
57     urlGen = rubis.checkPropertiesFileAndGetURLGenerator();
58     if (urlGen == null)
59       Runtime.getRuntime().exit(1);
60     itemsPerCategory = rubis.getItemsPerCategory();
61   }
62
63
64   /**
65    * Main program accepts any combination of the following arguments: <pre>
66    * all: generate the complete database
67    * users: generate only users
68    * items: generate only items
69    * bids: generate bids and items (it is not possible to create bids without creating the related items)
70    * comments: generate comments and items (it is not possible to create comments without creating the related items)
71    *
72    * @param args all|users|items|bids|comments
73    */

74   public static void main(String JavaDoc[] args)
75   {
76     System.out.println("RUBiS database initialization - (C) Rice University/INRIA 2001\n");
77
78     InitDB initDB = new InitDB();
79     int argc = Array.getLength(args);
80     String JavaDoc params = "";
81
82     if (argc == 0)
83     {
84       System.out.println("Command line : java -classpath .:./database edu.rice.rubis.client.InitDB parameters");
85       System.out.println("Using Makefile: make initDB PARAM=\"parameters\"");
86       System.out.println("where parameter is one or any combination of the following arguments:");
87       System.out.println(" all: generate the complete database");
88       System.out.println(" users: generate only users");
89       System.out.println(" items: generate only items");
90       System.out.println(" bids: generate bids and items (it is not possible to create bids without creating the related items)");
91       System.out.println(" comments: generate comments and items (it is not possible to create comments without creating the related items)");
92       Runtime.getRuntime().exit(1);
93     }
94
95     for (int i = 0 ; i < argc ; i++)
96      params = params +" "+ args[i];
97     
98     if ((params.indexOf("users") != -1) || (params.indexOf("all") != -1))
99       initDB.generateUsers();
100     
101     if ((params.indexOf("items") != -1) || (params.indexOf("bids") != -1) ||
102         (params.indexOf("comments") != -1) || (params.indexOf("all") != -1))
103       initDB.generateItems((params.indexOf("bids") != -1) || (params.indexOf("all") != -1), (params.indexOf("comments") != -1) || (params.indexOf("all") != -1));
104   }
105
106
107   /**
108    * This method add users to the database according to the parameters
109    * given in the database.properties file.
110    */

111   public void generateUsers()
112   {
113     String JavaDoc firstname;
114     String JavaDoc lastname;
115     String JavaDoc nickname;
116     String JavaDoc email;
117     String JavaDoc password;
118     String JavaDoc regionName;
119     String JavaDoc HTTPreply;
120     int i;
121     URL JavaDoc url;
122
123     // Cache variables
124
int getNbOfUsers = rubis.getNbOfUsers();
125     int getNbOfRegions = rubis.getNbOfRegions();
126
127     System.out.print("Generating "+getNbOfUsers+" users ");
128     for (i = 0 ; i < getNbOfUsers ; i++)
129     {
130       firstname = "Great"+(i+1);
131       lastname = "User"+(i+1);
132       nickname = "user"+(i+1);
133       email = firstname+"."+lastname+"@rubis.com";
134       password = "password"+(i+1);
135       regionName = (String JavaDoc)rubis.getRegions().elementAt(i % getNbOfRegions);
136
137       // Call the HTTP server to register this user
138
url = urlGen.registerUser(firstname, lastname, nickname, email, password, regionName);
139       HTTPreply = callHTTPServer(url);
140       if (HTTPreply.indexOf("ERROR") != -1)
141       {
142         System.err.println("Failed to add user "+firstname+"|"+lastname+"|"+nickname+"|"+email+"|"+password+"|"+regionName);
143         System.err.println(HTTPreply);
144       }
145       if (i % 100 == 0)
146         System.out.print(".");
147     }
148     System.out.println(" Done!");
149   }
150
151
152   /**
153    * This method add items to the database according to the parameters
154    * given in the database.properties file.
155    */

156   public void generateItems(boolean generateBids, boolean generateComments)
157   {
158     // Items specific variables
159
String JavaDoc name;
160     String JavaDoc description;
161     float initialPrice;
162     float reservePrice;
163     float buyNow;
164     int duration;
165     int quantity;
166     int categoryId;
167     int sellerId;
168     int oldItems = rubis.getNbOfOldItems();
169     int activeItems = rubis.getTotalActiveItems();
170     int totalItems = oldItems + activeItems;
171     String JavaDoc staticDescription = "This incredible item is exactly what you need !<br>It has a lot of very nice features including "+
172       "a coffee option.<p>It comes with a free license for the free RUBiS software, that's really cool. But RUBiS even if it "+
173       "is free, is <B>(C) Rice University/INRIA 2001</B>. It is really hard to write an interesting generic description for "+
174       "automatically generated items, but who will really read this ?<p>You can also check some cool software available on "+
175       "http://sci-serv.inrialpes.fr. There is a very cool DSM system called SciFS for SCI clusters, but you will need some "+
176       "SCI adapters to be able to run it ! Else you can still try CART, the amazing 'Cluster Administration and Reservation "+
177       "Tool'. All those software are open source, so don't hesitate ! If you have a SCI Cluster you can also try the Whoops! "+
178       "clustered web server. Actually Whoops! stands for something ! Yes, it is a Web cache with tcp Handoff, On the fly "+
179       "cOmpression, parallel Pull-based lru for Sci clusters !! Ok, that was a lot of fun but now it is starting to be quite late "+
180       "and I'll have to go to bed very soon, so I think if you need more information, just go on <h1>http://sci-serv.inrialpes.fr</h1> "+
181       "or you can even try http://www.cs.rice.edu and try to find where Emmanuel Cecchet or Julie Marguerite are and you will "+
182       "maybe get fresh news about all that !!<p>";
183
184     // Comments specific variables
185
int staticDescriptionLength = staticDescription.length();
186     String JavaDoc[] staticComment = { "This is a very bad comment. Stay away from this seller !!<p>",
187                                "This is a comment below average. I don't recommend this user !!<p>",
188                                "This is a neutral comment. It is neither a good or a bad seller !!<p>",
189                                "This is a comment above average. You can trust this seller even if it is not the best deal !!<p>",
190                                "This is an excellent comment. You can make really great deals with this seller !!<p>" };
191     int[] staticCommentLength = { staticComment[0].length(), staticComment[1].length(), staticComment[2].length(),
192                                      staticComment[3].length(), staticComment[4].length()};
193     int[] ratingValue = { -5, -3, 0, 3, 5 };
194     int rating;
195     String JavaDoc comment;
196
197     // Bids specific variables
198
int nbBids;
199
200     // All purpose variables
201
int i, j;
202     URL JavaDoc url;
203     String JavaDoc HTTPreply;
204
205     // Cache variables
206
int getItemDescriptionLength = rubis.getItemDescriptionLength();
207     float getPercentReservePrice = rubis.getPercentReservePrice();
208     float getPercentBuyNow = rubis.getPercentBuyNow();
209     float getPercentUniqueItems = rubis.getPercentUniqueItems();
210     int getMaxItemQty = rubis.getMaxItemQty();
211     int getCommentMaxLength = rubis.getCommentMaxLength();
212     int getNbOfCategories = rubis.getNbOfCategories();
213     int getNbOfUsers = rubis.getNbOfUsers();
214     int getMaxBidsPerItem = rubis.getMaxBidsPerItem();
215
216     System.out.println("Generating "+oldItems+" old items and "+activeItems+" active items.");
217     if (generateBids)
218       System.out.println("Generating up to "+getMaxBidsPerItem+" bids per item.");
219     if (generateComments)
220       System.out.println("Generating 1 comment per item");
221
222     for (i = 0 ; i < totalItems ; i++)
223     {
224       // Generate the item
225
name = "RUBiS automatically generated item #"+(i+1);
226       int descriptionLength = rand.nextInt(getItemDescriptionLength)+1;
227       description = "";
228       while (staticDescriptionLength < descriptionLength)
229       {
230         description = description+staticDescription;
231         descriptionLength -= staticDescriptionLength;
232       }
233       description += staticDescription.substring(0, descriptionLength);
234       initialPrice = rand.nextInt(5000)+1;
235       duration = rand.nextInt(7)+1;
236       if (i < oldItems)
237       { // This is an old item
238
duration = -duration; // give a negative auction duration so that auction will be over
239
if (i < getPercentReservePrice*oldItems/100)
240           reservePrice = rand.nextInt(1000)+initialPrice;
241         else
242           reservePrice = 0;
243         if (i < getPercentBuyNow*oldItems/100)
244           buyNow = rand.nextInt(1000)+initialPrice+reservePrice;
245         else
246           buyNow = 0;
247         if (i < getPercentUniqueItems*oldItems/100)
248           quantity = 1;
249         else
250           quantity = rand.nextInt(getMaxItemQty)+1;
251       }
252       else
253       {
254         if (i < getPercentReservePrice*activeItems/100)
255           reservePrice = rand.nextInt(1000)+initialPrice;
256         else
257           reservePrice = 0;
258         if (i < getPercentBuyNow*activeItems/100)
259           buyNow = rand.nextInt(1000)+initialPrice+reservePrice;
260         else
261           buyNow = 0;
262         if (i < getPercentUniqueItems*activeItems/100)
263           quantity = 1;
264         else
265           quantity = rand.nextInt(getMaxItemQty)+1;
266       }
267       categoryId = i % getNbOfCategories;
268       // Hopefully everything is ok and we will not have a deadlock here
269
while (itemsPerCategory[categoryId] == 0)
270         categoryId = (categoryId + 1) % getNbOfCategories;
271       if (i >= oldItems)
272         itemsPerCategory[categoryId]--;
273       sellerId = rand.nextInt(getNbOfUsers) + 1;
274       
275       // Call the HTTP server to register this item
276
url = urlGen.registerItem(name, description, initialPrice, reservePrice, buyNow, duration, quantity, sellerId, categoryId+1);
277       HTTPreply = callHTTPServer(url);
278       if (HTTPreply.indexOf("ERROR") != -1)
279       {
280         System.err.println("Failed to add item "+name+" ("+url+")");
281         System.err.println(HTTPreply);
282       }
283
284       if (generateBids)
285       { // Now deal with the bids
286
nbBids = rand.nextInt(getMaxBidsPerItem);
287         for (j = 0 ; j < nbBids ; j++)
288         {
289           int addBid = rand.nextInt(10)+1;
290           url = urlGen.storeBid(i+1, rand.nextInt(getNbOfUsers)+1, initialPrice, initialPrice+addBid, initialPrice+addBid*2, rand.nextInt(quantity)+1, quantity);
291           HTTPreply = callHTTPServer(url);
292           if (HTTPreply.indexOf("ERROR") != -1)
293           {
294             System.err.println("Failed to bid #"+j+" on item "+name+" ("+url+")");
295             System.err.println(HTTPreply);
296           }
297           initialPrice += addBid; // We use initialPrice as minimum bid
298
}
299       }
300
301       if (generateComments)
302       { // Generate the comment
303
rating = rand.nextInt(5);
304         int commentLength = rand.nextInt(getCommentMaxLength)+1;
305         comment = "";
306         while (staticCommentLength[rating] < commentLength)
307         {
308           comment = comment+staticComment[rating];
309           commentLength -= staticCommentLength[rating];
310         }
311         comment += staticComment[rating].substring(0, commentLength);
312             
313         // Call the HTTP server to store this comment
314
url = urlGen.storeComment(i+1, sellerId, rand.nextInt(getNbOfUsers)+1, ratingValue[rating], comment);
315         HTTPreply = callHTTPServer(url);
316         if (HTTPreply.indexOf("ERROR") != -1)
317         {
318           System.err.println("Failed to add comment for item #"+(i+1)+" ("+url+")");
319           System.err.println(HTTPreply);
320         }
321       }
322
323       if (i % 10 == 0)
324         System.out.print(".");
325     }
326     System.out.println(" Done!");
327   }
328
329
330
331   /**
332    * Call the HTTP Server according to the given URL and get the reply
333    *
334    * @param url URL to access
335    * @return <code>String</code> containing the web server reply (HTML file)
336    */

337   private String JavaDoc callHTTPServer(URL JavaDoc url)
338   {
339     String JavaDoc HTMLReply = "";
340     BufferedInputStream JavaDoc in = null;
341     int retry = 0;
342         
343     while (retry < 5)
344     {
345       // Open the connexion
346
try
347       {
348         in = new BufferedInputStream JavaDoc(url.openStream(), 4096);
349       }
350       catch (IOException JavaDoc ioe)
351       {
352         System.err.println("Unable to open URL "+url+" ("+ioe.getMessage()+")");
353         retry++;
354         try
355         {
356           Thread.currentThread().sleep(1000L);
357         }
358         catch (InterruptedException JavaDoc i)
359         {
360           System.err.println("Interrupted in callHTTPServer()");
361           return null;
362         }
363         continue;
364       }
365
366       // Get the data
367
try
368       {
369         byte[] buffer = new byte[4096];
370         int read;
371
372         while ((read = in.read(buffer, 0, buffer.length)) != -1)
373         {
374           if (read > 0)
375             HTMLReply = HTMLReply + new String JavaDoc(buffer, 0, read);
376         }
377       }
378       catch (IOException JavaDoc ioe)
379       {
380         System.err.println("Unable to read from URL "+url+" ("+ioe.getMessage()+")");
381         return null;
382       }
383
384       // No retry at this point
385
break;
386     }
387         
388     try
389     {
390       if (in != null)
391         in.close();
392     }
393     catch (IOException JavaDoc ioe)
394     {
395         System.err.println("Unable to close URL "+url+" ("+ioe.getMessage()+")");
396     }
397     return HTMLReply;
398   }
399
400 }
401
Popular Tags