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.delta;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.repository.RepositoryConstants;
38
39 import javax.jcr.RepositoryException;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45
46
47 public class PathExistenceDelegateTask extends ConditionalDelegateTask {
48
49 private static final Logger log = LoggerFactory.getLogger(PathExistenceDelegateTask.class);
50
51 private final String repository;
52 private final String[] pathsToCheckForExistence;
53 private final String[] pathsToCheckForNonExistence;
54
55 public PathExistenceDelegateTask(String taskName, String taskDescription, String[] pathsToCheckForNonExistence, String[] pathsToCheckForExistence, Task ifTrue) {
56 this(taskName, taskDescription, RepositoryConstants.CONFIG, pathsToCheckForNonExistence, pathsToCheckForExistence, ifTrue);
57 }
58
59 public PathExistenceDelegateTask(String taskName, String taskDescription, String repository, String[] pathsToCheckForNonExistence, String[] pathsToCheckForExistence, Task ifTrue) {
60 super(taskName, taskDescription, ifTrue);
61 this.repository = repository;
62 this.pathsToCheckForExistence = pathsToCheckForExistence;
63 this.pathsToCheckForNonExistence = pathsToCheckForNonExistence;
64 }
65
66 @Override
67 protected boolean condition(InstallContext ctx) {
68 try {
69 if (pathsToCheckForNonExistence != null) {
70 for (String pathToCheck : pathsToCheckForNonExistence) {
71 if (ctx.getJCRSession(repository).nodeExists(pathToCheck)) {
72 log.info("Expected path '{}' to not exist but it did. Not executing task.", pathToCheck);
73 return false;
74 }
75 }
76 }
77 if (pathsToCheckForExistence != null) {
78 for (String pathToCheck : pathsToCheckForExistence) {
79 if (!ctx.getJCRSession(repository).nodeExists(pathToCheck)) {
80 log.info("Expected path '{}' to exist but it did not. Not executing task.", pathToCheck);
81 return false;
82 }
83 }
84 }
85 } catch (RepositoryException e) {
86 log.error("RepositoryException occurred checking provided conditions. Not executing task.", e);
87 return false;
88 }
89
90 return true;
91 }
92
93 }