View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.core.NodeData;
40  import info.magnolia.cms.util.NodeDataUtil;
41  import info.magnolia.module.delta.AbstractRepositoryTask;
42  import info.magnolia.module.delta.Delta;
43  import info.magnolia.module.delta.DeltaBuilder;
44  import info.magnolia.module.delta.ModuleFilesExtraction;
45  import info.magnolia.module.delta.TaskExecutionException;
46  import info.magnolia.module.delta.Task;
47  import info.magnolia.module.delta.Condition;
48  import info.magnolia.module.model.ModuleDefinition;
49  import info.magnolia.module.model.Version;
50  import info.magnolia.module.model.VersionComparator;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import javax.jcr.RepositoryException;
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.LinkedList;
58  import java.util.List;
59  import java.util.Map;
60  import java.util.TreeMap;
61  
62  /**
63   * Extend this and register your deltas in the constructor using the register method.
64   * Add your own install tasks by overriding the getExtraInstallTasks() method.
65   * In most cases, modules won't need to override any other method.
66   *
67   * @see info.magnolia.module.DefaultModuleVersionHandler
68   * @author gjoseph
69   * @version $Revision: $ ($Author: $)
70   */
71  public abstract class AbstractModuleVersionHandler implements ModuleVersionHandler {
72  
73      protected Logger log = LoggerFactory.getLogger(getClass());
74  
75      private final Map<Version, Delta> allDeltas;
76  
77      public AbstractModuleVersionHandler() {
78          allDeltas = new TreeMap<Version, Delta>(new VersionComparator());
79      }
80  
81      /**
82       * Registers the delta needed to update to version v from the previous one.
83       * Adds a Task to update the module version in the repository.
84       */
85      protected void register(Delta delta) {
86          final Version v = delta.getVersion();
87          if (allDeltas.containsKey(v)) {
88              throw new IllegalStateException("Version " + v + " was already registered in this ModuleVersionHandler.");
89          }
90          delta.getTasks().addAll(getDefaultUpdateTasks(v));
91          delta.getConditions().addAll(getDefaultUpdateConditions(v));
92          allDeltas.put(v, delta);
93      }
94  
95      public Version getCurrentlyInstalled(InstallContext ctx) {
96          try {
97              log.debug("checking currently installed version of module [{}]", ctx.getCurrentModuleDefinition());
98  
99              // check if this module was ever installed
100             if (!ctx.hasModulesNode()) {
101                 return null;
102             }
103             final Content moduleNode = ctx.getOrCreateCurrentModuleNode();
104             final NodeData versionProp = moduleNode.getNodeData("version");
105             if (!versionProp.isExist()) {
106                 return null;
107             }
108 
109             return Version.parseVersion(versionProp.getString());
110         } catch (RepositoryException e) {
111             throw new RuntimeException(e); // TODO
112         }
113     }
114 
115     public List<Delta> getDeltas(InstallContext installContext, Version from) {
116         if (from == null) {
117             return Collections.singletonList(getInstall(installContext));
118         }
119 
120         return getUpdateDeltas(installContext, from);
121     }
122 
123     protected List<Delta> getUpdateDeltas(InstallContext installContext, Version from) {
124         final List<Delta> deltas = new LinkedList<Delta>();
125         for (Version v : allDeltas.keySet()) {
126             if (v.isStrictlyAfter(from)) {
127                 deltas.add(allDeltas.get(v));
128             }
129         }
130 
131         // if there was no delta for the version being installed, we still need to add the default update tasks
132         final Version toVersion = installContext.getCurrentModuleDefinition().getVersion();
133         if (toVersion.isStrictlyAfter(from) && !allDeltas.containsKey(toVersion)) {
134             deltas.add(getDefaultUpdate(installContext));
135         }
136         return deltas;
137     }
138 
139     /**
140      * @deprecated since 4.2, renamed to getDefaultUpdate(InstallContext installContext)
141      */
142     protected Delta getUpdate(InstallContext installContext) {
143         return getDefaultUpdate(installContext);
144     }
145 
146     /**
147      * The minimal delta to be applied for each update, even if no delta was specifically registered
148      * for the version being installed.
149      */
150     protected Delta getDefaultUpdate(InstallContext installContext) {
151         final Version toVersion = installContext.getCurrentModuleDefinition().getVersion();
152         final List<Task> defaultUpdateTasks = getDefaultUpdateTasks(toVersion);
153         final List<Condition> defaultUpdateConditions = getDefaultUpdateConditions(toVersion);
154         return DeltaBuilder.update(toVersion, "").addTasks(defaultUpdateTasks).addConditions(defaultUpdateConditions);
155     }
156 
157     protected List<Task> getDefaultUpdateTasks(Version forVersion) {
158         final List<Task> defaultUpdates = new ArrayList<Task>(2);
159         defaultUpdates.add(new ModuleFilesExtraction());
160         defaultUpdates.add(new ModuleVersionUpdateTask(forVersion));
161         return defaultUpdates;
162     }
163 
164     protected List<Condition> getDefaultUpdateConditions(Version forVersion) {
165         return Collections.emptyList();
166     }
167 
168     /**
169      *
170      * @see #getBasicInstallTasks(InstallContext) override this method if you need a different set of default install tasks.
171      * @see #getExtraInstallTasks(InstallContext) override this method if you need extra tasks for install.
172      */
173     protected Delta getInstall(InstallContext installContext) {
174         final List<Task> installTasks = new ArrayList<Task>();
175         installTasks.addAll(getBasicInstallTasks(installContext));
176         installTasks.addAll(getExtraInstallTasks(installContext));
177         installTasks.add(new ModuleVersionToLatestTask());
178         final List<Condition> conditions = getInstallConditions();
179         final Version version = installContext.getCurrentModuleDefinition().getVersion();
180         return DeltaBuilder.install(version, "").addTasks(installTasks).addConditions(conditions);
181     }
182 
183     protected abstract List<Task> getBasicInstallTasks(InstallContext installContext);
184 
185     /**
186      * Override this method to add specific install tasks to your module.
187      * Returns an empty list by default.
188      */
189     protected List<Task> getExtraInstallTasks(InstallContext installContext) {
190         return Collections.emptyList();
191     }
192 
193     protected List<Condition> getInstallConditions() {
194         return Collections.emptyList();
195     }
196 
197     public Delta getStartupDelta(InstallContext installContext) {
198         final ModuleDefinition moduleDef = installContext.getCurrentModuleDefinition();
199         final List<Task> tasks = getStartupTasks(installContext);
200         return DeltaBuilder.startup(moduleDef, tasks);
201     }
202 
203     /**
204      * Override this method to add specific startup tasks to your module.
205      * Returns an empty list by default.
206      */
207     protected List<Task> getStartupTasks(InstallContext installContext) {
208         return Collections.emptyList();
209     }
210 
211     // TODO : make this mandatory and "hidden" ?
212     public class ModuleVersionToLatestTask extends AbstractRepositoryTask {
213         protected ModuleVersionToLatestTask() {
214             super("Version number", "Sets installed module version number.");
215         }
216 
217         protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
218             // make sure we have the /modules node
219             if (!ctx.hasModulesNode()) {
220                 final HierarchyManager hm = ctx.getConfigHierarchyManager();
221                 hm.createContent("/", ModuleManagerImpl.MODULES_NODE, ItemType.CONTENT.getSystemName());
222             }
223 
224             final Content moduleNode = ctx.getOrCreateCurrentModuleNode();
225             final NodeData nodeData = NodeDataUtil.getOrCreate(moduleNode, "version");
226             nodeData.setValue(getVersion(ctx).toString());
227         }
228 
229         protected Version getVersion(InstallContext ctx) {
230             return ctx.getCurrentModuleDefinition().getVersion();
231         }
232     }
233 
234     public class ModuleVersionUpdateTask extends ModuleVersionToLatestTask {
235         private final Version toVersion;
236 
237         protected ModuleVersionUpdateTask(Version toVersion) {
238             super();
239             this.toVersion = toVersion;
240         }
241 
242         protected Version getVersion(InstallContext ctx) {
243             return toVersion;
244         }
245     }
246 
247 }