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 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
52
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
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
107 moduleManager.startModules();
108
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
129
130
131
132
133 log.error("Could not perform installation: {}", e.getMessage(), e);
134 moduleManager.getInstallContext().error("Could not perform installation: " + e.getMessage(), e);
135
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 }