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.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Path;
38 import info.magnolia.cms.core.SystemProperty;
39 import org.apache.commons.io.FileUtils;
40 import org.apache.commons.io.filefilter.FileFilterUtils;
41 import org.apache.commons.io.filefilter.IOFileFilter;
42 import org.apache.commons.lang.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.Iterator;
50 import java.util.Set;
51 import java.util.SortedSet;
52 import java.util.TreeSet;
53
54
55
56
57
58
59
60
61 public final class Bootstrapper {
62 private static final Logger log = LoggerFactory.getLogger(Bootstrapper.class);
63
64
65
66
67
68
69 public interface BootstrapFilter {
70
71 boolean accept(String filename);
72 }
73
74
75
76
77 private Bootstrapper() {
78
79 }
80
81
82
83
84
85
86
87
88
89 public static void bootstrapRepositories(String[] bootdirs, BootstrapFilter filter) {
90 log.info("Trying to initialize repositories from: \n {}", StringUtils.join(bootdirs, "\n "));
91
92 Iterator repositoryNames = ContentRepository.getAllRepositoryNames();
93 while (repositoryNames.hasNext()) {
94 String repositoryName = (String) repositoryNames.next();
95
96 if (!bootstrapRepository(bootdirs, repositoryName, filter)) {
97
98 break;
99 }
100
101 log.info("Repository [{}] has been initialized.", repositoryName);
102 }
103 }
104
105
106
107
108 public static boolean bootstrapRepository(String[] bootdirs, String repositoryName, BootstrapFilter filter) {
109 Set xmlfileset = getBootstrapFiles(bootdirs, repositoryName, filter);
110
111 if (xmlfileset.isEmpty()) {
112 log.debug("No bootstrap files found for repository [{}], skipping...", repositoryName);
113 return true;
114 }
115
116 log.info("Trying to import content from {} files into repository [{}]", Integer.toString(xmlfileset.size()), repositoryName);
117
118 final File[] files = (File[]) xmlfileset.toArray(new File[xmlfileset.size()]);
119 return bootstrapFiles(repositoryName, files);
120 }
121
122
123
124
125 private static boolean bootstrapFiles(String repositoryName, File[] files) {
126 try {
127 for (int k = 0; k < files.length; k++) {
128 File xmlFile = files[k];
129 log.debug("Importing {}", xmlFile);
130 DataTransporter.executeBootstrapImport(xmlFile, repositoryName);
131 }
132 }
133 catch (IOException ioe) {
134 log.error(ioe.getMessage(), ioe);
135 }
136 catch (OutOfMemoryError e) {
137 int maxMem = (int) (Runtime.getRuntime().maxMemory() / 1024 / 1024);
138 int needed = Math.max(256, maxMem + 128);
139 log.error("Unable to complete bootstrapping: out of memory.\n"
140 + "{} MB were not enough, try to increase the amount of memory available by adding the -Xmx{}m parameter to the server startup script.\n"
141 + "You will need to completely remove the Magnolia webapp before trying again",
142 Integer.toString(maxMem), Integer.toString(needed));
143 return false;
144 }
145 return true;
146 }
147
148
149
150
151
152
153
154
155 private static SortedSet getBootstrapFiles(String[] bootdirs, final String repositoryName, final BootstrapFilter filter) {
156 SortedSet xmlfileset = new TreeSet(new BootstrapFilesComparator());
157
158 for (int j = 0; j < bootdirs.length; j++) {
159 String bootdir = bootdirs[j];
160 File xmldir = new File(bootdir);
161 if (!xmldir.exists() || !xmldir.isDirectory()) {
162 continue;
163 }
164
165 Collection files = FileUtils.listFiles(xmldir, new IOFileFilter(){
166 public boolean accept(File file) {
167 return accept(file.getParentFile(), file.getName());
168 }
169 public boolean accept(File dir, String name) {
170 return name.startsWith(repositoryName + ".")
171 && filter.accept(name)
172 && (name.endsWith(DataTransporter.XML) || name.endsWith(DataTransporter.ZIP) || name
173 .endsWith(DataTransporter.GZ) || name.endsWith(DataTransporter.PROPERTIES));
174 }
175 }, FileFilterUtils.trueFileFilter());
176
177 xmlfileset.addAll(files);
178 }
179
180 return xmlfileset;
181 }
182
183
184
185
186
187 public static String[] getBootstrapDirs() {
188 String bootdirProperty = SystemProperty.getProperty(SystemProperty.MAGNOLIA_BOOTSTRAP_ROOTDIR);
189
190 if (StringUtils.isEmpty(bootdirProperty)) {
191 return new String[0];
192 }
193
194 String[] bootDirs = StringUtils.split(bootdirProperty);
195
196
197 for (int j = 0; j < bootDirs.length; j++) {
198 bootDirs[j] = Path.getAbsoluteFileSystemPath(bootDirs[j]);
199 }
200 return bootDirs;
201 }
202
203 }