1 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 ; 56 import java.util.Enumeration ; 57 import java.util.Vector ; 58 import org.jahia.services.cache.Cache; 59 import org.jahia.services.cache.CacheFactory; 60 61 62 70 public class JahiaApplicationsManagerBaseService extends JahiaApplicationsManagerService { 71 72 73 private static org.apache.log4j.Logger logger = 74 org.apache.log4j.Logger.getLogger (JahiaApplicationsManagerBaseService.class); 75 76 private static final String REGISTRY_CACHE_NAME = "ApplicationDefinitionsCache"; 77 78 79 private static JahiaApplicationsManagerBaseService instance; 80 81 82 private Cache registry; 83 84 85 private ApplicationBean dummyComparator; 86 87 88 private boolean isLoaded = false; 89 90 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 103 public static synchronized JahiaApplicationsManagerBaseService getInstance () { 104 if (instance == null) { 105 instance = new JahiaApplicationsManagerBaseService (); 106 } 107 108 return instance; 109 } 110 111 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 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 137 public Vector getWebSites () 138 throws JahiaException { 139 140 return ServicesRegistry.getInstance () 141 .getJahiaApplicationsPersistanceService ().getWebSites (); 142 143 } 144 145 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 (appID))) { 160 app = (ApplicationBean) registry.get(new 161 Integer (appID)); 162 return app; 163 } 164 app = ServicesRegistry.getInstance () 166 .getJahiaApplicationsPersistanceService () 167 .get_application_definition (appID); 168 if (app != null) { 169 registry.put (new Integer (app.getID ()), app); 170 } 171 return app; 172 } 173 } 174 175 182 public ApplicationBean getApplication (String 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 [] 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 app = ServicesRegistry.getInstance () 204 .getJahiaApplicationsPersistanceService () 205 .get_application_definition (context); 206 if (app != null) { 207 registry.put(new Integer (app.getID()), app); 208 } 209 return app; 210 } 211 } 212 213 218 public Vector getApplications () 219 throws JahiaException { 220 221 checkIsLoaded (); 222 223 Vector apps = new Vector (ServicesRegistry.getInstance () 224 .getJahiaApplicationsPersistanceService () 225 .get_applications_list (false)); 226 Collections.sort (apps, dummyComparator); 227 return apps; 228 } 229 230 237 public Vector getApplications (int jahiaID) 238 throws JahiaException { 239 240 checkIsLoaded (); 241 242 Vector apps = new Vector (); 243 Enumeration 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 (app.getID ()))); 249 } 250 } 251 Collections.sort (apps, dummyComparator); 252 return apps; 253 } 254 255 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 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 (app.getID ()), app); 300 return true; 301 } 302 303 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 (app.getID ()), app); 323 return true; 324 } 325 326 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 (appID)); 344 } 345 346 352 public void deleteApplicationGroups (ApplicationBean app) 353 throws JahiaException { 354 355 checkIsLoaded (); 356 357 Vector vec = new Vector (); 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 grpName = ""; 369 String pattern = appID + "_"; 370 for (int i = 0; i < size; i++) { 371 grpName = (String ) 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 390 public void createApplicationGroups (ApplicationBean theApp, JahiaField theField) 391 throws JahiaException { 392 393 395 ApplicationContext appContext = ServicesRegistry.getInstance () 397 .getJahiaApplicationContextService () 398 .getApplicationContext (theApp.getID ()); 399 400 Enumeration updatedRoles = appContext.getRoles ().elements (); 401 String groupName = ""; 402 String role = ""; 403 while (updatedRoles.hasMoreElements ()) { 404 role = (String ) updatedRoles.nextElement (); 405 groupName = 406 Integer.toString (theApp.getID ()) + "_" + Integer.toString ( 407 theField.getID ()) + "_" + role; 408 ServicesRegistry.getInstance ().getJahiaGroupManagerService ().createGroup (0, 409 groupName, null); } 411 412 } 413 414 419 public void deleteApplicationGroups (ApplicationBean theApp, JahiaField theField) 420 throws JahiaException { 421 422 424 ApplicationContext appContext = ServicesRegistry.getInstance () 425 .getJahiaApplicationContextService () 426 .getApplicationContext (theApp.getID ()); 427 428 Enumeration roles = appContext.getRoles ().elements (); 429 String groupName = ""; 430 String role = ""; 431 while (roles.hasMoreElements ()) { 432 role = (String ) 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); 439 grp.removeMembers (); 441 442 ServicesRegistry.getInstance ().getJahiaGroupManagerService ().deleteGroup (grp); 443 } 444 445 } 446 447 455 public JahiaDOMObject getApplicationDefsAsDOM (int siteID) 456 throws JahiaException { 457 return ServicesRegistry.getInstance () 458 .getJahiaApplicationsPersistanceService () 459 .getApplicationDefsAsDOM (siteID); 460 } 461 462 466 private void loadAllApplications () 467 throws JahiaException { 468 469 synchronized (registry) { 470 Vector 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 I = new Integer (app.getID ()); 479 registry.put (I, app); 480 } 481 } 482 } 483 } 484 485 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 } | Popular Tags |