1 /************************************************************************** 2 Copyright 2005 Webstersmalley 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 *************************************************************************/ 16 17 18 package com.webstersmalley.picweb.offline; 19 20 import java.io.File; 21 import java.util.Iterator; 22 import java.util.List; 23 import java.util.concurrent.ExecutorService; 24 import java.util.concurrent.Executors; 25 import java.util.concurrent.TimeUnit; 26 27 import com.webstersmalley.picweb.offline.config.ConfigurationModel; 28 29 /*** 30 * @author Matthew Smalley 31 */ 32 public class ImageProcessor 33 { 34 private static final long TIMEOUT = 3600; 35 private List images; 36 private ConfigurationModel model; 37 private ExecutorService executor; 38 private String rootFolder; 39 40 public ImageProcessor(List images, ConfigurationModel model, String rootFolder) 41 { 42 this.images = images; 43 this.model = model; 44 this.rootFolder = rootFolder; 45 executor = Executors.newFixedThreadPool(2); 46 } 47 48 public void execute() 49 { 50 Iterator it = images.iterator(); 51 while (it.hasNext()) 52 { 53 File image = (File)it.next(); 54 createThumbnail(image); 55 createImageCopy(image); 56 } 57 executor.shutdown(); 58 try 59 { 60 executor.awaitTermination(TIMEOUT, TimeUnit.SECONDS); 61 } catch (InterruptedException e) 62 { 63 throw new RuntimeException("Generation timed out"); 64 } 65 } 66 67 private void createThumbnail(File source) 68 { 69 executor.execute(new ImageResizeTask(source, new File(model.getOutputFolder() + File.separator + rootFolder + File.separator + "THUMB-" + source.getName()), model.getThumbSize())); 70 } 71 72 private void createImageCopy(File source) 73 { 74 executor.execute(new ImageResizeTask(source, new File(model.getOutputFolder() + File.separator + rootFolder + File.separator + "IMAGE-" + source.getName()), model.getImageSize())); 75 } 76 }