001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.web.live; 018 019import javax.jcr.Node; 020import javax.jcr.NodeIterator; 021import javax.jcr.RepositoryException; 022import javax.jcr.Session; 023 024import org.apache.avalon.framework.logger.AbstractLogEnabled; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028 029import org.ametys.plugins.repository.AmetysObjectIterable; 030import org.ametys.web.repository.site.Site; 031import org.ametys.web.repository.site.SiteManager; 032import org.ametys.web.skin.Skin; 033import org.ametys.web.skin.SkinsManager; 034 035/** 036 * {@link LivePopulator} for synchronizing sites. 037 */ 038public class SitesLivePopulator extends AbstractLogEnabled implements LivePopulator, Serviceable 039{ 040 private SitePopulator _sitePopulator; 041 private SkinsManager _skinsManager; 042 private SiteManager _siteManager; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 _sitePopulator = (SitePopulator) manager.lookup(SitePopulator.ROLE); 048 _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 049 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 050 } 051 052 @Override 053 public void populate(Session session, Session liveSession) 054 { 055 // First delete sites in live (to avoid that a deleted site in workspace default still remains in live workspace) 056 _deleteLiveSites(liveSession); 057 058 AmetysObjectIterable<Site> siteIterable = _siteManager.getSites(); 059 060 for (Site site : siteIterable) 061 { 062 try 063 { 064 if (getLogger().isInfoEnabled()) 065 { 066 getLogger().info("Populate live workspace for site " + site.getName()); 067 } 068 069 Skin skin = _skinsManager.getSkin(site.getSkinId()); 070 071 if (skin == null) 072 { 073 throw new IllegalArgumentException("There's no skin for site " + site.getName() + ". Unable to rebuild live workspace."); 074 } 075 076 _sitePopulator.populate(site, skin); 077 } 078 catch (Exception e) 079 { 080 getLogger().error("Unable to populate live workspace for site: " + site, e); 081 } 082 } 083 } 084 085 private void _deleteLiveSites(Session liveSession) 086 { 087 try 088 { 089 if (getLogger().isInfoEnabled()) 090 { 091 getLogger().info("Remove all sites from live"); 092 } 093 094 Node liveRootNode = liveSession.getRootNode(); 095 if (liveRootNode.hasNode("ametys:root" + SiteManager.ROOT_SITES_PATH)) 096 { 097 NodeIterator childNodes = liveRootNode.getNode("ametys:root" + SiteManager.ROOT_SITES_PATH).getNodes(); 098 while (childNodes.hasNext()) 099 { 100 Node siteNode = childNodes.nextNode(); 101 siteNode.remove(); 102 } 103 } 104 } 105 catch (RepositoryException e) 106 { 107 getLogger().error("Failed to delete all sites from live workspace", e); 108 } 109 } 110}