View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * DelegatingPageManager
33   * 
34   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35   * @version $Id: $
36   */
37  
38  public class PageImporter
39  {
40      /* source page manager impl */
41      private PageManager sourceManager;
42      /* destination page manager impl */
43      private PageManager destManager;    
44      /* rootFolder to start importing from */
45      private String rootFolder;
46      /* flag: overwrite folders during import */
47      private boolean overwriteFolders = false;
48      /* flag: overwrite pages during import */ 
49      private boolean overwritePages = true;
50      /* count of total folders imported */    
51      private int folderCount = 0;    
52      /* count of total pages imported */
53      private int pageCount = 0;
54      /* count of total links imported */
55      private int linkCount = 0;
56      
57      public static void main(String args[])
58      {
59          String fileName = System.getProperty("org.apache.jetspeed.page.import.configuration", "import.properties");
60          PropertiesConfiguration configuration = new PropertiesConfiguration();
61          try
62          {
63              configuration.load(fileName);        
64              String [] bootAssemblies = configuration.getStringArray("boot.assemblies");
65              String [] assemblies = configuration.getStringArray("assemblies");
66              ClassPathXmlApplicationContext ctx;            
67              
68              if (bootAssemblies != null)
69              {
70                  ApplicationContext bootContext = new ClassPathXmlApplicationContext(bootAssemblies, true);
71                  ctx = new ClassPathXmlApplicationContext(assemblies, true, bootContext);
72              }
73              else
74              {
75                  ctx = new ClassPathXmlApplicationContext(assemblies, true);
76              }
77              
78              String rootFolder = configuration.getString("root.folder", "/");
79              boolean overwriteFolders = configuration.getBoolean("overwrite.folders", true);
80              boolean overwritePages = configuration.getBoolean("overwrite.pages", true);
81              boolean fullImport = configuration.getBoolean("full.import", true);
82              String sourcePageManager = configuration.getString("source.page.manager", "castorPageManager");
83              String destPageManager = configuration.getString("dest.page.manager", "dbPageManager");
84              
85              PageManager srcManager = (PageManager)ctx.getBean(sourcePageManager);
86              PageManager dstManager = (PageManager)ctx.getBean(destPageManager);
87              PageImporter importer = new PageImporter(srcManager, dstManager, rootFolder, overwriteFolders, overwritePages);
88              if (fullImport)
89              {
90                  importer.fullImport();
91              }
92              else
93              {
94                  importer.folderTreeImport();
95              }
96          }
97          catch (Exception e)
98          {
99              System.err.println("Failed to import: " + e);
100             e.printStackTrace();
101         }
102         
103     }
104     
105     public PageImporter(PageManager sourceManager, 
106                         PageManager destManager, 
107                         String rootFolder,
108                         boolean overwriteFolders,
109                         boolean overwritePages)
110     {
111         this.sourceManager = sourceManager;
112         this.destManager = destManager;
113         this.rootFolder = rootFolder;
114         this.overwriteFolders = overwriteFolders;
115         this.overwritePages = overwritePages;
116     }
117         
118     public void fullImport()
119     throws JetspeedException
120     {
121         Folder fsRoot = sourceManager.getFolder(rootFolder);                
122         importFolder(fsRoot);
123         
124         
125         // create the root page security
126         PageSecurity sourcePageSecurity = null;
127         try
128         {
129             sourcePageSecurity = sourceManager.getPageSecurity();
130         }
131         catch (DocumentNotFoundException e)
132         {
133             // skip over it, not found
134         }
135         
136         if (sourcePageSecurity != null)
137         {
138             PageSecurity rootSecurity = destManager.copyPageSecurity(sourcePageSecurity);        
139             destManager.updatePageSecurity(rootSecurity);
140         }
141     }
142 
143     public void folderTreeImport()
144     throws JetspeedException
145     {
146         Folder fsRoot = sourceManager.getFolder(rootFolder);                
147         importFolder(fsRoot);                            
148     }
149     
150     private Folder importFolder(Folder srcFolder)
151     throws JetspeedException
152     {
153         Folder dstFolder = lookupFolder(srcFolder.getPath());        
154         if (null != dstFolder)
155         {
156             if (isOverwriteFolders())
157             {
158                 System.out.println("overwriting folder " + srcFolder.getPath());
159                 destManager.removeFolder(dstFolder);
160                 dstFolder = destManager
161                         .copyFolder(srcFolder, srcFolder.getPath());
162                 destManager.updateFolder(dstFolder);
163                 folderCount++;
164 
165             } else
166                 System.out.println("skipping folder " + srcFolder.getPath());
167         } else
168         {
169             System.out.println("importing new folder " + srcFolder.getPath());
170             dstFolder = destManager.copyFolder(srcFolder, srcFolder.getPath());
171             destManager.updateFolder(dstFolder);
172             folderCount++;
173         }
174         Iterator pages = srcFolder.getPages().iterator();
175         while (pages.hasNext())
176         {
177             Page srcPage = (Page)pages.next();
178             Page dstPage = lookupPage(srcPage.getPath());
179             if (null != dstPage)
180             {
181                 if (isOverwritePages())
182                 {
183                     System.out.println("overwriting page " + srcPage.getPath());                            
184                     destManager.removePage(dstPage);
185                     dstPage = destManager.copyPage(srcPage, srcPage.getPath());
186                     destManager.updatePage(dstPage);
187                     pageCount++;                    
188                 }
189                 else
190                     System.out.println("skipping page " + srcPage.getPath());                
191             }
192             else            
193             {
194                 System.out.println("importing new page " + srcPage.getPath());
195                 dstPage = destManager.copyPage(srcPage, srcPage.getPath());
196                 destManager.updatePage(dstPage);
197                 pageCount++;
198             }
199         }
200 
201         Iterator links = srcFolder.getLinks().iterator();
202         while (links.hasNext())
203         {
204             Link srcLink = (Link)links.next();
205             Link dstLink = lookupLink(srcLink.getPath());
206             if (null != dstLink)
207             {
208                 if (isOverwritePages())
209                 {
210                     System.out.println("overwriting link " + srcLink.getPath());                            
211                     destManager.removeLink(dstLink);
212                     dstLink = destManager.copyLink(srcLink, srcLink.getPath());
213                     destManager.updateLink(dstLink);
214                     linkCount++;                    
215                 }
216                 else
217                     System.out.println("skipping link " + srcLink.getPath());                
218             }
219             else            
220             {
221                 System.out.println("importing new link " + srcLink.getPath());
222                 dstLink = destManager.copyLink(srcLink, srcLink.getPath());
223                 destManager.updateLink(dstLink);
224                 linkCount++;
225             }
226         }
227         
228         Iterator folders = srcFolder.getFolders().iterator();
229         while (folders.hasNext())
230         {
231             Folder folder = (Folder)folders.next();
232             importFolder(folder);
233         }
234         
235         return dstFolder;
236     }
237     
238     private Page lookupPage(String path)
239     {
240         try
241         {
242             return destManager.getPage(path);
243         }
244         catch (Exception e)
245         {
246             return null;
247         }
248     }
249     
250     private Link lookupLink(String path)
251     {
252         try
253         {
254             return destManager.getLink(path);
255         }
256         catch (Exception e)
257         {
258             return null;
259         }
260     }
261     
262     private Folder lookupFolder(String path)
263     {
264         try
265         {
266             return destManager.getFolder(path);
267         }
268         catch (Exception e)
269         {
270             return null;
271         }
272     }
273     
274     /***
275      * @return Returns the overwrite.
276      */
277     public boolean isOverwriteFolders()
278     {
279         return overwriteFolders;
280     }
281     /***
282      * @param overwrite The overwrite to set.
283      */
284     public void setOverwriteFolders(boolean overwrite)
285     {
286         this.overwriteFolders = overwrite;
287     }
288     
289     /***
290      * @return Returns the destManager.
291      */
292     public PageManager getDestManager()
293     {
294         return destManager;
295     }
296     /***
297      * @param destManager The destManager to set.
298      */
299     public void setDestManager(PageManager destManager)
300     {
301         this.destManager = destManager;
302     }
303     /***
304      * @return Returns the folderCount.
305      */
306     public int getFolderCount()
307     {
308         return folderCount;
309     }
310     /***
311      * @param folderCount The folderCount to set.
312      */
313     public void setFolderCount(int folderCount)
314     {
315         this.folderCount = folderCount;
316     }
317     /***
318      * @return Returns the overwritePages.
319      */
320     public boolean isOverwritePages()
321     {
322         return overwritePages;
323     }
324     /***
325      * @param overwritePages The overwritePages to set.
326      */
327     public void setOverwritePages(boolean overwritePages)
328     {
329         this.overwritePages = overwritePages;
330     }
331     /***
332      * @return Returns the pageCount.
333      */
334     public int getPageCount()
335     {
336         return pageCount;
337     }
338     /***
339      * @param pageCount The pageCount to set.
340      */
341     public void setPageCount(int pageCount)
342     {
343         this.pageCount = pageCount;
344     }
345     /***
346      * @return Returns the linkCount.
347      */
348     public int getLinkCount()
349     {
350         return linkCount;
351     }
352     /***
353      * @param linkCount The linkCount to set.
354      */
355     public void setLinkCount(int linkCount)
356     {
357         this.linkCount = linkCount;
358     }
359     /***
360      * @return Returns the rootFolder.
361      */
362     public String getRootFolder()
363     {
364         return rootFolder;
365     }
366     /***
367      * @param rootFolder The rootFolder to set.
368      */
369     public void setRootFolder(String rootFolder)
370     {
371         this.rootFolder = rootFolder;
372     }
373     /***
374      * @return Returns the sourceManager.
375      */
376     public PageManager getSourceManager()
377     {
378         return sourceManager;
379     }
380     /***
381      * @param sourceManager The sourceManager to set.
382      */
383     public void setSourceManager(PageManager sourceManager)
384     {
385         this.sourceManager = sourceManager;
386     }
387 }