View Javadoc

1   /**
2    * This file Copyright (c) 2008-2014 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.templatingkit.setup;
35  
36  import java.io.IOException;
37  
38  import info.magnolia.cms.util.ClasspathResourcesUtil;
39  import info.magnolia.importexport.BootstrapUtil;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.delta.AbstractTask;
42  import info.magnolia.module.delta.TaskExecutionException;
43  import info.magnolia.module.resources.ResourceTypes;
44  import info.magnolia.module.resources.setup.InstallBinaryResourcesTask;
45  import info.magnolia.module.resources.setup.InstallTextResourceTask;
46  import info.magnolia.module.resources.setup.InstallTextResourcesTask;
47  import info.magnolia.module.templatingkit.resources.STKResourceModel;
48  
49  import javax.jcr.ImportUUIDBehavior;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  /**
55   * Installs all css files under <code>/templating-kit/themes/themeName/css</code> to the
56   * resources and the images under <code>/templating-kit/themes/themeName/img</code> to the
57   * dms while themeName is a property in the module descriptor.
58   * 
59   * @author pbracher
60   * @version $Id$
61   */
62  public class ThemeInstallTask extends AbstractTask {
63  
64      protected static final String THEME_NAME_PROPERTY = "themeName";
65      protected static final String DEFAULT_THEME_PATH_PATTERN = "/templating-kit/themes/%s/%s/.*";
66  
67      /**
68       * We have to warn if we overwrite everything during an update
69       * but be quiet if this is a fresh installation.
70       */
71      boolean isUpdate;
72      private boolean stripExtensions;
73  
74      public ThemeInstallTask() {
75          this(false);
76      }
77  
78      public ThemeInstallTask(boolean isUpdate) {
79          this(false, true);
80      }
81  
82      public ThemeInstallTask(boolean isUpdate, boolean stripExtensions) {
83          super("Install theme", "Installs css, js files and images.");
84          this.isUpdate = isUpdate;
85          this.stripExtensions = stripExtensions;
86      }
87  
88      // the tasks are not build in the constructor as we need the themeName from the module definition
89      @Override
90      public void execute(final InstallContext ctx) throws TaskExecutionException {
91          final String themeName = getThemeName(ctx);
92  
93          ResourceNameFilter filter = new ResourceNameFilter(ctx.getCurrentModuleDefinition().getName(), themeName);
94  
95          final String[] resourcesToBootstrap = ClasspathResourcesUtil.findResources(filter);
96  
97          try {
98              if (resourcesToBootstrap != null) {
99                  BootstrapUtil.bootstrap(resourcesToBootstrap, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
100             }
101         } catch (IOException e) {
102             throw new TaskExecutionException("Could not bootstrap: " + e.getMessage(), e);
103         } catch (RepositoryException e) {
104             throw new TaskExecutionException("Could not bootstrap: " + e.getMessage(), e);
105         }
106 
107         new InstallTextResourcesTask("", "", InstallTextResourceTask.DEFAULT_ENCODING, String.format(DEFAULT_THEME_PATH_PATTERN, themeName, ResourceTypes.CSS_SUFFIX),
108                 ResourceTypes.PROCESSED_CSS, true, STKResourceModel.class.getName(), false, stripExtensions).execute(ctx);
109 
110         new InstallTextResourcesTask("", "", InstallTextResourceTask.DEFAULT_ENCODING, String.format(DEFAULT_THEME_PATH_PATTERN, themeName, ResourceTypes.JS_SUFFIX),
111                 ResourceTypes.PROCESSED_JS, true, STKResourceModel.class.getName(), false, stripExtensions).execute(ctx);
112 
113         new InstallBinaryResourcesTask("", "", String.format(DEFAULT_THEME_PATH_PATTERN, themeName, "img"), false, stripExtensions).execute(ctx);
114 
115         if (isUpdate && resourcesToBootstrap != null) {
116             String aggNames = "";
117             for (String name : resourcesToBootstrap) {
118                 aggNames = aggNames + StringUtils.substringAfterLast(StringUtils.removeEnd(name, ".xml"), ".") + ", ";
119             }
120             ctx.warn("Theme(s) " + StringUtils.removeEnd(aggNames, ", ") + " have been overwritten completely");
121         }
122     }
123 
124     protected String getThemeName(InstallContext ctx) {
125         return ctx.getCurrentModuleDefinition().getProperty(THEME_NAME_PROPERTY);
126     }
127 
128     /**
129      * Filter allowing only theme xml files to pass.
130      */
131     public class ResourceNameFilter implements info.magnolia.cms.util.ClasspathResourcesUtil.Filter {
132 
133         private String moduleName;
134 
135         private String themeName;
136 
137         public ResourceNameFilter(String moduleName, String themeName) {
138             this.moduleName = moduleName;
139             this.themeName = themeName;
140         }
141 
142         @Override
143         public boolean accept(String name) {
144             return name.startsWith("/mgnl-bootstrap/" + this.moduleName + "/config.modules.standard-templating-kit.config.themes.") && name.endsWith(".xml") && name.contains(this.themeName);
145         }
146     }
147 }