View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.delta;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.exchange.ActivationManager;
40  import info.magnolia.cms.exchange.ActivationManagerFactory;
41  import info.magnolia.cms.exchange.Subscriber;
42  import info.magnolia.cms.security.Permission;
43  import info.magnolia.cms.security.Role;
44  import info.magnolia.cms.security.Security;
45  import info.magnolia.cms.util.ContentUtil;
46  import info.magnolia.importexport.Bootstrapper;
47  import info.magnolia.module.InstallContext;
48  import info.magnolia.module.model.ModuleDefinition;
49  import info.magnolia.module.model.RepositoryDefinition;
50  
51  import javax.jcr.RepositoryException;
52  
53  /**
54   * Bootstrap empty repositories for the current module. (loading is already performed before install tasks)
55   * 
56   * @author gjoseph
57   * @version $Revision: 32667 $ ($Author: gjoseph $)
58   */
59  public class SetupModuleRepositoriesTask extends AbstractTask {
60  
61      public SetupModuleRepositoriesTask() {
62          super("Setup module repositories", "Bootstrap empty repositories, grant them to superuser and subscribe them so that activation can be used.");
63      }
64  
65      // TODO finer exception handling ?
66      public void execute(InstallContext ctx) throws TaskExecutionException {
67          try {
68              final ModuleDefinition def = ctx.getCurrentModuleDefinition();
69              // register repositories
70              for (RepositoryDefinition repDef : def.getRepositories()) {
71                  for (final String workspace : repDef.getWorkspaces()) {
72                      // bootstrap the workspace if empty
73                      if (!ContentRepository.checkIfInitialized(workspace)) {
74                          final String[] bootstrapDirs = Bootstrapper.getBootstrapDirs();
75                          Bootstrapper.bootstrapRepository(bootstrapDirs, workspace, new Bootstrapper.BootstrapFilter() {
76                              public boolean accept(String filename) {
77                                  return filename.startsWith(workspace + ".");
78                              }
79                          });
80                      }
81  
82                      // TODO : split these in separate tasks ?
83                      grantRepositoryToSuperuser(workspace);
84                      subscribeRepository(workspace);
85                  }
86              }
87          }
88          catch (Throwable e) {
89              throw new TaskExecutionException("Could not bootstrap workspace: " + e.getMessage(), e);
90          }
91  
92      }
93  
94      private void grantRepositoryToSuperuser(String workspace) {
95          // grant repository to superuser - TODO : maybe this shouldn't use the RoleManager, as we shouldn't assume it's ready yet.
96          final Role superuser = Security.getRoleManager().getRole("superuser");
97          superuser.addPermission(workspace, "/*", Permission.ALL);
98      }
99  
100     /**
101      * Register the repository to get used for activation
102      * TODO - use an API for this? But same remark as above, the component might not be ready yet.
103      */
104     private void subscribeRepository(String repository) throws TaskExecutionException {
105         ActivationManager sManager = ActivationManagerFactory.getActivationManager();
106         for (Subscriber subscriber : sManager.getSubscribers()) {
107             if (!subscriber.isSubscribed("/", repository)) {
108                 Content subscriptionsNode = ContentUtil.getContent(ContentRepository.CONFIG, sManager.getConfigPath()
109                         + "/"
110                         + subscriber.getName()
111                         + "/subscriptions");
112                 try {
113                     Content newSubscription = subscriptionsNode.createContent(repository, ItemType.CONTENTNODE);
114                     newSubscription.createNodeData("toURI").setValue("/");
115                     newSubscription.createNodeData("repository").setValue(repository);
116                     newSubscription.createNodeData("fromURI").setValue("/");
117                     // subscriptionsNode.save();
118                 }
119                 catch (RepositoryException re) {
120                     throw new TaskExecutionException("wasn't able to subscribe repository [" + repository + "]", re);
121                 }
122             }
123         }
124     }
125 }