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