KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > AutoDeployMonitor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * @(#) AutoDeployMonitor.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.server;
38
39 import java.io.File JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import com.sun.enterprise.util.io.FileUtils;
42
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45 import com.sun.logging.LogDomains;
46
47 /**
48  * Monitors the auto deploy directory for new archives.
49  *
50  * @atuthor Nazrul Islam
51  * @since JDK 1.4
52  */

53 class AutoDeployMonitor extends AbstractMonitor {
54
55     /** logger for this auto deploy monitor */
56     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
57
58     /** singleton instance */
59     private static AutoDeployMonitor _instance = null;
60
61     /** filter for the archives */
62     private static ArchiveFilter _archiveFilter = null;
63
64     /** suffix added to the archives when deployed successfully */
65     private static final String JavaDoc SUCCESS_EXT = ".deployed";
66
67     /** suffix added to the archives when not deployed */
68     private static final String JavaDoc ERROR_EXT = ".notdeployed";
69
70     /**
71      * Constructor - prohibits anyone from constructing this object.
72      *
73      * @param pollInterval polling interval
74      */

75     private AutoDeployMonitor(long pollInterval) {
76         super(pollInterval);
77     }
78
79     /**
80      * Returns the singleton instance. If the object was not created already,
81      * it uses the given polling interval.
82      *
83      * @param pollInterval polling interval
84      */

85     static AutoDeployMonitor getInstance(long pollInterval) {
86         if (_instance == null) {
87             _instance = new AutoDeployMonitor(pollInterval);
88             _archiveFilter = new ArchiveFilter();
89         }
90         return _instance;
91     }
92
93     /**
94      * Periodically monitors the auto deploy directory of a server.
95      * If any new archive is detected, this monitor makes a callback
96      * to the listener provided in the monitorable entry.
97      *
98      * <p> This method checks the file extension of the archives found
99      * under the auto deploy directory. If the file extensions are .ear,
100      * .jar, .war, or .rar, it makes a callback to the listener.
101      * It is assumed that after processing the callback the handler will
102      * rename the archive with proper extension. For example, for a successful
103      * deployment, foo.ear may be renamed to foo.ear.deployed.
104      *
105      */

106     public void run() {
107
108         try {
109             synchronized (_monitoredEntries) {
110                 Iterator JavaDoc iter = _monitoredEntries.iterator();
111                 MonitorableEntry entry = null;
112
113                 // should be only one entry encapsulating the auto deploy dir
114
while (iter.hasNext()) {
115                     entry = (MonitorableEntry) iter.next();
116                     File JavaDoc autoDeployDir = entry.getMonitoredFile();
117
118                     File JavaDoc[] archives = autoDeployDir.listFiles(_archiveFilter);
119
120                     if ( (archives == null) || (archives.length == 0) ) {
121                         // no new archive
122
return;
123                     }
124
125                     MonitorListener l = entry.getListener();
126
127                     for (int i=0; i<archives.length; i++) {
128
129                         _logger.log(Level.FINE,
130                             "[AutoDeployMonitor] Found " + archives[i]);
131
132                         boolean success = l.deploy(entry, archives[i]);
133                         if (success) {
134                             File JavaDoc successExt =
135                                 new File JavaDoc(archives[i].getParentFile(),
136                                          archives[i].getName()+SUCCESS_EXT);
137                             archives[i].renameTo(successExt);
138                         } else {
139                             File JavaDoc errorExt =
140                                 new File JavaDoc(archives[i].getParentFile(),
141                                          archives[i].getName()+ERROR_EXT);
142                             archives[i].renameTo(errorExt);
143                         }
144                     }
145                 }
146             }
147         } catch (Throwable JavaDoc t) {
148             // catches any uncaught exceptions thrown from the handlers
149
_logger.log(Level.WARNING, "core.exception", t);
150         }
151     }
152
153     /**
154      * A filter for deployable archives.
155      */

156     private static class ArchiveFilter implements java.io.FileFilter JavaDoc {
157
158         /**
159          * Returns true if the given file is an ear, jar, war or rar.
160          *
161          * @param pathname file to be tested
162          * @return true if the given file is an ear, jar, war or rar
163          */

164         public boolean accept(File JavaDoc pathname) {
165
166             if ( FileUtils.isEar(pathname)
167                     || FileUtils.isJar(pathname)
168                     || FileUtils.isWar(pathname)
169                     || FileUtils.isRar(pathname) ) {
170                 return true;
171             } else {
172                 return false;
173             }
174         }
175     }
176 }
177
Popular Tags