Upload of distributions
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 23 Jul 2009 15:52:29 +0000 (15:52 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 23 Jul 2009 15:52:29 +0000 (15:52 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2738 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

15 files changed:
demo/pom.xml
demo/site/org.argeo.slc.demo.log4j/log4j.properties
modules/server/org.argeo.slc.server.catalina/META-INF/MANIFEST.MF
modules/server/org.argeo.slc.webapp.war/META-INF/MANIFEST.MF
modules/server/org.argeo.slc.webapp.war/WEB-INF/slc-service-servlet.xml
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/build/ResourceDistribution.java
runtime/org.argeo.slc.server/pom.xml
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/FileUploadBean.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/InstallModule.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/UninstallModule.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/AvailableModulesHtml.java
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/deploy/DynamicRuntime.java
runtime/org.argeo.slc.support.equinox/src/main/java/org/argeo/slc/equinox/EquinoxRuntime.java
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiBundle.java
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiRuntime.java

index 9f0a494c9beff6ea74129f0366d32bf0d8ce5e22..4b6cc07156e3c23da99fe2d32c415735ce222572 100644 (file)
@@ -99,7 +99,6 @@
                                                                        org.argeo.slc.ria
                                                                </slc.osgi.start>
                                                                <slc.osgi.bundles>
-                                                                       ${user.home}/dev/src/sparta/dist/runtime/target;in=*.jar,
                                                                        ${basedir}/site;in=*;ex=pom.xml;ex=target,
                                                                        ${basedir}/../modules/server;in=*;ex=pom.xml;ex=target;ex=.*,
                                                                        ${basedir}/../modules/agent;in=*;ex=pom.xml;ex=target
index 875e36cef584e2d49675059785c2451491f6213f..7da0cc4c883f7b6fa3ccfcefe48c88b2ca34c016 100644 (file)
@@ -12,8 +12,9 @@ log4j.logger.org.argeo.slc.execution.SimpleExecutionSpec=DEBUG
 log4j.logger.org.hibernate=WARN
 
 log4j.logger.org.springframework=WARN
-log4j.logger.org.springframework.jms=WARN
-log4j.logger.org.springframework.security=WARN
+#log4j.logger.org.springframework.web=DEBUG
+#log4j.logger.org.springframework.jms=WARN
+#log4j.logger.org.springframework.security=WARN
 
 log4j.logger.org.apache.activemq=WARN
 log4j.logger.org.apache.activemq.transport=WARN
index 35de2e24bf198dd605dc7d8254720ee30318c2c6..ad5203f46cdb63b0f1462b49ee64079820ccab6a 100644 (file)
@@ -3,4 +3,6 @@ Fragment-Host: com.springsource.org.apache.catalina;bundle-version="[6
  .0.16,7.0.0)"\r
 Bundle-Version: 0.11.4.SNAPSHOT\r
 Bundle-SymbolicName: org.argeo.slc.server.catalina\r
-\r
+Import-Package: org.springframework.security.providers,\r
+ org.springframework.security,\r
+ org.springframework.security.ui\r
index 834a4474ac40df029942734307edc653bb0284fc..5946614557c9041e857dd20fa4825141745f8eb3 100644 (file)
@@ -38,6 +38,8 @@ Import-Package: net.sf.cglib.core,
  org.springframework.orm.hibernate3.support,\r
  org.springframework.osgi.web.context.support,\r
  org.springframework.oxm,\r
+ org.springframework.security.context,\r
+ org.springframework.security.providers,\r
  org.springframework.security.ui.webapp,\r
  org.springframework.security.userdetails.memory,\r
  org.springframework.transaction,\r
@@ -47,6 +49,4 @@ Import-Package: net.sf.cglib.core,
  org.springframework.web.filter,\r
  org.springframework.web.servlet,\r
  org.springframework.web.servlet.handler,\r
- org.springframework.web.servlet.mvc,\r
- org.springframework.security.context,\r
- org.springframework.security.providers\r
+ org.springframework.web.servlet.mvc\r
index 42f0ec533b4471364e6ed99698a8ea7ad8322e99..5ea3a76744356d1c53cc7c0cfcefda4b8a5b2e7c 100644 (file)
                <property name="modularDistributions" ref="modularDistributions" />
        </bean>
 
+       <bean name="/installModule.service" class="org.argeo.slc.web.mvc.management.InstallModule">
+               <property name="dynamicRuntime" ref="dynamicRuntime" />
+       </bean>
+
+       <bean name="/uninstallModule.service" class="org.argeo.slc.web.mvc.management.UninstallModule">
+               <property name="dynamicRuntime" ref="dynamicRuntime" />
+       </bean>
 
        <!-- MVC -->
        <bean id="handlerMapping"
index 633be7a7fd97d9f2e46cddc12f54b7b4a42ff75e..a74d788fc562d6106e073b1243d7e4b6e2801113 100644 (file)
@@ -9,33 +9,38 @@ import org.argeo.slc.build.Distribution;
 import org.springframework.core.io.Resource;
 
 public class ResourceDistribution implements Distribution, StreamReadable {
-       private Resource location;
+       private Resource resource;
 
        public ResourceDistribution() {
        }
 
        public ResourceDistribution(Resource location) {
-               this.location = location;
+               this.resource = location;
        }
 
        public String getDistributionId() {
-               return location.toString();
+               return resource.toString();
        }
 
-       public Resource getLocation() {
-               return location;
+       public Resource getResource() {
+               return resource;
        }
 
-       public void setLocation(Resource location) {
-               this.location = location;
+       public void setResource(Resource resource) {
+               this.resource = resource;
        }
 
        public InputStream getInputStream() {
                try {
-                       return location.getInputStream();
+                       return resource.getInputStream();
                } catch (IOException e) {
                        throw new SlcException("Cannot get input stream", e);
                }
        }
 
+       @Override
+       public String toString() {
+               return resource.toString();
+       }
+
 }
index 6f1792043bfd457244edcd36ef3a203dea081798..f7b245046d937dd31aacaa48a255d5f966ac4708 100644 (file)
                        <artifactId>org.springframework.oxm</artifactId>
                </dependency>
 
+               <!-- Web -->
+               <dependency>
+                       <groupId>org.apache.commons</groupId>
+                       <artifactId>com.springsource.org.apache.commons.fileupload</artifactId>
+               </dependency>
+
                <!-- J2EE -->
                <dependency>
                        <groupId>javax.servlet</groupId>
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/FileUploadBean.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/FileUploadBean.java
new file mode 100644 (file)
index 0000000..4c06496
--- /dev/null
@@ -0,0 +1,14 @@
+package org.argeo.slc.web.mvc;
+
+
+public class FileUploadBean {
+       private byte[] file;
+
+       public void setFile(byte[] file) {
+               this.file = file;
+       }
+
+       public byte[] getFile() {
+               return file;
+       }
+}
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/InstallModule.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/InstallModule.java
new file mode 100644 (file)
index 0000000..6b61fd9
--- /dev/null
@@ -0,0 +1,81 @@
+package org.argeo.slc.web.mvc.management;
+
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.argeo.slc.core.build.ResourceDistribution;
+import org.argeo.slc.deploy.DynamicRuntime;
+import org.argeo.slc.deploy.Module;
+import org.argeo.slc.web.mvc.AbstractServiceController;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.web.servlet.ModelAndView;
+
+public class InstallModule extends AbstractServiceController {// extends
+       private DynamicRuntime<?> dynamicRuntime;
+
+       // Create a factory for disk-based file items
+       private FileItemFactory factory = new DiskFileItemFactory();
+
+       // Create a new file upload handler
+       private ServletFileUpload upload = new ServletFileUpload(factory);
+
+       @Override
+       @SuppressWarnings(value = { "unchecked" })
+       protected void handleServiceRequest(HttpServletRequest request,
+                       HttpServletResponse response, ModelAndView modelAndView)
+                       throws Exception {
+               // Parse the request
+               List<FileItem> items = upload.parseRequest(request);
+
+               byte[] arr = null;
+               for (FileItem item : items) {
+                       if (!item.isFormField()) {
+                               arr = item.get();
+                               break;
+                       }
+               }
+
+               ByteArrayResource res = new ByteArrayResource(arr);
+               Module module = dynamicRuntime.installModule(new ResourceDistribution(
+                               res));
+
+               // TODO: customize whether the module is started or not
+               dynamicRuntime.startModule(module);
+       }
+
+       // protected ModelAndView onSubmit(HttpServletRequest request,
+       // HttpServletResponse response, Object command, BindException errors)
+       // throws Exception {
+       // FileUploadBean bean = (FileUploadBean) command;
+       //
+       // byte[] file = bean.getFile();
+       // if (file == null) {
+       // throw new SlcException("Upload is empty.");
+       // }
+       //
+       // ByteArrayResource res = new ByteArrayResource(file);
+       // dynamicRuntime.installModule(new ResourceDistribution(res));
+       //
+       // return super.onSubmit(request, response, command, errors);
+       // }
+       //
+       // protected void initBinder(HttpServletRequest request,
+       // ServletRequestDataBinder binder) throws ServletException {
+       // // to actually be able to convert Multipart instance to byte[]
+       // // we have to register a custom editor
+       // binder.registerCustomEditor(byte[].class,
+       // new ByteArrayMultipartFileEditor());
+       // // now Spring knows how to handle multipart object and convert them
+       // }
+
+       public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
+               this.dynamicRuntime = dynamicRuntime;
+       }
+
+}
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/UninstallModule.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/management/UninstallModule.java
new file mode 100644 (file)
index 0000000..94c61b0
--- /dev/null
@@ -0,0 +1,30 @@
+package org.argeo.slc.web.mvc.management;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.argeo.slc.build.BasicNameVersion;\r
+import org.argeo.slc.build.NameVersion;\r
+import org.argeo.slc.deploy.DynamicRuntime;\r
+import org.argeo.slc.web.mvc.AbstractServiceController;\r
+import org.springframework.web.servlet.ModelAndView;\r
+\r
+/** . */\r
+public class UninstallModule extends AbstractServiceController {\r
+       private DynamicRuntime<?> dynamicRuntime;\r
+\r
+       @Override\r
+       protected void handleServiceRequest(HttpServletRequest request,\r
+                       HttpServletResponse response, ModelAndView modelAndView)\r
+                       throws Exception {\r
+               String name = request.getParameter("name");\r
+               String version = request.getParameter("version");\r
+               NameVersion nameVersion = new BasicNameVersion(name, version);\r
+               dynamicRuntime.uninstallModule(nameVersion);\r
+       }\r
+\r
+       public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {\r
+               this.dynamicRuntime = dynamicRuntime;\r
+       }\r
+\r
+}\r
index 7a860996db26c80b5972f569c3704828245b573e..ca6434c4c6005621ea846171d6eaa449e8956f59 100644 (file)
@@ -22,7 +22,7 @@ public class AvailableModulesHtml extends AbstractAvailableModules {
                        String moduleUrl = null;
                        if (distribution instanceof ResourceDistribution) {
                                String url = ((ResourceDistribution) distribution)
-                                               .getLocation().getURL().toString();
+                                               .getResource().getURL().toString();
                                if (url.startsWith("reference:"))
                                        moduleUrl = url;
                        }
index e2552732abebcb2d3f170f820df3095363287a58..0984b252f49a98611504a24bcd65a413bebc4f9c 100644 (file)
@@ -1,7 +1,16 @@
 package org.argeo.slc.deploy;
 
+import org.argeo.slc.build.Distribution;
+import org.argeo.slc.build.NameVersion;
+
 public interface DynamicRuntime<M extends Module> extends
                ModularDeployedSystem<M> {
        public void shutdown();
 
+       public M installModule(Distribution distribution);
+
+       public void uninstallModule(NameVersion nameVersion);
+
+       public void startModule(NameVersion nameVersion);
+
 }
index 60d36280c321eb4e74db01627afdc57b37f93811..45bbe234dcce849b883fe4517a146ee2de230606 100644 (file)
@@ -1,13 +1,10 @@
 package org.argeo.slc.equinox;
 
 import org.argeo.slc.SlcException;
-import org.argeo.slc.deploy.DynamicRuntime;
-import org.argeo.slc.osgi.OsgiBundle;
 import org.argeo.slc.osgi.OsgiRuntime;
 import org.eclipse.core.runtime.adaptor.EclipseStarter;
 
-public class EquinoxRuntime extends OsgiRuntime implements
-               DynamicRuntime<OsgiBundle> {
+public class EquinoxRuntime extends OsgiRuntime {
 
        public void shutdown() {
                try {
index 6dd75716afa336052d565bb2e19576cb7c378400..9fb09bebf588ea6fe071ac31d71b41760b87f4c6 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.slc.osgi;
 
 import org.argeo.slc.build.BasicNameVersion;
 import org.argeo.slc.build.Distribution;
+import org.argeo.slc.core.build.ResourceDistribution;
 import org.argeo.slc.deploy.DeploymentData;
 import org.argeo.slc.deploy.Module;
 import org.argeo.slc.deploy.TargetData;
@@ -10,7 +11,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 
 public class OsgiBundle extends BasicNameVersion implements Module {
-       private Distribution distribution;
+       private ResourceDistribution distribution;
 
        private Long internalBundleId;
 
@@ -51,11 +52,15 @@ public class OsgiBundle extends BasicNameVersion implements Module {
                return distribution;
        }
 
+       public ResourceDistribution getResourceDistribution() {
+               return distribution;
+       }
+
        public TargetData getTargetData() {
                throw new UnsupportedOperationException();
        }
 
-       public void setDistribution(Distribution distribution) {
+       public void setResourceDistribution(ResourceDistribution distribution) {
                this.distribution = distribution;
        }
 
index e433d7e29e18a14597460a1b483959c659c023a5..504f0cfa9e34446b8f9d40dad09890bf8337fd0c 100644 (file)
@@ -4,21 +4,25 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import org.argeo.slc.SlcException;
+import org.argeo.slc.StreamReadable;
 import org.argeo.slc.UnsupportedException;
 import org.argeo.slc.build.Distribution;
+import org.argeo.slc.build.NameVersion;
 import org.argeo.slc.core.build.VersionedResourceDistribution;
 import org.argeo.slc.deploy.DeploymentData;
-import org.argeo.slc.deploy.ModularDeployedSystem;
+import org.argeo.slc.deploy.DynamicRuntime;
 import org.argeo.slc.deploy.TargetData;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
 import org.springframework.context.ResourceLoaderAware;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
 import org.springframework.osgi.context.BundleContextAware;
 
-public class OsgiRuntime implements ModularDeployedSystem<OsgiBundle>,
-               BundleContextAware, ResourceLoaderAware {
+public class OsgiRuntime implements BundleContextAware, ResourceLoaderAware,
+               DynamicRuntime<OsgiBundle> {
        private String uuid = UUID.randomUUID().toString();
        private BundleContext bundleContext;
        private ResourceLoader resourceLoader;
@@ -33,7 +37,7 @@ public class OsgiRuntime implements ModularDeployedSystem<OsgiBundle>,
                        if (location != null) {
                                Resource resource = resourceLoader.getResource(location);
                                osgiBundle
-                                               .setDistribution(new VersionedResourceDistribution(
+                                               .setResourceDistribution(new VersionedResourceDistribution(
                                                                osgiBundle.getName(), osgiBundle.getVersion(),
                                                                resource));
                        }
@@ -41,6 +45,56 @@ public class OsgiRuntime implements ModularDeployedSystem<OsgiBundle>,
                return modules;
        }
 
+       public OsgiBundle installModule(Distribution distribution) {
+               if (!(distribution instanceof StreamReadable))
+                       throw new UnsupportedException("distribution", distribution);
+
+               StreamReadable sr = (StreamReadable) distribution;
+               Bundle bundle;
+               try {
+                       bundle = bundleContext.installBundle(sr.toString(), sr
+                                       .getInputStream());
+               } catch (BundleException e) {
+                       throw new SlcException(
+                                       "Cannot install OSGi bundle " + distribution, e);
+               }
+               return new OsgiBundle(bundle);
+       }
+
+       public void uninstallModule(NameVersion nameVersion) {
+               Bundle bundle = findBundle(nameVersion);
+               try {
+                       bundle.uninstall();
+               } catch (BundleException e) {
+                       throw new SlcException("Cannot uninstall " + bundle, e);
+               }
+       }
+
+       public void startModule(NameVersion nameVersion) {
+               Bundle bundle = findBundle(nameVersion);
+               try {
+                       bundle.start();
+                       // TODO: use bundle manager
+               } catch (BundleException e) {
+                       throw new SlcException("Cannot uninstall " + bundle, e);
+               }
+       }
+
+       protected Bundle findBundle(NameVersion nameVersion) {
+               Bundle[] bundles = bundleContext.getBundles();
+               for (Bundle bundle : bundles) {
+                       OsgiBundle osgiBundle = new OsgiBundle(bundle);
+                       if (osgiBundle.equals(nameVersion)) {
+                               return bundle;
+                       }
+               }
+               throw new SlcException("Could not find bundle " + nameVersion);
+       }
+
+       public void shutdown() {
+               throw new UnsupportedException();
+       }
+
        public String getDeployedSystemId() {
                return uuid;
        }