KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sites > JahiaSitesBaseService


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
// JahiaSitesBaseService
14
//
15
// NK 12.03.2001
16
//
17
package org.jahia.services.sites;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.jahia.data.JahiaDOMObject;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.exceptions.JahiaInitializationException;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.services.acl.ACLResource;
28 import org.jahia.services.acl.JahiaACLEntry;
29 import org.jahia.services.acl.JahiaBaseACL;
30 import org.jahia.services.cache.Cache;
31 import org.jahia.services.cache.CacheFactory;
32 import org.jahia.services.cache.CacheListener;
33 import org.jahia.services.filemanager.JahiaFilemanagerService;
34 import org.jahia.services.usermanager.JahiaGroup;
35 import org.jahia.settings.SettingsBean;
36
37
38 /**
39  * Jahia Multi Sites Management Service
40  *
41  * @author Khue ng
42  */

43 public class JahiaSitesBaseService extends JahiaSitesService implements CacheListener {
44
45     /** logger reference. */
46     final private static Logger logger = Logger.getLogger (JahiaSitesBaseService.class);
47
48
49     protected static JahiaSitesBaseService instance = null;
50
51     public static final String JavaDoc SITE_CACHE_BYID = "JahiaSiteByIDCache";
52     public static final String JavaDoc SITE_CACHE_BYNAME = "JahiaSiteByNameCache";
53     /** The cache in memory */
54     private Cache siteCacheByID = null;
55     private Cache siteCacheByName = null;
56
57     // list of sites going to be deleted.
58
// This is used by some services like search engine to avoid useless indexation.
59
private Vector JavaDoc sitesToDelete = new Vector JavaDoc ();
60
61     /** reference to the jahia private settings */
62     protected SettingsBean mJahiaSettings = null;
63
64
65     /**
66      * Default constructor, creates a new <code>JahiaSitesBaseService</code> instance.
67      *
68      * @throws JahiaException
69      */

70     protected JahiaSitesBaseService () throws JahiaException {
71         logger.info ("***** Starting the Jahia Sites Base Service *****");
72
73         setServiceName ("JahiaSitesBaseService");
74     }
75
76
77     /**
78      * Retrieves the unique instance of this singleton class.
79      *
80      * @return the unique instance of this class
81      *
82      * @throws JahiaException
83      */

84     public static synchronized JahiaSitesBaseService getInstance ()
85             throws JahiaException {
86         if (instance == null) {
87             instance = new JahiaSitesBaseService ();
88         }
89         return instance;
90     }
91
92
93     /**
94      * return the list of all sites
95      *
96      * @return Enumeration an enumeration of JahiaSite bean
97      *
98      * @todo this only returns the entries that are in the cache !! If the
99      * cache was flushed in the meantime, this method is FALSE !
100      */

101     public Enumeration JavaDoc getSites () throws JahiaException {
102         Vector JavaDoc allSites = new Vector JavaDoc ();
103         JahiaSite site;
104         Object JavaDoc[] siteCacheKeys = siteCacheByID.keys ();
105         for (int i = 0; i < siteCacheKeys.length; i++) {
106             site = (JahiaSite) siteCacheByID.get (siteCacheKeys[i]);
107             if (site != null) {
108                 // site may be null since we also cache null entries for speed.
109
allSites.add(site);
110             }
111         }
112
113         return allSites.elements ();
114     }
115
116
117     /**
118      * @param jSettings
119      */

120     public void init (SettingsBean jSettings)
121             throws JahiaInitializationException {
122         if (!isInitialized ()) {
123             mIsServiceInitialized = true;
124             mJahiaSettings = jSettings;
125         }
126
127         siteCacheByID = CacheFactory.createCache (SITE_CACHE_BYID);
128         siteCacheByID.registerListener(this);
129         siteCacheByName = CacheFactory.createCache (SITE_CACHE_BYNAME);
130         siteCacheByName.registerListener(this);
131
132         try {
133             loadSitesInCache(jSettings);
134         } catch (JahiaException je) {
135             throw new JahiaInitializationException("Error while loading sites in cache", je);
136         }
137
138         initFilemanagers ();
139     }
140
141     /**
142      * return a site bean looking at it id
143      *
144      * @param id the JahiaSite id
145      *
146      * @return JahiaSite the JahiaSite bean
147      */

148     public JahiaSite getSite(int id) throws JahiaException {
149         Integer JavaDoc idObj = new Integer JavaDoc(id);
150         JahiaSite site = (JahiaSite) siteCacheByID.get(idObj);
151         if (site != null) {
152             return site;
153         }
154
155         // try to load from db
156
site = JahiaSitesPersistance.getInstance().dbGetSite(id);
157         synchronized (this) {
158             // if the site could be loaded, add it into the cache
159
if (!siteCacheByID.containsKey(idObj)) {
160                 siteCacheByID.put(idObj, site);
161             }
162             if (site != null && site.getServerName() != null && !siteCacheByName.containsKey(site.getServerName())) {
163                 siteCacheByName.put(site.getServerName(), site);
164             }
165         }
166
167         return site;
168     }
169
170
171     /**
172      * return a site bean looking at it key
173      *
174      * @param siteKey
175      * the site key
176      *
177      * @return JahiaSite the JahiaSite bean
178      */

179     public JahiaSite getSiteByKey(String JavaDoc siteKey) throws JahiaException {
180         if (siteKey == null) {
181             return null;
182         }
183         JahiaSite site = null;
184
185         // try to find the site in the cache
186
Object JavaDoc[] siteCacheKeys = siteCacheByID.keys();
187         for (int i = 0; i < siteCacheKeys.length; i++) {
188             site = (JahiaSite) siteCacheByID.get(siteCacheKeys[i]);
189             if (site != null) {
190                 // site might be null because we cache non-existent entries too.
191
if (siteKey.equals(site.getSiteKey())) {
192                     return site;
193                 }
194             }
195         }
196         // the site was not found in the cache, try to load it from the
197
// database.
198
site = JahiaSitesPersistance.getInstance().dbGetSiteByKey(siteKey);
199
200         // if the site could be loaded from the database, add it into the
201
// cache.
202
if (site != null) {
203             synchronized (this) {
204                 Integer JavaDoc siteIDObj = new Integer JavaDoc(site.getID());
205                 if (!siteCacheByID.containsKey(siteIDObj)) {
206                     siteCacheByID.put(siteIDObj, site);
207                 }
208                 if (site.getServerName() != null && !siteCacheByName.containsKey(site.getServerName())) {
209                     siteCacheByName.put(site.getServerName(), site);
210                 }
211             }
212         }
213
214         return site;
215     }
216
217     /**
218      * Find a site by it's server name value
219      *
220      * @param serverName
221      * the server name to look for
222      *
223      * @return if found, returns the site with the corresponding serverName, or
224      * the first one if there are multiple, or null if there are none.
225      *
226      * @throws JahiaException
227      * thrown if there was a problem communicating with the
228      * database.
229      */

230     public JahiaSite getSiteByServerName(String JavaDoc serverName) throws JahiaException {
231         if (serverName == null) {
232             return null;
233         }
234         JahiaSite site = (JahiaSite) siteCacheByName.get(serverName);
235
236         if (siteCacheByName.containsKey(serverName)) {
237             return site;
238         }
239         // the site was not found in the cache, try to load it from the
240
// database.
241
site = JahiaSitesPersistance.getInstance().dbGetSiteByServerName(serverName);
242
243         // if the site could be loaded from the database, add it into the
244
// cache.
245
if (site != null) {
246             synchronized (this) {
247                 Integer JavaDoc siteIDObj = new Integer JavaDoc(site.getID());
248                 if (!siteCacheByID.containsKey(siteIDObj)) {
249                     siteCacheByID.put(siteIDObj, site);
250                 }
251                 if (!siteCacheByName.containsKey(serverName)) {
252                     siteCacheByName.put(serverName, site);
253                 }
254             }
255         }
256
257         return site;
258     }
259
260
261     // --------------------------------------------------------------------------
262
// FH 10 May 2001 Cache storing improvments for performance issues.
263
/**
264      * return a site looking at it's name
265      *
266      * @param name the site name
267      *
268      * @return JahiaSite the JahiaSite bean or null
269      */

270     public JahiaSite getSite(String JavaDoc name) throws JahiaException {
271         if (name == null) {
272             return null;
273         }
274         JahiaSite site = (JahiaSite) siteCacheByName.get(name);
275
276         if (site != null) {
277             return site;
278         }
279         // try to load the site from the database.
280
site = JahiaSitesPersistance.getInstance().dbGetSite(name);
281
282         if (site != null) {
283             synchronized (this) {
284                 Integer JavaDoc siteIDObj = new Integer JavaDoc(site.getID());
285                 if (!siteCacheByID.containsKey(siteIDObj)) {
286                     siteCacheByID.put(siteIDObj, site);
287                 }
288                 if (!siteCacheByName.containsKey(name)) {
289                     siteCacheByName.put(name, site);
290                 }
291             }
292         }
293         return site;
294     }
295
296
297     // --------------------------------------------------------------------------
298
/**
299      * Add a new site only if there is no other site with same server name
300      *
301      * @param site the JahiaSite bean
302      *
303      * @return boolean false if there is another site using same server name
304      */

305     public synchronized boolean addSite (JahiaSite site)
306             throws JahiaException {
307
308         if (site == null) {
309             return false;
310         }
311
312
313         // check there is no site with same server name before adding
314
if (getSite (site.getServerName ()) == null
315                 && getSiteByKey (site.getSiteKey ()) == null) {
316
317             JahiaSitesPersistance.getInstance ().dbAddSite (site);
318
319             if (site.getID () == -1) {
320                 return false;
321             }
322
323             siteCacheByID.put (new Integer JavaDoc (site.getID ()), site);
324             if (site.getServerName() != null) {
325                 siteCacheByName.put(site.getServerName(), site);
326             }
327             return true;
328         }
329
330         return false;
331     }
332
333
334     /**
335      * remove a site
336      *
337      * @param site the JahiaSite bean
338      */

339     public synchronized void removeSite (JahiaSite site) throws JahiaException {
340         JahiaSitesPersistance.getInstance ().dbRemoveSite (site.getID ());
341         siteCacheByID.remove (new Integer JavaDoc (site.getID ()));
342         siteCacheByName.remove(site.getServerName());
343     }
344
345
346     /**
347      * Update a JahiaSite definition
348      *
349      * @param site the site bean object
350      */

351     public synchronized void updateSite (JahiaSite site) throws JahiaException {
352         JahiaSitesPersistance.getInstance ().dbUpdateSite (site);
353         siteCacheByID.put (new Integer JavaDoc (site.getID ()), site);
354         siteCacheByName.put(site.getServerName(), site);
355     }
356
357
358     /**
359      * load all sites from database in cache
360      */

361     protected void loadSitesInCache (SettingsBean settingsBean) throws JahiaException {
362
363         Vector JavaDoc sites = JahiaSitesPersistance.getInstance ().dbGetSites ();
364
365         //--------------------------------------------------------------
366
// ACL on templates Patch
367
//
368
// 30.01.2002 : NK Patch : give admin permission for root admin grp
369
// and site's admin grp on the site ACL if not set
370
// This is needed for ACL who have as parent , the site's ACL
371
//
372

373         // root admin group
374
JahiaGroup rootAdminGrp = ServicesRegistry.getInstance ()
375                 .getJahiaGroupManagerService ().getAdministratorGroup (0);
376
377         // site admin group
378
JahiaGroup siteAdminGrp = null;
379
380         // settings default permissions
381
JahiaACLEntry adminAclEntry = new JahiaACLEntry(
382           JahiaBaseACL.ALL_RIGHTS, JahiaACLEntry.ACL_YES);
383
384         // End Patch
385
//---------------------------------------------------------------
386

387         if (sites != null) {
388             int size = sites.size ();
389             for (int i = 0; i < size; i++) {
390                 JahiaSite site = (JahiaSite) sites.get (i);
391
392                 //--------------------------------------------------------------
393
// ACL on templates Patch
394
//
395
// 30.01.2002 : NK Patch : give admin permission for root admin grp
396
// and site's admin grp on the site ACL if not set
397
// This is needed for ACL who have as parent , the site's ACL
398
//
399

400
401                 if (!ACLResource.checkAdminAccess (site, rootAdminGrp)) {
402
403                     site.getACL ().setGroupEntry (rootAdminGrp, adminAclEntry);
404
405                     siteAdminGrp = ServicesRegistry.getInstance ()
406                             .getJahiaGroupManagerService ().getAdministratorGroup (
407                                     site.getID ());
408
409                     site.getACL ().setGroupEntry (siteAdminGrp, adminAclEntry);
410                 }
411
412                 // End Patch
413
//--------------------------------------------------------------
414

415
416                 // start and create the site's new templates folder if not exists
417
JahiaSiteTools.startTemplateObserver (site, settingsBean);
418
419                 // start and create the site's new webapps folder if not exists
420
JahiaSiteTools.startWebAppsObserver (site, settingsBean);
421
422                 siteCacheByID.put (new Integer JavaDoc (site.getID ()), site);
423                 siteCacheByName.put(site.getServerName(), site);
424             }
425         }
426     }
427
428
429     /**
430      * Add a site to the list of site going to be deleted
431      *
432      * @param siteID the site id
433      */

434     public synchronized void addSiteToDelete (int siteID) {
435         Integer JavaDoc I = new Integer JavaDoc (siteID);
436         if (!this.sitesToDelete.contains (I)) {
437             this.sitesToDelete.add (I);
438         }
439     }
440
441     /**
442      * Remove a given site from the list of site going to be deleted
443      *
444      * @param siteID the site id
445      */

446     public synchronized void removeSiteToDelete (int siteID) {
447         this.sitesToDelete.remove (new Integer JavaDoc (siteID));
448     }
449
450     /**
451      * Return true if the given site is going to be deleted
452      *
453      * @param siteID the site id
454      *
455      * @return boolean
456      */

457     public synchronized boolean isSiteToBeDeleted (int siteID) {
458         return (this.sitesToDelete.contains (new Integer JavaDoc (siteID)));
459     }
460
461     // javadocs automaticaly imported from the JahiaSitesService class.
462
//
463
public int getNbSites ()
464             throws JahiaException {
465         return JahiaSitesPersistance.getInstance ().getNbSites ();
466     }
467
468
469     /**
470      * returns a DOM representation of a site
471      *
472      * @param siteID
473      */

474     public JahiaDOMObject getSiteAsDOM (int siteID) throws JahiaException {
475
476         return JahiaSitesPersistance.getInstance ().getSiteAsDOM (siteID);
477
478     }
479
480
481     /**
482      * returns a DOM representation of a site's properties
483      *
484      * @param siteID
485      */

486     public JahiaDOMObject getSitePropsAsDOM (int siteID) throws JahiaException {
487
488         return JahiaSitesPersistance.getInstance ().getSitePropsAsDOM (siteID);
489
490     }
491
492
493     /**
494      * create the filemager's directory for the site
495      */

496     public void initFilemanagers () {
497
498         try {
499             JahiaFilemanagerService fmngServ = ServicesRegistry.getInstance ()
500                     .getJahiaFilemanagerService ();
501             if (fmngServ == null) {
502                 return;
503             }
504             Enumeration JavaDoc enumeration = getSites ();
505             JahiaSite site = null;
506             while (enumeration.hasMoreElements ()) {
507                 site = (JahiaSite) enumeration.nextElement ();
508                 fmngServ.createDirectory (site);
509             }
510         } catch (Throwable JavaDoc t) {
511             t.printStackTrace ();
512         }
513     }
514
515     /**
516      * Returns a properties as a String.
517      *
518      * @param siteID
519      * @param key the property name
520      */

521     public String JavaDoc getProperty (int siteID, String JavaDoc key)
522             throws JahiaException {
523         return JahiaSitesPersistance.getInstance ().getProperty (siteID, key);
524     }
525
526     /**
527      * This method is called each time the cache flushes its items.
528      *
529      * @param cacheName the name of the cache which flushed its items.
530      */

531     public void onCacheFlush(String JavaDoc cacheName) {
532         if (SITE_CACHE_BYID.equals(cacheName) ||
533             SITE_CACHE_BYNAME.equals(cacheName)) {
534             try {
535                 siteCacheByID.flush(false);
536                 siteCacheByName.flush(false);
537                 loadSitesInCache(mJahiaSettings);
538
539             } catch (JahiaException e) {
540                 logger.warn(
541                         "Could not reload Jahia Sites after the Site Cache was flushed!", e);
542             }
543         }
544     }
545
546     public void onCachePut(String JavaDoc cacheName, Object JavaDoc entryKey) {
547         // do nothing;
548
}
549
550 }
551
Popular Tags