View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.importexport;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.util.SiblingsHelper;
39  import info.magnolia.cms.util.StringLengthComparator;
40  import info.magnolia.context.MgnlContext;
41  
42  import java.io.File;
43  import java.io.FileOutputStream;
44  import java.io.IOException;
45  import java.io.InputStream;
46  import java.util.ArrayList;
47  import java.util.Arrays;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  import javax.jcr.RepositoryException;
53  
54  import org.apache.commons.io.IOUtils;
55  import org.apache.commons.lang.StringUtils;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  
60  /**
61   *
62   * @author philipp
63   * @version $Revision: 32667 $ ($Author: gjoseph $)
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          // sort by length --> import parent node first
70          List list = new ArrayList(Arrays.asList(resourceNames));
71  
72          Collections.sort(list, new StringLengthComparator());
73  
74          for (Iterator iter = list.iterator(); iter.hasNext();) {
75              String resourceName = (String) iter.next();
76  
77              // windows again
78              resourceName = StringUtils.replace(resourceName, "\\", "/");
79  
80              String name = StringUtils.removeEnd(StringUtils.substringAfterLast(resourceName, "/"), ".xml");
81  
82              String repository = StringUtils.substringBefore(name, ".");
83              String pathName = StringUtils.substringAfter(StringUtils.substringBeforeLast(name, "."), "."); //$NON-NLS-1$
84              String nodeName = StringUtils.substringAfterLast(name, ".");
85              String fullPath;
86              if (StringUtils.isEmpty(pathName)) {
87                  pathName = "/";
88                  fullPath = "/" + nodeName;
89              }
90              else {
91                  pathName = "/" + StringUtils.replace(pathName, ".", "/");
92                  fullPath = pathName + "/" + nodeName;
93              }
94  
95              log.debug("Will bootstrap {}", resourceName);
96              final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
97              if (stream == null) {
98                  throw new IOException("Can't find resource to bootstrap at " + resourceName);
99              }
100 
101             // if the node already exists we will keep the order
102             String nameOfNodeAfterTheImportedNode = null;
103             
104             final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
105 
106             // if the path already exists --> delete it
107             try {
108 
109                 // hm can be null if module is not properly registered and the repository has not been created
110                 if (hm != null && hm.isExist(fullPath)) {
111                     // but keep the order
112                     Content node = hm.getContent(fullPath);
113                     SiblingsHelper siblings = SiblingsHelper.of(node);
114                     if(!siblings.isLast()){
115                         nameOfNodeAfterTheImportedNode = siblings.next().getName();
116                     }
117                     
118                     hm.delete(fullPath);
119                     log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
120                 }
121             } catch (RepositoryException e) {
122                 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
123             }
124 
125             DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
126         
127             if(nameOfNodeAfterTheImportedNode != null){
128                 Content newNode = hm.getContent(fullPath);
129                 newNode.getParent().orderBefore(nodeName, nameOfNodeAfterTheImportedNode);
130             }
131         
132         }
133     }
134 
135     public static void export(Content content, File directory) throws IOException, RepositoryException{
136         String fileName = content.getHierarchyManager().getName() + content.getHandle().replace("/", ".") + ".xml";
137         File file = new File(directory, fileName);
138         FileOutputStream out = new FileOutputStream(file);
139         try{
140             DataTransporter.executeExport(out,false, true, content.getWorkspace().getSession(), content.getHandle(), content.getHierarchyManager().getName(), DataTransporter.XML);
141         }
142         finally{
143             IOUtils.closeQuietly(out);
144         }
145     }
146 
147 }