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.deployment;
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.jcr.Node;
26  import javax.jcr.Session;
27  
28  import org.exoplatform.portal.config.UserPortalConfigService;
29  import org.exoplatform.services.jcr.RepositoryService;
30  import org.exoplatform.services.jcr.core.ManageableRepository;
31  import org.exoplatform.services.jcr.ext.common.SessionProvider;
32  import org.exoplatform.services.log.ExoLogger;
33  import org.exoplatform.services.log.Log;
34  import org.exoplatform.services.wcm.javascript.XJavascriptService;
35  import org.exoplatform.services.wcm.skin.XSkinService;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  import org.picocontainer.Startable;
38  
39  /**
40   * Created by The eXo Platform SAS
41   * Author : Hoa Pham
42   * hoa.pham@exoplatform.com
43   * Sep 6, 2008
44   */
45  public class WCMContentInitializerService implements Startable{
46  
47    /** The list deployment plugin. */
48    private List<DeploymentPlugin> listDeploymentPlugin = new ArrayList<DeploymentPlugin>();
49  
50    /** The repository service. */
51    private RepositoryService repositoryService;
52  
53    /** The log. */
54    private static final Log LOG = ExoLogger.getLogger(WCMContentInitializerService.class.getName());
55  
56    /**
57     * Instantiates a new wCM content initializer service.
58     *
59     * @param userPortalConfigService
60     */
61    public WCMContentInitializerService(UserPortalConfigService userPortalConfigService) {
62      this.repositoryService = WCMCoreUtils.getService(RepositoryService.class);
63    }
64  
65    /**
66     * Adds the plugin.
67     *
68     * @param deploymentPlugin the deployment plugin
69     */
70    public void addPlugin(DeploymentPlugin deploymentPlugin) {
71      listDeploymentPlugin.add(deploymentPlugin);
72    }
73  
74    /* (non-Javadoc)
75     * @see org.picocontainer.Startable#start()
76     */
77    public void start() {
78      SessionProvider sessionProvider = null;
79      try {
80        sessionProvider = SessionProvider.createSystemProvider();
81        ManageableRepository repository = repositoryService.getCurrentRepository();
82        Session session = sessionProvider.getSession(repository.getConfiguration().getDefaultWorkspaceName(), repository);
83        Node serviceFolder = (Node) session.getItem("/exo:services");
84        Node contentInitializerService = null;
85        if (serviceFolder.hasNode("WCMContentInitializerService")) {
86          contentInitializerService = serviceFolder.getNode("WCMContentInitializerService");
87        } else {
88          contentInitializerService = serviceFolder.addNode("WCMContentInitializerService", "nt:unstructured");
89        }
90        Set<String> newSiteNames = new HashSet<String>();
91        //use DeploymentPlugin list to deploy data
92        boolean firstTimeDeploy = !contentInitializerService.hasNode("WCMContentInitializerServiceLog");
93        Date date = new Date();
94        StringBuffer logData = new StringBuffer();
95        for (DeploymentPlugin deploymentPlugin : listDeploymentPlugin) {
96          try {
97            if (firstTimeDeploy || deploymentPlugin.isOverride() || !isDeployed(deploymentPlugin)) {
98              deploymentPlugin.deploy(sessionProvider);
99              if (deploymentPlugin.getSiteName() != null) {
100               newSiteNames.add(deploymentPlugin.getSiteName());
101             }
102             logData.append("deploy " + deploymentPlugin.getName()
103                 + " deployment plugin succesfully at " + date.toString() + "\n");
104           }
105         } catch (Exception e) {
106           if (LOG.isErrorEnabled()) {
107             LOG.error("deploy " + deploymentPlugin.getName() + " deployment plugin failure at "
108               + date.toString() + " by " + e + "\n");
109           }
110           logData.append("deploy " + deploymentPlugin.getName()
111               + " deployment plugin failure at " + date.toString() + " by " + e + "\n");
112         }
113       }
114 
115       //marks sites with deployed data
116       for (String newSiteName : newSiteNames)
117       if (!contentInitializerService.hasNode(newSiteName)) {
118         contentInitializerService.addNode(newSiteName, "nt:base");
119       }
120       //add log data
121       Node contentInitializerServiceLog = contentInitializerService.hasNode("WCMContentInitializerServiceLog") ?
122                                           contentInitializerService.getNode("WCMContentInitializerServiceLog") :
123                                           contentInitializerService.addNode("WCMContentInitializerServiceLog", "nt:file");
124       Node contentInitializerServiceLogContent = contentInitializerServiceLog.hasNode("jcr:content") ?
125                                                  contentInitializerServiceLog.getNode("jcr:content") :
126                                                  contentInitializerServiceLog.addNode("jcr:content", "nt:resource");
127       contentInitializerServiceLogContent.setProperty("jcr:encoding", "UTF-8");
128       contentInitializerServiceLogContent.setProperty("jcr:mimeType", "text/plain");
129       contentInitializerServiceLogContent.setProperty("jcr:data", logData.toString());
130       contentInitializerServiceLogContent.setProperty("jcr:lastModified", date.getTime());
131       session.save();
132       XJavascriptService jsService = WCMCoreUtils.getService(XJavascriptService.class);
133       XSkinService xSkinService = WCMCoreUtils.getService(XSkinService.class);
134       xSkinService.start();
135       jsService.start();
136     } catch (Exception e) {
137       if (LOG.isErrorEnabled()) {
138         LOG.error("Error when start WCMContentInitializerService: ", e);
139       }
140     } finally {
141       sessionProvider.close();
142     }
143   }
144   
145   /**
146    * indicates if this plugin was deployed,
147    * always return true for legacy data. 
148    */
149   public boolean isDeployed(DeploymentPlugin deploymentPlugin) {
150     SessionProvider sessionProvider = null;
151     try {
152       sessionProvider = SessionProvider.createSystemProvider();
153       ManageableRepository repository = WCMCoreUtils.getRepository();
154       Node serviceFolder = (Node) sessionProvider.getSession(repository.getConfiguration().getDefaultWorkspaceName(), repository)
155                           .getItem("/exo:services");
156       Node contentInitializerService = serviceFolder.hasNode("WCMContentInitializerService") ? 
157                                        serviceFolder.getNode("WCMContentInitializerService") :
158                                        serviceFolder.addNode("WCMContentInitializerService", "nt:unstructured");
159       String sideName = deploymentPlugin.getSiteName();
160       boolean ret = (sideName == null) || contentInitializerService.hasNode(sideName);
161       return ret;
162     } catch (Exception e) {
163       return false;
164     } finally {
165       if (sessionProvider != null) {
166         sessionProvider.close();
167       }
168     }
169   }
170 
171   /* (non-Javadoc)
172    * @see org.picocontainer.Startable#stop()
173    */
174   public void stop() {}
175 }