1 24 package org.riotfamily.common.xml; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.springframework.core.io.Resource; 33 34 37 public class BeanConfigurationWatcher { 38 39 private ConfigurableBean bean; 40 41 private List files; 42 43 private long lastModified; 44 45 private ArrayList listeners = new ArrayList (); 46 47 public BeanConfigurationWatcher(ConfigurableBean bean) { 48 this.bean = bean; 49 this.lastModified = System.currentTimeMillis(); 50 } 51 52 public void setFiles(List files) { 53 this.files = files; 54 } 55 56 public void setResources(List resources) { 57 files = new ArrayList (); 58 Iterator it = resources.iterator(); 59 while (it.hasNext()) { 60 Resource res = (Resource) it.next(); 61 try { 62 files.add(res.getFile()); 63 } 64 catch (IOException e) { 65 } 66 } 67 } 68 69 public void addListener(ConfigurationEventListener listener) { 70 listeners.add(listener); 71 } 72 73 public synchronized void checkForModifications() { 74 if (bean.isReloadable()) { 75 long mtime = 0; 76 Iterator it = files.iterator(); 77 while (it.hasNext()) { 78 File file = (File ) it.next(); 79 mtime = Math.max(mtime, file.lastModified()); 80 } 81 if (mtime > lastModified) { 82 lastModified = System.currentTimeMillis(); 83 bean.configure(); 84 it = listeners.iterator(); 85 while (it.hasNext()) { 86 ConfigurationEventListener listener = (ConfigurationEventListener) it.next(); 87 listener.beanReconfigured(bean); 88 } 89 } 90 } 91 } 92 93 } 94 | Popular Tags |