KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > JahiaApplicationsManagerBaseService


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  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 10-OCT-2000, Xo3 SA, Khue Nguyen: Initial version
37  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.services.applications;
44
45 import org.jahia.data.JahiaDOMObject;
46 import org.jahia.data.applications.ApplicationBean;
47 import org.jahia.data.applications.ApplicationContext;
48 import org.jahia.data.fields.JahiaField;
49 import org.jahia.exceptions.JahiaException;
50 import org.jahia.exceptions.JahiaInitializationException;
51 import org.jahia.registries.ServicesRegistry;
52 import org.jahia.services.usermanager.JahiaGroup;
53 import org.jahia.settings.SettingsBean;
54
55 import java.util.Collections JavaDoc;
56 import java.util.Enumeration JavaDoc;
57 import java.util.Vector JavaDoc;
58 import org.jahia.services.cache.Cache;
59 import org.jahia.services.cache.CacheFactory;
60
61
62 /**
63  * This Service is used to manage the jahia application definitions.
64  *
65  * @author Khue ng
66  * @version 1.0
67  * @todo FIXME this class does not support a cache limit, it should never be
68  * limited in it's current implementation
69  */

70 public class JahiaApplicationsManagerBaseService extends JahiaApplicationsManagerService {
71
72     /** logging */
73     private static org.apache.log4j.Logger logger =
74             org.apache.log4j.Logger.getLogger (JahiaApplicationsManagerBaseService.class);
75
76     private static final String JavaDoc REGISTRY_CACHE_NAME = "ApplicationDefinitionsCache";
77
78     /** the instance * */
79     private static JahiaApplicationsManagerBaseService instance;
80
81     /** the cache of application definition * */
82     private Cache registry;
83
84     /** dummy comparator application bean * */
85     private ApplicationBean dummyComparator;
86
87     /** is loaded status * */
88     private boolean isLoaded = false;
89
90     /**
91      * constructor
92      */

93     protected JahiaApplicationsManagerBaseService () {
94
95         logger.debug ("Starting the Jahia Applications Manager Base Service");
96         dummyComparator = new ApplicationBean (-1, -1, "", "", 0, false, -1, "", "");
97     }
98
99
100     /**
101      * return the singleton instance
102      */

103     public static synchronized JahiaApplicationsManagerBaseService getInstance () {
104         if (instance == null) {
105             instance = new JahiaApplicationsManagerBaseService ();
106         }
107
108         return instance;
109     }
110
111     /**
112      * Initialze disk path
113      *
114      * @param jSettings
115      */

116     public void init (SettingsBean jSettings)
117             throws JahiaInitializationException {
118         try {
119
120             registry = CacheFactory.createCache(REGISTRY_CACHE_NAME);
121
122             loadAllApplications ();
123         } catch (Throwable JavaDoc t) {
124             t.printStackTrace ();
125             throw new JahiaInitializationException (
126                     "JahiaApplicationsManagerBaseService.init, exception occured : "
127                     + t.getMessage (), t);
128         }
129         this.isLoaded = true;
130     }
131
132     /**
133      * return a vector of distinct Jahia web site for all application definitions
134      *
135      * @return a Vector of Jahia Web site
136      */

137     public Vector JavaDoc getWebSites ()
138             throws JahiaException {
139
140         return ServicesRegistry.getInstance ()
141                 .getJahiaApplicationsPersistanceService ().getWebSites ();
142
143     }
144
145     /**
146      * return an Application Definition get directly from db
147      *
148      * @param appID the appID
149      *
150      * @return ApplicationBean, the Application Definition
151      */

152     public ApplicationBean getApplication (int appID)
153             throws JahiaException {
154
155         checkIsLoaded ();
156
157         synchronized (registry) {
158             ApplicationBean app;
159             if (registry.containsKey(new Integer JavaDoc (appID))) {
160                 app = (ApplicationBean) registry.get(new
161                     Integer JavaDoc(appID));
162                 return app;
163             }
164             // try to load from db
165
app = ServicesRegistry.getInstance ()
166                   .getJahiaApplicationsPersistanceService ()
167                   .get_application_definition (appID);
168             if (app != null) {
169                registry.put (new Integer JavaDoc (app.getID ()), app);
170             }
171             return app;
172         }
173     }
174
175     /**
176      * return an Application Definition looking at its context.
177      *
178      * @param context , the context
179      *
180      * @return ApplicationBean, the Application Definition
181      */

182     public ApplicationBean getApplication (String JavaDoc context)
183             throws JahiaException {
184
185         checkIsLoaded ();
186
187         if (context == null) {
188             return null;
189         }
190
191         synchronized (registry) {
192             ApplicationBean app = null;
193             Object JavaDoc[] keys = registry.keys();
194             for (int i=0; i < keys.length; i++) {
195                 app = (ApplicationBean) registry.get (keys[i]);
196                 if (app != null
197                         && app.getContext () != null
198                         && app.getContext ().equals (context)) {
199                     return app;
200                 }
201             }
202             // try to load from db
203
app = ServicesRegistry.getInstance ()
204                     .getJahiaApplicationsPersistanceService ()
205                     .get_application_definition (context);
206             if (app != null) {
207                 registry.put(new Integer JavaDoc(app.getID()), app);
208             }
209             return app;
210         }
211     }
212
213     /**
214      * return all application Definitions
215      *
216      * @return Enumeration an enumerations of ApplicationBean or null if empty
217      */

218     public Vector JavaDoc getApplications ()
219             throws JahiaException {
220
221         checkIsLoaded ();
222
223         Vector JavaDoc apps = new Vector JavaDoc (ServicesRegistry.getInstance ()
224                 .getJahiaApplicationsPersistanceService ()
225                 .get_applications_list (false));
226         Collections.sort (apps, dummyComparator);
227         return apps;
228     }
229
230     /**
231      * return all application Definitions for a gived site ( jahia id )
232      *
233      * @param jahiaID the JahiaID web site
234      *
235      * @return Vector of ApplicationBean
236      */

237     public Vector JavaDoc getApplications (int jahiaID)
238             throws JahiaException {
239
240         checkIsLoaded ();
241
242         Vector JavaDoc apps = new Vector JavaDoc ();
243         Enumeration JavaDoc enumeration = getApplications().elements();
244         ApplicationBean app = null;
245         while (enumeration.hasMoreElements ()) {
246             app = (ApplicationBean) enumeration.nextElement ();
247             if (app.getJahiaID () == jahiaID) {
248                 apps.add (registry.get (new Integer JavaDoc (app.getID ())));
249             }
250         }
251         Collections.sort (apps, dummyComparator);
252         return apps;
253     }
254
255     /**
256      * set an application Visible to users
257      *
258      * @param appID
259      * @param visible status
260      *
261      * @return false on error
262      */

263     public boolean setVisible (int appID, boolean visible)
264             throws JahiaException {
265
266         checkIsLoaded ();
267
268         ApplicationBean app = getApplication (appID);
269         if (app != null) {
270             if (visible) {
271                 app.setVisible (1);
272             } else {
273                 app.setVisible (0);
274             }
275             return saveDefinition (app);
276         }
277         return false;
278     }
279
280     /**
281      * Add a new Application Definition.
282      * both in ApplicationsRegistry and in Persistance
283      *
284      * @param app the app Definition
285      *
286      * @return false on error
287      */

288     public boolean addDefinition (ApplicationBean app)
289             throws JahiaException {
290
291         checkIsLoaded ();
292
293         if (app == null) {
294             return false;
295         }
296         ServicesRegistry.getInstance ()
297                 .getJahiaApplicationsPersistanceService ()
298                 .add_application (app);
299         registry.put (new Integer JavaDoc (app.getID ()), app);
300         return true;
301     }
302
303     /**
304      * Save the Application Definition.
305      * both in ApplicationsRegistry and in Persistance
306      *
307      * @param app the app Definition
308      *
309      * @return false on error
310      */

311     public boolean saveDefinition (ApplicationBean app)
312             throws JahiaException {
313
314         checkIsLoaded ();
315
316         if (app == null) {
317             return false;
318         }
319         ServicesRegistry.getInstance ()
320                 .getJahiaApplicationsPersistanceService ()
321                 .update_application (app);
322         registry.put (new Integer JavaDoc (app.getID ()), app);
323         return true;
324     }
325
326     /**
327      * Removes an application from the persistant storage area and from registry.
328      *
329      * @param appID identifier of the application to remove from the persistant
330      * storage area
331      *
332      * @throws org.jahia.exceptions.JahiaException
333      * generated if there was an error while removing the application
334      * data from persistant storage area
335      */

336     public void removeApplication (int appID)
337             throws JahiaException {
338
339         checkIsLoaded ();
340         ServicesRegistry.getInstance ()
341                 .getJahiaApplicationsPersistanceService ()
342                 .removeApplication (appID);
343         registry.remove (new Integer JavaDoc (appID));
344     }
345
346     //--------------------------------------------------------------------------
347
/**
348      * delete groups associated with an application.
349      * When deleting an Application definition, should call this method to
350      * remove unused groups
351      */

352     public void deleteApplicationGroups (ApplicationBean app)
353             throws JahiaException {
354
355         checkIsLoaded ();
356
357         Vector JavaDoc vec = new Vector JavaDoc ();
358
359         vec = ServicesRegistry.getInstance ()
360                 .getJahiaGroupManagerService ()
361                 .getGroupnameList ();
362
363         if (app != null && vec != null) {
364
365             int appID = app.getID ();
366             int size = vec.size ();
367             JahiaGroup grp = null;
368             String JavaDoc grpName = "";
369             String JavaDoc pattern = appID + "_";
370             for (int i = 0; i < size; i++) {
371                 grpName = (String JavaDoc) vec.get (i);
372                 if (grpName.startsWith (pattern)) {
373                     grp = ServicesRegistry.getInstance ()
374                             .getJahiaGroupManagerService ()
375                             .lookupGroup (grpName);
376                     if (grp != null) {
377                         ServicesRegistry.getInstance ()
378                                 .getJahiaGroupManagerService ()
379                                 .deleteGroup (grp);
380                     }
381                 }
382             }
383         }
384     }
385
386     //--------------------------------------------------------------------------
387
/**
388      * create groups for each context, that is for each field id
389      */

390     public void createApplicationGroups (ApplicationBean theApp, JahiaField theField)
391             throws JahiaException {
392
393         //System.out.println("create_application_groups started");
394

395         // update roles
396
ApplicationContext appContext = ServicesRegistry.getInstance ()
397                 .getJahiaApplicationContextService ()
398                 .getApplicationContext (theApp.getID ());
399
400         Enumeration JavaDoc updatedRoles = appContext.getRoles ().elements ();
401         String JavaDoc groupName = "";
402         String JavaDoc role = "";
403         while (updatedRoles.hasMoreElements ()) {
404             role = (String JavaDoc) updatedRoles.nextElement ();
405             groupName =
406                     Integer.toString (theApp.getID ()) + "_" + Integer.toString (
407                             theField.getID ()) + "_" + role;
408             ServicesRegistry.getInstance ().getJahiaGroupManagerService ().createGroup (0,
409                     groupName, null); // Hollis all app role groups are of site 0 !!!
410
}
411
412     }
413
414     //--------------------------------------------------------------------------
415
/**
416      * delete groups associated with a gived context, that is attached to a field id
417      * and all its members
418      */

419     public void deleteApplicationGroups (ApplicationBean theApp, JahiaField theField)
420             throws JahiaException {
421
422         //System.out.println("delete_application_groups started");
423

424         ApplicationContext appContext = ServicesRegistry.getInstance ()
425                 .getJahiaApplicationContextService ()
426                 .getApplicationContext (theApp.getID ());
427
428         Enumeration JavaDoc roles = appContext.getRoles ().elements ();
429         String JavaDoc groupName = "";
430         String JavaDoc role = "";
431         while (roles.hasMoreElements ()) {
432             role = (String JavaDoc) roles.nextElement ();
433             groupName =
434                     Integer.toString (theApp.getID ()) + "_" + Integer.toString (
435                             theField.getID ()) + "_" + role;
436             JahiaGroup grp = ServicesRegistry.getInstance ().getJahiaGroupManagerService ()
437                     .lookupGroup (0, groupName); // Hollis : All App group roles are in site 0 !!!
438

439             // delete all members
440
grp.removeMembers ();
441
442             ServicesRegistry.getInstance ().getJahiaGroupManagerService ().deleteGroup (grp);
443         }
444
445     }
446
447     //--------------------------------------------------------------------------
448
/**
449      * return a DOM document of applications definitions
450      *
451      * @param siteID the site id
452      *
453      * @return JahiaDOMObject a DOM representation of this object
454      */

455     public JahiaDOMObject getApplicationDefsAsDOM (int siteID)
456             throws JahiaException {
457         return ServicesRegistry.getInstance ()
458                 .getJahiaApplicationsPersistanceService ()
459                 .getApplicationDefsAsDOM (siteID);
460     }
461
462     //--------------------------------------------------------------------------
463
/**
464      * load all application Definitions in registry
465      */

466     private void loadAllApplications ()
467             throws JahiaException {
468
469         synchronized (registry) {
470             Vector JavaDoc apps = ServicesRegistry.getInstance ()
471                     .getJahiaApplicationsPersistanceService ()
472                     .get_applications_list (false);
473             if (apps != null) {
474                 ApplicationBean app = null;
475                 int size = apps.size ();
476                 for (int i = 0; i < size; i++) {
477                     app = (ApplicationBean) apps.get (i);
478                     Integer JavaDoc I = new Integer JavaDoc (app.getID ());
479                     registry.put (I, app);
480                 }
481             }
482         }
483     }
484
485     //--------------------------------------------------------------------------
486
/**
487      * throw an exception if the service hasn't been loaded successfully
488      */

489     private void checkIsLoaded () throws JahiaException {
490
491         if (!isLoaded) {
492             throw new JahiaException (
493                     "Error accessing a service that was not initialized successfully",
494                     "Error accessing a service that was not initialized successfully",
495                     JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY);
496         }
497     }
498
499
500 } // end JahiaApplicationsService
501
Popular Tags