]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/ServerController.java
aabb7ebe40794830bba66dee49b6314c3ba7fe20
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / ServerController.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.web.mvc.controllers;
17
18 import java.util.List;
19
20 import javax.servlet.http.HttpServletRequest;
21
22 import org.apache.commons.fileupload.FileItem;
23 import org.apache.commons.fileupload.FileItemFactory;
24 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
25 import org.apache.commons.fileupload.servlet.ServletFileUpload;
26 import org.argeo.slc.BasicNameVersion;
27 import org.argeo.slc.NameVersion;
28 import org.argeo.slc.core.build.ResourceDistribution;
29 import org.argeo.slc.deploy.DynamicRuntime;
30 import org.argeo.slc.deploy.Module;
31 import org.argeo.slc.msg.ExecutionAnswer;
32 import org.springframework.core.io.ByteArrayResource;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.ui.Model;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestParam;
37
38 /**
39 * Sends back the results, rendered or as collection.
40 */
41
42 @Controller
43 public class ServerController {
44
45 // IoC
46 private DynamicRuntime<?> dynamicRuntime;
47
48 // Create a factory for disk-based file items
49 private FileItemFactory factory = new DiskFileItemFactory();
50 // Create a new file upload handler
51 private ServletFileUpload upload = new ServletFileUpload(factory);
52
53 // SERVER HANDLING
54
55 @RequestMapping("/isServerReady.service")
56 protected ExecutionAnswer isServerReady(Model model) {
57 // Does nothing for now, it will return an OK answer.
58 return ExecutionAnswer.ok("Execution completed properly");
59 }
60
61 @RequestMapping("/shutdownRuntime.service")
62 protected ExecutionAnswer shutdownRuntime(Model model) {
63 new Thread() {
64 public void run() {
65 // wait in order to let call return
66 try {
67 Thread.sleep(3000);
68 } catch (InterruptedException e) {
69 // silent
70 }
71 dynamicRuntime.shutdown();
72 }
73 }.start();
74 return ExecutionAnswer.ok("Server shutting down...");
75 }
76
77 // MODULE HANDLING
78
79 @SuppressWarnings("unchecked")
80 @RequestMapping("/installModule.service")
81 public void installModule(HttpServletRequest request) throws Exception {
82
83 // TODO : handle the exception better
84
85 // Parse the request
86 List<FileItem> items = upload.parseRequest(request);
87
88 byte[] arr = null;
89 for (FileItem item : items) {
90 if (!item.isFormField()) {
91 arr = item.get();
92 break;
93 }
94 }
95
96 ByteArrayResource res = new ByteArrayResource(arr);
97 Module module = dynamicRuntime.installModule(new ResourceDistribution(
98 res));
99 // TODO: customize whether the module is started or not
100 dynamicRuntime.startModule(module);
101 }
102
103 @RequestMapping("/uninstallModule.service")
104 public void uninstallModule(@RequestParam(value = "name") String name,
105 @RequestParam(value = "version") String version) {
106 NameVersion nameVersion = new BasicNameVersion(name, version);
107 dynamicRuntime.uninstallModule(nameVersion);
108 }
109
110 // IoC
111 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
112 this.dynamicRuntime = dynamicRuntime;
113 }
114
115 }