| 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 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
protected BuildPluginManager pluginManager; |
| 42 | |
|
| 43 | |
private static final int DOMAIN_PING_TIMEOUT = 1000; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
protected boolean failOnExportError; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
protected boolean skipExport; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
protected String exportPropFileName; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
protected abstract File getDeployGenSrcDir(); |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
protected abstract String getProjectProcessesXarDir(); |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
protected abstract String getTmpExportToGeneratedSrcDir(); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 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 | |
|
| 98 | |
|
| 99 | |
return; |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | if (!skipExport) { |
| 103 | |
|
| 104 | |
|
| 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 | |
|
| 113 | 0 | getMFUtils().cleanup(); |
| 114 | 0 | } |
| 115 | |
|
| 116 | 0 | } |
| 117 | |
|
| 118 | |
private void validateGeneratedSrc() throws MojoExecutionException { |
| 119 | |
|
| 120 | |
|
| 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 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 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 | |
|
| 143 | |
|
| 144 | 0 | FileArtifactStorage destStorage = new FileArtifactStorage(); |
| 145 | 0 | destStorage.addNotificationListener(new NullArtifactNotificationListener()); |
| 146 | 0 | destStorage.setRoot(tmpExportDir); |
| 147 | |
|
| 148 | |
|
| 149 | 0 | ExportPropertiesArtifact expProps = getExportProperties(); |
| 150 | |
|
| 151 | |
|
| 152 | 0 | addExportIgnores(expProps); |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | 0 | DSArtifactStorage storage = getMFUtils().getDSArtifactStorage(); |
| 157 | 0 | ESBArtifactCopyJob copyJob = new ESBArtifactCopyJob(storage, destStorage, expProps, failOnExportError); |
| 158 | |
|
| 159 | |
|
| 160 | 0 | copyJob.setCleanBeforeCopy(true); |
| 161 | |
|
| 162 | 0 | getLog().info("Exporting resources from Sonic Domain ..."); |
| 163 | |
|
| 164 | |
|
| 165 | 0 | copyJob.copy(); |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | 0 | File dxsiExtractDir = new File(getDependencyDirectory(), "dxsi"); |
| 171 | |
|
| 172 | |
|
| 173 | 0 | File xarTmpDir = new File(dxsiExtractDir, "xarTmp"); |
| 174 | |
|
| 175 | 0 | FileUtilities.copyFiles(xarTmpDir.getPath(), tmpExportDir, null, null, null, false); |
| 176 | |
|
| 177 | |
|
| 178 | 0 | beautifyXarDirectory(tmpExportDirFile); |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | 0 | getLog().info("Deleting all files from " + getDeployGenSrcDir().getPath() + " directory."); |
| 186 | 0 | FileUtilities.deleteFilesInDir(getDeployGenSrcDir().getPath(), "**/*", "**/.*"); |
| 187 | |
|
| 188 | |
|
| 189 | 0 | new File(getDeployGenSrcDir().getPath()).mkdirs(); |
| 190 | 0 | getLog().info("Copying files to " + getDeployGenSrcDir().getPath() + " directory."); |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 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 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
protected void addExportIgnores(final ExportPropertiesArtifact _expProps) { |
| 215 | 0 | addExportIgnoresForDependencies(_expProps); |
| 216 | |
|
| 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 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 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 | |
|
| 239 | |
|
| 240 | |
|
| 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 | |
|
| 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 | |
|
| 332 | |
|
| 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 | |
} |