Coverage Report - com.aurea.maven.plugins.sonic.esb.AbstractExportGeneratedSrcMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractExportGeneratedSrcMojo
0%
0/122
0%
0/36
3.5
 
 1  
 package com.aurea.maven.plugins.sonic.esb;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.util.List;
 6  
 
 7  
 import org.apache.maven.plugin.BuildPluginManager;
 8  
 import org.apache.maven.plugin.MojoExecutionException;
 9  
 import org.apache.maven.plugin.MojoFailureException;
 10  
 import org.codehaus.plexus.util.FileUtils;
 11  
 
 12  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 13  
 import com.aurea.maven.plugins.sonic.utils.ProjectAnalyzer;
 14  
 import com.aurea.maven.plugins.sonic.utils.xmlsorter.XMLSort;
 15  
 import com.progress.sonic.utilities.esb.admin.ESBAdmin;
 16  
 import com.progress.sonic.utilities.esb.admin.ESBArtifactCopyJob;
 17  
 import com.progress.sonic.utilities.esb.admin.EndpointAdmin;
 18  
 import com.progress.sonic.utilities.esb.admin.ProcessAdmin;
 19  
 import com.progress.sonic.utilities.esb.admin.ServiceAdmin;
 20  
 import com.progress.sonic.utilities.mfutils.MFUtils;
 21  
 import com.progress.sonic.utilities.mfutils.NullArtifactNotificationListener;
 22  
 import com.sonicsw.deploy.IArtifact;
 23  
 import com.sonicsw.deploy.artifact.ArtifactFactory;
 24  
 import com.sonicsw.deploy.artifact.ESBArtifact;
 25  
 import com.sonicsw.deploy.artifact.SonicFSArtifact;
 26  
 import com.sonicsw.deploy.storage.DSArtifactStorage;
 27  
 import com.sonicsw.deploy.storage.FileArtifactStorage;
 28  
 import com.sonicsw.deploy.tools.common.ExportPropertiesArtifact;
 29  
 
 30  
 /**
 31  
  * 
 32  
  */
 33  0
 public abstract class AbstractExportGeneratedSrcMojo extends AbstractESBConnectMojo {
 34  
 
 35  
         /**
 36  
          * The Maven PluginManager Object.
 37  
          * 
 38  
          * @component
 39  
          * @required
 40  
          */
 41  
         protected BuildPluginManager pluginManager;
 42  
 
 43  
         private static final int DOMAIN_PING_TIMEOUT = 1000;
 44  
 
 45  
         /**
 46  
          * Defines whether Maven should fail if the esb export failed.
 47  
          * 
 48  
          * @parameter property="sonicesb.failOnExportError" default-value="true"
 49  
          * @required
 50  
          */
 51  
         protected boolean failOnExportError;
 52  
 
 53  
         /**
 54  
          * Defines whether the export from the directory Service shall be skipped.
 55  
          * 
 56  
          * @parameter property="sonicesb.skipExport" default-value="false"
 57  
          * @required
 58  
          */
 59  
         protected boolean skipExport;
 60  
 
 61  
         /**
 62  
          * File containing definition of additional artifacts to be exported from
 63  
          * the Sonic Domain.
 64  
          * 
 65  
          * @parameter default-value="ExportProperties.xml"
 66  
          * @required
 67  
          */
 68  
         protected String exportPropFileName;
 69  
 
 70  
         /**
 71  
          * @return the directory for storing the generated-src (endpoints,
 72  
          *         connections, etc. Eg. /deploy/generated-src/xar)
 73  
          */
 74  
         protected abstract File getDeployGenSrcDir();
 75  
 
 76  
         /**
 77  
          * @return the directory containing the Processes xar structure for the
 78  
          *         current project.
 79  
          */
 80  
         protected abstract String getProjectProcessesXarDir();
 81  
 
 82  
         /**
 83  
          * @return the temporaroy directory to export to from the Sonic Domain,
 84  
          *         before moving it to the deploy/generated-src location.
 85  
          */
 86  
         protected abstract String getTmpExportToGeneratedSrcDir();
 87  
 
 88  
         /**
 89  
          * {@inheritDoc}
 90  
          */
 91  
         protected final void doExecute() throws MojoExecutionException, MojoFailureException {
 92  0
                 super.doExecute();
 93  
 
 94  
                 try {
 95  0
                         if (!skipExport && !MFUtils.pingDomain(getManagementUrl(), DOMAIN_PING_TIMEOUT)) {
 96  0
                                 getLog().warn("Could not connect to Sonic Domain");
 97  
                                 // At this place we should inject the esbdl and lib files into
 98  
                                 // the generated source directory ....
 99  
                                 return;
 100  
                         }
 101  
 
 102  0
                         if (!skipExport) {
 103  
                                 // export the latest version of artifacts from the Domain to the
 104  
                                 // deploy generated-src direcotry
 105  0
                                 exportToDeployGeneratedSrc();
 106  
                         } else {
 107  0
                                 getLog().info("Skipping export of resources from the directory Service");
 108  
                         }
 109  
 
 110  0
                         validateGeneratedSrc();
 111  
                 } finally {
 112  
                         // cleanup/free DS resources
 113  0
                         getMFUtils().cleanup();
 114  0
                 }
 115  
 
 116  0
         }
 117  
 
 118  
         private void validateGeneratedSrc() throws MojoExecutionException {
 119  
 
 120  
                 // Check for the existing of the generated sources directory
 121  0
                 if (!getDeployGenSrcDir().exists() || !getDeployGenSrcDir().isDirectory()) {
 122  0
                         throw new MojoExecutionException("The generated directory " + getDeployGenSrcDir()
 123  
                                         + " does not exist. Build cannot continue.");
 124  
                 }
 125  
 
 126  
                 // perhaps other things like the process definitions, etc could also be
 127  
                 // validated
 128  
 
 129  0
         }
 130  
 
 131  
         /**
 132  
          * exports the local process files to the /deploy/generated-src directory.
 133  
          * 
 134  
          * @return true if the export succeeded.
 135  
          */
 136  
         protected void exportToDeployGeneratedSrc() throws MojoExecutionException {
 137  
                 try {
 138  0
                         String tmpExportDir = getOutputDirectory() + getTmpExportToGeneratedSrcDir();
 139  0
                         File tmpExportDirFile = new File(tmpExportDir);
 140  0
                         tmpExportDirFile.mkdirs();
 141  
 
 142  
                         // export to a temporary location, so that if the export fails, the
 143  
                         // generatedSrc location isn't broken
 144  0
                         FileArtifactStorage destStorage = new FileArtifactStorage();
 145  0
                         destStorage.addNotificationListener(new NullArtifactNotificationListener());
 146  0
                         destStorage.setRoot(tmpExportDir);
 147  
 
 148  
                         // get the list of artifacts to be exported
 149  0
                         ExportPropertiesArtifact expProps = getExportProperties();
 150  
 
 151  
                         // add the list of things to ignore
 152  0
                         addExportIgnores(expProps);
 153  
 
 154  
                         // prepare to copy from local DS to the esb export directory
 155  
                         // (filesystem)
 156  0
                         DSArtifactStorage storage = getMFUtils().getDSArtifactStorage();
 157  0
                         ESBArtifactCopyJob copyJob = new ESBArtifactCopyJob(storage, destStorage, expProps, failOnExportError);
 158  
 
 159  
                         // make sure that the destination store gets cleaned before copy
 160  0
                         copyJob.setCleanBeforeCopy(true);
 161  
 
 162  0
                         getLog().info("Exporting resources from Sonic Domain ...");
 163  
 
 164  
                         // "execute" the copy job
 165  0
                         copyJob.copy();
 166  
 
 167  
                         // // tailor any SonicConnect paths
 168  
                         // tailorSonicConnectConfigs();
 169  
 
 170  0
                         File dxsiExtractDir = new File(getDependencyDirectory(), "dxsi");
 171  
                         // File dependencyTmpDir = new File(dxsiExtractDir,
 172  
                         // "dependencyTmp");
 173  0
                         File xarTmpDir = new File(dxsiExtractDir, "xarTmp");
 174  
 
 175  0
                         FileUtilities.copyFiles(xarTmpDir.getPath(), tmpExportDir, null, null, null, false);
 176  
 
 177  
                         // beautify the various Sonic files
 178  0
                         beautifyXarDirectory(tmpExportDirFile);
 179  
 
 180  
                         // copy files to the generatedSrc directory
 181  
 
 182  
                         // delete all files first to make sure that the resulting XAR
 183  
                         // structure does not contain any old artifacts.
 184  
                         // exclude hidden files e.g. .svn
 185  0
                         getLog().info("Deleting all files from " + getDeployGenSrcDir().getPath() + " directory.");
 186  0
                         FileUtilities.deleteFilesInDir(getDeployGenSrcDir().getPath(), "**/*", "**/.*");
 187  
 
 188  
                         // copy the exported ESB files into place
 189  0
                         new File(getDeployGenSrcDir().getPath()).mkdirs();
 190  0
                         getLog().info("Copying files to " + getDeployGenSrcDir().getPath() + " directory.");
 191  
                         // SFR removed +"/ESB" restriction whcih was introduced in rev 9744
 192  
                         // we have to copy SonicFS as well if there are dependencies to
 193  
                         // SonicFS artifacts
 194  
                         // this at least shows the developer that he has a missing
 195  
                         // dependency
 196  
                         // otherwise you only knwo after deployment test!
 197  0
                         FileUtilities.copyFiles(tmpExportDir, getDeployGenSrcDir().getPath(), null, null, null, false);
 198  
 
 199  0
                 } catch (Exception e) {
 200  0
                         getLog().error("Export of resources failed", e);
 201  0
                         throw new MojoExecutionException(e.getMessage(), e);
 202  
                 } finally {
 203  0
                         getMFUtils().cleanup();
 204  0
                 }
 205  0
         }
 206  
 
 207  
         /**
 208  
          * adds any Sonic artifacts from dependent maven artifacts to the export
 209  
          * ignores.
 210  
          * 
 211  
          * @param _expProps
 212  
          *            the export properties to be updated.
 213  
          */
 214  
         protected void addExportIgnores(final ExportPropertiesArtifact _expProps) {
 215  0
                 addExportIgnoresForDependencies(_expProps);
 216  
                 // add system resources
 217  0
                 IArtifact sonicfs = ArtifactFactory
 218  
                                 .createArtifact(SonicFSArtifact.TYPE, "System/SonicESB/jsHelperFunctions.js");
 219  0
                 _expProps.addIgnore(sonicfs);
 220  0
                 sonicfs = ArtifactFactory.createArtifact(SonicFSArtifact.TYPE, "System/SonicESB/7.6/jsHelperFunctions.js");
 221  0
                 _expProps.addIgnore(sonicfs);
 222  0
                 sonicfs = ArtifactFactory.createArtifact(SonicFSArtifact.TYPE,
 223  
                                 "System/SonicESB/7.6/Common/jsHelperFunctions.js");
 224  0
                 _expProps.addIgnore(sonicfs);
 225  
 
 226  
                 // // Ignore Sonic Connect Service Type
 227  
                 // sonicfs = ArtifactFactory.createArtifact(ESBArtifact.SERVICE_TYPE,
 228  
                 // "SonicConnect");
 229  
                 // _expProps.addIgnore(sonicfs);
 230  
                 //
 231  
                 // Finally add our own Sonicfs root to the ignores
 232  0
                 sonicfs = ArtifactFactory.createArtifact(SonicFSArtifact.TYPE, "workspace/" + getProjectName() + "/");
 233  0
                 _expProps.addIgnore(sonicfs);
 234  0
                 getLog().info("Done...Building Artifact ignore List from Dependencies : ...");
 235  0
         }
 236  
 
 237  
         /**
 238  
          * @return the export properties for the pending export, by reading any
 239  
          *         ExportProperties.xml file (if one is defined) and adding the
 240  
          *         local processes.
 241  
          */
 242  
         protected ExportPropertiesArtifact getExportProperties() throws MojoExecutionException {
 243  
 
 244  0
                 ExportPropertiesArtifact expProps = new ExportPropertiesArtifact();
 245  
 
 246  0
                 File expPropFile = getExportPropFile();
 247  
 
 248  
                 try {
 249  0
                         getLog().debug("TBO ---- File Information: " + expPropFile.getAbsolutePath());
 250  0
                         if (expPropFile.exists() && expPropFile.isFile() && expPropFile.canRead()) {
 251  0
                                 expProps.init(expPropFile);
 252  
                                 // expProps.addIgnore(ExportPropertiesArtifact.DEFAULT_IGNORE);
 253  0
                                 expProps.addIgnore(ExportPropertiesArtifact.getDefaultIgnore());
 254  0
                                 getLog().info("Using existing property file : " + getExportPropFile());
 255  
                         }
 256  0
                 } catch (Exception e) {
 257  0
                         getLog().warn("Error reading " + getExportPropFile() + "! Will not use it.");
 258  0
                         expProps = new ExportPropertiesArtifact();
 259  0
                 }
 260  
 
 261  0
                 final ESBAdmin esbAdmin = getMFUtils().getESBAdmin();
 262  0
                 final ProcessAdmin processAdmin = new ProcessAdmin(esbAdmin.getEsbApi());
 263  0
                 final ServiceAdmin serviceAdmin = new ServiceAdmin(esbAdmin.getEsbApi());
 264  0
                 final EndpointAdmin endpointAdmin = new EndpointAdmin(esbAdmin.getEsbApi());
 265  
 
 266  
                 try {
 267  0
                         getLog().info("Scan for local service configurations to be included in domain export...");
 268  0
                         for (File serviceFile : ProjectAnalyzer.getServiceConfigSourceFiles(getSonicEsbSourceDirectory()
 269  
                                         .getAbsolutePath())) {
 270  0
                                 String serviceConfigName = ProjectAnalyzer.getServiceConfigNameFromFile(serviceFile);
 271  0
                                 getLog().info("Found ESB Service Configuration : " + serviceConfigName);
 272  
 
 273  0
                                 getLog().info("Importing '" + serviceConfigName + "' service config in Sonic Domain");
 274  0
                                 serviceAdmin.importServiceConfig(serviceFile, true);
 275  
 
 276  0
                                 String endpoint = ProjectAnalyzer.getServiceConfigEntryRefFromFile(serviceFile);
 277  0
                                 if (endpoint != null && !endpoint.isEmpty()) {
 278  0
                                         getLog().info(
 279  
                                                         "Creating '" + endpoint + "' endpoint in Sonic Domain for ESB Service " + serviceConfigName);
 280  0
                                         endpointAdmin.createTopicEndpoint(endpoint);
 281  
                                 }
 282  
 
 283  0
                                 IArtifact artifact = new ESBArtifact(ESBArtifact.SERVICE, serviceConfigName);
 284  0
                                 expProps.addRoot(artifact);
 285  
                         }
 286  
 
 287  0
                         getLog().info("Scan for Sonic Connect service configuration to be included in domain export...");
 288  0
                         File defaultSonicConnectServiceConfig = new File(project.getBasedir(), connectServiceLocation);
 289  0
                         if (defaultSonicConnectServiceConfig.exists()) {
 290  0
                                 String serviceConfigName = ProjectAnalyzer
 291  
                                                 .getServiceConfigNameFromFile(defaultSonicConnectServiceConfig);
 292  0
                                 getLog().info("Found ESB Service Configuration : " + serviceConfigName);
 293  
 
 294  0
                                 getLog().info("Importing '" + serviceConfigName + "' service config in Sonic Domain");
 295  0
                                 serviceAdmin.importServiceConfig(defaultSonicConnectServiceConfig, true);
 296  
 
 297  0
                                 String endpoint = ProjectAnalyzer.getServiceConfigEntryRefFromFile(defaultSonicConnectServiceConfig);
 298  0
                                 if (endpoint != null && !endpoint.isEmpty()) {
 299  0
                                         getLog().info(
 300  
                                                         "Creating '" + endpoint + "' endpoint in Sonic Domain for ESB Service " + serviceConfigName);
 301  0
                                         endpointAdmin.createTopicEndpoint(endpoint);
 302  
                                 }
 303  
 
 304  0
                                 IArtifact artifact = new ESBArtifact(ESBArtifact.SERVICE, serviceConfigName);
 305  0
                                 expProps.addRoot(artifact);
 306  
                         }
 307  
 
 308  0
                         getLog().info("Scan for local processes to be included in domain export...");
 309  0
                         for (File processFile : ProjectAnalyzer.getProcessSourceFiles(getSonicEsbSourceDirectory()
 310  
                                         .getAbsolutePath())) {
 311  0
                                 String processName = ProjectAnalyzer.getProcessNameFromFile(processFile);
 312  0
                                 getLog().info("Found ESB Process : " + processName);
 313  
 
 314  0
                                 getLog().info("Importing '" + processName + "' process in Sonic Domain");
 315  0
                                 processAdmin.importProcess(processFile, true);
 316  
 
 317  0
                                 IArtifact artifact = new ESBArtifact(ESBArtifact.PROCESS, processName);
 318  0
                                 expProps.addRoot(artifact);
 319  
                         }
 320  
 
 321  0
                 } catch (Exception e) {
 322  0
                         getLog().warn("Using empty root list as export properties");
 323  0
                         getLog().debug(e);
 324  0
                         expProps = new ExportPropertiesArtifact();
 325  0
                 }
 326  
 
 327  0
                 return expProps;
 328  
         }
 329  
 
 330  
         /**
 331  
          * @return the file containing the definition of any addition artifacts to
 332  
          *         be exported from the Sonic Domain.
 333  
          */
 334  
         protected File getExportPropFile() {
 335  
 
 336  0
                 return new File(getSonicEsbSourceDirectory(), exportPropFileName);
 337  
         }
 338  
 
 339  
         private void beautifyXarDirectory(final File xarDir) {
 340  
 
 341  0
                 getLog().info("Beautifying ESB artifacts");
 342  0
                 String[] dirsToBeautify = { "ESB/Endpoints", "ESB/Services" };
 343  0
                 for (String dir : dirsToBeautify) {
 344  0
                         File workDir = new File(xarDir, dir);
 345  0
                         if (workDir.canRead()) {
 346  
                                 try {
 347  0
                                         List<File> workFiles = FileUtils.getFiles(workDir, "*.xml", "", true);
 348  0
                                         for (File workFile : workFiles) {
 349  0
                                                 getLog().debug("Beautifying file " + workFile.getName());
 350  0
                                                 XMLSort.sortXMLFile(workFile);
 351  0
                                         }
 352  0
                                 } catch (IOException e) {
 353  0
                                         getLog().error(e);
 354  0
                                 }
 355  
                         }
 356  
                 }
 357  0
         }
 358  
 
 359  
 }