KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > autodeploy > AutoDeployControllerImpl


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  * AutoDeployMoniter.java
26  *
27  * Created on February 19, 2003, 10:21 AM
28  *
29  */

30
31 package com.sun.enterprise.deployment.autodeploy;
32
33
34 import com.sun.enterprise.deployment.backend.DeploymentLogger;
35 import java.util.Vector JavaDoc;
36 import java.util.Timer JavaDoc;
37 import java.util.TimerTask JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.io.File JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import com.sun.enterprise.config.ConfigContextEventListener;
43 import com.sun.enterprise.config.ConfigContextEvent;
44 import com.sun.enterprise.admin.common.constant.AdminConstants;
45 import com.sun.enterprise.util.i18n.StringManager;
46
47
48 /**
49  * Main class for autodeploy service. It uses jdk timer class (java.util.Timer) as scheduler.</br>
50  * Presently its not implemented as MBean but later on, we can add the MBean behaviour easily.</br>
51  *
52  * Implements ConfigContextEventListener for handling config changes. Supports auto deployment, with </br>
53  * multiple source directories, using a single java.util.Timer and a single AutoDeployTask instance.</br>
54  * So there will be only one thread running for deployment, even when yoy have more than one input</br>
55  * source directory. It will complete deployment of all the modules/apps from one directory and then </br>
56  * moves on to next.
57  *
58  * @author vikas
59  */

