1 19 20 package org.apache.geronimo.mavenplugins.car; 21 22 import org.apache.geronimo.kernel.config.InvalidConfigException; 23 import org.apache.geronimo.kernel.config.NoSuchConfigException; 24 import org.apache.geronimo.kernel.config.ConfigurationData; 25 import org.apache.geronimo.kernel.repository.Artifact; 26 import org.apache.geronimo.kernel.repository.ArtifactManager; 27 import org.apache.geronimo.kernel.repository.ArtifactResolver; 28 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 29 import org.apache.geronimo.kernel.repository.Environment; 30 import org.apache.geronimo.kernel.repository.FileWriteMonitor; 31 import org.apache.geronimo.kernel.repository.Dependency; 32 import org.apache.geronimo.kernel.repository.WritableListableRepository; 33 import org.apache.geronimo.kernel.repository.Repository; 34 import org.apache.geronimo.system.repository.Maven2Repository; 35 import org.apache.geronimo.system.configuration.RepositoryConfigurationStore; 36 import org.apache.geronimo.system.resolver.ExplicitDefaultArtifactResolver; 37 import org.apache.geronimo.genesis.dependency.DependencyTree; 38 39 import org.apache.maven.artifact.repository.ArtifactRepository; 40 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.BufferedInputStream ; 46 import java.io.BufferedReader ; 47 import java.io.FileReader ; 48 import java.io.InputStreamReader ; 49 50 import java.util.Iterator ; 51 import java.util.HashSet ; 52 import java.util.LinkedHashSet ; 53 import java.util.Set ; 54 import java.util.Collections ; 55 import java.util.HashMap ; 56 import java.util.zip.ZipEntry ; 57 import java.util.jar.JarFile ; 58 59 import org.codehaus.plexus.util.FileUtils; 60 61 67 public class InstallModulesMojo 68 extends AbstractCarMojo { 69 75 private File targetRepositoryDirectory = null; 76 77 84 private String artifact = null; 85 86 92 private ArtifactRepository sourceRepository = null; 93 94 100 private File explicitResolutionProperties = null; 101 102 110 private ArtifactResolver geronimoArtifactResolver; 111 112 private WritableListableRepository targetRepo; 113 114 private RepositoryConfigurationStore targetStore; 115 116 private WritableListableRepository sourceRepo; 117 118 private RepositoryConfigurationStore sourceStore; 119 120 123 private Set installedArtifacts = new HashSet (); 124 125 protected void doExecute() throws Exception { 126 DependencyTree dependencies = dependencyHelper.getDependencies(project); 127 generateExplicitVersionProperties(explicitResolutionProperties, dependencies); 128 129 133 Maven2RepositoryAdapter.ArtifactLookup lookup = new ArtifactLookupImpl(new HashMap ()); 134 sourceRepo = new Maven2RepositoryAdapter(dependencies, lookup); 135 sourceStore = new RepositoryConfigurationStore(sourceRepo); 137 138 FileUtils.forceMkdir(targetRepositoryDirectory); 139 140 targetRepo = new Maven2Repository(targetRepositoryDirectory); 141 targetStore = new RepositoryConfigurationStore(targetRepo); 142 143 ArtifactManager artifactManager = new DefaultArtifactManager(); 144 geronimoArtifactResolver = new ExplicitDefaultArtifactResolver( 145 explicitResolutionProperties.getPath(), 146 artifactManager, 147 Collections.singleton(sourceRepo), 148 null); 149 150 if (artifact != null) { 151 install(Artifact.create(artifact)); 152 } else { 153 Iterator iter = getDependencies().iterator(); 154 while (iter.hasNext()) { 155 156 Artifact artifact = mavenToGeronimoArtifact((org.apache.maven.artifact.Artifact) iter.next()); 157 if (isModuleArtifact(artifact)) { 158 install(artifact); 159 } 160 } 161 } 162 } 163 164 169 protected Set getDependencies() { 170 Set dependenciesSet = new HashSet (); 171 172 org.apache.maven.artifact.Artifact artifact = project.getArtifact(); 173 if (artifact != null && artifact.getFile() != null) { 174 dependenciesSet.add(artifact); 175 } 176 177 Set projectArtifacts = project.getArtifacts(); 178 if (projectArtifacts != null) { 179 dependenciesSet.addAll(projectArtifacts); 180 } 181 182 return dependenciesSet; 183 } 184 185 191 private void install(final Artifact artifact) throws Exception { 192 assert artifact != null; 193 194 if (installedArtifacts.contains(artifact)) { 195 log.debug("Skipping artifact; already installed: " + artifact); 196 } else { 197 if (!sourceRepo.contains(artifact)) { 199 throw new Exception ("Missing artifact in source repository: " + artifact); 200 } 201 202 if (isModuleArtifact(artifact)) { 203 installModule(artifact); 204 } else { 205 installDependency(artifact); 206 } 207 } 208 } 209 210 216 private void installModule(final Artifact artifact) throws Exception { 217 assert artifact != null; 218 assert isModuleArtifact(artifact); 219 220 boolean install = true; 221 222 if (!sourceStore.containsConfiguration(artifact)) { 224 throw new Exception ("Missing module artifact in source repository: " + artifact); 225 } 226 227 if (targetStore.containsConfiguration(artifact)) { 229 if (hasModuleChanged(artifact)) { 230 log.debug("Old module exists in target store; uninstalling: " + artifact); 231 targetStore.uninstall(artifact); 232 } else { 233 log.debug("Same module exists in target store; skipping: " + artifact); 234 install = false; 235 } 236 } 237 238 if (install) { 240 log.info("Installing module: " + artifact); 241 242 File file = sourceRepo.getLocation(artifact); 243 InputStream input = new BufferedInputStream (new FileInputStream (file)); 244 245 try { 246 FileWriteMonitor monitor = new FileWriteMonitor() { 247 public void writeStarted(final String file, final int bytes) { 248 log.debug("Installing module: " + file + " (" + bytes + " bytes)"); 249 } 250 251 public void writeProgress(int bytes) { 252 } 254 255 public void writeComplete(int bytes) { 256 } 258 }; 259 260 targetStore.install(input, (int) file.length(), artifact, monitor); 261 262 installedArtifacts.add(artifact); 263 } 264 finally { 265 input.close(); 266 } 267 } 268 269 installModuleDependencies(artifact); 271 } 272 273 279 private void installModuleDependencies(final Artifact artifact) throws Exception { 280 assert artifact != null; 281 assert isModuleArtifact(artifact); 282 283 log.debug("Installing module dependencies for artifact: " + artifact); 284 285 try { 286 ConfigurationData config = targetStore.loadConfiguration(artifact); 287 Environment env = config.getEnvironment(); 288 LinkedHashSet deps = new LinkedHashSet (); 289 290 Iterator iter = env.getDependencies().iterator(); 291 while (iter.hasNext()) { 292 Dependency dep = (Dependency) iter.next(); 293 deps.add(dep.getArtifact()); 294 } 295 296 installDependencies(deps); 297 } 298 catch (IOException e) { 299 throw new InvalidConfigException("Unable to load module: " + artifact, e); 300 } 301 catch (NoSuchConfigException e) { 302 throw new InvalidConfigException("Unable to load module: " + artifact, e); 303 } 304 } 305 306 312 private void installDependency(final Artifact artifact) throws Exception { 313 assert artifact != null; 314 assert !isModuleArtifact(artifact); 315 316 boolean install = true; 317 318 if (targetRepo.contains(artifact)) { 320 if (hasDependencyChanged(artifact)) { 321 File file = targetRepo.getLocation(artifact); 322 log.debug("Old dependency exists in target repo; deleting: " + file); 323 FileUtils.forceDelete(file); 324 } else { 325 log.debug("Same dependency exists in target repo; skipping: " + artifact); 326 install = false; 327 } 328 } 329 330 if (install) { 331 log.info("Installing dependency: " + artifact); 332 333 File file = sourceRepo.getLocation(artifact); 335 InputStream input = new BufferedInputStream (new FileInputStream (file)); 336 try { 337 FileWriteMonitor monitor = new FileWriteMonitor() { 338 public void writeStarted(final String file, final int bytes) { 339 log.debug("Copying dependency: " + file + " (" + bytes + " bytes)"); 340 } 341 342 public void writeProgress(int bytes) { 343 } 345 346 public void writeComplete(int bytes) { 347 } 349 }; 350 351 targetRepo.copyToRepository(input, (int) file.length(), artifact, monitor); 352 353 installedArtifacts.add(artifact); 354 } 355 finally { 356 input.close(); 357 } 358 } 359 360 installDependencies(sourceRepo.getDependencies(artifact)); 362 } 363 364 370 private void installDependencies(final Set dependencies) throws Exception { 371 assert dependencies != null; 372 373 Set resolved = geronimoArtifactResolver.resolveInClassLoader(dependencies); 374 Iterator iter = resolved.iterator(); 375 376 while (iter.hasNext()) { 377 Artifact a = (Artifact) iter.next(); 378 install(a); 379 } 380 } 381 382 389 private boolean hasModuleChanged(final Artifact module) throws IOException { 390 assert module != null; 391 392 String sourceChecksum = loadChecksum(sourceRepo, module); 393 String targetChecksum = loadChecksum(targetRepo, module); 394 395 return !sourceChecksum.equals(targetChecksum); 396 } 397 398 406 private String loadChecksum(final Repository repo, final Artifact artifact) throws IOException { 407 assert repo != null; 408 assert artifact != null; 409 410 File file = repo.getLocation(artifact); 411 BufferedReader reader; 412 413 if (file.isDirectory()) { 414 File serFile = new File (file, "META-INF/config.ser.sha1"); 415 reader = new BufferedReader (new FileReader (serFile)); 416 } else { 417 JarFile jarFile = new JarFile (file); 418 ZipEntry entry = jarFile.getEntry("META-INF/config.ser.sha1"); 419 reader = new BufferedReader (new InputStreamReader (jarFile.getInputStream(entry))); 420 } 421 422 String checksum = reader.readLine(); 423 reader.close(); 424 425 return checksum; 426 } 427 428 434 private boolean hasDependencyChanged(final Artifact artifact) { 435 assert artifact != null; 436 437 File source = sourceRepo.getLocation(artifact); 438 File target = targetRepo.getLocation(artifact); 439 440 return (source.length() != target.length()) || 441 (source.lastModified() > target.lastModified()); 442 } 443 } 444 | Popular Tags |