]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/InstallModule.java
Introduce H2 database support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / management / InstallModule.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.management;
18
19 import java.util.List;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.commons.fileupload.FileItem;
25 import org.apache.commons.fileupload.FileItemFactory;
26 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
27 import org.apache.commons.fileupload.servlet.ServletFileUpload;
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.web.mvc.AbstractServiceController;
32 import org.springframework.core.io.ByteArrayResource;
33 import org.springframework.web.servlet.ModelAndView;
34
35 public class InstallModule extends AbstractServiceController {// extends
36 private DynamicRuntime<?> dynamicRuntime;
37
38 // Create a factory for disk-based file items
39 private FileItemFactory factory = new DiskFileItemFactory();
40
41 // Create a new file upload handler
42 private ServletFileUpload upload = new ServletFileUpload(factory);
43
44 @Override
45 @SuppressWarnings(value = { "unchecked" })
46 protected void handleServiceRequest(HttpServletRequest request,
47 HttpServletResponse response, ModelAndView modelAndView)
48 throws Exception {
49 // Parse the request
50 List<FileItem> items = upload.parseRequest(request);
51
52 byte[] arr = null;
53 for (FileItem item : items) {
54 if (!item.isFormField()) {
55 arr = item.get();
56 break;
57 }
58 }
59
60 ByteArrayResource res = new ByteArrayResource(arr);
61 Module module = dynamicRuntime.installModule(new ResourceDistribution(
62 res));
63
64 // TODO: customize whether the module is started or not
65 dynamicRuntime.startModule(module);
66 }
67
68 // protected ModelAndView onSubmit(HttpServletRequest request,
69 // HttpServletResponse response, Object command, BindException errors)
70 // throws Exception {
71 // FileUploadBean bean = (FileUploadBean) command;
72 //
73 // byte[] file = bean.getFile();
74 // if (file == null) {
75 // throw new SlcException("Upload is empty.");
76 // }
77 //
78 // ByteArrayResource res = new ByteArrayResource(file);
79 // dynamicRuntime.installModule(new ResourceDistribution(res));
80 //
81 // return super.onSubmit(request, response, command, errors);
82 // }
83 //
84 // protected void initBinder(HttpServletRequest request,
85 // ServletRequestDataBinder binder) throws ServletException {
86 // // to actually be able to convert Multipart instance to byte[]
87 // // we have to register a custom editor
88 // binder.registerCustomEditor(byte[].class,
89 // new ByteArrayMultipartFileEditor());
90 // // now Spring knows how to handle multipart object and convert them
91 // }
92
93 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
94 this.dynamicRuntime = dynamicRuntime;
95 }
96
97 }