| 1 |  |  package com.aurea.maven.plugins.sonic.esb; | 
  | 2 |  |   | 
  | 3 |  |  import java.io.File; | 
  | 4 |  |  import java.io.FileInputStream; | 
  | 5 |  |  import java.util.ArrayList; | 
  | 6 |  |  import java.util.List; | 
  | 7 |  |  import java.util.Properties; | 
  | 8 |  |   | 
  | 9 |  |  import no.geosoft.cc.util.SmartTokenizer; | 
  | 10 |  |   | 
  | 11 |  |  import org.apache.maven.artifact.handler.ArtifactHandler; | 
  | 12 |  |  import org.apache.maven.plugin.MojoExecutionException; | 
  | 13 |  |  import org.apache.maven.plugin.MojoFailureException; | 
  | 14 |  |  import org.apache.maven.project.MavenProjectHelper; | 
  | 15 |  |  import org.codehaus.plexus.archiver.Archiver; | 
  | 16 |  |  import org.codehaus.plexus.util.xml.XmlStreamReader; | 
  | 17 |  |  import org.codehaus.plexus.util.xml.Xpp3Dom; | 
  | 18 |  |  import org.codehaus.plexus.util.xml.Xpp3DomBuilder; | 
  | 19 |  |   | 
  | 20 |  |  import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultServiceType; | 
  | 21 |  |  import com.aurea.maven.plugins.sonic.utils.FileUtilities; | 
  | 22 |  |  import com.progress.sonic.utilities.esb.admin.ESBAdmin; | 
  | 23 |  |  import com.progress.sonic.utilities.esb.admin.ESBArtifactCopyJob; | 
  | 24 |  |  import com.progress.sonic.utilities.esb.admin.EndpointAdmin; | 
  | 25 |  |  import com.progress.sonic.utilities.esb.admin.ServiceAdmin; | 
  | 26 |  |  import com.progress.sonic.utilities.mfutils.MFUtils; | 
  | 27 |  |  import com.progress.sonic.utilities.mfutils.NullArtifactNotificationListener; | 
  | 28 |  |  import com.sonicsw.deploy.IArtifact; | 
  | 29 |  |  import com.sonicsw.deploy.artifact.ESBArtifact; | 
  | 30 |  |  import com.sonicsw.deploy.artifact.SonicFSArtifact; | 
  | 31 |  |  import com.sonicsw.deploy.storage.FileArtifactStorage; | 
  | 32 |  |  import com.sonicsw.deploy.tools.common.ExportPropertiesArtifact; | 
  | 33 |  |  import com.sonicsw.esb.mgmtapi.config.IEndpointConfig; | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 | 0 |  public class MavenEsbSvcPackageMojo extends EsbPackageMojo { | 
  | 52 |  |   | 
  | 53 |  |          private static final int DOMAIN_PING_TIMEOUT = 1000; | 
  | 54 |  |   | 
  | 55 |  |           | 
  | 56 |  |   | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 |  |          MavenProjectHelper projectHelper; | 
  | 60 |  |   | 
  | 61 |  |           | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |          ArtifactHandler handler; | 
  | 65 |  |   | 
  | 66 |  |           | 
  | 67 |  |   | 
  | 68 |  |   | 
  | 69 |  |          private String classpathSeparator; | 
  | 70 |  |   | 
  | 71 |  |           | 
  | 72 |  |   | 
  | 73 |  |   | 
  | 74 | 0 |          private String ctClasspathPattern = null; | 
  | 75 |  |   | 
  | 76 |  |           | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 | 0 |          private String ctPrependClasspath = null; | 
  | 80 |  |   | 
  | 81 |  |           | 
  | 82 |  |   | 
  | 83 |  |   | 
  | 84 | 0 |          private String typeClasspathPattern = null; | 
  | 85 |  |   | 
  | 86 |  |           | 
  | 87 |  |   | 
  | 88 |  |   | 
  | 89 | 0 |          private String typePrependClasspath = null; | 
  | 90 |  |   | 
  | 91 |  |           | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 | 0 |          private String instancePrependClasspath = null; | 
  | 95 |  |   | 
  | 96 |  |           | 
  | 97 |  |   | 
  | 98 |  |   | 
  | 99 |  |          private boolean instanceSelfFirst; | 
  | 100 |  |   | 
  | 101 |  |           | 
  | 102 |  |   | 
  | 103 |  |   | 
  | 104 |  |          private boolean typeSelfFirst; | 
  | 105 |  |   | 
  | 106 |  |           | 
  | 107 |  |   | 
  | 108 |  |   | 
  | 109 |  |          protected void doExecute() throws MojoExecutionException, MojoFailureException { | 
  | 110 |  |   | 
  | 111 | 0 |                  getLog().info("Packaging ESB ServiceType ..."); | 
  | 112 | 0 |                  project.getArtifact().setArtifactHandler(handler); | 
  | 113 | 0 |                  getLog().debug(project.getArtifact().getArtifactHandler().getLanguage()); | 
  | 114 |  |   | 
  | 115 |  |                  try { | 
  | 116 |  |                           | 
  | 117 | 0 |                          createJarFile(); | 
  | 118 | 0 |                          super.doExecute(); | 
  | 119 |  |   | 
  | 120 |  |                           | 
  | 121 |  |                           | 
  | 122 | 0 |                  } catch (MojoExecutionException e) { | 
  | 123 | 0 |                          getLog().error("Error packaging ESB Service Type : " + e.getMessage(), e); | 
  | 124 | 0 |                          throw new MojoExecutionException("Error packaging ESB Service Type : ", e); | 
  | 125 | 0 |                  } | 
  | 126 | 0 |          } | 
  | 127 |  |   | 
  | 128 |  |          private File createJarFile() throws MojoExecutionException { | 
  | 129 |  |   | 
  | 130 |  |                  File jarFile; | 
  | 131 |  |   | 
  | 132 |  |                  try { | 
  | 133 | 0 |                          String jarFileName = getPackageXarDir() + "/SonicFS/" + getSonicFSResourcesDir() + "/" | 
  | 134 |  |                                          + getGroupId().replaceAll("\\.", "/") + "/" + getArtifactId() + "/" + getVersion() + "/" | 
  | 135 |  |                                          + getFinalAssemblyBaseName() + ".jar"; | 
  | 136 |  |   | 
  | 137 | 0 |                          jarFile = new File(jarFileName); | 
  | 138 | 0 |                          jarFile.getParentFile().mkdirs(); | 
  | 139 |  |   | 
  | 140 |  |                           | 
  | 141 |  |                           | 
  | 142 | 0 |                          FileUtilities.copyFiles(project.getBasedir() + "/src/main/resources", getOutputDirectory() + "/classes"); | 
  | 143 |  |   | 
  | 144 | 0 |                          Archiver archiver = archiverManager.getArchiver(jarFile); | 
  | 145 | 0 |                          archiver.setDestFile(jarFile); | 
  | 146 | 0 |                          archiver.addDirectory(new File(getOutputDirectory() + "/classes")); | 
  | 147 | 0 |                          archiver.createArchive(); | 
  | 148 | 0 |                  } catch (Exception e) { | 
  | 149 | 0 |                          throw new MojoExecutionException("Error creating ESB Servicetype Jar file", e); | 
  | 150 | 0 |                  } | 
  | 151 |  |   | 
  | 152 | 0 |                  return jarFile; | 
  | 153 |  |          } | 
  | 154 |  |   | 
  | 155 |  |           | 
  | 156 |  |   | 
  | 157 |  |   | 
  | 158 |  |          protected File createXarFile() throws Exception { | 
  | 159 | 0 |                  String xarDir = getPackageXarDir(); | 
  | 160 | 0 |                  new File(xarDir).mkdirs(); | 
  | 161 |  |   | 
  | 162 |  |                  try { | 
  | 163 | 0 |                          boolean connectedToDS = false; | 
  | 164 | 0 |                          EndpointAdmin endpointAdmin = null; | 
  | 165 | 0 |                          ServiceAdmin serviceAdmin = null; | 
  | 166 |  |   | 
  | 167 | 0 |                          if (!MFUtils.pingDomain(getManagementUrl(), DOMAIN_PING_TIMEOUT)) { | 
  | 168 | 0 |                                  getLog().warn("Could not connect to Sonic Domain at " + getManagementUrl()); | 
  | 169 |  |                          } else { | 
  | 170 | 0 |                                  connectedToDS = true; | 
  | 171 | 0 |                                  final ESBAdmin esbAdmin = getMFUtils().getESBAdmin(); | 
  | 172 | 0 |                                  serviceAdmin = new ServiceAdmin(esbAdmin.getEsbApi()); | 
  | 173 | 0 |                                  endpointAdmin = new EndpointAdmin(esbAdmin.getEsbApi()); | 
  | 174 |  |                          } | 
  | 175 |  |   | 
  | 176 | 0 |                          ArrayList<IArtifact> serviceTypes = new ArrayList<IArtifact>(); | 
  | 177 |  |   | 
  | 178 |  |                          try { | 
  | 179 | 0 |                                  StringBuffer buf = new StringBuffer(); | 
  | 180 | 0 |                                  buf.append("SonicFS/System/SonicESB/7.0/properties/servicetypes"); | 
  | 181 | 0 |                                  buf.append(","); | 
  | 182 | 0 |                                  buf.append("SonicFS/System/SonicESB/7.5/properties/servicetypes"); | 
  | 183 | 0 |                                  buf.append(","); | 
  | 184 | 0 |                                  buf.append("SonicFS/System/SonicESB/properties/servicetypes"); | 
  | 185 |  |   | 
  | 186 | 0 |                                  FileUtilities.copyFiles(getSonicEsbSourceDirectory().getAbsolutePath(), xarDir, | 
  | 187 |  |                                                  "**/*.properties,**/*.gif,**/*.xml", null, buf.toString(), true); | 
  | 188 |  |   | 
  | 189 | 0 |                                  FileUtilities.copyFiles(getSonicEsbSourceDirectory().getAbsolutePath(), xarDir, "**/*.esbstyp", null, | 
  | 190 |  |                                                  "ESB/ServiceTypes", true); | 
  | 191 |  |   | 
  | 192 | 0 |                                  for (String srcFile : FileUtilities.getFileList(xarDir, "**/*.esbstyp", null)) { | 
  | 193 | 0 |                                          File svcFile = new File(xarDir, srcFile); | 
  | 194 | 0 |                                          Xpp3Dom svcDom = Xpp3DomBuilder.build(new XmlStreamReader(svcFile)); | 
  | 195 | 0 |                                          String svcName = svcDom.getAttribute("name"); | 
  | 196 | 0 |                                          serviceTypes.add(new ESBArtifact(ESBArtifact.SERVICE_TYPE, svcName)); | 
  | 197 | 0 |                                          File destFile = new File(svcFile.getParentFile(), svcName + ".xml"); | 
  | 198 | 0 |                                          svcFile.renameTo(destFile); | 
  | 199 |  |   | 
  | 200 | 0 |                                          if (connectedToDS) { | 
  | 201 | 0 |                                                  File serviceTypePropertiesFile = new File(xarDir | 
  | 202 |  |                                                                  + "/SonicFS/System/SonicESB/properties/servicetypes", svcName + ".properties"); | 
  | 203 | 0 |                                                  Properties serviceTypeProperties = new Properties(); | 
  | 204 | 0 |                                                  serviceTypeProperties.load(new FileInputStream(serviceTypePropertiesFile)); | 
  | 205 | 0 |                                                  String serviceTypeInstance = serviceTypeProperties.getProperty("type.defaultInstance", svcName); | 
  | 206 |  |   | 
  | 207 | 0 |                                                  getLog().info("Importing '" + svcName + "' ESB Service Type in Sonic Domain"); | 
  | 208 | 0 |                                                  serviceAdmin.importServiceType(destFile, serviceTypePropertiesFile, true); | 
  | 209 |  |   | 
  | 210 | 0 |                                                  String endpoint = serviceTypeInstance + ".Entry"; | 
  | 211 | 0 |                                                  if (endpoint != null && !endpoint.isEmpty()) { | 
  | 212 | 0 |                                                          if (!endpointAdmin.isEndpointExisting(endpoint)) { | 
  | 213 | 0 |                                                                  getLog().info( | 
  | 214 |  |                                                                                  "Creating '" + endpoint + "' endpoint in Sonic Domain for ESB Service Type " | 
  | 215 |  |                                                                                                  + svcName); | 
  | 216 |  |                                                          } | 
  | 217 | 0 |                                                          final IEndpointConfig endpointConfig = endpointAdmin.createTopicEndpoint(endpoint); | 
  | 218 |  |   | 
  | 219 | 0 |                                                          if (!serviceAdmin.isServiceConfigExisting(serviceTypeInstance)) { | 
  | 220 | 0 |                                                                  getLog().info( | 
  | 221 |  |                                                                                  "Creating '" + serviceTypeInstance | 
  | 222 |  |                                                                                                  + "' service config in Sonic Domain for ESB Service Type " + svcName); | 
  | 223 |  |                                                          } | 
  | 224 | 0 |                                                          serviceAdmin.createServiceTypeConfig(serviceTypeInstance, svcName, endpointConfig, false); | 
  | 225 |  |                                                  } | 
  | 226 |  |                                          } | 
  | 227 |  |                                  } | 
  | 228 |  |   | 
  | 229 | 0 |                          } catch (Exception e) { | 
  | 230 | 0 |                                  throw new MojoExecutionException("Error copying files", e); | 
  | 231 | 0 |                          } | 
  | 232 |  |   | 
  | 233 | 0 |                          injectServiceClasspath(xarDir); | 
  | 234 |  |   | 
  | 235 |  |                           | 
  | 236 | 0 |                          if (serviceTypes.size() > 0) { | 
  | 237 |  |                                  try { | 
  | 238 |  |   | 
  | 239 | 0 |                                          FileArtifactStorage fas = new FileArtifactStorage(); | 
  | 240 | 0 |                                          fas.addNotificationListener(new NullArtifactNotificationListener()); | 
  | 241 | 0 |                                          fas.setRoot(xarDir); | 
  | 242 |  |   | 
  | 243 | 0 |                                          ExportPropertiesArtifact expProperties = new ExportPropertiesArtifact(); | 
  | 244 |  |                                           | 
  | 245 | 0 |                                          expProperties.addRoot(new SonicFSArtifact("/")); | 
  | 246 |  |   | 
  | 247 | 0 |                                          expProperties.addRoot(serviceTypes.toArray(new ESBArtifact[] {})); | 
  | 248 |  |   | 
  | 249 |  |                                           | 
  | 250 |  |                                           | 
  | 251 | 0 |                                          expProperties.addIgnore(new SonicFSArtifact("workspace/")); | 
  | 252 |  |   | 
  | 253 | 0 |                                          if (connectedToDS) { | 
  | 254 | 0 |                                                  new ESBArtifactCopyJob(fas, getMFUtils().getDSArtifactStorage(), expProperties, false).copy(); | 
  | 255 | 0 |                                                  getLog().info("ESB Service type imported into local DS"); | 
  | 256 |  |                                          } | 
  | 257 | 0 |                                  } catch (Exception e) { | 
  | 258 | 0 |                                          getLog().error("Failed to import ESB Service Type into local DS", e); | 
  | 259 | 0 |                                  } | 
  | 260 |  |                          } | 
  | 261 |  |                  } finally { | 
  | 262 | 0 |                          getMFUtils().cleanup(); | 
  | 263 | 0 |                  } | 
  | 264 |  |   | 
  | 265 | 0 |                  return super.createXarFile(); | 
  | 266 |  |          } | 
  | 267 |  |   | 
  | 268 |  |           | 
  | 269 |  |   | 
  | 270 |  |   | 
  | 271 |  |   | 
  | 272 |  |   | 
  | 273 |  |          @Override | 
  | 274 |  |          protected String getXarExcludePatterns() { | 
  | 275 |  |   | 
  | 276 | 0 |                  if (xarExcludes != null) { | 
  | 277 | 0 |                          return xarExcludes; | 
  | 278 |  |                  } | 
  | 279 |  |   | 
  | 280 | 0 |                  return super.getXarExcludePatterns() + ",**/ServiceTypes/*"; | 
  | 281 |  |   | 
  | 282 |  |          } | 
  | 283 |  |   | 
  | 284 |  |          private void injectServiceClasspath(final String _xarDir) throws MojoExecutionException { | 
  | 285 |  |   | 
  | 286 | 0 |                  getLog().info("Setting Service Type classpath ..."); | 
  | 287 |  |   | 
  | 288 | 0 |                  String[] jarFiles = FileUtilities.getFileList(_xarDir + "/SonicFS", "**/*.jar", null); | 
  | 289 |  |   | 
  | 290 | 0 |                  List<String> instanceJars = new ArrayList<String>(); | 
  | 291 | 0 |                  List<String> typeJars = new ArrayList<String>(); | 
  | 292 | 0 |                  List<String> ctJars = new ArrayList<String>(); | 
  | 293 |  |   | 
  | 294 | 0 |                  if (typePrependClasspath != null) { | 
  | 295 | 0 |                          SmartTokenizer st = new SmartTokenizer(typePrependClasspath, classpathSeparator); | 
  | 296 | 0 |                          while (st.hasMoreTokens()) { | 
  | 297 | 0 |                                  typeJars.add(st.nextToken()); | 
  | 298 |  |                          } | 
  | 299 |  |                  } | 
  | 300 |  |   | 
  | 301 | 0 |                  if (instancePrependClasspath != null) { | 
  | 302 | 0 |                          SmartTokenizer st = new SmartTokenizer(instancePrependClasspath, classpathSeparator); | 
  | 303 | 0 |                          while (st.hasMoreTokens()) { | 
  | 304 | 0 |                                  instanceJars.add(st.nextToken()); | 
  | 305 |  |                          } | 
  | 306 |  |                  } | 
  | 307 |  |   | 
  | 308 | 0 |                  if (ctPrependClasspath != null) { | 
  | 309 | 0 |                          SmartTokenizer st = new SmartTokenizer(ctPrependClasspath, classpathSeparator); | 
  | 310 | 0 |                          while (st.hasMoreTokens()) { | 
  | 311 | 0 |                                  ctJars.add(st.nextToken()); | 
  | 312 |  |                          } | 
  | 313 |  |                  } | 
  | 314 |  |   | 
  | 315 | 0 |                  for (int i = 0; i < jarFiles.length; i++) { | 
  | 316 |  |   | 
  | 317 | 0 |                          String jar = jarFiles[i].replaceAll("\\\\", "/"); | 
  | 318 | 0 |                          getLog().debug("TBO: Jar File: " + jar); | 
  | 319 | 0 |                          if (typeClasspathPattern != null) { | 
  | 320 | 0 |                                  getLog().debug("The Type Classpath Pattern = " + typeClasspathPattern); | 
  | 321 | 0 |                                  if (jar.matches(typeClasspathPattern)) { | 
  | 322 | 0 |                                          getLog().debug("there is a match"); | 
  | 323 | 0 |                                          typeJars.add("sonicfs:///" + jar); | 
  | 324 | 0 |                                          jar = null; | 
  | 325 |  |                                  } else { | 
  | 326 | 0 |                                          getLog().debug("THERE IS NO MATCH"); | 
  | 327 |  |                                  } | 
  | 328 |  |                          } | 
  | 329 | 0 |                          if (jar != null && ctClasspathPattern != null) { | 
  | 330 | 0 |                                  if (jar.matches(ctClasspathPattern)) { | 
  | 331 | 0 |                                          ctJars.add("sonicfs:///" + jar); | 
  | 332 | 0 |                                          jar = null; | 
  | 333 |  |                                  } | 
  | 334 |  |                          } | 
  | 335 | 0 |                          if (jar != null) { | 
  | 336 | 0 |                                  instanceJars.add("sonicfs:///" + jar); | 
  | 337 |  |                          } | 
  | 338 |  |                  } | 
  | 339 |  |   | 
  | 340 | 0 |                  String svcTypeDir = getPackageXarDir() + "/ESB/ServiceTypes"; | 
  | 341 |  |   | 
  | 342 | 0 |                  String[] stFiles = FileUtilities.getFileList(svcTypeDir, "*.xml", null); | 
  | 343 |  |   | 
  | 344 | 0 |                  for (String stFileName : stFiles) { | 
  | 345 | 0 |                          getLog().debug("TBO: File Name: " + stFileName); | 
  | 346 | 0 |                          DefaultServiceType st = new DefaultServiceType(); | 
  | 347 | 0 |                          File stFile = new File(svcTypeDir, stFileName); | 
  | 348 |  |                          try { | 
  | 349 | 0 |                                  st.load(new FileInputStream(stFile)); | 
  | 350 | 0 |                                  getLog().debug("TBO: File Loaded"); | 
  | 351 | 0 |                                  st.setInstanceClasspath(instanceJars, instanceSelfFirst); | 
  | 352 | 0 |                                  getLog().debug("TBO: Instance Classpath Set"); | 
  | 353 | 0 |                                  st.setTypeClasspath(typeJars, typeSelfFirst); | 
  | 354 | 0 |                                  getLog().debug("TBO: Type Classpath Set"); | 
  | 355 | 0 |                                  st.setContainerClasspath(ctJars); | 
  | 356 | 0 |                                  getLog().debug("TBO: File Container Path Set"); | 
  | 357 | 0 |                                  st.save(stFile); | 
  | 358 | 0 |                          } catch (Exception e) { | 
  | 359 | 0 |                                  throw new MojoExecutionException(e.getMessage(), e); | 
  | 360 | 0 |                          } | 
  | 361 |  |                  } | 
  | 362 | 0 |          } | 
  | 363 |  |  } |