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.context.MgnlContext;
37 import info.magnolia.importexport.BootstrapUtil;
38 import info.magnolia.importexport.DataTransporter;
39 import info.magnolia.module.InstallContext;
40
41 import java.io.IOException;
42 import java.io.InputStream;
43
44 import javax.jcr.ImportUUIDBehavior;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.Session;
47
48 import org.apache.commons.lang3.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56 @Deprecated
57 public class PartialBootstrapTask extends AbstractTask {
58
59 private static Logger log = LoggerFactory.getLogger(PartialBootstrapTask.class);
60
61 private final String resource;
62 private final String itemName;
63 private final String itemPath;
64 private final int importUUIDBehavior;
65
66 private String targetResource;
67 static private final int defaultImportUUIDBehavior = ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW;
68
69 public PartialBootstrapTask(String name, String resource, String itemPath) {
70 this(name, resource, itemPath, defaultImportUUIDBehavior);
71 }
72
73 public PartialBootstrapTask(String name, String resource, String itemPath, int importUUIDBehavior) {
74 this(name, String.format("Bootstrap part '%s' of resource '%s'.", itemPath, resource),
75 resource, itemPath, importUUIDBehavior);
76 }
77
78
79
80
81
82
83
84 public PartialBootstrapTask(String name, String description, String resource, String itemPath) {
85 this(name, description, resource, itemPath, defaultImportUUIDBehavior);
86 }
87
88
89
90
91
92
93
94
95 public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource) {
96 this(name, description, resource, itemPath, targetResource, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
97 }
98
99 public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource, int importUUIDBehavior) {
100 this(name, description, resource, itemPath, importUUIDBehavior);
101 this.targetResource = targetResource;
102 }
103
104 public PartialBootstrapTask(String name, String description, String resource, String itemPath, int importUUIDBehavior) {
105 super(name, description);
106
107 this.importUUIDBehavior = importUUIDBehavior;
108 this.resource = resource;
109 this.itemPath = StringUtils.chomp(itemPath, "/").replace(".", "..");
110 this.itemName = StringUtils.substringAfterLast(itemPath, "/");
111 }
112
113 @Override
114 public void execute(InstallContext ctx) throws TaskExecutionException {
115
116 try {
117 String outputResourceName = getOutputResourceName(resource, itemPath);
118
119 bootstrap(outputResourceName, itemName, importUUIDBehavior, getNodeStream(resource, itemPath));
120
121 } catch (IOException e) {
122 throw new TaskExecutionException("Cant find resource file");
123 } catch (RepositoryException e) {
124 throw new TaskExecutionException("Cant bootstrap resource file");
125 }
126
127 }
128
129 protected InputStream getNodeStream(String fileName, String nodePath) {
130
131 return BootstrapFileUtil.getElementAsStream(fileName, nodePath);
132
133 }
134
135 protected void bootstrap(String resourceName, String itemName, int importUUIDBehavior, InputStream stream) throws IOException, RepositoryException {
136
137
138 String name = BootstrapUtil.getFilenameFromResource(resourceName, ".xml");
139 String repository = BootstrapUtil.getWorkspaceNameFromResource(resourceName);
140 String pathName = BootstrapUtil.getPathnameFromResource(resourceName);
141 String fullPath = BootstrapUtil.getFullpathFromResource(resourceName);
142
143 log.debug("Will bootstrap {}", resourceName);
144 if (stream == null) {
145 throw new IOException("Can't find resource to bootstrap at " + resourceName);
146 }
147
148
149 try {
150 final Session session = MgnlContext.getJCRSession(repository);
151
152
153 if (session != null && session.nodeExists(fullPath)) {
154 session.removeItem(fullPath);
155 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
156 }
157 } catch (RepositoryException e) {
158 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
159 }
160
161 DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
162 }
163
164 protected String getOutputResourceName(final String resource, final String itemPath) {
165
166 String inputResourceName = BootstrapUtil.getFilenameFromResource(resource, ".xml");
167
168 String tmpitemPath = itemPath.replace("/", ".");
169
170 tmpitemPath = StringUtils.removeStart(tmpitemPath, ".");
171 tmpitemPath = StringUtils.substringAfter(tmpitemPath, ".");
172 String outputResourceName = inputResourceName + "." + tmpitemPath;
173 if (StringUtils.isNotEmpty(targetResource)) {
174 outputResourceName = targetResource;
175 }
176 return outputResourceName;
177 }
178
179 protected String getResource() {
180 return resource;
181 }
182
183 protected String getItemName() {
184 return itemName;
185 }
186
187 protected String getItemPath() {
188 return itemPath;
189 }
190
191 }