]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/ServerController.java
Move NameVersion
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / ServerController.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.web.mvc.controllers;
18
19 import java.util.List;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 import org.apache.commons.fileupload.FileItem;
24 import org.apache.commons.fileupload.FileItemFactory;
25 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
26 import org.apache.commons.fileupload.servlet.ServletFileUpload;
27 import org.argeo.slc.BasicNameVersion;
28 import org.argeo.slc.NameVersion;
29 import org.argeo.slc.core.build.ResourceDistribution;
30 import org.argeo.slc.deploy.DynamicRuntime;
31 import org.argeo.slc.deploy.Module;
32 import org.argeo.slc.msg.ExecutionAnswer;
33 import org.springframework.core.io.ByteArrayResource;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.ui.Model;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestParam;
38
39 /**
40 * Sends back the results, rendered or as collection.
41 */
42
43 @Controller
44 public class ServerController {
45
46 // IoC
47 private DynamicRuntime<?> dynamicRuntime;
48
49 // Create a factory for disk-based file items
50 private FileItemFactory factory = new DiskFileItemFactory();
51 // Create a new file upload handler
52 private ServletFileUpload upload = new ServletFileUpload(factory);
53
54 // SERVER HANDLING
55
56 @RequestMapping("/isServerReady.service")
57 protected ExecutionAnswer isServerReady(Model model) {
58 // Does nothing for now, it will return an OK answer.
59 return ExecutionAnswer.ok("Execution completed properly");
60 }
61
62 @RequestMapping("/shutdownRuntime.service")
63 protected ExecutionAnswer shutdownRuntime(Model model) {
64 new Thread() {
65 public void run() {
66 // wait in order to let call return
67 try {
68 Thread.sleep(3000);
69 } catch (InterruptedException e) {
70 // silent
71 }
72 dynamicRuntime.shutdown();
73 }
74 }.start();
75 return ExecutionAnswer.ok("Server shutting down...");
76 }
77
78 // MODULE HANDLING
79
80 @SuppressWarnings("unchecked")
81 @RequestMapping("/installModule.service")
82 public void installModule(HttpServletRequest request) throws Exception {
83
84 // TODO : handle the exception better
85
86 // Parse the request
87 List<FileItem> items = upload.parseRequest(request);
88
89 byte[] arr = null;
90 for (FileItem item : items) {
91 if (!item.isFormField()) {
92 arr = item.get();
93 break;
94 }
95 }
96
97 ByteArrayResource res = new ByteArrayResource(arr);
98 Module module = dynamicRuntime.installModule(new ResourceDistribution(
99 res));
100 // TODO: customize whether the module is started or not
101 dynamicRuntime.startModule(module);
102 }
103
104 @RequestMapping("/uninstallModule.service")
105 public void uninstallModule(@RequestParam(value = "name") String name,
106 @RequestParam(value = "version") String version) {
107 NameVersion nameVersion = new BasicNameVersion(name, version);
108 dynamicRuntime.uninstallModule(nameVersion);
109 }
110
111 // IoC
112 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
113 this.dynamicRuntime = dynamicRuntime;
114 }
115
116 }