View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 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.ehcache3.setup;
35  
36  import info.magnolia.init.MagnoliaConfigurationProperties;
37  import info.magnolia.init.MagnoliaInitPaths;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.module.DefaultModuleVersionHandler;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.cache.setup.MigrateEhcache15ConfigurationTask;
42  import info.magnolia.module.delta.BootstrapSingleResource;
43  import info.magnolia.module.delta.DeltaBuilder;
44  import info.magnolia.module.delta.NodeExistsDelegateTask;
45  import info.magnolia.module.delta.NodeVisitorTask;
46  import info.magnolia.module.delta.Task;
47  import info.magnolia.module.delta.WarnCondition;
48  import info.magnolia.repository.RepositoryConstants;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Node;
55  import javax.jcr.RepositoryException;
56  
57  /**
58   * <code>VersionHandler</code> implementation for the EhCache3 module.
59   */
60  public class EhCache3ModuleVersionHandler extends DefaultModuleVersionHandler {
61  
62      @Inject
63      public EhCache3ModuleVersionHandler(MagnoliaConfigurationProperties configurationProperties, MagnoliaInitPaths magnoliaInitPaths) {
64          register(DeltaBuilder.update("5.5.5", "")
65                  .addTask(new MoveDiskStoreDirectoryTask())
66          );
67  
68          register(DeltaBuilder.update("5.8", "")
69                  .addTask(new NodeExistsDelegateTask("Rename 'expiry' in ehcache3 configuration to 'expiryPolicy'", "/modules/cache/config/cacheFactory/delegateFactories/ehcache3",
70                          new NodeVisitorTask("Rename 'expiry' in ehcache3 configuration to 'expiryPolicy'", "Expiry configuration was deprecated", RepositoryConstants.CONFIG, "/modules/cache/config/cacheFactory/delegateFactories/ehcache3") {
71                              @Override
72                              protected boolean nodeMatches(Node node) {
73                                  return NodeUtil.getName(node).equals("expiry");
74                              }
75  
76                              @Override
77                              protected void operateOnNode(InstallContext installContext, Node node) {
78                                  try {
79                                      NodeUtil.renameNode(node, "expiryPolicy");
80                                  } catch (RepositoryException e) {
81                                      installContext.error(e.getMessage(), e);
82                                  }
83                              }
84                          })
85                  )
86                  .addCondition(new WarnCondition("Cached items persisted on disk will be removed!", "Cached items persisted on disk will be removed!", "Persisted cached items removed."))
87                  .addTask(new CleanUpDiskStoreDirectoryTask(configurationProperties, magnoliaInitPaths))
88          );
89      }
90  
91      @Override
92      protected List<Task> getExtraInstallTasks(InstallContext installContext) {
93          final List<Task> tasks = new ArrayList<>();
94          //update from 1.x
95          tasks.add(new NodeExistsDelegateTask("EhCache configuration", "/modules/cache/config/cacheFactory/delegateFactories/ehcache/defaultCacheConfiguration",
96                  new MigrateEhcache15ConfigurationTask()
97          ));
98          tasks.add(new NodeExistsDelegateTask("Configure EhCache3", "/modules/cache/config/cacheFactory/delegateFactories/ehcache",
99                  //update from 2.x
100                 new MigrateEhCache2ConfigurationTask(),
101                 //clean install
102                 new BootstrapSingleResource("EhCache3 clean install", "EhCache3 clean install", "/info/magnolia/module/ehcache3/setup/config.modules.cache.config.cacheFactory.delegateFactories.ehcache3.xml")
103         ));
104         return tasks;
105     }
106 }