2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.web
.mvc
.controllers
;
19 import java
.util
.List
;
21 import javax
.servlet
.http
.HttpServletRequest
;
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
;
40 * Sends back the results, rendered or as collection.
44 public class ServerController
{
45 public final static String KEY_ANSWER
= "__answer";
48 private DynamicRuntime
<?
> dynamicRuntime
;
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
);
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");
63 @RequestMapping("/shutdownRuntime.service")
64 protected ExecutionAnswer
shutdownRuntime(Model model
) {
67 // wait in order to let call return
70 } catch (InterruptedException e
) {
73 dynamicRuntime
.shutdown();
76 return ExecutionAnswer
.ok("Server shutting down...");
81 @SuppressWarnings("unchecked")
82 @RequestMapping("/installModule.service")
83 public void installModule(HttpServletRequest request
) throws Exception
{
85 // TODO : handle the exception better
88 List
<FileItem
> items
= upload
.parseRequest(request
);
91 for (FileItem item
: items
) {
92 if (!item
.isFormField()) {
98 ByteArrayResource res
= new ByteArrayResource(arr
);
99 Module module
= dynamicRuntime
.installModule(new ResourceDistribution(
101 // TODO: customize whether the module is started or not
102 dynamicRuntime
.startModule(module
);
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
);
113 public void setDynamicRuntime(DynamicRuntime
<?
> dynamicRuntime
) {
114 this.dynamicRuntime
= dynamicRuntime
;