View Javadoc
1   /**
2    * This file Copyright (c) 2007-2018 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.module.delta;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.importexport.BootstrapUtil;
38  import info.magnolia.importexport.DataTransporter;
39  import info.magnolia.module.InstallContext;
40  
41  import java.io.IOException;
42  import java.io.InputStream;
43  
44  import javax.jcr.ImportUUIDBehavior;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Session;
47  
48  import org.apache.commons.lang3.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * This task is used to bootstrap a part of a file.
54   * @deprecated since 5.4.4. Use {@link info.magnolia.module.delta.BootstrapSingleModuleResource#BootstrapSingleModuleResource(String, String, String, String)}
55   */
56  @Deprecated
57  public class PartialBootstrapTask extends AbstractTask {
58  
59      private static Logger log = LoggerFactory.getLogger(PartialBootstrapTask.class);
60  
61      private final String resource;
62      private final String itemName;
63      private final String itemPath;
64      private final int importUUIDBehavior;
65  
66      private String targetResource;
67      static private final int defaultImportUUIDBehavior = ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW;
68  
69      public PartialBootstrapTask(String name, String resource, String itemPath) {
70          this(name, resource, itemPath, defaultImportUUIDBehavior);
71      }
72  
73      public PartialBootstrapTask(String name, String resource, String itemPath, int importUUIDBehavior) {
74          this(name, String.format("Bootstrap part '%s' of resource '%s'.", itemPath, resource),
75                  resource, itemPath, importUUIDBehavior);
76      }
77  
78      /**
79       * Bootstraps fragment of file.
80       *
81       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
82       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
83       */
84      public PartialBootstrapTask(String name, String description, String resource, String itemPath) {
85          this(name, description, resource, itemPath, defaultImportUUIDBehavior);
86      }
87  
88      /**
89       * Bootstraps newly created file.
90       *
91       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
92       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
93       * @param targetResource - A target bootstrap file name in case you want to bootstrap the file in a different node /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.target.xml
94       */
95      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource) {
96          this(name, description, resource, itemPath, targetResource, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
97      }
98  
99      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource, int importUUIDBehavior) {
100         this(name, description, resource, itemPath, importUUIDBehavior);
101         this.targetResource = targetResource;
102     }
103 
104     public PartialBootstrapTask(String name, String description, String resource, String itemPath, int importUUIDBehavior) {
105         super(name, description);
106 
107         this.importUUIDBehavior = importUUIDBehavior;
108         this.resource = resource;
109         this.itemPath = StringUtils.chomp(itemPath, "/").replace(".", "..");
110         this.itemName = StringUtils.substringAfterLast(itemPath, "/");
111     }
112 
113     @Override
114     public void execute(InstallContext ctx) throws TaskExecutionException {
115 
116         try {
117             String outputResourceName = getOutputResourceName(resource, itemPath);
118             //bootstrap
119             bootstrap(outputResourceName, itemName, importUUIDBehavior, getNodeStream(resource, itemPath));
120 
121         } catch (IOException e) {
122             throw new TaskExecutionException("Cant find resource file");
123         } catch (RepositoryException e) {
124             throw new TaskExecutionException("Cant bootstrap resource file");
125         }
126 
127     }
128 
129     protected InputStream getNodeStream(String fileName, String nodePath) {
130 
131         return BootstrapFileUtil.getElementAsStream(fileName, nodePath);
132 
133     }
134 
135     protected void bootstrap(String resourceName, String itemName, int importUUIDBehavior, InputStream stream) throws IOException, RepositoryException {
136         //TODO: Code partially copied from BootstrapUtil class, need to refactor the core class to accept Inputstreams
137 
138         String name = BootstrapUtil.getFilenameFromResource(resourceName, ".xml");
139         String repository = BootstrapUtil.getWorkspaceNameFromResource(resourceName);
140         String pathName = BootstrapUtil.getPathnameFromResource(resourceName);
141         String fullPath = BootstrapUtil.getFullpathFromResource(resourceName);
142 
143         log.debug("Will bootstrap {}", resourceName);
144         if (stream == null) {
145             throw new IOException("Can't find resource to bootstrap at " + resourceName);
146         }
147 
148         // if the path already exists --> delete it
149         try {
150             final Session session = MgnlContext.getJCRSession(repository);
151 
152             // hm can be null if module is not properly registered and the repository has not been created
153             if (session != null && session.nodeExists(fullPath)) {
154                 session.removeItem(fullPath);
155                 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
156             }
157         } catch (RepositoryException e) {
158             throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
159         }
160 
161         DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
162     }
163 
164     protected String getOutputResourceName(final String resource, final String itemPath) {
165         // get name as config.modules.xxx
166         String inputResourceName = BootstrapUtil.getFilenameFromResource(resource, ".xml");
167         //replacing all "/" with "."; getting string after first node
168         String tmpitemPath = itemPath.replace("/", ".");
169 
170         tmpitemPath = StringUtils.removeStart(tmpitemPath, ".");
171         tmpitemPath = StringUtils.substringAfter(tmpitemPath, ".");
172         String outputResourceName = inputResourceName + "." + tmpitemPath;
173         if (StringUtils.isNotEmpty(targetResource)) {
174             outputResourceName = targetResource;
175         }
176         return outputResourceName;
177     }
178 
179     protected String getResource() {
180         return resource;
181     }
182 
183     protected String getItemName() {
184         return itemName;
185     }
186 
187     protected String getItemPath() {
188         return itemPath;
189     }
190 
191 }