Search in sources :

Example 1 with FileStorageEvent

use of org.commonjava.maven.galley.event.FileStorageEvent in project galley by Commonjava.

the class Transfer method openOutputStream.

public OutputStream openOutputStream(final TransferOperation accessType, final boolean fireEvents, final EventMetadata eventMetadata, boolean deleteFilesOnPath) throws IOException {
    provider.waitForWriteUnlock(resource);
    try {
        provider.lockWrite(resource);
        if (deleteFilesOnPath) {
            deleteFilesOnPath();
        }
        OutputStream stream = provider.openOutputStream(resource);
        if (stream == null) {
            return null;
        }
        final TransferUnlocker unlocker = new TransferUnlocker(resource, provider);
        if (fireEvents) {
            logger.info("Wrapping output stream to: {} using event metadata: {}", this, eventMetadata);
            stream = new TransferOutputStream(stream, unlocker, new FileStorageEvent(accessType, this, eventMetadata), fileEventManager);
        } else {
            logger.info("Wrapping output stream to: {} WITHOUT event metadata", this);
            stream = new TransferOutputStream(stream, unlocker);
        }
        stream = decorator == null ? stream : decorator.decorateWrite(stream, this, accessType, eventMetadata);
        return stream;
    } catch (final IOException e) {
        if (fireEvents) {
            fileEventManager.fire(new FileErrorEvent(this, e, eventMetadata));
        }
        throw e;
    }
}
Also used : FileStorageEvent(org.commonjava.maven.galley.event.FileStorageEvent) OutputStream(java.io.OutputStream) TransferOutputStream(org.commonjava.maven.galley.util.TransferOutputStream) FileErrorEvent(org.commonjava.maven.galley.event.FileErrorEvent) IOException(java.io.IOException) TransferOutputStream(org.commonjava.maven.galley.util.TransferOutputStream)

Example 2 with FileStorageEvent

use of org.commonjava.maven.galley.event.FileStorageEvent in project indy by Commonjava.

the class EventAuditListener method transformFileEvent.

private void transformFileEvent(org.commonjava.maven.galley.event.FileEvent event, FileEvent fileEvent) {
    EventMetadata metadata = event.getEventMetadata();
    final TrackingKey trackingKey = (TrackingKey) metadata.get(EventConstants.TRACKING_KEY);
    if (trackingKey == null) {
        logger.trace("No tracking key. Skip audit.");
        return;
    }
    Transfer transfer = event.getTransfer();
    if (transfer == null) {
        logger.trace("No transfer. Skip audit.");
        return;
    }
    final Location location = transfer.getLocation();
    if (!(location instanceof KeyedLocation)) {
        logger.trace("Not in a keyed location: {}", transfer);
        return;
    }
    try {
        final KeyedLocation keyedLocation = (KeyedLocation) location;
        final StoreKey affectedStore = keyedLocation.getKey();
        if (affectedStore.getType() == group) {
            logger.trace("Not auditing content stored directly in group: {}. This content is generally aggregated metadata, and can be recalculated. Groups may not be stable in some build environments", affectedStore);
            return;
        }
        final String path = transfer.getPath();
        fileEvent.setTargetPath(path);
        // TODO figure out what's the NodeId
        fileEvent.setNodeId(indyConfig.getNodeId());
        fileEvent.setSessionId(trackingKey.getId());
        fileEvent.setTimestamp(new Date());
        TransferMetadata artifactData = contentDigester.digest(affectedStore, path, metadata);
        fileEvent.setMd5(artifactData.getDigests().get(ContentDigest.MD5));
        fileEvent.setSha1(artifactData.getDigests().get(ContentDigest.SHA_1));
        fileEvent.setChecksum(artifactData.getDigests().get(ContentDigest.SHA_256));
        fileEvent.setSize(artifactData.getSize());
        fileEvent.setStoreKey(affectedStore.toString());
        Map<String, String> extra = new HashMap<>();
        if (event instanceof FileStorageEvent) {
            extra.put(EventConstants.STORE_EFFECT, ((FileStorageEvent) event).getType().name());
        }
        if (StoreType.remote == affectedStore.getType()) {
            final RemoteRepository repo = (RemoteRepository) storeManager.getArtifactStore(affectedStore);
            if (repo != null) {
                fileEvent.setSourceLocation(repo.getUrl());
                fileEvent.setSourcePath(transfer.getPath());
            }
        }
        // TODO fix this, it should be the url of indy, or recalculate it in AuditQuery side.
        fileEvent.setTargetLocation("");
        fileEvent.setExtra(extra);
    } catch (final IndyWorkflowException | IndyDataException e) {
        logger.error(String.format("Failed to transform file event. Reason: %s", e.getMessage()), e);
    }
}
Also used : KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) FileStorageEvent(org.commonjava.maven.galley.event.FileStorageEvent) HashMap(java.util.HashMap) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) StoreKey(org.commonjava.indy.model.core.StoreKey) Date(java.util.Date) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) TrackingKey(org.commonjava.indy.folo.model.TrackingKey) IndyDataException(org.commonjava.indy.data.IndyDataException) TransferMetadata(org.commonjava.maven.galley.io.checksum.TransferMetadata) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location)

