1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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
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 }