Revision: 4110
          http://vexi.svn.sourceforge.net/vexi/?rev=4110&view=rev
Author:   mkpg2
Date:     2011-04-15 00:55:53 +0000 (Fri, 15 Apr 2011)

Log Message:
-----------
Improve. Support multiple 'vexis' inputs to allow multiple applications to be 
contained in a single deployment directory.

Modified Paths:
--------------
    
trunk/org.vexi-build.deployment/src/main/java/org/vexi/build/deployment/VexiDeploymentAssembler.java

Modified: 
trunk/org.vexi-build.deployment/src/main/java/org/vexi/build/deployment/VexiDeploymentAssembler.java
===================================================================
--- 
trunk/org.vexi-build.deployment/src/main/java/org/vexi/build/deployment/VexiDeploymentAssembler.java
        2011-04-14 12:50:22 UTC (rev 4109)
+++ 
trunk/org.vexi-build.deployment/src/main/java/org/vexi/build/deployment/VexiDeploymentAssembler.java
        2011-04-15 00:55:53 UTC (rev 4110)
@@ -1,113 +1,169 @@
 package org.vexi.build.deployment;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.*;
+import java.util.*;
 
 import org.vexi.util.IOUtil;
 
-import ebuild.api.IAssemblerArgument;
-import ebuild.api.IEBuild;
-import ebuild.api.IInput;
-import ebuild.api.IInputMap;
+import ebuild.api.*;
 import ebuild.api.log.ILogger;
