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.importexport;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.Path;
38 import info.magnolia.cms.util.StringLengthComparator;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.jcr.util.NodeTypes;
41 import info.magnolia.jcr.util.NodeUtil;
42
43 import java.io.File;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.Iterator;
51 import java.util.List;
52
53 import javax.jcr.Node;
54 import javax.jcr.RepositoryException;
55 import javax.jcr.Session;
56
57 import org.apache.commons.io.IOUtils;
58 import org.apache.commons.lang.StringUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62
63
64
65 public class BootstrapUtil {
66 private static final Logger log = LoggerFactory.getLogger(BootstrapUtil.class);
67
68 public static void bootstrap(String[] resourceNames, int importUUIDBehavior) throws IOException, RepositoryException {
69
70 List<String> list = new ArrayList<String>(Arrays.asList(resourceNames));
71
72 Collections.sort(list, new StringLengthComparator());
73
74 for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
75 String resourceName = iter.next();
76
77 String name = getFilenameFromResource(resourceName, ".xml");
78 String workspace = getWorkspaceNameFromResource(resourceName);
79 String pathName = getPathnameFromResource(resourceName);
80 String fullPath = getFullpathFromResource(resourceName);
81
82 log.debug("Will bootstrap {}", resourceName);
83
84 final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
85 if (stream == null) {
86 throw new IOException("Can't find resource to bootstrap at " + resourceName);
87 }
88
89
90 String nameOfNodeAfterTheImportedNode = null;
91
92 final Session session = MgnlContext.getJCRSession(workspace);
93
94 try {
95
96
97 if (session != null && StringUtils.isNotEmpty(fullPath) && session.nodeExists(fullPath)) {
98
99 Node node = session.getNode(fullPath);
100 if (!NodeUtil.isLastSibling(node)) {
101 nameOfNodeAfterTheImportedNode = NodeUtil.getSiblingAfter(node).getName();
102 }
103 if (node.getDepth() == 1) {
104 Node parent = node.getParent();
105 Node tmp = NodeUtil.createPath(parent, Path.getUniqueLabel(parent, "tmp"), NodeTypes.Content.NAME);
106 NodeUtil.moveNode(node, tmp);
107 tmp.remove();
108 } else {
109 node.remove();
110 }
111 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
112 }
113 } catch (RepositoryException e) {
114 IOUtils.closeQuietly(stream);
115 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
116 }
117 DataTransporter.importXmlStream(stream, workspace, pathName, name, false, importUUIDBehavior, false, true);
118
119 if (nameOfNodeAfterTheImportedNode != null) {
120 Node newNode = session.getNode(fullPath);
121 NodeUtil.orderBefore(newNode, nameOfNodeAfterTheImportedNode);
122 }
123
124 }
125 }
126
127
128
129
130 public static void export(Content content, File directory) throws IOException, RepositoryException {
131 BootstrapUtil.export(content.getJCRNode(), directory);
132 }
133
134 public static void export(Node content, File directory) throws IOException, RepositoryException {
135
136 Session session = content.getSession();
137 String workspace = session.getWorkspace().getName();
138 String path = content.getPath();
139 String fileName = workspace + DataTransporter.createExportPath(path) + ".xml";
140 File file = new File(directory, fileName);
141 FileOutputStream out = new FileOutputStream(file);
142 try {
143 DataTransporter.executeExport(out, false, true, session, path, workspace, DataTransporter.XML);
144 } finally {
145 IOUtils.closeQuietly(out);
146 }
147 }
148
149
150
151
152 public static String getWorkspaceNameFromResource(final String resourcePath) {
153 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
154
155 String name = getFilenameFromResource(resourceName, ".xml");
156 String fullPath = DataTransporter.revertExportPath(name);
157 return StringUtils.substringBefore(fullPath, "/");
158 }
159
160
161
162
163 public static String getFullpathFromResource(final String resourcePath) {
164 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
165
166 String name = getFilenameFromResource(resourceName, ".xml");
167 String fullPath = DataTransporter.revertExportPath(name);
168 String repository = StringUtils.substringBefore(fullPath, "/");
169
170 return StringUtils.removeStart(fullPath, repository);
171 }
172
173
174
175
176 public static String getPathnameFromResource(final String resourcePath) {
177 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
178
179 String name = getFilenameFromResource(resourceName, ".xml");
180 String fullPath = DataTransporter.revertExportPath(name);
181 String pathName = StringUtils.substringAfter(StringUtils.substringBeforeLast(fullPath, "/"), "/");
182
183 if (!pathName.startsWith("/")) {
184 pathName = "/" + pathName;
185 }
186 return pathName;
187 }
188
189
190
191
192
193 public static String getFilenameFromResource(final String resourcePath, final String extension) {
194 String ext = StringUtils.defaultIfEmpty(extension, ".xml");
195 String tmpResourcePath = resourcePath;
196 if (resourcePath.contains("/")) {
197 tmpResourcePath = StringUtils.substringAfterLast(resourcePath, "/");
198 }
199 return StringUtils.removeEnd(tmpResourcePath, ext.startsWith(".") ? ext : "." + ext);
200 }
201
202 }