View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.RoleManager;
45  import info.magnolia.cms.security.Security;
46  import info.magnolia.cms.util.ContentUtil;
47  import info.magnolia.importexport.Bootstrapper;
48  import info.magnolia.module.InstallContext;
49  import info.magnolia.module.model.ModuleDefinition;
50  import info.magnolia.module.model.RepositoryDefinition;
51  import info.magnolia.repository.RepositoryConstants;
52  
53  import javax.jcr.RepositoryException;
54  
55  /**
56   * Bootstrap empty repositories for the current module. (loading is already performed before install tasks)
57   *
58   * @author gjoseph
59   * @version $Revision$ ($Author$)
60   */
61  public class SetupModuleRepositoriesTask extends AbstractTask {
62  
63      public SetupModuleRepositoriesTask() {
64          super("Setup module repositories", "Bootstrap empty repositories, grant them to superuser and subscribe them so that activation can be used.");
65      }
66  
67      // TODO finer exception handling ?
68      @Override
69      public void execute(InstallContext ctx) throws TaskExecutionException {
70          try {
71              final ModuleDefinition def = ctx.getCurrentModuleDefinition();
72              // register repositories
73              for (RepositoryDefinition repDef : def.getRepositories()) {
74                  for (final String workspace : repDef.getWorkspaces()) {
75                      // bootstrap the workspace if empty
76                      if (!ContentRepository.checkIfInitialized(workspace)) {
77                          final String[] bootstrapDirs = Bootstrapper.getBootstrapDirs();
78                          Bootstrapper.bootstrapRepository(bootstrapDirs, workspace, new Bootstrapper.BootstrapFilter() {
79                              @Override
80                              public boolean accept(String filename) {
81                                  return filename.startsWith(workspace + ".");
82                              }
83                          });
84                      }
85  
86                      // TODO : split these in separate tasks ?
87                      grantRepositoryToSuperuser(workspace);
88                      subscribeRepository(workspace);
89                  }
90              }
91          }
92          catch (Throwable e) {
93              throw new TaskExecutionException("Could not bootstrap workspace: " + e.getMessage(), e);
94          }
95  
96      }
97  
98      private void grantRepositoryToSuperuser(String workspace) {
99          // grant repository to superuser - TODO : maybe this shouldn't use the RoleManager, as we shouldn't assume it's ready yet.
100         RoleManager roleMan = Security.getRoleManager();
101         final Role superuser = roleMan.getRole("superuser");
102         roleMan.addPermission(superuser, workspace, "/*", Permission.ALL);
103     }
104 
105     /**
106      * Register the repository to get used for activation
107      * TODO - use an API for this? But same remark as above, the component might not be ready yet.
108      */
109     private void subscribeRepository(String repository) throws TaskExecutionException {
110         ActivationManager sManager = ActivationManagerFactory.getActivationManager();
111         for (Subscriber subscriber : sManager.getSubscribers()) {
112             if (!subscriber.isSubscribed("/", repository)) {
113                 Content subscriptionsNode = ContentUtil.getContent(RepositoryConstants.CONFIG, sManager.getConfigPath()
114                         + "/"
115                         + subscriber.getName()
116                         + "/subscriptions");
117                 try {
118                     Content newSubscription = subscriptionsNode.createContent(repository, ItemType.CONTENTNODE);
119                     newSubscription.createNodeData("toURI").setValue("/");
120                     newSubscription.createNodeData("repository").setValue(repository);
121                     newSubscription.createNodeData("fromURI").setValue("/");
122                     // subscriptionsNode.save();
123                 }
124                 catch (RepositoryException re) {
125                     throw new TaskExecutionException("wasn't able to subscribe repository [" + repository + "]", re);
126                 }
127             }
128         }
129     }
130 }