KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > server > ContainersMonitor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ContainersMonitor.java 1139 2006-10-06 09:19:58Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.server;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.urlToFile;
29
30 import java.io.File JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.WeakHashMap JavaDoc;
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 /**
44  * This class monitors the archives managed by containers and reload them if the
45  * archive is changed.
46  * @author Florent Benoit
47  */

48 public class ContainersMonitor extends Thread JavaDoc {
49
50     /**
51      * Sleep time for the thread of the cleaner (5s).
52      */

53     private static final int SLEEP_TIME = 5000;
54
55     /**
56      * Logger.
57      */

58     private JLog logger = JLogFactory.getLog(ContainersMonitor.class);
59
60     /**
61      * Map between container and the last updated file.
62      */

63     private Map JavaDoc<EZBContainer, Long JavaDoc> modifiedFiles = null;
64
65     /**
66      * Embedded server which is monitored.
67      */

68     private Embedded embedded = null;
69
70     /**
71      * Initializing period ?.
72      */

73     private boolean bootInProgress = false;
74
75     /**
76      * Stop order received ?
77      */

78     private boolean stopped = false;
79
80     /**
81      * Builds a new monitor by initializing lists.
82      * @param embedded the embedded server which is monitored.
83      */

84     public ContainersMonitor(final Embedded embedded) {
85         this.embedded = embedded;
86         this.modifiedFiles = new WeakHashMap JavaDoc<EZBContainer, Long JavaDoc>();
87     }
88
89
90     /**
91      * Init containers (call at startup).
92      */

93     public void init() {
94         // Start the boot process.
95
bootInProgress = true;
96
97         // Initialize containers
98
scanNewContainers();
99
100         // No more in the boot process.
101
bootInProgress = false;
102     }
103
104     /**
105      * Start the thread of this class It will clean all the work entries.
106      */

107     @Override JavaDoc
108     public void run() {
109
110         for (;;) {
111             // Stop the thread
112
if (stopped) {
113                 return;
114             }
115
116
117             // Check existing container is not modified ?
118
for (EZBContainer container : embedded.getContainers().values()) {
119                 if (container.isAvailable()) {
120                     checkContainer(container);
121                 }
122             }
123
124             // Check new containers to start
125
scanNewContainers();
126
127             try {
128                 Thread.sleep(SLEEP_TIME);
129             } catch (InterruptedException JavaDoc e) {
130                 throw new RuntimeException JavaDoc("Thread fail to sleep");
131             }
132         }
133     }
134
135     /**
136      * Scan all files present in the ejb3 directory and create container for
137      * each one.
138      */

139     private void scanNewContainers() {
140         // get files
141
File[] files = embedded.getServerConfig().getEjb3Directory().listFiles();
142
143         // exit if there are no files to scan.
144
if (files == null) {
145             return;
146         }
147
148         // analyze each file.
149
for (File f : files) {
150             if (f.getName().toLowerCase().endsWith(".jar")) {
151                 EZBArchive archive = ArchiveManager.getInstance().getArchive(f);
152
153                 // check that no container already exist for this archive
154
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                         // wait that files are copied before creating/starting
164
// the container
165
try {
166                             Thread.sleep(SLEEP_TIME);
167                         } catch (InterruptedException JavaDoc e) {
168                             throw new RuntimeException JavaDoc("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     /**
185      * Check a container (and its archive) and see if there is a need to reload
186      * the container.
187      * @param container the container to monitor.
188      */

189     protected void checkContainer(final EZBContainer container) {
190         // get lastmodified for this container (any ?)
191
long previousLastModified = 0;
192         Long JavaDoc l = modifiedFiles.get(container);
193         if (l != null) {
194             previousLastModified = l.longValue();
195         }
196
197         // get archive
198
EZBArchive archive = container.getArchive();
199
200         // Get URL
201
URL JavaDoc 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         // No file archive, abort.
210
if (!file.exists()) {
211             return;
212         }
213
214         long updatedModified = getLastModified(file);
215         modifiedFiles.put(container, Long.valueOf(updatedModified));
216
217         // first check. nothing to do
218
if (previousLastModified == 0) {
219             return;
220         }
221         // container was modified, need to relaunch it
222
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     /**
235      * Gets the last modified attribute of a given archive.<br> If it is a
236      * directory, returns the last modified file of the archive.
237      * @param archive the archive to monitor.
238      * @return the last modified version of the given archive.
239      */

240     protected long getLastModified(final File archive) {
241         if (archive.isFile()) {
242             return archive.lastModified();
243         }
244         // else
245
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     /**
257      * Receives a stop order.
258      */

259     public void stopOrder() {
260         this.stopped = true;
261     }
262 }
263
Popular Tags