Search in sources :

Example 76 with Closure

use of groovy.lang.Closure in project groovy-core by groovy.

the class JmxEventListener method handleNotification.

/**
 * This is the implemented method for NotificationListener.  It is called by an event emitter to dispatch
 * JMX events to listeners.  Here it handles internal JmxBuilder events.
 *
 * @param notification the notification object passed to closure used to handle JmxBuilder events.
 * @param handback     - In this case, the handback is the closure to execute when the event is handled.
 */
public void handleNotification(Notification notification, Object handback) {
    Map event = (Map) handback;
    if (event != null) {
        Object del = event.get("managedObject");
        Object callback = event.get("callback");
        if (callback != null && callback instanceof Closure) {
            Closure closure = (Closure) callback;
            closure.setDelegate(del);
            if (closure.getMaximumNumberOfParameters() == 1)
                closure.call(buildOperationNotificationPacket(notification));
            else
                closure.call();
        }
    }
}
Also used : Closure(groovy.lang.Closure) Map(java.util.Map) HashMap(java.util.HashMap)

Example 77 with Closure

use of groovy.lang.Closure in project groovy-core by groovy.

the class StreamingJsonDelegate method cloneDelegateAndGetContent.

public static void cloneDelegateAndGetContent(Writer w, Closure c, boolean first) {
    StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first);
    Closure cloned = (Closure) c.clone();
    cloned.setDelegate(delegate);
    cloned.setResolveStrategy(Closure.DELEGATE_FIRST);
    cloned.call();
}
Also used : Closure(groovy.lang.Closure)

Example 78 with Closure

use of groovy.lang.Closure in project indy by Commonjava.

the class PromotionValidationTools method runParallelInBatchAndWait.

private <T> void runParallelInBatchAndWait(Collection<Collection<T>> batches, Closure closure, Logger logger) {
    final CountDownLatch latch = new CountDownLatch(batches.size());
    batches.forEach(batch -> ruleParallelExecutor.execute(() -> {
        try {
            logger.trace("The paralleled exe on batch {}", batch);
            batch.forEach(e -> {
                String depthStr = MDC.get(ITERATION_DEPTH);
                RequestContextHelper.setContext(ITERATION_DEPTH, depthStr == null ? "0" : String.valueOf(Integer.parseInt(depthStr) + 1));
                RequestContextHelper.setContext(ITERATION_ITEM, String.valueOf(e));
                try {
                    closure.call(e);
                } finally {
                    MDC.remove(ITERATION_ITEM);
                    MDC.remove(ITERATION_DEPTH);
                }
            });
        } finally {
            latch.countDown();
        }
    }));
    waitForCompletion(latch);
}
Also used : Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Closure(groovy.lang.Closure) ReadOnlyTransfer.readOnlyWrappers(org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrappers) WeftExecutorService(org.commonjava.cdi.util.weft.WeftExecutorService) Batcher.getParalleledBatchSize(org.commonjava.indy.promote.util.Batcher.getParalleledBatchSize) ProjectRelationship(org.commonjava.atlas.maven.graph.rel.ProjectRelationship) MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) TransferManager(org.commonjava.maven.galley.TransferManager) MavenPomReader(org.commonjava.maven.galley.maven.parse.MavenPomReader) Map(java.util.Map) ContentDigest(org.commonjava.maven.galley.io.checksum.ContentDigest) URI(java.net.URI) StoreKey(org.commonjava.indy.model.core.StoreKey) ArtifactStoreQuery(org.commonjava.indy.data.ArtifactStoreQuery) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ContentManager(org.commonjava.indy.content.ContentManager) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) ArtifactRef(org.commonjava.atlas.maven.ident.ref.ArtifactRef) Optional(java.util.Optional) PackagePath(org.commonjava.indy.pkg.npm.content.PackagePath) IndyDataException(org.commonjava.indy.data.IndyDataException) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) ContentDigester(org.commonjava.indy.content.ContentDigester) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) PoolWeftExecutorService(org.commonjava.cdi.util.weft.PoolWeftExecutorService) Batcher.batch(org.commonjava.indy.promote.util.Batcher.batch) ReadOnlyTransfer.readOnlyWrapper(org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrapper) MavenModelProcessor(org.commonjava.maven.galley.maven.rel.MavenModelProcessor) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Transfer(org.commonjava.maven.galley.model.Transfer) FORCE_CHECKSUM(org.commonjava.maven.galley.io.ChecksummingTransferDecorator.FORCE_CHECKSUM) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ModelProcessorConfig(org.commonjava.maven.galley.maven.rel.ModelProcessorConfig) Location(org.commonjava.maven.galley.model.Location) ArtifactPathUtils(org.commonjava.maven.galley.maven.util.ArtifactPathUtils) LocationUtils(org.commonjava.indy.util.LocationUtils) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) PromoteConfig(org.commonjava.indy.promote.conf.PromoteConfig) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) MavenMetadataReader(org.commonjava.maven.galley.maven.parse.MavenMetadataReader) TypeMapper(org.commonjava.maven.galley.maven.spi.type.TypeMapper) MDC(org.slf4j.MDC) StoreResource(org.commonjava.indy.content.StoreResource) RequestContextHelper(org.commonjava.indy.util.RequestContextHelper) StoreDataManager(org.commonjava.indy.data.StoreDataManager) TransferException(org.commonjava.maven.galley.TransferException) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 79 with Closure

