1 25 26 package org.objectweb.easybeans.server; 27 28 import static org.objectweb.easybeans.util.url.URLUtils.urlToFile; 29 30 import java.io.File ; 31 import java.net.URL ; 32 import java.util.Map ; 33 import java.util.WeakHashMap ; 34 35 import org.objectweb.easybeans.api.EZBArchive; 36 import org.objectweb.easybeans.api.EZBArchiveException; 37 import org.objectweb.easybeans.api.EZBContainer; 38 import org.objectweb.easybeans.api.EZBContainerException; 39 import org.objectweb.easybeans.container.archive.ArchiveManager; 40 import org.objectweb.easybeans.log.JLog; 41 import org.objectweb.easybeans.log.JLogFactory; 42 43 48 public class ContainersMonitor extends Thread { 49 50 53 private static final int SLEEP_TIME = 5000; 54 55 58 private JLog logger = JLogFactory.getLog(ContainersMonitor.class); 59 60 63 private Map <EZBContainer, Long > modifiedFiles = null; 64 65 68 private Embedded embedded = null; 69 70 73 private boolean bootInProgress = false; 74 75 78 private boolean stopped = false; 79 80 84 public ContainersMonitor(final Embedded embedded) { 85 this.embedded = embedded; 86 this.modifiedFiles = new WeakHashMap <EZBContainer, Long >(); 87 } 88 89 90 93 public void init() { 94 bootInProgress = true; 96 97 scanNewContainers(); 99 100 bootInProgress = false; 102 } 103 104 107 @Override 108 public void run() { 109 110 for (;;) { 111 if (stopped) { 113 return; 114 } 115 116 117 for (EZBContainer container : embedded.getContainers().values()) { 119 if (container.isAvailable()) { 120 checkContainer(container); 121 } 122 } 123 124 scanNewContainers(); 126 127 try { 128 Thread.sleep(SLEEP_TIME); 129 } catch (InterruptedException e) { 130 throw new RuntimeException ("Thread fail to sleep"); 131 } 132 } 133 } 134 135 139 private void scanNewContainers() { 140 File[] files = embedded.getServerConfig().getEjb3Directory().listFiles(); 142 143 if (files == null) { 145 return; 146 } 147 148 for (File f : files) { 150 if (f.getName().toLowerCase().endsWith(".jar")) { 151 EZBArchive archive = ArchiveManager.getInstance().getArchive(f); 152 153 boolean alreadyExist = false; 155 for (EZBContainer container : embedded.getContainers().values()) { 156 if (container.getArchive().equals(archive)) { 157 alreadyExist = true; 158 } 159 160 } 161 if (!alreadyExist) { 162 if (!bootInProgress) { 163 try { 166 Thread.sleep(SLEEP_TIME); 167 } catch (InterruptedException e) { 168 throw new RuntimeException ("Thread fail to sleep"); 169 } 170 } 171 logger.info("Creating container for archive {0}.", f); 172 173 EZBContainer container = embedded.createContainer(archive); 174 try { 175 container.start(); 176 } catch (EZBContainerException e) { 177 logger.error("Cannot start container {0}", container.getName(), e); 178 } 179 } 180 } 181 } 182 } 183 184 189 protected void checkContainer(final EZBContainer container) { 190 long previousLastModified = 0; 192 Long l = modifiedFiles.get(container); 193 if (l != null) { 194 previousLastModified = l.longValue(); 195 } 196 197 EZBArchive archive = container.getArchive(); 199 200 URL url = null; 202 try { 203 url = archive.getURL(); 204 } catch (EZBArchiveException e1) { 205 logger.warn("Cannot get URL on the container {0}", archive.getName()); 206 return; 207 } 208 File file = urlToFile(url); 209 if (!file.exists()) { 211 return; 212 } 213 214 long updatedModified = getLastModified(file); 215 modifiedFiles.put(container, Long.valueOf(updatedModified)); 216 217 if (previousLastModified == 0) { 219 return; 220 } 221 if (updatedModified > previousLastModified) { 223 logger.info("Container with archive {0} was modified. Reloading...", archive.getName()); 224 container.stop(); 225 try { 226 container.start(); 227 } catch (EZBContainerException e) { 228 e.printStackTrace(); 229 } 230 } 231 232 } 233 234 240 protected long getLastModified(final File archive) { 241 if (archive.isFile()) { 242 return archive.lastModified(); 243 } 244 File[] files = archive.listFiles(); 246 long last = 0; 247 if (files != null) { 248 for (File f : files) { 249 last = Math.max(last, getLastModified(f)); 250 } 251 } 252 return last; 253 } 254 255 256 259 public void stopOrder() { 260 this.stopped = true; 261 } 262 } 263 | Popular Tags |