1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
54
55
56
57
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
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
104 moduleManager.startModules();
105
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
125
126
127
128
129 log.error("Could not perform installation: " + e.getMessage(), e);
130 moduleManager.getInstallContext().error("Could not perform installation: " + e.getMessage(), e);
131
132 }
133 }
134 };
135 new Thread(runnable).start();
136 }
137
138 protected void render(String templateName, Writer out) throws ModuleManagementException {
139
140 final FreemarkerHelper freemarkerHelper = new FreemarkerHelper() {
141 protected void addDefaultData(Map data, Locale locale, String i18nBasename) {
142 final WebContext webCtx = (WebContext) MgnlContext.getInstance();
143
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 }