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.util.NodeTypes;
37  import info.magnolia.module.DefaultModuleVersionHandler;
38  import info.magnolia.module.InstallContext;
39  import info.magnolia.module.cache.ehcache.EhCacheFactory;
40  import info.magnolia.module.delta.AbstractRepositoryTask;
41  import info.magnolia.module.delta.ArrayDelegateTask;
42  import info.magnolia.module.delta.BootstrapSingleResource;
43  import info.magnolia.module.delta.CreateNodeTask;
44  import info.magnolia.module.delta.MoveNodeTask;
45  import info.magnolia.module.delta.NodeExistsDelegateTask;
46  import info.magnolia.module.delta.PropertyValueDelegateTask;
47  import info.magnolia.module.delta.Task;
48  import info.magnolia.module.delta.TaskExecutionException;
49  import info.magnolia.module.delta.WarnTask;
50  import info.magnolia.repository.RepositoryConstants;
51  
52  import java.util.ArrayList;
53  import java.util.List;
54  
55  import javax.jcr.Node;
56  import javax.jcr.Property;
57  import javax.jcr.RepositoryException;
58  import javax.jcr.Session;
59  
60  /**
61   * <code>VersionHandler</code> implementation for the {@link info.magnolia.module.cache.EhCacheModule}.
62   */
63  public class EhCacheModuleVersionHandler extends DefaultModuleVersionHandler {
64  
65      @Override
66      protected List<Task> getExtraInstallTasks(InstallContext installContext) {
67          final List<Task> tasks = new ArrayList<Task>();
68          tasks.add(new NodeExistsDelegateTask("EhCache configuration", "/modules/cache/config/cacheFactory/defaultCacheConfiguration",
69                  //update
70                  new ArrayDelegateTask("Ehcache upgrade",
71                          new PropertyValueDelegateTask("EhCache default config", "Moves config:/modules/cache/config/cacheFactory/defaultCacheConfiguration to /modules/cache/config/cacheFactory/caches/default.",
72                                  RepositoryConstants.CONFIG, "/modules/cache/config/cacheFactory", "class", EhCacheFactory.class.getName(), true,
73                                  new ArrayDelegateTask("",
74                                          new CreateNodeTask("Default cache config", "/modules/cache/config/cacheFactory/", "caches", NodeTypes.ContentNode.NAME),
75                                          new MoveNodeTask("Default cache config", "/modules/cache/config/cacheFactory/defaultCacheConfiguration", "/modules/cache/config/cacheFactory/caches/default", false),
76                                          new EhCache28PersistenceConfigSettingsTask()
77                                  ), new WarnTask("Please update cache default settings", "Please update cache default settings. You are not using " + EhCacheFactory.class.getName() + ", so we did not update your configuration."))),
78                  //clean install
79                  new BootstrapSingleResource("EhCache clean install", "EhCache clean install", "/info/magnolia/module/ehcache/setup/config.modules.cache.config.cacheFactory.xml")
80          ));
81          return tasks;
82      }
83  
84      private static class EhCache28PersistenceConfigSettingsTask extends AbstractRepositoryTask {
85          public EhCache28PersistenceConfigSettingsTask() {
86              super("EhCache persistence settings", "");
87          }
88  
89          @Override
90          protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
91              final Session session = ctx.getConfigJCRSession();
92              final Node defaultCache = session.getNode("/modules/cache/config/cacheFactory/caches/default");
93              final Node persistence = defaultCache.addNode("persistence", NodeTypes.ContentNode.NAME);
94              persistence.setProperty("synchronousWrites", "false");
95              final boolean overflowToDisk = getBooleanAndRemove(defaultCache, "overflowToDisk", true);
96              final boolean diskPersistent = getBooleanAndRemove(defaultCache, "diskPersistent", true);
97              if (overflowToDisk && diskPersistent) {
98                  // persistence.setProperty("strategy", "localRestartable"); // Needs ehcacheee  see MGNLCACHE67
99                  persistence.setProperty("strategy", "localTempSwap");
100                 ctx.warn("EhCache: needs ehcache enterprise edition / BigMemory for persistence. Disabling persistance for the time beeing.");
101             } else if (overflowToDisk) {
102                 persistence.setProperty("strategy", "localTempSwap");
103             } else {
104                 ctx.warn("EhCache: overflowToDisk or diskPersistent was set to false, so we'll set the persistent strategy to 'none'");
105                 persistence.setProperty("strategy", "none");
106             }
107         }
108 
109         private boolean getBooleanAndRemove(Node node, String propertyName, boolean defaultValue) throws RepositoryException {
110             if (node.hasProperty(propertyName)) {
111                 final Property property = node.getProperty(propertyName);
112                 final boolean val = property.getBoolean();
113                 property.remove();
114                 return val;
115             }
116             return defaultValue;
117         }
118     }
119 }