1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.page; |
18 |
|
|
19 |
|
import java.util.Iterator; |
20 |
|
|
21 |
|
import org.apache.commons.configuration.PropertiesConfiguration; |
22 |
|
import org.apache.jetspeed.exception.JetspeedException; |
23 |
|
import org.apache.jetspeed.om.folder.Folder; |
24 |
|
import org.apache.jetspeed.om.page.Link; |
25 |
|
import org.apache.jetspeed.om.page.Page; |
26 |
|
import org.apache.jetspeed.om.page.PageSecurity; |
27 |
|
import org.apache.jetspeed.page.document.DocumentNotFoundException; |
28 |
|
import org.springframework.context.ApplicationContext; |
29 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class PageImporter |
39 |
|
{ |
40 |
|
|
41 |
|
private PageManager sourceManager; |
42 |
|
|
43 |
|
private PageManager destManager; |
44 |
|
|
45 |
|
private String rootFolder; |
46 |
|
|
47 |
0 |
private boolean overwriteFolders = false; |
48 |
|
|
49 |
0 |
private boolean overwritePages = true; |
50 |
|
|
51 |
0 |
private int folderCount = 0; |
52 |
|
|
53 |
0 |
private int pageCount = 0; |
54 |
|
|
55 |
0 |
private int linkCount = 0; |
56 |
|
|
57 |
|
public static void main(String args[]) |
58 |
|
{ |
59 |
0 |
String fileName = System.getProperty("org.apache.jetspeed.page.import.configuration", "import.properties"); |
60 |
0 |
PropertiesConfiguration configuration = new PropertiesConfiguration(); |
61 |
|
try |
62 |
|
{ |
63 |
0 |
configuration.load(fileName); |
64 |
0 |
String [] bootAssemblies = configuration.getStringArray("boot.assemblies"); |
65 |
0 |
String [] assemblies = configuration.getStringArray("assemblies"); |
66 |
|
ClassPathXmlApplicationContext ctx; |
67 |
|
|
68 |
0 |
if (bootAssemblies != null) |
69 |
|
{ |
70 |
0 |
ApplicationContext bootContext = new ClassPathXmlApplicationContext(bootAssemblies, true); |
71 |
0 |
ctx = new ClassPathXmlApplicationContext(assemblies, true, bootContext); |
72 |
0 |
} |
73 |
|
else |
74 |
|
{ |
75 |
0 |
ctx = new ClassPathXmlApplicationContext(assemblies, true); |
76 |
|
} |
77 |
|
|
78 |
0 |
String rootFolder = configuration.getString("root.folder", "/"); |
79 |
0 |
boolean overwriteFolders = configuration.getBoolean("overwrite.folders", true); |
80 |
0 |
boolean overwritePages = configuration.getBoolean("overwrite.pages", true); |
81 |
0 |
boolean fullImport = configuration.getBoolean("full.import", true); |
82 |
0 |
String sourcePageManager = configuration.getString("source.page.manager", "castorPageManager"); |
83 |
0 |
String destPageManager = configuration.getString("dest.page.manager", "dbPageManager"); |
84 |
|
|
85 |
0 |
PageManager srcManager = (PageManager)ctx.getBean(sourcePageManager); |
86 |
0 |
PageManager dstManager = (PageManager)ctx.getBean(destPageManager); |
87 |
0 |
PageImporter importer = new PageImporter(srcManager, dstManager, rootFolder, overwriteFolders, overwritePages); |
88 |
0 |
if (fullImport) |
89 |
|
{ |
90 |
0 |
importer.fullImport(); |
91 |
|
} |
92 |
|
else |
93 |
|
{ |
94 |
0 |
importer.folderTreeImport(); |
95 |
|
} |
96 |
|
} |
97 |
0 |
catch (Exception e) |
98 |
|
{ |
99 |
0 |
System.err.println("Failed to import: " + e); |
100 |
0 |
e.printStackTrace(); |
101 |
0 |
} |
102 |
|
|
103 |
0 |
} |
104 |
|
|
105 |
|
public PageImporter(PageManager sourceManager, |
106 |
|
PageManager destManager, |
107 |
|
String rootFolder, |
108 |
|
boolean overwriteFolders, |
109 |
|
boolean overwritePages) |
110 |
0 |
{ |
111 |
0 |
this.sourceManager = sourceManager; |
112 |
0 |
this.destManager = destManager; |
113 |
0 |
this.rootFolder = rootFolder; |
114 |
0 |
this.overwriteFolders = overwriteFolders; |
115 |
0 |
this.overwritePages = overwritePages; |
116 |
0 |
} |
117 |
|
|
118 |
|
public void fullImport() |
119 |
|
throws JetspeedException |
120 |
|
{ |
121 |
0 |
Folder fsRoot = sourceManager.getFolder(rootFolder); |
122 |
0 |
importFolder(fsRoot); |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
0 |
PageSecurity sourcePageSecurity = null; |
127 |
|
try |
128 |
|
{ |
129 |
0 |
sourcePageSecurity = sourceManager.getPageSecurity(); |
130 |
|
} |
131 |
0 |
catch (DocumentNotFoundException e) |
132 |
|
{ |
133 |
|
|
134 |
0 |
} |
135 |
|
|
136 |
0 |
if (sourcePageSecurity != null) |
137 |
|
{ |
138 |
0 |
PageSecurity rootSecurity = destManager.copyPageSecurity(sourcePageSecurity); |
139 |
0 |
destManager.updatePageSecurity(rootSecurity); |
140 |
|
} |
141 |
0 |
} |
142 |
|
|
143 |
|
public void folderTreeImport() |
144 |
|
throws JetspeedException |
145 |
|
{ |
146 |
0 |
Folder fsRoot = sourceManager.getFolder(rootFolder); |
147 |
0 |
importFolder(fsRoot); |
148 |
0 |
} |
149 |
|
|
150 |
|
private Folder importFolder(Folder srcFolder) |
151 |
|
throws JetspeedException |
152 |
|
{ |
153 |
0 |
Folder dstFolder = lookupFolder(srcFolder.getPath()); |
154 |
0 |
if (null != dstFolder) |
155 |
|
{ |
156 |
0 |
if (isOverwriteFolders()) |
157 |
|
{ |
158 |
0 |
System.out.println("overwriting folder " + srcFolder.getPath()); |
159 |
0 |
destManager.removeFolder(dstFolder); |
160 |
0 |
dstFolder = destManager |
161 |
|
.copyFolder(srcFolder, srcFolder.getPath()); |
162 |
0 |
destManager.updateFolder(dstFolder); |
163 |
0 |
folderCount++; |
164 |
|
|
165 |
|
} else |
166 |
0 |
System.out.println("skipping folder " + srcFolder.getPath()); |
167 |
|
} else |
168 |
|
{ |
169 |
0 |
System.out.println("importing new folder " + srcFolder.getPath()); |
170 |
0 |
dstFolder = destManager.copyFolder(srcFolder, srcFolder.getPath()); |
171 |
0 |
destManager.updateFolder(dstFolder); |
172 |
0 |
folderCount++; |
173 |
|
} |
174 |
0 |
Iterator pages = srcFolder.getPages().iterator(); |
175 |
0 |
while (pages.hasNext()) |
176 |
|
{ |
177 |
0 |
Page srcPage = (Page)pages.next(); |
178 |
0 |
Page dstPage = lookupPage(srcPage.getPath()); |
179 |
0 |
if (null != dstPage) |
180 |
|
{ |
181 |
0 |
if (isOverwritePages()) |
182 |
|
{ |
183 |
0 |
System.out.println("overwriting page " + srcPage.getPath()); |
184 |
0 |
destManager.removePage(dstPage); |
185 |
0 |
dstPage = destManager.copyPage(srcPage, srcPage.getPath()); |
186 |
0 |
destManager.updatePage(dstPage); |
187 |
0 |
pageCount++; |
188 |
|
} |
189 |
|
else |
190 |
0 |
System.out.println("skipping page " + srcPage.getPath()); |
191 |
|
} |
192 |
|
else |
193 |
|
{ |
194 |
0 |
System.out.println("importing new page " + srcPage.getPath()); |
195 |
0 |
dstPage = destManager.copyPage(srcPage, srcPage.getPath()); |
196 |
0 |
destManager.updatePage(dstPage); |
197 |
0 |
pageCount++; |
198 |
|
} |
199 |
0 |
} |
200 |
|
|
201 |
0 |
Iterator links = srcFolder.getLinks().iterator(); |
202 |
0 |
while (links.hasNext()) |
203 |
|
{ |
204 |
0 |
Link srcLink = (Link)links.next(); |
205 |
0 |
Link dstLink = lookupLink(srcLink.getPath()); |
206 |
0 |
if (null != dstLink) |
207 |
|
{ |
208 |
0 |
if (isOverwritePages()) |
209 |
|
{ |
210 |
0 |
System.out.println("overwriting link " + srcLink.getPath()); |
211 |
0 |
destManager.removeLink(dstLink); |
212 |
0 |
dstLink = destManager.copyLink(srcLink, srcLink.getPath()); |
213 |
0 |
destManager.updateLink(dstLink); |
214 |
0 |
linkCount++; |
215 |
|
} |
216 |
|
else |
217 |
0 |
System.out.println("skipping link " + srcLink.getPath()); |
218 |
|
} |
219 |
|
else |
220 |
|
{ |
221 |
0 |
System.out.println("importing new link " + srcLink.getPath()); |
222 |
0 |
dstLink = destManager.copyLink(srcLink, srcLink.getPath()); |
223 |
0 |
destManager.updateLink(dstLink); |
224 |
0 |
linkCount++; |
225 |
|
} |
226 |
0 |
} |
227 |
|
|
228 |
0 |
Iterator folders = srcFolder.getFolders().iterator(); |
229 |
0 |
while (folders.hasNext()) |
230 |
|
{ |
231 |
0 |
Folder folder = (Folder)folders.next(); |
232 |
0 |
importFolder(folder); |
233 |
0 |
} |
234 |
|
|
235 |
0 |
return dstFolder; |
236 |
|
} |
237 |
|
|
238 |
|
private Page lookupPage(String path) |
239 |
|
{ |
240 |
|
try |
241 |
|
{ |
242 |
0 |
return destManager.getPage(path); |
243 |
|
} |
244 |
0 |
catch (Exception e) |
245 |
|
{ |
246 |
0 |
return null; |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
private Link lookupLink(String path) |
251 |
|
{ |
252 |
|
try |
253 |
|
{ |
254 |
0 |
return destManager.getLink(path); |
255 |
|
} |
256 |
0 |
catch (Exception e) |
257 |
|
{ |
258 |
0 |
return null; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
private Folder lookupFolder(String path) |
263 |
|
{ |
264 |
|
try |
265 |
|
{ |
266 |
0 |
return destManager.getFolder(path); |
267 |
|
} |
268 |
0 |
catch (Exception e) |
269 |
|
{ |
270 |
0 |
return null; |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
public boolean isOverwriteFolders() |
278 |
|
{ |
279 |
0 |
return overwriteFolders; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
public void setOverwriteFolders(boolean overwrite) |
285 |
|
{ |
286 |
0 |
this.overwriteFolders = overwrite; |
287 |
0 |
} |
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
public PageManager getDestManager() |
293 |
|
{ |
294 |
0 |
return destManager; |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
public void setDestManager(PageManager destManager) |
300 |
|
{ |
301 |
0 |
this.destManager = destManager; |
302 |
0 |
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
public int getFolderCount() |
307 |
|
{ |
308 |
0 |
return folderCount; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
public void setFolderCount(int folderCount) |
314 |
|
{ |
315 |
0 |
this.folderCount = folderCount; |
316 |
0 |
} |
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
public boolean isOverwritePages() |
321 |
|
{ |
322 |
0 |
return overwritePages; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
public void setOverwritePages(boolean overwritePages) |
328 |
|
{ |
329 |
0 |
this.overwritePages = overwritePages; |
330 |
0 |
} |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
public int getPageCount() |
335 |
|
{ |
336 |
0 |
return pageCount; |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
public void setPageCount(int pageCount) |
342 |
|
{ |
343 |
0 |
this.pageCount = pageCount; |
344 |
0 |
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
public int getLinkCount() |
349 |
|
{ |
350 |
0 |
return linkCount; |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
public void setLinkCount(int linkCount) |
356 |
|
{ |
357 |
0 |
this.linkCount = linkCount; |
358 |
0 |
} |
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
public String getRootFolder() |
363 |
|
{ |
364 |
0 |
return rootFolder; |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
public void setRootFolder(String rootFolder) |
370 |
|
{ |
371 |
0 |
this.rootFolder = rootFolder; |
372 |
0 |
} |
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
public PageManager getSourceManager() |
377 |
|
{ |
378 |
0 |
return sourceManager; |
379 |
|
} |
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
public void setSourceManager(PageManager sourceManager) |
384 |
|
{ |
385 |
0 |
this.sourceManager = sourceManager; |
386 |
0 |
} |
387 |
|
} |