View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.cache.setup;
35  
36  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
37  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.cache.AbstractListeningFlushPolicy;
41  import info.magnolia.module.cache.FlushAllListeningPolicy;
42  import info.magnolia.module.delta.NodeVisitorTask;
43  import info.magnolia.objectfactory.Components;
44  import info.magnolia.repository.RepositoryConstants;
45  import info.magnolia.repository.RepositoryManager;
46  
47  import java.util.ArrayList;
48  import java.util.Collection;
49  
50  import javax.jcr.Node;
51  import javax.jcr.PropertyIterator;
52  import javax.jcr.RepositoryException;
53  
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  /**
58   * Migrates configuration of {@link FlushAllListeningPolicy}.
59   */
60  public class MigrateFlushListeningPolicyTask extends NodeVisitorTask {
61  
62      private static final Logger log = LoggerFactory.getLogger(MigrateFlushListeningPolicyTask.class);
63  
64      protected static final String EXCLUDED_WORKSPACES = "excludedWorkspaces";
65      protected static final String REPOSITORIES = "repositories";
66      private static final String TASK_NAME = String.format("Migrate '%s' configuration.", FlushAllListeningPolicy.class.getName());
67      private final String abstractListeningFlushPolicyClassName;
68  
69      public MigrateFlushListeningPolicyTask(String path, Class<? extends AbstractListeningFlushPolicy> abstractListeningFlushPolicyClass) {
70          super(TASK_NAME, TASK_NAME, RepositoryConstants.CONFIG, "/" + path);
71          this.abstractListeningFlushPolicyClassName = abstractListeningFlushPolicyClass.getName();
72      }
73  
74      @Override
75      protected boolean nodeMatches(Node node) {
76          try {
77              return node.hasProperty("class") && abstractListeningFlushPolicyClassName.equals(node.getProperty("class").getString());
78          } catch (RepositoryException e) {
79              log.error("Cannot check if '{}' matches.", node, e);
80          }
81          return false;
82      }
83  
84      @Override
85      protected void operateOnNode(InstallContext installContext, Node node) {
86          Collection<String> includedRepositories = new ArrayList<String>();
87          Collection<String> includedWorkspaces = Components.getComponent(RepositoryManager.class).getWorkspaceNames();
88          try {
89              if (node.hasNode(REPOSITORIES)) {
90                  Node repositores = node.getNode(REPOSITORIES);
91                  PropertyIterator propertyIterator = new FilteringPropertyIterator(repositores.getProperties(), new JCRMgnlPropertyHidingPredicate());
92                  while (propertyIterator.hasNext()) {
93                      includedRepositories.add(propertyIterator.nextProperty().getString());
94                  }
95                  repositores.remove();
96              }
97              includedWorkspaces.removeAll(includedRepositories);
98              Node excludedWorkspaces = node.addNode(EXCLUDED_WORKSPACES, NodeTypes.ContentNode.NAME);
99  
100             for (String workspace : includedWorkspaces) {
101                 excludedWorkspaces.setProperty(workspace, workspace);
102             }
103             installContext.info(String.format("The configuration '%s' was migrated. All workspaces are now registered by default except those defined under '%s'. "
104                     + "The obsolete property '%s' was removed.", node, EXCLUDED_WORKSPACES, REPOSITORIES));
105 
106         } catch (RepositoryException e) {
107             installContext.error(String.format("Cannot execute task '%s' for node '%s'", this.getDescription(), node), e);
108         }
109     }
110 }