View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.groovy.setup;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  
39  import javax.jcr.Node;
40  import javax.jcr.RepositoryException;
41  import javax.jcr.Session;
42  
43  import org.apache.commons.io.IOUtils;
44  import org.apache.commons.lang.StringUtils;
45  
46  import info.magnolia.cms.core.MgnlNodeType;
47  import info.magnolia.cms.util.ClasspathResourcesUtil;
48  import info.magnolia.jcr.util.NodeUtil;
49  import info.magnolia.module.InstallContext;
50  import info.magnolia.module.delta.AbstractTask;
51  import info.magnolia.module.delta.TaskExecutionException;
52  
53  
54  /**
55   * This install task will install a Groovy file into the groovyModule file system.
56   *
57   * @version $Id$
58   *
59   */
60  public class InstallGroovyFile extends AbstractTask {
61      private String pattern;
62  
63      public InstallGroovyFile(String taskName, String taskDescription) {
64          super(taskName, taskDescription);
65      }
66  
67      public InstallGroovyFile(String taskName, String taskDescription, String pattern) {
68          super(taskName, taskDescription);
69          this.pattern = pattern;
70      }
71  
72      @Override
73      public void execute(InstallContext installContext) throws TaskExecutionException {
74          try {
75              // Get the resources
76              String[] resources = ClasspathResourcesUtil.findResources(pattern);
77              for(String resource:resources) {
78                  createResource(resource, installContext);
79              }
80  
81          } catch (Exception e) {
82              throw new TaskExecutionException("Can't install resource [" + pattern + "]", e);
83          }
84      }
85  
86  
87      private void createResource(String resource, InstallContext installContext) throws RepositoryException, IOException {
88          //Init
89          String name = StringUtils.substringAfterLast(resource, "/");
90          name = StringUtils.substringBeforeLast(name, ".");
91          String path = StringUtils.substringBeforeLast(resource, "/");
92          //Get Session
93          Session session = installContext.getJCRSession("scripts");
94          //Create folder hierarchy if needed
95          Node parent = NodeUtil.createPath(session.getRootNode(), path, MgnlNodeType.NT_FOLDER);
96          //Create the Groovy file node
97          Node node = null;
98          if(parent.hasNode(name)) {
99              node = parent.getNode(name);
100         } else {
101             node = NodeUtil.createPath(parent, name, MgnlNodeType.NT_CONTENT);
102         }
103 
104         InputStream stream = ClasspathResourcesUtil.getStream(resource);
105         try{
106             String content = IOUtils.toString(stream);
107             node.setProperty("text", content);
108             node.setProperty("script", true);
109             node.setProperty("enabled", false);
110         }
111         finally{
112             IOUtils.closeQuietly(stream);
113         }
114     }
115 
116 }