use of groovy.lang.Closure in project indy by Commonjava.

the class PromotionValidationToolsTest method testParalleledInBatch.

@Test
public void testParalleledInBatch() {
    PromoteConfig config = new PromoteConfig();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    PromotionValidationTools tools = new PromotionValidationTools(null, null, null, null, null, null, null, null, executor, config);
    List<String> errors = Collections.synchronizedList(new ArrayList<>());
    Closure closure = new Closure<String>(null) {

        @Override
        public String call(Object arg) {
            if (((String) arg).startsWith("err_")) {
                errors.add((String) arg);
            }
            return null;
        }
    };
    tools.paralleledInBatch(array, closure);
    verifyIt(errors);
}
Also used : Closure(groovy.lang.Closure) PromoteConfig(org.commonjava.indy.promote.conf.PromoteConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 80 with Closure

use of groovy.lang.Closure in project indy by Commonjava.

the class PromotionValidationToolsTest method testParalleledInBatch_smallSize.

@Test
public void testParalleledInBatch_smallSize() {
    PromoteConfig config = new PromoteConfig();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
    PromotionValidationTools tools = new PromotionValidationTools(null, null, null, null, null, null, null, null, executor, config);
    List<String> errors = Collections.synchronizedList(new ArrayList<>());
    Closure closure = new Closure<String>(null) {

        @Override
        public String call(Object arg) {
            if (((String) arg).startsWith("err_")) {
                errors.add((String) arg);
            }
            return null;
        }
    };
    tools.paralleledInBatch(array, closure);
    verifyIt(errors);
}
Also used : Closure(groovy.lang.Closure) PromoteConfig(org.commonjava.indy.promote.conf.PromoteConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Aggregations

Closure (groovy.lang.Closure)251 Map (java.util.Map)55 HashMap (java.util.HashMap)38 ArrayList (java.util.ArrayList)37 GroovyObject (groovy.lang.GroovyObject)33 List (java.util.List)33 Binding (groovy.lang.Binding)22 GString (groovy.lang.GString)21 LinkedHashMap (java.util.LinkedHashMap)20 LinkedList (java.util.LinkedList)19 Collection (java.util.Collection)17 GroovyShell (groovy.lang.GroovyShell)14 Test (org.junit.Test)14 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 FileType (groovy.io.FileType)12 FileVisitResult (groovy.io.FileVisitResult)12 File (java.io.File)12 Iterator (java.util.Iterator)10 GroovyBugError (org.codehaus.groovy.GroovyBugError)10 IOException (java.io.IOException)9