KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > deamons > filewatcher > templateobserver > TemplatesObserver


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
// TemplatesObserver
15
//
16
// NK 23.04.2001
17
//
18
//
19

20
21 package org.jahia.services.deamons.filewatcher.templateobserver;
22
23
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.exceptions.JahiaInitializationException;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.services.deamons.filewatcher.JahiaFileWatcherService;
28 import org.jahia.services.pages.JahiaPageTemplateService;
29 import org.jahia.services.sites.JahiaSite;
30 import org.jahia.services.sites.JahiaSitesService;
31 import org.jahia.services.templates_deployer.JahiaTemplatesDeployerService;
32
33 import java.util.Observable JavaDoc;
34 import java.util.Observer JavaDoc;
35 import java.util.Vector JavaDoc;
36
37
38 /**
39  * An Observable object
40  *
41  * @author Khue ng
42  * @version 1.0
43  */

44 public class TemplatesObserver implements Observer JavaDoc {
45
46     private static org.apache.log4j.Logger logger =
47             org.apache.log4j.Logger.getLogger (TemplatesObserver.class);
48
49     /** used to attach an observer with a site * */
50     private int mSiteID = -1;
51
52     /** the path of the folder to watch * */
53     private String JavaDoc mPath;
54
55     /** check new file by last modif date or not * */
56     private boolean mCheckDate = false;
57
58     /** the interval * */
59     private long mInterval;
60
61     /** Check bot files and directory * */
62     private boolean mFileOnly = true;
63
64
65     /**
66      * Constructor
67      *
68      * @param int siteID, used to attach a observer with a site
69      * @param String path, the path of directory to watch.
70      * @param boolean checkDate, check by last modif date or not
71      * @param long interval, the interval of check
72      * @param boolean fileOnly, check both new files and directory
73      */

74     public TemplatesObserver (int siteID,
75                               String JavaDoc path,
76                               boolean checkDate,
77                               long interval,
78                               boolean fileOnly) throws JahiaException {
79
80
81         mSiteID = siteID;
82         mPath = path;
83         mCheckDate = checkDate;
84         mInterval = interval;
85         mFileOnly = fileOnly;
86
87         JahiaFileWatcherService fws = ServicesRegistry
88                 .getInstance ()
89                 .getJahiaFileWatcherService ();
90
91         if (fws != null) {
92
93             try {
94                 fws.addFileWatcher (mPath,
95                         mPath,
96                         mCheckDate,
97                         mInterval,
98                         mFileOnly);
99
100                 fws.registerObserver (mPath, this);
101                 fws.startFileWatcher (mPath);
102
103             } catch (Throwable JavaDoc t) {
104
105                 logger.debug ("Error in templates observer initialization", t);
106
107                 throw new JahiaInitializationException ("TemplatesObserver::init failed ");
108             }
109         }
110     }
111
112
113     //-------------------------------------------------------------------------
114
/**
115      * Handles new files detection
116      */

117     public void update (Observable JavaDoc subject,
118                         Object JavaDoc args) {
119
120         synchronized (args) {
121
122             JahiaPageTemplateService pts = ServicesRegistry.getInstance ()
123                     .getJahiaPageTemplateService ();
124
125             JahiaSitesService ss = ServicesRegistry.getInstance ()
126                     .getJahiaSitesService ();
127
128             JahiaTemplatesDeployerService tds = ServicesRegistry.getInstance ()
129                     .getJahiaTemplatesDeployerService ();
130
131             if (pts != null && pts.isInitialized ()
132                     && ss != null && ss.isInitialized ()
133                     && tds != null && tds.isInitialized ()) {
134
135                 Vector JavaDoc files = (Vector JavaDoc) args;
136
137                 try {
138                     JahiaSite site = ss.getSite (mSiteID);
139                     if (site != null) {
140                         tds.deploy (site, files);
141                     }
142                     org.jahia.services.database.ConnectionDispenser.terminateConnection ();
143                 } catch (Throwable JavaDoc t) {
144                     logger.error ("Error in templates observer initialization", t);
145                 }
146             }
147         }
148     }
149
150
151 } // end JahiaTemplateObserverService
Popular Tags