60 public class AutoDeployControllerImpl implements AutoDeployController,ConfigContextEventListener {
61     
62     
63     
64     /**
65      *vector (of File objects) containing the list of source directories
66      */

67     private Vector JavaDoc autodeployDirs=new Vector JavaDoc();
68     private long pollingInterval = AutoDeployConstants.MIN_POOLING_INTERVAL;
69     private boolean verify = false;
70     private boolean preJspCompilation = false;
71     
72     private Timer JavaDoc timer =null;
73     private AutoDeployTask deployTask=null;
74     
75     public static final Logger sLogger=DeploymentLogger.get();
76     private static StringManager localStrings =
77                             StringManager.getManager( AutoDeployControllerImpl.class );
78     
79     
80     public AutoDeployControllerImpl(String JavaDoc autodeployDir, long pollingInterval) throws AutoDeploymentException {
81         try {
82             addAutoDeployDir(autodeployDir);
83             setPollingInterval(pollingInterval);
84         } catch(AutoDeploymentException ae){
85             sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentStartFailure");
86             throw ae;
87         }
88     }
89     
90     public AutoDeployControllerImpl(String JavaDoc[] autodeployDirs, long pollingInterval) throws AutoDeploymentException {
91         try {
92             for (int i=0;i<autodeployDirs.length;i++) {
93                 addAutoDeployDir(autodeployDirs[i]);
94             }
95             setPollingInterval(pollingInterval);
96         } catch(AutoDeploymentException ae){
97             sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentStartFailure");
98             throw ae;
99         }
100         
101     }
102         
103     /**
104      * start autodeployment.
105      */

106     public boolean enableAutoDeploy() {
107         timer = new Timer JavaDoc();
108         deployTask=new AutoDeployTask();
109         timer.schedule(deployTask,AutoDeployConstants.STARTING_DELAY*1000,pollingInterval*1000);
110         String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.autoDeployment_service_enabled");
111         sLogger.log(Level.FINE, msg+System.currentTimeMillis());
112         return true;
113         
114     }
115     
116     /**
117      * stop/disable autodeployment thread.
118      */

119     public boolean disableAutoDeploy() {
120         if(deployTask!=null) {
121             deployTask.cancel();
122         }
123         if(timer!=null) {
124             timer.cancel();
125         }
126         String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.autoDeployment_service_disabled");
127         sLogger.log(Level.INFO, msg);
128         return true;
129         
130         
131     }
132     
133     /**
134      * get all the input source directory.
135      */

136     public String JavaDoc[] getAllAutoDeployDirs() {
137         if(autodeployDirs != null && ! autodeployDirs.isEmpty()) {
138             String JavaDoc[] dirs=new String JavaDoc[autodeployDirs.size()];
139             int i=0;
140             for (Enumeration JavaDoc list = autodeployDirs.elements() ; list.hasMoreElements() ;i++) {
141                 
142                 dirs[i]=((File JavaDoc)list.nextElement()).getAbsolutePath();
143             }
144             
145             return dirs;
146         }else {
147             return null;
148         }
149         
150     }
151     
152     /**
153      * add input source directory. to the list(Vector).
154      */

155     public void addAutoDeployDir(String JavaDoc autodeployDir) throws AutoDeploymentException {
156         if(!validateDir(autodeployDir)) {
157            String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.invalid_source_dir",autodeployDir);
158            sLogger.log(Level.INFO, msg);
159         }
160         if((locateAlreadyExistingDir(autodeployDir)== -1)){
161             autodeployDirs.add(new File JavaDoc(autodeployDir));
162         } else {
163         String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.duplicate_source_dir",autodeployDir);
164         sLogger.log(Level.WARNING, msg);
165         //ignore, dir already exist
166
}
167         
168     }
169     
170     
171     /**
172      * remove input source dir. from the list(Vector).
173      */

174     
175     public void removeAutoDeployDir(String JavaDoc autodeployDir) {
176         int location=locateAlreadyExistingDir(autodeployDir);
177         if(location>=0) {
178             this.autodeployDirs.remove(location);
179         }
180     }
181     
182     /**
183      * get polling duration.
184      */

185     public long getPollingInterval() {
186         return pollingInterval;
187         
188     }
189     
190     /**
191      * set polling interval, polling interval can not be -ve. && more than
192      */

193     public void setPollingInterval(long pollInterval) throws AutoDeploymentException {
194         
195         if(validatePollingInterval(pollInterval)) {
196             this.pollingInterval=pollInterval;
197         } else{
198             String JavaDoc sInterval= new String JavaDoc(""+pollInterval);
199             String JavaDoc sMinInterval= new String JavaDoc(""+AutoDeployConstants.MIN_POOLING_INTERVAL);
200             String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.invalid_pooling_interval", sInterval, sMinInterval);
201             sLogger.log(Level.INFO, msg+pollInterval);
202             throw new AutoDeploymentException(msg+pollInterval);
203         }
204     }
205     
206     /**
207      * is verify flag enabled or not. If enabled verification happnes before </br>
208      * every deployment.
209      */

210     public boolean isVerifyEnabled() {
211         return verify;
212     }
213     
214     /**
215      * set verify flag.
216      */

217     public void setVerify(boolean verify) {
218         this.verify=verify;
219     }
220     
221     /**
222      * is PreJspCompilation flag enabled or not. If enabled PreJspCompilation compilation ignored.</br>
223      *
224      */

225     public boolean isPreJspCompilationEnabled() {
226         return preJspCompilation;
227     }
228     
229     /**
230      * set PreJspCompilation flag.
231      */

232     public void setPreJspCompilation(boolean preJspCompilation) {
233         this.preJspCompilation=preJspCompilation;
234     }
235     
236     
237     private boolean validateDir(String JavaDoc dir) {
238         boolean valid=false;
239         File JavaDoc autodeployDir=new File JavaDoc(dir);
240         if(autodeployDir != null && autodeployDir.exists()
241                 && autodeployDir.isDirectory()&& autodeployDir.canWrite() &&
242                 autodeployDir.canRead() /*&& (locateAlreadyExistingDir(dir)== -1)*/) {
243             valid=true;
244         }
245         return valid;
246         
247     }
248     
249     private int locateAlreadyExistingDir(String JavaDoc dir) {
250         //return -1 if the vector autoDeployDirs dont have this dir
251
int location=-1;
252         String JavaDoc extingDirs[]=getAllAutoDeployDirs();
253         if(extingDirs!=null) {
254             for (int i=0; i<extingDirs.length ; i++) {
255                 if(dir.equalsIgnoreCase(extingDirs[i])) {
256                     location=i; //I got this dir in list
257
break;
258                 }
259             }
260             
261         }
262         return location;
263     }
264     
265     private boolean validatePollingInterval(long time) {
266         boolean valid=false;
267         
268         if(time >= AutoDeployConstants.MIN_POOLING_INTERVAL) {
269             valid= true;
270         }
271         return valid;
272         
273     }
274     
275     
276     /**
277      *Implement notification listner.</br>
278      * before config add, delete, set, update or flush. type is in ccce
279      */

280     public void preChangeNotification(ConfigContextEvent ccce) {
281         //not implemented
282
}
283     
284     
285     /**
286      *Implement notification listner.</br>
287      * after config add, delete, set, update or flush. type is in ccce
288      */

289     public void postChangeNotification(ConfigContextEvent ccce) {
290         //not implemented
291
}
292     
293     /**
294      *Implement notification listner.</br>
295      * before config add, delete, set, update or flush. type is in ccce
296      */

297     public void preAccessNotification(ConfigContextEvent ccce) {
298         //not implemented
299
}
300     
301     /**
302      *Implement notification listner.</br>
303      * after config add, delete, set, update or flush. type is in ccce
304      */

305     public void postAccessNotification(ConfigContextEvent ccce) {
306         //not implemented
307
}
308     
309     
310     
311     
312     private class AutoDeployTask extends TimerTask JavaDoc {
313         
314         /**
315          * Implementation of TimerTask run method. It will call deployAll() </br>
316          * method of AutoDeployer for every source dirs. </br>
317          *
318          *for (Enumeration list = autodeployDirs.elements() ; list.hasMoreElements() ;) { </br>
319          * File sourceDir = (File)list.nextElement(); </br>
320          * AutoDeployer.deployAll(sourceDir,verify); </br>
321          * }
322          *
323          */

324         private AutoDeployer currentDeployer= null;
325         
326         private DirectoryScanner directoryScanner = null;
327         
328         public void run() {
329             
330             try{
331                 if (sLogger.isLoggable(Level.FINEST)) {
332                     String JavaDoc msg = localStrings.getString("enterprise.deployment.autodeploy.thread_started");
333                     sLogger.log(Level.FINEST, msg + System.currentTimeMillis());
334                 }
335                 
336                 if(autodeployDirs != null && ! autodeployDirs.isEmpty()) {
337                     
338                     for (Enumeration JavaDoc list = autodeployDirs.elements() ; list.hasMoreElements() ;) {
339                         
340                         if(currentDeployer !=null && currentDeployer.isCancelled()) {//if last cycle is terminated using "cancel" signal let me JUST BREAK
341
break;
342                         }
343                         
344                         File JavaDoc sourceDir = (File JavaDoc)list.nextElement();
345                         if (sLogger.isLoggable(Level.FINEST)) {
346                             sLogger.log(Level.FINEST,
347                                         localStrings.getString("enterprise.deployment.autodeploy.processing_source_directory",sourceDir));
348                         }
349                         // quick check to see if our only file is the .autodeploystatus
350
File JavaDoc[] sourceDirFiles = sourceDir.listFiles();
351                         if (sourceDirFiles.length==1
352                                 && AutoDeployedFilesManager.STATUS_DIR_NAME.equals(sourceDirFiles[0].getName())) {
353                             continue;
354                         }
355                         //making a quick check that this dir contain any new deployable entity.
356
AutoDeployer deployer = getAutoDeployer();
357                         try {
358                             deployer.deployAll(sourceDir, true);
359                             deployer.undeployAll(sourceDir);
360                         }catch(Exception JavaDoc ae) {
361                             sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentFailure",
362                                     new Object JavaDoc[] {ae.getMessage()});
363                             //let me continue for the next directory
364
}
365                         
366                         if(deployer.isCancelled()) {//if this cycle is terminated using "cancel" signal let me JUST BREAK
367
break;
368                         }
369                     }
370                     
371                 }
372                 
373             }catch(Exception JavaDoc e){
374                 currentDeployer=null;
375                 sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentFailure", new Object JavaDoc[] {e.getMessage()});
376             }
377         }
378         
379         /**
380          * cancel method will ensure that if there is any current deployment is in process,</br>
381          * it will be completed before terminating the run
382          * @return
383          */

384         public boolean cancel(){
385             boolean flag=false;
386             //let me just complete this execution
387
if(currentDeployer !=null) {
388                 currentDeployer.setCancel(true);
389             }
390             super.cancel();
391             flag=true;
392             return flag;
393             
394         }
395         
396         private DirectoryScanner getDirectoryScanner() {
397             if (directoryScanner == null) {
398                 directoryScanner = new AutoDeployDirectoryScanner();
399             }
400             return directoryScanner;
401         }
402
403         private AutoDeployer getAutoDeployer() {
404             if (currentDeployer == null) {
405                 currentDeployer = new AutoDeployer(verify, preJspCompilation);
406             } else {
407                 /*
408                  *Before returning the pre-existing instance, set the characteristics because
409                  *they might have been changed since the .
410                  */

411                 currentDeployer.setVerify(verify);
412                 currentDeployer.setJspPreCompilation(preJspCompilation);
413             }
414             currentDeployer.setDirectoryScanner(getDirectoryScanner());
415             return currentDeployer;
416         }
417     }
418 }
419
Popular Tags