View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.ui;
35  
36  import info.magnolia.cms.util.SimpleFreemarkerHelper;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.module.InstallContext;
39  import info.magnolia.module.InstallStatus;
40  import info.magnolia.module.ModuleManagementException;
41  import info.magnolia.module.ModuleManager;
42  
43  import java.io.IOException;
44  import java.io.Writer;
45  import java.util.HashMap;
46  import java.util.Map;
47  
48  import freemarker.template.TemplateException;
49  
50  /**
51   * An implementation of ModuleManagerWebUI which is meant to be used through a web interface,
52   * with human interaction.
53   */
54  public class ModuleManagerWebUI implements ModuleManagerUI {
55      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ModuleManagerWebUI.class);
56  
57      public static final String INSTALLER_PATH = "/.magnolia/installer";
58  
59      private final ModuleManager moduleManager;
60      private final SimpleFreemarkerHelper freemarkerUtil;
61  
62      public ModuleManagerWebUI(ModuleManager moduleManager) {
63          this.moduleManager = moduleManager;
64          this.freemarkerUtil = new SimpleFreemarkerHelper(getClass(), true);
65      }
66  
67      @Override
68      public void onStartup() {
69          final ModuleManager.ModuleManagementState moduleMgtState = moduleManager.getStatus();
70          if (moduleMgtState.needsUpdateOrInstall()) {
71              log.info("\n" +
72                      "*********************************************************************************************************\n" +
73                      "*                                                                                                       *\n" +
74                      "* Magnolia needs module updates or installs, point your browser to your Magnolia instance and confirm ! *\n" +
75                      "*                                                                                                       *\n" +
76                      "*********************************************************************************************************");
77          } else {
78              moduleManager.startModules();
79          }
80      }
81  
82      @Override
83      public boolean execute(Writer out, String command) throws ModuleManagementException {
84          if (command == null) {
85              render("listTasks", out);
86              return false;
87          }
88          final InstallContext installCtx = moduleManager.getInstallContext();
89          final InstallStatus status = installCtx.getStatus();
90          if ("status".equals(command) || "start".equals(command)) {
91              if (status == null) {
92                  performInstallOrUpdate();
93                  render("inProgress", out);
94              } else {
95                  // template names match statuses
96                  render(status.name(), out);
97              }
98              return false;
99          } else if ("inProgressCall".equals(command)) {
100             render("inProgressCall", out);
101             return false;
102         } else if ("finish".equals(command) && status.equals(InstallStatus.installDone)) {
103             MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
104                 @Override
105                 public void doExec() {
106                     //TODO : actually check for status before executing
107                     moduleManager.startModules();
108                     //moduleManager.getStatus().done();
109                 }
110             }, false);
111             return true;
112         }
113         throw new IllegalStateException("Unexpected state In ModuleManagerWebUI.");
114     }
115 
116     @Override
117     public void renderTempPage(Writer out) throws ModuleManagementException {
118         render("temp", out);
119     }
120 
121     protected void performInstallOrUpdate() {
122         final Runnable runnable = new Runnable() {
123             @Override
124             public void run() {
125                 try {
126                     moduleManager.performInstallOrUpdate();
127                 } catch (Throwable e) {
128 //                    final InstallContext installContext = moduleManager.getInstallContext();
129 //                    final ModuleDefinition currentModule = installContext.getCurrentModuleDefinition();
130 //                    final String message = "Could not perform installation" + (currentModule != null ? " of " + currentModule.getName() : "") + ": " + e.getMessage();
131 //                    log.error(message, e);
132 //                    installContext.error(message, e);
133                     log.error("Could not perform installation: {}", e.getMessage(), e);
134                     moduleManager.getInstallContext().error("Could not perform installation: " + e.getMessage(), e);
135                     // TODO set status ? here the status page continues on reloading itself ...
136                 }
137             }
138         };
139         new Thread(runnable).start();
140     }
141 
142     protected void render(String templateName, Writer out) throws ModuleManagementException {
143         final String tmpl = getClass().getSimpleName() + "." + templateName + ".html";
144         final Map<String, Object> ctx = new HashMap<>();
145         ctx.put("contextPath", MgnlContext.getContextPath());
146         ctx.put("installerPath", INSTALLER_PATH);
147         ctx.put("status", moduleManager.getStatus());
148         ctx.put("context", moduleManager.getInstallContext());
149         try {
150             freemarkerUtil.render(tmpl, ctx, out);
151         } catch (TemplateException | IOException e) {
152             throw new ModuleManagementException("Couldn't render template: " + e.getMessage(), e);
153         }
154     }
155 
156 }