-import ebuild.api.plugin.AbstractAssembler;
-import ebuild.api.plugin.BuildPluginException;
-import ebuild.api.plugin.IPropertyMap;
-import ebuild.util.CollectionUtil;
-import ebuild.util.FileUtil;
-import ebuild.util.JSONUtil;
+import ebuild.api.plugin.*;
+import ebuild.util.*;
 
 public class VexiDeploymentAssembler extends AbstractAssembler{
+       private Collection<String> asNameList(Collection<File> files){
+               List<String> r = new ArrayList(files.size());
+               for(File f: files){
+            r.add(f.getName());
+        }
+               return r;
+       }
+
     public Collection<File> assemble(IAssemblerArgument arg) throws 
BuildPluginException {
-        ILogger logger = arg.getLogger();
-        IEBuild ebuild = arg.getEBuild();
-        IPropertyMap props = arg.getPropertyMap();
-        IInputMap inputs = arg.getInputMap();
-        
+        final ILogger logger = arg.getLogger();
+        final IEBuild ebuild = arg.getEBuild();
+        final IPropertyMap props = arg.getPropertyMap();
+        final IInputMap inputs = arg.getInputMap();
+        final File output = arg.getOutputDirectory();
+        final String templateName = props.getString("launcher-page", 
"index.html");
+        final File launcher = 
inputs.expectInput("launcher").expectLoneArtifact();
+        final File core = inputs.expectInput("core").expectLoneArtifact();
         try {
-            File output = arg.getOutputDirectory();
-            File launcher = 
inputs.expectInput("launcher").expectLoneArtifact();
-            File core = inputs.expectInput("core").expectLoneArtifact();
-            Collection<File> vexiFiles = 
inputs.expectInput("vexis").getArtifacts();
-            
-            output.mkdirs();
-            
-            logger.log("launcher:" );
-            logger.log("    "+ebuild.formatAsDisplayPath(launcher));
-            
-            List<String> vexis = new ArrayList(vexiFiles.size());
-            FileUtil.copyToDir(launcher, output);
-            FileUtil.copyToDir(core, output);
-            FileUtil.copyToDir(launcher, output);
-            for(File f: vexiFiles){
-                FileUtil.copyToDir(f, output);
-                vexis.add(f.getName());
-            }
-            
-            
-            IInput launcherPageInput = inputs.getInput("launcher-page");
-            if(launcherPageInput!=null){
-                for(File f: launcherPageInput.getArtifacts()){
-                    FileUtil.mergeCopy(f, output);
+               new Callable<Object, Exception>() {
+                       private String template;
+                       
+                       private void copyToDir(String name, File f) throws 
IOException{
+                               logger.log(name+":" );
+                    logger.log("    "+ebuild.formatAsDisplayPath(f));
+                    FileUtil.copyToDir(f, output);
                 }
-                File file = new File(output, props.getString("launcher-page", 
"index.html"));
-                if(!file.isFile()) throw new BuildPluginException("Expected 
launcher-page template file: "+ebuild.formatAsDisplayPath(file));
-                IOUtil.stringToFile(generateLaunchHtml(file, props, core, 
launcher, vexis), file);
-            }
-            
-            String launcherJson = props.getString("launcher-json");
-            if(launcherJson!=null){
-                File file = new File(output, launcherJson);
-                Map m = generateLaunchJson(props, core, launcher, vexis);
-                JSONUtil.writeObject(file, m);
-            }            
-            return CollectionUtil.singletonList(output);
+                   
+                       private void copyToDir(String name, Collection<File> 
fs) throws IOException{
+                               logger.log(name+":" );
+                               for(File f: fs){
+                                       logger.log("    
"+ebuild.formatAsDisplayPath(f));
+                                       FileUtil.copyToDir(f, output);
+                               }
+                }
+                       private Map<String,IInput> justVexis(){
+                               Map<String,IInput> r = new 
HashMap(inputs.inputKeys().size());
+                               for(String k: inputs.inputKeys()){
+                                       if(k.startsWith("vexis")){
+                                               String name = 
k.substring("vexis".length());
+                                               r.put(name,inputs.getInput(k));
+                                       }
+                               }
+                               return r;
+                       }
+                       private String insertBeforeSuffix(String 
filename_suffix, String insert){
+                               String suffix = 
FileUtil.fileSuffix(filename_suffix);
+                               String filename = 
FileUtil.removeSuffix(filename_suffix);
+                               String r = filename+insert;
+                               if(suffix!=null){
+                                       r+=suffix;
+                               }
+                               return r;
+                       }
+                       
+                   private String generateParams(Collection<String> vexis) 
throws Exception{
+                       String ROOT = props.expectString("root");
+                       StringBuilder sb  = new StringBuilder();
+                       sb.append(
+                           "     <param name='core' 
value='"+ROOT+"/"+core.getName()+"'/>\n"+
+                           "     <param name='option0' value='-t'/>\n"+
+                           "     <param name='option1' 
value='"+props.getString("main-template","main")+"'/>\n");
+                       int i=0;
+                       for(String v: vexis){
+                           sb.append(
+                          "     <param name='vexi"+(i++)+"' 
value='"+ROOT+"/"+v+"'/>\n");
+                       }
+                       return sb.toString();
+                   }
+
+                   public String generateLaunchHtml(Collection<String> vexis) 
throws Exception{
+                       String ROOT = props.expectString("root");
+                       
+                       Map<String,String> model = new HashMap(); 
+                       model.put("applet-params", generateParams(vexis));
+                       model.put("applet-jar", ROOT+"/"+launcher.getName());
+                       
model.put("applet-class",props.expectString("applet-class"));
+                       
+                       String error = BasicTemplate.checkVars(template, 
model.keySet()); 
+                       if(error!=null) throw new BuildPluginException("Problem 
with launch template '"+templateName+"': " +error);
+                       return BasicTemplate.apply(template, model);
+                   }
+                       
+                       private void generateLauncherPage(String _name, 
Collection<String> vexis) throws Exception{
+                    String filename = insertBeforeSuffix(templateName, _name);
+                    // HACK file copied out, but o
+                    File outputfile = new File(output, filename);
+                    IOUtil.stringToFile(generateLaunchHtml(vexis), outputfile);
+                       }
+                       
+                               public Object run() throws Exception {
+                           output.mkdirs();
+
+                                       copyToDir("launcher", launcher);
+                                       copyToDir("core", core);
+                                       
+                                       
//////////////////////////////////////////////////////////
+                                       // PROCESS launcher page input 
(template and static files)
+                               final IInput launcherPageInput = 
inputs.getInput("launcher-page");
+                               if(launcherPageInput!=null){ 
+                                       for(File f: 
launcherPageInput.getArtifacts()){
+                                       if(f.getName().equals(templateName)){
+                                               this.template = 
IOUtil.fileToString(f); 
+                                       }else{
+                                               FileUtil.mergeCopy(f, output);
+                                       }
+                               }
+                               if(this.template==null){
+                                       throw new BuildPluginException("Input 
'launcher-page', but no template file: "+templateName);
+                               }
+                               }
+
+                               
+                                       
//////////////////////////////////////////////////////////
+                                       // PROCESS vexi inputs
+                                       // two modes
+                           //    - a single list of vexis
+                           //    - multiple named applications
+                                       Map<String,IInput> vexisInput = 
justVexis();
+                                       if(vexisInput.size()==0) throw new 
BuildPluginException("No 'vexis*' inputs provided");
+                                       
+                                       Set<File> vexiFilesAll = new HashSet();
+                           Map<String,Collection<String>> vexisMap = new 
HashMap();
+                                       for(String _name: vexisInput.keySet()){
+                                               IInput input = 
vexisInput.get(_name);
+                                               Collection<File> vexiFiles = 
input.getArtifacts();
+                                               Collection<String> vexis = 
asNameList(vexiFiles);
+                                               if(template!=null) 
generateLauncherPage(_name, vexis);
+                                               vexisMap.put("vexis"+_name, 
vexis);
+                                               vexiFilesAll.addAll(vexiFiles);
+                                       }
+                                       
+                                       copyToDir("vexis", vexiFilesAll);
+
+                           //////////////////////
+                           // GENERATE json
+                           String launcherJson = 
props.getString("launcher-json");
+                           if(launcherJson!=null){
+                           Map m = new LinkedHashMap();
+                           m.put("applet-jar",launcher.getName());
+                           
m.put("applet-class",props.expectString("applet-class"));
+                           m.put("core-signed",core.getName());
+                           m.putAll(vexisMap);
+
+                               File file = new File(output, launcherJson);
+                               JSONUtil.writeObject(file, m);
+                           }            
+                                       return null;
+                               }
+               }.run();
+               
         } catch (Exception e) {
             throw BuildPluginException.wrap(e);
         }
-
+        return CollectionUtil.singletonList(output);
     }
-    
-    public String generateLaunchHtml(File templateFile, IPropertyMap props, 
File core, File launcher, Collection<String> vexis) throws Exception{
-        String template = IOUtil.fileToString(templateFile);
-        String ROOT = props.expectString("root");
-        
-        Map<String,String> model = new HashMap(); 
-        model.put("applet-params", generateParams(props, core, vexis));
-        model.put("applet-jar", ROOT+"/"+launcher.getName());
-        model.put("applet-class",props.expectString("applet-class"));
-        
-        String error = BasicTemplate.checkVars(template, model.keySet()); 
-        if(error!=null) throw new BuildPluginException("Problem with launch 
template '"+templateFile.getName()+"': " +error);
-        return BasicTemplate.apply(template, model);
-    }
-    
-    private String generateParams(IPropertyMap props, File core, 
Collection<String> vexis) throws Exception{
-        String ROOT = props.expectString("root");
-        StringBuilder sb  = new StringBuilder();
-        sb.append(
-            "     <param name='core' value='"+ROOT+"/"+core.getName()+"'/>\n"+
-            "     <param name='option0' value='-t'/>\n"+
-            "     <param name='option1' 
value='"+props.getString("main-template","main")+"'/>\n");
-        int i=0;
-        for(String v: vexis){
-            sb.append(
-           "     <param name='vexi"+(i++)+"' value='"+ROOT+"/"+v+"'/>\n");
-        }
-        return sb.toString();
-    }
-    
-    public Map generateLaunchJson(IPropertyMap props, File core, File 
launcher, Collection<String> vexis) throws Exception{
-        Map m = new LinkedHashMap();
-        m.put("applet-jar",launcher.getName());
-        m.put("applet-class",props.expectString("applet-class"));
-        m.put("core-signed",core.getName());
-        m.put("vexis", vexis);
-        return m;
-    }
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Benefiting from Server Virtualization: Beyond Initial Workload 
Consolidation -- Increasing the use of server virtualization is a top
priority.Virtualization can reduce costs, simplify management, and improve 
application availability and disaster protection. Learn more about boosting 
the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to