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
.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
;
40 * Sends back the results, rendered or as collection.
44 public class ServerController
{
47 private DynamicRuntime
<?
> dynamicRuntime
;
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
);
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");
62 @RequestMapping("/shutdownRuntime.service")
63 protected ExecutionAnswer
shutdownRuntime(Model model
) {
66 // wait in order to let call return
69 } catch (InterruptedException e
) {
72 dynamicRuntime
.shutdown();
75 return ExecutionAnswer
.ok("Server shutting down...");
80 @SuppressWarnings("unchecked")
81 @RequestMapping("/installModule.service")
82 public void installModule(HttpServletRequest request
) throws Exception
{
84 // TODO : handle the exception better
87 List
<FileItem
> items
= upload
.parseRequest(request
);
90 for (FileItem item
: items
) {
91 if (!item
.isFormField()) {
97 ByteArrayResource res
= new ByteArrayResource(arr
);
98 Module module
= dynamicRuntime
.installModule(new ResourceDistribution(
100 // TODO: customize whether the module is started or not
101 dynamicRuntime
.startModule(module
);
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
);
112 public void setDynamicRuntime(DynamicRuntime
<?
> dynamicRuntime
) {
113 this.dynamicRuntime
= dynamicRuntime
;