Example 3 with FileStorageEvent

use of org.commonjava.maven.galley.event.FileStorageEvent in project indy by Commonjava.

the class ImpliedRepositoryDetectorTest method addRepositoryFromPomStorageEvent.

// @Test
// public void idWithSpaceInIt_ConvertToDashes()
// {
// String in = "my id";
// String out = detector.formatId( in );
// assertThat( out, equalTo( "i-my-id" ) );
// }
// 
// @Test
// public void idWithPlusInIt_ConvertToDashes()
// {
// String in = "my+id";
// String out = detector.formatId( in );
// assertThat( out, equalTo( "i-my-id" ) );
// }
@Test
public void addRepositoryFromPomStorageEvent() throws Exception {
    Transfer txfr = writeTransfer("one-repo.pom");
    final FileStorageEvent event = new FileStorageEvent(TransferOperation.DOWNLOAD, txfr, new EventMetadata());
    detector.detectRepos(event);
    synchronized (detector) {
        detector.wait();
    }
    assertThat(storeManager.query().getRemoteRepository(MAVEN_PKG_KEY, "i-repo-one"), notNullValue());
    assertThat(getGroup().getConstituents().contains(new StoreKey(MAVEN_PKG_KEY, StoreType.remote, "i-repo-one")), equalTo(true));
}
Also used : FileStorageEvent(org.commonjava.maven.galley.event.FileStorageEvent) Transfer(org.commonjava.maven.galley.model.Transfer) StoreKey(org.commonjava.indy.model.core.StoreKey) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 4 with FileStorageEvent

use of org.commonjava.maven.galley.event.FileStorageEvent in project indy by Commonjava.

the class ImpliedRepositoryDetectorTest method addImpliedPluginRepositoryToNewGroup.

@Test
public void addImpliedPluginRepositoryToNewGroup() throws Exception {
    Transfer txfr = writeTransfer("one-plugin-repo.pom");
    final FileStorageEvent event = new FileStorageEvent(TransferOperation.DOWNLOAD, txfr, new EventMetadata());
    detector.detectRepos(event);
    synchronized (detector) {
        detector.wait();
    }
    assertThat(storeManager.query().getRemoteRepository(MAVEN_PKG_KEY, "i-repo-one"), notNullValue());
    assertThat(getGroup().getConstituents().contains(new StoreKey(StoreType.remote, "i-repo-one")), equalTo(true));
}
Also used : FileStorageEvent(org.commonjava.maven.galley.event.FileStorageEvent) Transfer(org.commonjava.maven.galley.model.Transfer) StoreKey(org.commonjava.indy.model.core.StoreKey) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Aggregations

FileStorageEvent (org.commonjava.maven.galley.event.FileStorageEvent)4 StoreKey (org.commonjava.indy.model.core.StoreKey)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 Transfer (org.commonjava.maven.galley.model.Transfer)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)1 IndyDataException (org.commonjava.indy.data.IndyDataException)1 TrackingKey (org.commonjava.indy.folo.model.TrackingKey)1 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)1 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)1 FileErrorEvent (org.commonjava.maven.galley.event.FileErrorEvent)1 TransferMetadata (org.commonjava.maven.galley.io.checksum.TransferMetadata)1 Location (org.commonjava.maven.galley.model.Location)1 TransferOutputStream (org.commonjava.maven.galley.util.TransferOutputStream)1