KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > deamons > filewatcher > webappobserver > WebAppsObserver


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// WebAppsObserver
15
//
16
// NK 23.04.2001
17
//
18
//
19

20
21 package org.jahia.services.deamons.filewatcher.webappobserver;
22
23 import org.jahia.bin.Jahia;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.exceptions.JahiaInitializationException;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.security.license.License;
28 import org.jahia.services.deamons.filewatcher.JahiaFileWatcherService;
29 import org.jahia.services.jef.InvalidJefFileException;
30 import org.jahia.services.jef.JecFile;
31 import org.jahia.services.jef.JefFileInvalidKeyValueException;
32 import org.jahia.services.jef.JefFileKeyNotFoundException;
33 import org.jahia.services.sites.JahiaSite;
34 import org.jahia.services.sites.JahiaSitesService;
35 import org.jahia.services.webapps_deployer.JahiaWebAppsDeployerService;
36
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.Observable JavaDoc;
40 import java.util.Observer JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 /**
44  * An Observable object for web apps
45  *
46  * @author Khue ng
47  * @version 1.0
48  */

49 public class WebAppsObserver implements Observer JavaDoc {
50
51     private static org.apache.log4j.Logger logger =
52             org.apache.log4j.Logger.getLogger (WebAppsObserver.class);
53
54     /** updatedOnce true if update() has been executed once * */
55     private boolean mUpdatedOnce = false;
56
57     /** used to attach an observer with a site * */
58     private int mSiteID = -1;
59
60     /** the path of the folder to watch * */
61     private String JavaDoc mPath;
62
63     /** check new file by last modif date or not * */
64     private boolean mCheckDate = false;
65
66     /** the interval * */
67     private long mInterval;
68
69     /** Check bot files and directory * */
70     private boolean mFileOnly = true;
71
72
73     /**
74      * Constructor
75      *
76      * @param siteID used to attach a observer with a site
77      * @param path the path of directory to watch.
78      * @param checkDate check by last modif date or not
79      * @param interval the interval of check
80      * @param fileOnly check both new files and directory
81      */

82     public WebAppsObserver (int siteID,
83                             String JavaDoc path,
84                             boolean checkDate,
85                             long interval,
86                             boolean fileOnly)
87             throws JahiaException {
88
89         mSiteID = siteID;
90         mPath = path;
91         mCheckDate = checkDate;
92         mInterval = interval;
93         mFileOnly = fileOnly;
94
95         JahiaFileWatcherService fws = ServicesRegistry
96                 .getInstance ()
97                 .getJahiaFileWatcherService ();
98
99         if (fws != null) {
100
101             try {
102                 fws.addFileWatcher (mPath,
103                         mPath,
104                         mCheckDate,
105                         mInterval,
106                         mFileOnly);
107
108                 fws.registerObserver (mPath, this);
109                 fws.startFileWatcher (mPath);
110
111             } catch (JahiaException e) {
112
113                 logger.error ("init:: ", e);
114                 throw new JahiaInitializationException (
115                         "WebAppsObserver::init failed ", e);
116             }
117         }
118     }
119
120     /**
121      * Handles new files detection
122      */

123     public void update (Observable JavaDoc subject,
124                         Object JavaDoc args) {
125
126         synchronized (args) {
127
128             JahiaWebAppsDeployerService ads = ServicesRegistry.getInstance ()
129                     .getJahiaWebAppsDeployerService ();
130
131             JahiaSitesService ss = ServicesRegistry.getInstance ()
132                     .getJahiaSitesService ();
133
134             if (ads != null && ads.isInitialized ()
135                     && ss != null && ss.isInitialized () && Jahia.isInitiated ()) {
136
137                 Vector JavaDoc files = (Vector JavaDoc) args;
138                 int size = files.size ();
139                 File f = null;
140                 JecFile jec = null;
141                 boolean reScan = false;
142
143                 License coreLicense = Jahia.getCoreLicense ();
144
145                 if (coreLicense != null && !mUpdatedOnce) {
146
147                     // get license info
148
int jecLicenseType = 0;
149                     int jecLicenseTypeRelComp = 0;
150
151                     // decrypt Jahia Encrypted Components
152
for (int i = 0; i < size; i++) {
153                         f = (File) files.get (i);
154                         if (f.isFile () &&
155                                 (f.getName ().toLowerCase ()).endsWith (".jec")) {
156
157                             try {
158                                 jec = new JecFile (f.getAbsolutePath ());
159                                 jecLicenseType = jec.getLicenseType ();
160                                 jecLicenseTypeRelComp = jec.
161                                         getLicenseTypeRelComp ();
162                                 /*
163                                 if (licenseKey.compareLicense(jecLicenseType,
164                                     jecLicenseTypeRelComp)) {
165                                 */

166                                 jec.extractFiles ();
167                                 f.delete ();
168                                 reScan = true;
169                                 /*
170                                 }
171                                 */

172                                 jec = null;
173                             } catch (JefFileKeyNotFoundException knf) {
174                                 File tmpFile = new File (f.getAbsolutePath () +
175                                         "_error");
176                                 f.renameTo (tmpFile);
177                             } catch (JefFileInvalidKeyValueException ikv) {
178                                 File tmpFile = new File (f.getAbsolutePath () +
179                                         "_error");
180                                 f.renameTo (tmpFile);
181                             } catch (InvalidJefFileException ijf) {
182                                 File tmpFile = new File (f.getAbsolutePath () +
183                                         "_error");
184                                 f.renameTo (tmpFile);
185                             } catch (IOException JavaDoc ioe) {
186                                 ioe.printStackTrace ();
187                             } catch (JahiaException je) {
188                                 // security exception
189
je.printStackTrace ();
190                             } catch (Throwable JavaDoc t) {
191                                 t.printStackTrace ();
192                             }
193                         }
194                     }
195
196                     mUpdatedOnce = true;
197                 }
198
199                 if (reScan) {
200                     org.jahia.services.database.ConnectionDispenser.
201                             terminateConnection ();
202                     return;
203                 }
204
205                 try {
206                     JahiaSite site = ServicesRegistry.getInstance ()
207                             .getJahiaSitesService ()
208                             .getSite (mSiteID);
209                     if (site != null) {
210                         ads.deploy (site, files);
211                     }
212                     org.jahia.services.database.ConnectionDispenser.
213                             terminateConnection ();
214                 } catch (Throwable JavaDoc t) {
215                     logger.error ("Web apps observer error : ", t);
216                     org.jahia.services.database.ConnectionDispenser.
217                             abortConnection ();
218                 }
219             }
220         }
221     }
222
223 }
Popular Tags