]> 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
clean org.argeo.slc.server project:
[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.build.BasicNameVersion;
28 import org.argeo.slc.build.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 public final static String KEY_ANSWER = "__answer";
46
47 // IoC
48 private DynamicRuntime<?> dynamicRuntime;
49
50 // Create a factory for disk-based file items
51 private FileItemFactory factory = new DiskFileItemFactory();
52 // Create a new file upload handler
53 private ServletFileUpload upload = new ServletFileUpload(factory);
54
55 // SERVER HANDLING
56
57 @RequestMapping("/isServerReady.service")
58 protected ExecutionAnswer isServerReady(Model model) {
59 // Does nothing for now, it will return an OK answer.
60 return ExecutionAnswer.ok("Execution completed properly");
61 }
62
63 @RequestMapping("/shutdownRuntime.service")
64 protected ExecutionAnswer shutdownRuntime(Model model) {
65 new Thread() {
66 public void run() {
67 // wait in order to let call return
68 try {
69 Thread.sleep(3000);
70 } catch (InterruptedException e) {
71 // silent
72 }
73 dynamicRuntime.shutdown();
74 }
75 }.start();
76 return ExecutionAnswer.ok("Server shutting down...");
77 }
78
79 // MODULE HANDLING
80
81 @SuppressWarnings("unchecked")
82 @RequestMapping("/installModule.service")
83 public void installModule(HttpServletRequest request) throws Exception {
84
85 // TODO : handle the exception better
86
87 // Parse the request
88 List<FileItem> items = upload.parseRequest(request);
89
90 byte[] arr = null;
91 for (FileItem item : items) {
92 if (!item.isFormField()) {
93 arr = item.get();
94 break;
95 }
96 }
97
98 ByteArrayResource res = new ByteArrayResource(arr);
99 Module module = dynamicRuntime.installModule(new ResourceDistribution(
100 res));
101 // TODO: customize whether the module is started or not
102 dynamicRuntime.startModule(module);
103 }
104
105 @RequestMapping("/uninstallModule.service")
106 public void uninstallModule(@RequestParam(value = "name") String name,
107 @RequestParam(value = "version") String version) {
108 NameVersion nameVersion = new BasicNameVersion(name, version);
109 dynamicRuntime.uninstallModule(nameVersion);
110 }
111
112 // IoC
113 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
114 this.dynamicRuntime = dynamicRuntime;
115 }
116
117 }