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
.management
;
19 import java
.util
.List
;
21 import javax
.servlet
.http
.HttpServletRequest
;
22 import javax
.servlet
.http
.HttpServletResponse
;
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
;
35 public class InstallModule
extends AbstractServiceController
{// extends
36 private DynamicRuntime
<?
> dynamicRuntime
;
38 // Create a factory for disk-based file items
39 private FileItemFactory factory
= new DiskFileItemFactory();
41 // Create a new file upload handler
42 private ServletFileUpload upload
= new ServletFileUpload(factory
);
45 @SuppressWarnings(value
= { "unchecked" })
46 protected void handleServiceRequest(HttpServletRequest request
,
47 HttpServletResponse response
, ModelAndView modelAndView
)
50 List
<FileItem
> items
= upload
.parseRequest(request
);
53 for (FileItem item
: items
) {
54 if (!item
.isFormField()) {
60 ByteArrayResource res
= new ByteArrayResource(arr
);
61 Module module
= dynamicRuntime
.installModule(new ResourceDistribution(
64 // TODO: customize whether the module is started or not
65 dynamicRuntime
.startModule(module
);
68 // protected ModelAndView onSubmit(HttpServletRequest request,
69 // HttpServletResponse response, Object command, BindException errors)
71 // FileUploadBean bean = (FileUploadBean) command;
73 // byte[] file = bean.getFile();
74 // if (file == null) {
75 // throw new SlcException("Upload is empty.");
78 // ByteArrayResource res = new ByteArrayResource(file);
79 // dynamicRuntime.installModule(new ResourceDistribution(res));
81 // return super.onSubmit(request, response, command, errors);
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
93 public void setDynamicRuntime(DynamicRuntime
<?
> dynamicRuntime
) {
94 this.dynamicRuntime
= dynamicRuntime
;