KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > imagedb > standalone > StandaloneImageTool


1 package org.springframework.samples.imagedb.standalone;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.springframework.context.ApplicationContext;
9 import org.springframework.context.support.FileSystemXmlApplicationContext;
10 import org.springframework.samples.imagedb.ImageDatabase;
11 import org.springframework.samples.imagedb.ImageDescriptor;
12 import org.springframework.util.StopWatch;
13
14 /**
15  * Standalone sample that simply lists the image descriptors in the database.
16  *
17  * <p>Accesses the very same "WEB-INF/applicationContext.xml" file as the web
18  * application. Needs to be executed in the web app root directory.
19  *
20  * <p>Takes an optional command line argument that specifies the number of
21  * calls to initiate for each image, a la "StandaloneImageTool 5". Can be
22  * used to get a rough performance impression.
23  *
24  * @author Juergen Hoeller
25  * @since 08.01.2004
26  */

27 public class StandaloneImageTool {
28
29     private final ImageDatabase imageDatabase;
30
31     public StandaloneImageTool(ImageDatabase imageDatabase) {
32         this.imageDatabase = imageDatabase;
33     }
34
35     public void listImages(int nrOfCalls) throws IOException JavaDoc {
36         List JavaDoc images = this.imageDatabase.getImages();
37         StopWatch stopWatch = new StopWatch();
38         for (Iterator JavaDoc it = images.iterator(); it.hasNext();) {
39             ImageDescriptor image = (ImageDescriptor) it.next();
40             stopWatch.start(image.getName());
41             ByteArrayOutputStream JavaDoc os = null;
42             for (int i = 0; i < nrOfCalls; i++) {
43                 os = new ByteArrayOutputStream JavaDoc();
44                 this.imageDatabase.streamImage(image.getName(), os);
45             }
46             stopWatch.stop();
47             System.out.println("Found image '" + image.getName() + "' with content size " + os.size() +
48                                " and description length " + image.getDescriptionLength());
49         }
50         System.out.println(stopWatch.prettyPrint());
51     }
52
53
54     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
55         int nrOfCalls = 1;
56         if (args.length > 1 && !"".equals(args[1])) {
57             nrOfCalls = Integer.parseInt(args[1]);
58         }
59         ApplicationContext context = new FileSystemXmlApplicationContext("WEB-INF/applicationContext.xml");
60         ImageDatabase idb = (ImageDatabase) context.getBean("imageDatabase");
61         StandaloneImageTool tool = new StandaloneImageTool(idb);
62         tool.listImages(nrOfCalls);
63     }
64
65 }
66
Popular Tags