View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.core.impl;
18  
19  import java.util.Collection;
20  import java.util.concurrent.ConcurrentHashMap;
21  
22  import javax.jcr.Node;
23  import javax.jcr.Session;
24  
25  import org.exoplatform.container.component.ComponentPlugin;
26  import org.exoplatform.services.jcr.core.ManageableRepository;
27  import org.exoplatform.services.jcr.ext.common.SessionProvider;
28  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
29  import org.exoplatform.services.log.ExoLogger;
30  import org.exoplatform.services.log.Log;
31  import org.exoplatform.services.wcm.core.NodeLocation;
32  import org.exoplatform.services.wcm.core.WCMConfigurationService;
33  import org.exoplatform.services.wcm.core.WebSchemaConfigService;
34  import org.exoplatform.services.wcm.core.WebSchemaHandler;
35  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
36  import org.picocontainer.Startable;
37  
38  /**
39   * Created by The eXo Platform SAS.
40   *
41   * @author : Hoa.Pham
42   * hoa.pham@exoplatform.com
43   * Jun 3, 2008
44   */
45  public class WebSchemaConfigServiceImpl implements WebSchemaConfigService, Startable {
46  
47    /** The web schema handlers. */
48    private ConcurrentHashMap<String, WebSchemaHandler> webSchemaHandlers = new ConcurrentHashMap<String, WebSchemaHandler>();
49  
50    /** The wcm config service. */
51    private WCMConfigurationService wcmConfigService;
52  
53    /** The log. */
54    private static final Log LOG = ExoLogger.getLogger(WebSchemaConfigServiceImpl.class.getName());
55  
56    /**
57     * Instantiates a new web schema config service impl.
58     *
59     * @param configurationService the configuration service
60     * @param nodeHierarchyCreator the hierarchy creator
61     */
62    public WebSchemaConfigServiceImpl(WCMConfigurationService configurationService, NodeHierarchyCreator nodeHierarchyCreator) {
63      this.wcmConfigService = WCMCoreUtils.getService(WCMConfigurationService.class);
64    }
65  
66    /*
67     * (non-Javadoc)
68     * @see
69     * org.exoplatform.services.wcm.core.WebSchemaConfigService#addWebSchemaHandler
70     * (org.exoplatform.container.component.ComponentPlugin)
71     */
72    public void addWebSchemaHandler(ComponentPlugin plugin) throws Exception {
73      if (plugin instanceof WebSchemaHandler) {
74        String clazz = plugin.getClass().getName();
75        webSchemaHandlers.putIfAbsent(clazz, (WebSchemaHandler)plugin);
76      }
77    }
78  
79    /* (non-Javadoc)
80     * @see org.exoplatform.services.wcm.core.WebSchemaConfigService#getAllWebSchemaHandler()
81     */
82    public Collection<WebSchemaHandler> getAllWebSchemaHandler() throws Exception {
83      return webSchemaHandlers.values();
84    }
85  
86    /* (non-Javadoc)
87     * @see org.exoplatform.services.wcm.core.WebSchemaConfigService#getWebSchemaHandlerByType(java.lang.Class)
88     */
89    public <T extends WebSchemaHandler> T getWebSchemaHandlerByType(Class<T> clazz){
90      WebSchemaHandler schemaHandler = webSchemaHandlers.get(clazz.getName());
91      if (schemaHandler == null) return null;
92      return clazz.cast(schemaHandler);
93    }
94  
95    /*
96     * (non-Javadoc)
97     * @see
98     * org.exoplatform.services.wcm.core.WebSchemaConfigService#createSchema(javax
99     * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
100    */
101   public void createSchema(SessionProvider sessionProvider, Node node) throws Exception {
102     for (WebSchemaHandler handler: getAllWebSchemaHandler()) {
103       if (handler.matchHandler(sessionProvider, node)) {
104         handler.onCreateNode(sessionProvider, node);
105       }
106     }
107   }
108 
109   /*
110    * (non-Javadoc)
111    * @see
112    * org.exoplatform.services.wcm.core.WebSchemaConfigService#updateSchemaOnModify
113    * (javax.jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
114    */
115   public void updateSchemaOnModify(SessionProvider sessionProvider, Node node) throws Exception {
116     for (WebSchemaHandler handler: getAllWebSchemaHandler()) {
117       if (handler.matchHandler(sessionProvider, node)) {
118         handler.onModifyNode(sessionProvider, node);
119       }
120     }
121   }
122 
123   /*
124    * (non-Javadoc)
125    * @see
126    * org.exoplatform.services.wcm.core.WebSchemaConfigService#updateSchemaOnRemove
127    * (javax.jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
128    */
129   public void updateSchemaOnRemove(SessionProvider sessionProvider, Node node) throws Exception {
130     for (WebSchemaHandler handler: getAllWebSchemaHandler()) {
131       if (handler.matchHandler(sessionProvider, node)) {
132         handler.onRemoveNode(sessionProvider, node);
133         return;
134       }
135     }
136   }
137 
138   /**
139    * Creates the live share portal folders.
140    */
141   private void createLiveSharePortalFolders() {
142     SessionProvider sessionProvider = null;
143     try {
144       sessionProvider = SessionProvider.createSystemProvider();
145       for (NodeLocation locationEntry: wcmConfigService.getAllLivePortalsLocation()) {
146         String repoName = locationEntry.getRepository();
147         try {
148           ManageableRepository repository = WCMCoreUtils.getRepository();
149           Session session = sessionProvider.getSession(locationEntry.getWorkspace(), repository);
150           Node livePortalsStorage = (Node)session.getItem(locationEntry.getPath());
151           String liveSharedPortalName = wcmConfigService.getSharedPortalName();
152           if(!livePortalsStorage.hasNode(liveSharedPortalName)) {
153             livePortalsStorage.addNode(liveSharedPortalName, "exo:portalFolder");
154             session.save();
155           }
156         } catch (Exception e) {
157           if (LOG.isErrorEnabled()) {
158             LOG.error("Error when try to create share portal folder for repository: "+ repoName, e);
159           }
160         }
161       }
162     } finally {
163       sessionProvider.close();
164     }
165   }
166 
167   /* (non-Javadoc)
168    * @see org.picocontainer.Startable#start()
169    */
170   public void start() {
171     if (LOG.isInfoEnabled()) {
172       LOG.info("Start WebSchemaConfigServiceImpl...");
173     }
174     createLiveSharePortalFolders();
175   }
176 
177   /* (non-Javadoc)
178    * @see org.picocontainer.Startable#stop()
179    */
180   public void stop() { }
181 }