KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > daemon > InitialDeployer


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.daemon;
6
7 import java.io.File JavaDoc;
8 import java.io.FilenameFilter JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.omg.CORBA.UserException JavaDoc;
14 import org.omg.PortableServer.POA JavaDoc;
15
16 import ve.luz.ica.jackass.deploy.Deployer;
17 import ve.luz.ica.jackass.util.ConfigurationManager;
18 import ve.luz.ica.remoteio.FileUtil;
19 import ve.luz.ica.remoteio.RemoteFile;
20
21 /**
22  * The InitialDeployer deploys all the applications in the jackass deployment
23  * directory on system initialization.
24  * @author Carlos Arévalo
25  */

26 public class InitialDeployer implements Runnable JavaDoc
27 {
28     private static final Log LOG = LogFactory.getLog(InitialDeployer.class);
29
30     private static final String JavaDoc TEMP_EXTENSION = ".tmp";
31     private static final int TIME_OUT = 5000;
32
33     private Deployer deployer;
34     private POA JavaDoc poa;
35
36     /**
37      * Class constructor
38      * @param deployerRef the reference to the deployer object that will be
39      * to deploy the applications.
40      * @param aPoa the poa used to create the remote file object
41      */

42     public InitialDeployer(Deployer deployerRef, POA JavaDoc aPoa)
43     {
44         this.deployer = deployerRef;
45         this.poa = aPoa;
46     }
47
48     /**
49      * Deploy existing applications
50      */

51     public void deployExistingApplications()
52     {
53         this.waitForInstantiators();
54
55         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
56         String JavaDoc deploymentPath = cf.getProperty(ConfigurationManager.DEPLOYMENT_PATH_PROPERTY);
57
58         File JavaDoc file = new File JavaDoc(deploymentPath);
59         ZipFileFilter filter = new ZipFileFilter();
60         String JavaDoc[] zipFiles = file.list(filter);
61
62         if (zipFiles == null) return;
63
64         for (int i = 0; i<zipFiles.length; ++i)
65         {
66             try
67             {
68                 if (LOG.isDebugEnabled()) LOG.debug("Deploying " + zipFiles[i]);
69                 File JavaDoc zipFile = new File JavaDoc(deploymentPath, zipFiles[i]);
70                 File JavaDoc newZipFile = new File JavaDoc(deploymentPath, zipFiles[i]+TEMP_EXTENSION);
71                 if (zipFile.renameTo(newZipFile))
72                 {
73                     if (LOG.isDebugEnabled()) LOG.debug("Zip file remaned to " + newZipFile.getName());
74                     RemoteFile remoteFile = FileUtil.createRemoteFile(newZipFile.getPath(), poa);
75                     deployer.deploy(remoteFile);
76                     newZipFile.delete();
77                 }
78             }
79             catch (Exception JavaDoc e)
80             {
81                 LOG.error(e);
82             }
83         }
84     }
85
86     /**
87      * Wait until notified that all the instantiators have registered
88      * or until a time out expires
89      */

90     private synchronized void waitForInstantiators()
91     {
92         try
93         {
94             wait(TIME_OUT);
95         }
96         catch (InterruptedException JavaDoc e)
97         {
98             e.printStackTrace();
99         }
100     }
101
102     /**
103      * This run method is executed in a separate thread to deploy
104      * existing applications on system startup
105      */

106     public void run()
107     {
108         this.deployExistingApplications();
109     }
110
111
112     /**
113      * A filter for files with a .zip extension.
114      */

115     private static class ZipFileFilter implements FilenameFilter JavaDoc
116     {
117         private static final String JavaDoc ZIP_EXTENSION = ".zip";
118
119         /**
120          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
121          */

122         public boolean accept(File JavaDoc dir, String JavaDoc name)
123         {
124             return name.endsWith(ZIP_EXTENSION);
125         }
126
127     }
128 }
129
130
131
Popular Tags