001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.page;
018:
019: import java.util.Iterator;
020:
021: import org.apache.commons.configuration.PropertiesConfiguration;
022: import org.apache.jetspeed.exception.JetspeedException;
023: import org.apache.jetspeed.om.folder.Folder;
024: import org.apache.jetspeed.om.page.Link;
025: import org.apache.jetspeed.om.page.Page;
026: import org.apache.jetspeed.om.page.PageSecurity;
027: import org.apache.jetspeed.page.document.DocumentNotFoundException;
028: import org.springframework.context.ApplicationContext;
029: import org.springframework.context.support.ClassPathXmlApplicationContext;
030:
031: /**
032: * DelegatingPageManager
033: *
034: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
035: * @version $Id: $
036: */
037:
038: public class PageImporter {
039: /* source page manager impl */
040: private PageManager sourceManager;
041: /* destination page manager impl */
042: private PageManager destManager;
043: /* rootFolder to start importing from */
044: private String rootFolder;
045: /* flag: overwrite folders during import */
046: private boolean overwriteFolders = false;
047: /* flag: overwrite pages during import */
048: private boolean overwritePages = true;
049: /* count of total folders imported */
050: private int folderCount = 0;
051: /* count of total pages imported */
052: private int pageCount = 0;
053: /* count of total links imported */
054: private int linkCount = 0;
055:
056: public static void main(String args[]) {
057: String fileName = System.getProperty(
058: "org.apache.jetspeed.page.import.configuration",
059: "import.properties");
060: PropertiesConfiguration configuration = new PropertiesConfiguration();
061: try {
062: configuration.load(fileName);
063: String[] bootAssemblies = configuration
064: .getStringArray("boot.assemblies");
065: String[] assemblies = configuration
066: .getStringArray("assemblies");
067: ClassPathXmlApplicationContext ctx;
068:
069: if (bootAssemblies != null) {
070: ApplicationContext bootContext = new ClassPathXmlApplicationContext(
071: bootAssemblies, true);
072: ctx = new ClassPathXmlApplicationContext(assemblies,
073: true, bootContext);
074: } else {
075: ctx = new ClassPathXmlApplicationContext(assemblies,
076: true);
077: }
078:
079: String rootFolder = configuration.getString("root.folder",
080: "/");
081: boolean overwriteFolders = configuration.getBoolean(
082: "overwrite.folders", true);
083: boolean overwritePages = configuration.getBoolean(
084: "overwrite.pages", true);
085: boolean fullImport = configuration.getBoolean(
086: "full.import", true);
087: String sourcePageManager = configuration.getString(
088: "source.page.manager", "castorPageManager");
089: String destPageManager = configuration.getString(
090: "dest.page.manager", "dbPageManager");
091:
092: PageManager srcManager = (PageManager) ctx
093: .getBean(sourcePageManager);
094: PageManager dstManager = (PageManager) ctx
095: .getBean(destPageManager);
096: PageImporter importer = new PageImporter(srcManager,
097: dstManager, rootFolder, overwriteFolders,
098: overwritePages);
099: if (fullImport) {
100: importer.fullImport();
101: } else {
102: importer.folderTreeImport();
103: }
104: } catch (Exception e) {
105: System.err.println("Failed to import: " + e);
106: e.printStackTrace();
107: }
108:
109: }
110:
111: public PageImporter(PageManager sourceManager,
112: PageManager destManager, String rootFolder,
113: boolean overwriteFolders, boolean overwritePages) {
114: this .sourceManager = sourceManager;
115: this .destManager = destManager;
116: this .rootFolder = rootFolder;
117: this .overwriteFolders = overwriteFolders;
118: this .overwritePages = overwritePages;
119: }
120:
121: public void fullImport() throws JetspeedException {
122: Folder fsRoot = sourceManager.getFolder(rootFolder);
123: importFolder(fsRoot);
124:
125: // create the root page security
126: PageSecurity sourcePageSecurity = null;
127: try {
128: sourcePageSecurity = sourceManager.getPageSecurity();
129: } catch (DocumentNotFoundException e) {
130: // skip over it, not found
131: }
132:
133: if (sourcePageSecurity != null) {
134: PageSecurity rootSecurity = destManager
135: .copyPageSecurity(sourcePageSecurity);
136: destManager.updatePageSecurity(rootSecurity);
137: }
138: }
139:
140: public void folderTreeImport() throws JetspeedException {
141: Folder fsRoot = sourceManager.getFolder(rootFolder);
142: importFolder(fsRoot);
143: }
144:
145: private Folder importFolder(Folder srcFolder)
146: throws JetspeedException {
147: Folder dstFolder = lookupFolder(srcFolder.getPath());
148: if (null != dstFolder) {
149: if (isOverwriteFolders()) {
150: System.out.println("overwriting folder "
151: + srcFolder.getPath());
152: destManager.removeFolder(dstFolder);
153: dstFolder = destManager.copyFolder(srcFolder, srcFolder
154: .getPath());
155: destManager.updateFolder(dstFolder);
156: folderCount++;
157:
158: } else
159: System.out.println("skipping folder "
160: + srcFolder.getPath());
161: } else {
162: System.out.println("importing new folder "
163: + srcFolder.getPath());
164: dstFolder = destManager.copyFolder(srcFolder, srcFolder
165: .getPath());
166: destManager.updateFolder(dstFolder);
167: folderCount++;
168: }
169: Iterator pages = srcFolder.getPages().iterator();
170: while (pages.hasNext()) {
171: Page srcPage = (Page) pages.next();
172: Page dstPage = lookupPage(srcPage.getPath());
173: if (null != dstPage) {
174: if (isOverwritePages()) {
175: System.out.println("overwriting page "
176: + srcPage.getPath());
177: destManager.removePage(dstPage);
178: dstPage = destManager.copyPage(srcPage, srcPage
179: .getPath());
180: destManager.updatePage(dstPage);
181: pageCount++;
182: } else
183: System.out.println("skipping page "
184: + srcPage.getPath());
185: } else {
186: System.out.println("importing new page "
187: + srcPage.getPath());
188: dstPage = destManager.copyPage(srcPage, srcPage
189: .getPath());
190: destManager.updatePage(dstPage);
191: pageCount++;
192: }
193: }
194:
195: Iterator links = srcFolder.getLinks().iterator();
196: while (links.hasNext()) {
197: Link srcLink = (Link) links.next();
198: Link dstLink = lookupLink(srcLink.getPath());
199: if (null != dstLink) {
200: if (isOverwritePages()) {
201: System.out.println("overwriting link "
202: + srcLink.getPath());
203: destManager.removeLink(dstLink);
204: dstLink = destManager.copyLink(srcLink, srcLink
205: .getPath());
206: destManager.updateLink(dstLink);
207: linkCount++;
208: } else
209: System.out.println("skipping link "
210: + srcLink.getPath());
211: } else {
212: System.out.println("importing new link "
213: + srcLink.getPath());
214: dstLink = destManager.copyLink(srcLink, srcLink
215: .getPath());
216: destManager.updateLink(dstLink);
217: linkCount++;
218: }
219: }
220:
221: Iterator folders = srcFolder.getFolders().iterator();
222: while (folders.hasNext()) {
223: Folder folder = (Folder) folders.next();
224: importFolder(folder);
225: }
226:
227: return dstFolder;
228: }
229:
230: private Page lookupPage(String path) {
231: try {
232: return destManager.getPage(path);
233: } catch (Exception e) {
234: return null;
235: }
236: }
237:
238: private Link lookupLink(String path) {
239: try {
240: return destManager.getLink(path);
241: } catch (Exception e) {
242: return null;
243: }
244: }
245:
246: private Folder lookupFolder(String path) {
247: try {
248: return destManager.getFolder(path);
249: } catch (Exception e) {
250: return null;
251: }
252: }
253:
254: /**
255: * @return Returns the overwrite.
256: */
257: public boolean isOverwriteFolders() {
258: return overwriteFolders;
259: }
260:
261: /**
262: * @param overwrite The overwrite to set.
263: */
264: public void setOverwriteFolders(boolean overwrite) {
265: this .overwriteFolders = overwrite;
266: }
267:
268: /**
269: * @return Returns the destManager.
270: */
271: public PageManager getDestManager() {
272: return destManager;
273: }
274:
275: /**
276: * @param destManager The destManager to set.
277: */
278: public void setDestManager(PageManager destManager) {
279: this .destManager = destManager;
280: }
281:
282: /**
283: * @return Returns the folderCount.
284: */
285: public int getFolderCount() {
286: return folderCount;
287: }
288:
289: /**
290: * @param folderCount The folderCount to set.
291: */
292: public void setFolderCount(int folderCount) {
293: this .folderCount = folderCount;
294: }
295:
296: /**
297: * @return Returns the overwritePages.
298: */
299: public boolean isOverwritePages() {
300: return overwritePages;
301: }
302:
303: /**
304: * @param overwritePages The overwritePages to set.
305: */
306: public void setOverwritePages(boolean overwritePages) {
307: this .overwritePages = overwritePages;
308: }
309:
310: /**
311: * @return Returns the pageCount.
312: */
313: public int getPageCount() {
314: return pageCount;
315: }
316:
317: /**
318: * @param pageCount The pageCount to set.
319: */
320: public void setPageCount(int pageCount) {
321: this .pageCount = pageCount;
322: }
323:
324: /**
325: * @return Returns the linkCount.
326: */
327: public int getLinkCount() {
328: return linkCount;
329: }
330:
331: /**
332: * @param linkCount The linkCount to set.
333: */
334: public void setLinkCount(int linkCount) {
335: this .linkCount = linkCount;
336: }
337:
338: /**
339: * @return Returns the rootFolder.
340: */
341: public String getRootFolder() {
342: return rootFolder;
343: }
344:
345: /**
346: * @param rootFolder The rootFolder to set.
347: */
348: public void setRootFolder(String rootFolder) {
349: this .rootFolder = rootFolder;
350: }
351:
352: /**
353: * @return Returns the sourceManager.
354: */
355: public PageManager getSourceManager() {
356: return sourceManager;
357: }
358:
359: /**
360: * @param sourceManager The sourceManager to set.
361: */
362: public void setSourceManager(PageManager sourceManager) {
363: this.sourceManager = sourceManager;
364: }
365: }
|