Coverage Report - com.aurea.maven.plugins.sonic.esb.ConnectPackageMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectPackageMojo
0%
0/76
0%
0/10
4.8
ConnectPackageMojo$SortedProperties
0%
0/7
0%
0/2
4.8
 
 1  
 /**
 2  
  * 
 3  
  */
 4  
 package com.aurea.maven.plugins.sonic.esb;
 5  
 
 6  
 import java.io.File;
 7  
 import java.io.FileInputStream;
 8  
 import java.io.FileNotFoundException;
 9  
 import java.io.FileOutputStream;
 10  
 import java.io.IOException;
 11  
 import java.util.Collections;
 12  
 import java.util.Enumeration;
 13  
 import java.util.Properties;
 14  
 import java.util.Vector;
 15  
 
 16  
 import org.apache.maven.artifact.handler.ArtifactHandler;
 17  
 import org.apache.maven.execution.MavenSession;
 18  
 import org.apache.maven.plugin.BuildPluginManager;
 19  
 import org.apache.maven.plugin.MojoExecutionException;
 20  
 import org.apache.maven.plugin.MojoFailureException;
 21  
 import org.codehaus.plexus.archiver.ArchiverException;
 22  
 import org.codehaus.plexus.archiver.zip.ZipArchiver;
 23  
 import org.codehaus.plexus.util.FileUtils;
 24  
 import org.twdata.maven.mojoexecutor.MojoExecutor;
 25  
 
 26  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 27  
 
 28  
 /**
 29  
  * @author Jamie Townsend
 30  
  * 
 31  
  * @goal connect-package
 32  
  */
 33  0
 public class ConnectPackageMojo extends EsbPackageMojo {
 34  
 
 35  
 
 36  
   /**
 37  
    * The Maven Session Object (only required for unpacking dependencies with the maven-unpack-plugin).
 38  
    * 
 39  
    * @parameter default-value="${session}"
 40  
    * @required
 41  
    * @readonly
 42  
    */
 43  
   private MavenSession    session;
 44  
 
 45  
   /**
 46  
    * The Maven PluginManager Object (only required for unpacking dependencies with the maven-unpack-plugin).
 47  
    * 
 48  
    * @component
 49  
    * @required
 50  
    */
 51  
   protected BuildPluginManager pluginManager;
 52  
 
 53  
 
 54  
   /**
 55  
    * @component
 56  
    */
 57  
   ArtifactHandler         handler;
 58  
   
 59  
   /**
 60  
    * the spring.xml configuration file for Sonic Connect
 61  
    * 
 62  
    * @parameter default-value="config/spring.xml"
 63  
    */
 64  
   protected String        connectSpringLocation;
 65  
   
 66  
   /**
 67  
    * the project.properties configuration file for Sonic Connect
 68  
    * 
 69  
    * @parameter default-value="${project.artifactId}.properties"
 70  
    */
 71  
   protected String        connectPropertiesLocation;
 72  
   
 73  
   /**
 74  
    * the .services folder for Sonic Connect services (expose/invoke)
 75  
    * 
 76  
    * @parameter default-value=".services"
 77  
    */
 78  
   protected String        connectServicesFolder;
 79  
 
 80  
   // /**
 81  
   // * borrowed from Eclipse sonicconnect plugin /resrc/defaultArchiveFilter.properties
 82  
   // *
 83  
   // * - added the Maven target and Sonic deploy directory.
 84  
   // *
 85  
   // * @parameter expression="${sonicconnect.excludeDirs}" default-value=".settings,bin,classes,target,deploy"
 86  
   // */
 87  
   // private String excludeDirs;
 88  
   //
 89  
   // /**
 90  
   // * borrowed from Eclipse sonicconnect plugin /resrc/defaultArchiveFilter.properties
 91  
   // *
 92  
   // * @parameter expression="${sonicconnect.excludeFileExtensions}" default-value=
 93  
   // * ".classpath,.project,.class,.esbp,.esbstyp,.java,.properties,.csar"
 94  
   // */
 95  
   // private String excludeFileExtensions;
 96  
 
 97  
   /**
 98  
    * {@inheritDoc}
 99  
    */
 100  
   @Override
 101  
   protected void doExecute() throws MojoExecutionException, MojoFailureException {
 102  
 
 103  0
     getLog().info("Packaging Sonic Connect artifact");
 104  0
     project.getArtifact().setArtifactHandler(handler);
 105  
     // getLog().info(project.getArtifact().getArtifactHandler().getLanguage());
 106  
 
 107  
     try {
 108  0
       createConnectFiles();
 109  0
       copyLibFilesToPackageDir();
 110  
 
 111  0
       super.doExecute();
 112  
 
 113  0
     } catch (final MojoExecutionException e) {
 114  0
       getLog().error("Error packaging Sonic Connect artifact : " + e.getMessage(), e);
 115  0
       throw new MojoExecutionException("Error packaging Sonic Connect artifact : ", e);
 116  0
     }
 117  0
   }
 118  
 
 119  
   private void copyLibFilesToPackageDir() throws MojoExecutionException {
 120  
 
 121  0
     final String targetLibDir = getPackageXarDir() + "/SonicFS/workspace/" + getProjectName() + "/lib";
 122  
     try {
 123  0
       FileUtilities.copyFiles(project.getBasedir() + "/lib", targetLibDir);
 124  0
     } catch (final Exception e) {
 125  
       // this should never happen
 126  0
       e.printStackTrace();
 127  0
       throw new MojoExecutionException("Error copy files from lib directory: " + e.getMessage(), e);
 128  0
     }
 129  
 
 130  0
   }
 131  
 
 132  
   /**
 133  
    * creates the .csar and .properties files associated with Sonic Connect projects.
 134  
    * 
 135  
    * @throws MojoExecutionException
 136  
    *           if something goes wrong
 137  
    */
 138  
   protected void createConnectFiles() throws MojoExecutionException {
 139  
 
 140  0
     final String packagedConnectFileTemplate = project.getBasedir() + "/lib/" + getProjectName();
 141  0
     final File connectCsarFile = new File(packagedConnectFileTemplate + ".csar");
 142  0
     final File connectPropertiesFile = new File(packagedConnectFileTemplate + ".properties");
 143  0
     final String baseDir = project.getBasedir().getAbsolutePath();
 144  
 
 145  
     // create the directory if it doesn't already exist
 146  0
     connectCsarFile.getParentFile().mkdirs();
 147  
 
 148  
     // delete the files if they exist to ensure they get re-generated
 149  0
     connectCsarFile.delete();
 150  0
     connectPropertiesFile.delete();
 151  
 
 152  0
     refreshDependenciesInLib();
 153  
 
 154  
     // create the minimal csar file
 155  
     try {
 156  0
       final ZipArchiver za = new ZipArchiver();
 157  0
       za.setDestFile(connectCsarFile);
 158  
       
 159  
       // add spring.xml
 160  0
       za.addFile(new File(baseDir, connectSpringLocation), "config/spring.xml");
 161  
 
 162  
       // add .esbcamel files
 163  0
       final String servicesDir = new File(baseDir, connectServicesFolder).getPath();
 164  0
       final String[] esbCamelFiles = FileUtilities.getFileList(servicesDir, "**/*.esbcamel", null);
 165  0
       for (String fileName : esbCamelFiles) {
 166  0
               final File file = new File(servicesDir, fileName);
 167  0
               za.addFile(file, ".services/" + fileName);
 168  
       }
 169  
   
 170  
       // add all .wsdl files (note that this will pick up all .wsdl files, regardless of location, including
 171  
       // target/**/.wsdl. also added the .xsd files as wsdl may have dependencies on the local xsd NVR)
 172  0
       final String[] wsdlFileNames = FileUtilities.getFileList(baseDir,
 173  
           "**/*.wsdl,**/*.xsd", null);
 174  0
       for (final String fileName : wsdlFileNames) {
 175  0
         za.addFile(new File(baseDir, fileName), fileName);
 176  
       }
 177  
 
 178  
       // add the artifact jar (if one exists)
 179  0
       final String jarFileName = project.getArtifactId() + "-" + project.getVersion() + ".jar";
 180  0
       za.addFile(new File(project.getBuild().getDirectory(), jarFileName), jarFileName);
 181  
 
 182  
       // add any dependent jar's
 183  0
       final String[] dependentJars = {
 184  
         "*.jar"
 185  
       };
 186  0
       za.addDirectory(new File(baseDir, "lib"), dependentJars, null);
 187  
 
 188  0
       za.createArchive();
 189  0
     } catch (final ArchiverException e) {
 190  0
             throw new MojoExecutionException(e.getMessage(), e);
 191  0
     } catch (final IOException e) {
 192  0
             throw new MojoExecutionException(e.getMessage(), e);
 193  0
       }
 194  
 
 195  
     // create the csar's properties file
 196  
 
 197  0
     final SortedProperties completePropertyList = new SortedProperties();
 198  
     try {
 199  
       // add the ${artifactId}.properties from the root directory
 200  0
       final String connectProperties = connectPropertiesLocation != null ? connectPropertiesLocation :
 201  
               project.getArtifactId() + ".properties";
 202  0
       final String[] topLevelPropertiesFileNames = FileUtilities.getFileList(baseDir,
 203  
                       connectProperties, null);
 204  0
       for (final String fileName : topLevelPropertiesFileNames) {
 205  0
         final Properties props = new Properties();
 206  0
         props.load(new FileInputStream(new File(baseDir, fileName)));
 207  0
         completePropertyList.putAll(props);
 208  
       }
 209  
 
 210  
       // add *.properties from the .services directory
 211  0
       final String[] servicesPropertiesFileNames = FileUtilities.getFileList(
 212  
           new File(baseDir, connectServicesFolder).getAbsolutePath(), "**/*.properties", null);
 213  0
       for (final String fileName : servicesPropertiesFileNames) {
 214  0
         final Properties props = new Properties();
 215  0
         props.load(new FileInputStream(new File(new File(baseDir, connectServicesFolder), fileName)));
 216  0
         completePropertyList.putAll(props);
 217  
       }
 218  
 
 219  
       // persiste the complete .properties file into /lib
 220  0
       completePropertyList.store(new FileOutputStream(new File(new File(baseDir, "lib"), project
 221  
           .getArtifactId()
 222  
           + ".properties")), "Generated by maven-sonicesb-plugin:connect-package");
 223  
 
 224  0
     } catch (final FileNotFoundException e) {
 225  0
       throw new MojoExecutionException(e.getMessage(), e);
 226  0
     } catch (final IOException e) {
 227  0
       throw new MojoExecutionException(e.getMessage(), e);
 228  0
     }
 229  0
   }
 230  
 
 231  
   /**
 232  
    * cleans out dependencies (in fact all files!) from the /lib directory and copies runtime dependencies back into
 233  
    * /lib.
 234  
    * 
 235  
    * @throws MojoExecutionException
 236  
    *           if something goes wrong
 237  
    */
 238  
   private void refreshDependenciesInLib() throws MojoExecutionException {
 239  
 
 240  0
     final String libDirName = project.getBasedir() + "/lib/";
 241  0
     final File markersDirectory = new File(libDirName, "markers");
 242  
 
 243  
     // delete any other files in the lib directory - perhaps the maven-clean-plugin should do this instead?
 244  0
     FileUtilities.deleteFilesInDir(project.getBasedir() + "/lib/", "*", "");
 245  
 
 246  0
     MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"), MojoExecutor
 247  
         .artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor.goal("copy-dependencies"),
 248  
         MojoExecutor.configuration(new MojoExecutor.Element[] {
 249  
           MojoExecutor.element(MojoExecutor.name("outputDirectory"), project.getBasedir() + "/lib"),
 250  
           MojoExecutor.element(MojoExecutor.name("includeScope"), "runtime"),
 251  
           MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
 252  
           MojoExecutor.element(MojoExecutor.name("includeTypes"), "jar"),
 253  
           MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "false"),
 254  
           MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "false")
 255  
         }), MojoExecutor.executionEnvironment(project, session, pluginManager));
 256  
 
 257  
     try {
 258  0
       FileUtils.deleteDirectory(markersDirectory);
 259  0
     } catch (final IOException e) {
 260  
       // This should never happen and if it does, it shouldn't really matter
 261  0
       getLog().error(e);
 262  0
     }
 263  0
   }
 264  
 
 265  
   // /**
 266  
   // * DeploymentArtifactsGenerator requires a Sonic Connect Archive Filter File. In the Sonic Workbench, this file is
 267  
   // * %SONIC_INSTALL%\Workbench8.0\eclipse \plugins\com.sonicsw.tools.sonicconnect_8
 268  
   // * .0.0\resrc\defaultArchiveFilter.properties since we don't have access to this file from Maven, we'll just make
 269  
   // our
 270  
   // * own. This also gives us the advantage of users being able to define additional exclusions should the need arise.
 271  
   // *
 272  
   // * @return the ArchiveFilterFile to be used
 273  
   // * @throws MojoExecutionException
 274  
   // * if something goes wrong
 275  
   // */
 276  
   // private File getArchiveFilterFile() throws MojoExecutionException {
 277  
   //
 278  
   // File archiveFilterFile;
 279  
   // try {
 280  
   // archiveFilterFile = File.createTempFile("sonicConnectArchiveFilter", "properties");
 281  
   // archiveFilterFile.deleteOnExit();
 282  
   //
 283  
   // Properties props = new Properties();
 284  
   // props.setProperty("excludeDirs", excludeDirs);
 285  
   // props.setProperty("excludeFileExtensions", excludeFileExtensions);
 286  
   // props.store(new FileOutputStream(archiveFilterFile),
 287  
   // "Temporary sonicConnectArchiveFilter.properties file used by the maven-sonicesb-plugin");
 288  
   //
 289  
   // } catch (IOException e) {
 290  
   // throw new MojoExecutionException("Error creating temporary file", e);
 291  
   // }
 292  
   // return archiveFilterFile;
 293  
   //
 294  
   // }
 295  
 
 296  0
   @SuppressWarnings("serial")
 297  0
   class SortedProperties extends Properties {
 298  
 
 299  
 
 300  
     @Override
 301  
     @SuppressWarnings({ "unchecked", "rawtypes" })
 302  
     public Enumeration keys() {
 303  
 
 304  0
       final Enumeration keysEnum = super.keys();
 305  0
       final Vector keyList = new Vector();
 306  0
       while (keysEnum.hasMoreElements()) {
 307  0
         keyList.add(keysEnum.nextElement());
 308  
       }
 309  0
       Collections.sort(keyList);
 310  0
       return keyList.elements();
 311  
     }
 312  
   }
 313  
 }