1 package org.springframework.samples.imagedb.standalone; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.util.Iterator ; 6 import java.util.List ; 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 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 { 36 List images = this.imageDatabase.getImages(); 37 StopWatch stopWatch = new StopWatch(); 38 for (Iterator it = images.iterator(); it.hasNext();) { 39 ImageDescriptor image = (ImageDescriptor) it.next(); 40 stopWatch.start(image.getName()); 41 ByteArrayOutputStream os = null; 42 for (int i = 0; i < nrOfCalls; i++) { 43 os = new ByteArrayOutputStream (); 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 [] args) throws IOException { 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 |