From e13a0bed17d03155e0b37a92bcbd56105e0c0d3c Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Fri, 24 May 2019 13:33:37 +0200 Subject: [PATCH] Switch sampler to use more of Spring to make the code simplier --- tools/plugs-packager-core/pom.xml | 7 +- .../core/digest/ProjectDigestImplTest.java | 17 ++- tools/plugs-packager-samples/pom.xml | 5 + .../sample/AbstractSamplerContext.java | 56 +++++++++ ...ample.java => PackagerSamplerContext.java} | 15 +-- ...textProxy.java => RandomSeedSupplier.java} | 30 ++--- .../{SampleSpec.java => RandomSource.java} | 10 +- .../packager/sample/RandomSourceImpl.java | 64 +++++++++++ .../plugs/tools/packager/sample/Sampler.java | 35 ++++-- .../tools/packager/sample/SamplerBean.java | 40 ------- .../tools/packager/sample/SamplerContext.java | 38 ++++++- .../tools/packager/sample/SamplerScope.java | 106 ++++++++++++++++++ ...SamplerScopeBeanFactoryPostProcessor.java} | 27 ++--- ...tFactory.java => SamplerScopeControl.java} | 11 +- .../packager/sample/SamplerScopeRegistry.java | 62 ++++++++++ .../packager/sample/SpringSamplerContext.java | 83 +++----------- .../sample/artifact/HibernateArtifact.java | 28 +---- .../sample/artifact/Jsr305Artifact.java | 28 +---- .../sample/artifact/OsgiCoreArtifact.java | 28 +---- ...s.java => SimpleProjectBuildArtifact.java} | 8 +- .../artifact/impl/HibernateArtifactImpl.java | 42 +++++++ .../artifact/impl/Jsr305ArtifactImpl.java | 42 +++++++ .../artifact/{ => impl}/MavenArtifact.java | 18 +-- .../impl/OsgiCoreArtifactImpl.java} | 29 +++-- .../SimpleProjectBuildArtifactImpl.java} | 26 ++--- .../impl/package-info.java} | 11 +- .../fs/{Paths.java => VirtualRoot.java} | 7 +- ...SystemSample.java => VirtualRootImpl.java} | 18 +-- .../sample/project/SimpleProject.java | 51 +-------- .../sample/project/SimpleProjectImpl.java | 67 +++++++++++ .../packager/sample/SamplerContextIT.java | 70 ++++++++++++ tools/plugs-packager-testing/pom.xml | 45 -------- .../plugs/tools/packager/testing/Fresh.java | 30 ----- .../packager/testing/SamplerExtension.java | 80 ------------- tools/pom.xml | 1 - 35 files changed, 695 insertions(+), 540 deletions(-) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/AbstractSamplerContext.java rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{Sample.java => PackagerSamplerContext.java} (70%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{SamplerContextProxy.java => RandomSeedSupplier.java} (57%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{SampleSpec.java => RandomSource.java} (84%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSourceImpl.java delete mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerBean.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScope.java rename tools/plugs-packager-samples/src/{test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactoryIT.java => main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeBeanFactoryPostProcessor.java} (54%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{SamplerContextFactory.java => SamplerScopeControl.java} (75%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeRegistry.java rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{Artifacts.java => SimpleProjectBuildArtifact.java} (81%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{ => impl}/MavenArtifact.java (79%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{fs/VirtualFileSystem.java => artifact/impl/OsgiCoreArtifactImpl.java} (50%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{SimpleArtifact.java => impl/SimpleProjectBuildArtifactImpl.java} (76%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/{project/Projects.java => artifact/impl/package-info.java} (70%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/{Paths.java => VirtualRoot.java} (84%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/{VirtualFileSystemSample.java => VirtualRootImpl.java} (76%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java create mode 100644 tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextIT.java delete mode 100644 tools/plugs-packager-testing/pom.xml delete mode 100644 tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/Fresh.java delete mode 100644 tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/SamplerExtension.java diff --git a/tools/plugs-packager-core/pom.xml b/tools/plugs-packager-core/pom.xml index 74cd5ee..ca58db1 100644 --- a/tools/plugs-packager-core/pom.xml +++ b/tools/plugs-packager-core/pom.xml @@ -57,7 +57,7 @@ pl.wavesoftware.plugs.tools - plugs-packager-testing + plugs-packager-samples ${project.version} test @@ -67,6 +67,11 @@ ${project.version} test + + org.springframework + spring-test + test + diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java index efac664..c8899a9 100644 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java @@ -18,11 +18,14 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigest; import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -import pl.wavesoftware.plugs.tools.packager.sample.project.Projects; -import pl.wavesoftware.plugs.tools.packager.testing.SamplerExtension; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; import java.io.IOException; @@ -32,14 +35,18 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -@ExtendWith(SamplerExtension.class) +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = PackagerSamplerContext.class) class ProjectDigestImplTest { + @Autowired + private SamplerContext context; + @Test - void digest(SamplerContext context) throws IOException { + void digest() throws IOException { // given ProjectDigest digester = new ProjectDigestImpl(); - Project project = context.get(Projects.SIMPLE); + Project project = context.get(SimpleProject.class); // when CharSequence digest = digester.digest(project); diff --git a/tools/plugs-packager-samples/pom.xml b/tools/plugs-packager-samples/pom.xml index 63dc782..762ce1a 100644 --- a/tools/plugs-packager-samples/pom.xml +++ b/tools/plugs-packager-samples/pom.xml @@ -57,6 +57,11 @@ ${project.version} test + + org.springframework + spring-test + test + diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/AbstractSamplerContext.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/AbstractSamplerContext.java new file mode 100644 index 0000000..8636e4c --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/AbstractSamplerContext.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +abstract class AbstractSamplerContext implements SamplerContext { + private static final Logger LOGGER = + LoggerFactory.getLogger(AbstractSamplerContext.class); + private final Map>, Object> samples = + new HashMap<>(); + + @Override + public T get(Class> spec) { + @SuppressWarnings("unchecked") + T val = (T) samples.computeIfAbsent(spec, ignored -> createNew(spec)); + return val; + } + + @Override + public T createNew(Class> spec) { + T sample = getSampler(spec).create(); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug( + "Created sample {} for spec {}", + Integer.toHexString(sample.hashCode()), spec + ); + } + return sample; + } + + @Override + public void destroy() { + samples.clear(); + } + + protected abstract Sampler getSampler(Class> spec); +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sample.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java similarity index 70% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sample.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java index 346ddf2..7fa3f2e 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sample.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java @@ -16,16 +16,11 @@ package pl.wavesoftware.plugs.tools.packager.sample; -import java.util.function.Supplier; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; -public interface Sample extends Supplier, AutoCloseable { - @Override - void close(); +@ComponentScan(includeFilters = @ComponentScan.Filter(Sampler.Component.class)) +@Import(SamplerContext.Configuration.class) +public class PackagerSamplerContext { - interface Easy extends Sample { - @Override - default void close() { - // nothing here - } - } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextProxy.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSeedSupplier.java similarity index 57% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextProxy.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSeedSupplier.java index 930c75f..0446b81 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextProxy.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSeedSupplier.java @@ -16,32 +16,18 @@ package pl.wavesoftware.plugs.tools.packager.sample; -import io.vavr.Lazy; - +import java.util.Random; import java.util.function.Supplier; -final class SamplerContextProxy implements SamplerContext { - - private final Lazy context; +final class RandomSeedSupplier implements Supplier { - SamplerContextProxy(Supplier supplier) { - this.context = Lazy.of(supplier); - } - - @Override - public T get(SampleSpec spec) { - return context.get().get(spec); - } - - @Override - public Sample createNew(SampleSpec spec) { - return context.get().createNew(spec); - } + private static final int BASE36 = 36; + private static final int MIN = 60_466_176; + private static final Random RANDOM = new Random(); @Override - public void close() { - if (context.isEvaluated()) { - context.get().close(); - } + public CharSequence get() { + int calc = RANDOM.nextInt(Integer.MAX_VALUE - MIN) + MIN; + return Integer.toString(calc, BASE36); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SampleSpec.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSource.java similarity index 84% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SampleSpec.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSource.java index 11908cf..a828f0d 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SampleSpec.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSource.java @@ -16,10 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.sample; -/** - * Defines a spec for a sample - * - * @param a type of a sample - */ -public interface SampleSpec { +import java.util.Random; +import java.util.function.Supplier; + +public interface RandomSource extends Supplier { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSourceImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSourceImpl.java new file mode 100644 index 0000000..dd71c9c --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSourceImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample; + +import io.vavr.Lazy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; + +import java.nio.charset.StandardCharsets; +import java.util.Random; +import java.util.zip.CRC32; + +final class RandomSourceImpl implements RandomSource { + private static final Logger LOGGER = + LoggerFactory.getLogger(RandomSourceImpl.class); + private static final String SAMPLER_SEED_PROPERTY = "sampler.seed"; + + private final RandomSeedSupplier seedSupplier = new RandomSeedSupplier(); + private final Environment environment; + private final Lazy randomLazy = Lazy.of(this::doGet); + + RandomSourceImpl(Environment environment) { + this.environment = environment; + } + + @Override + public Random get() { + return randomLazy.get(); + } + + private Random doGet() { + String seed = environment.getProperty( + SAMPLER_SEED_PROPERTY, + seedSupplier.get().toString() + ); + CRC32 crc32 = new CRC32(); + crc32.update(seed.getBytes(StandardCharsets.UTF_8)); + long randomSeed = crc32.getValue(); + LOGGER.info( + "Using seed: {}. To re-execute same exact case, set " + + "environmental variable `export SAMPLER_SEED={}` or Java's " + + "property `-D{}={}` with that seed value.", + seed, + seed, + SAMPLER_SEED_PROPERTY, seed + ); + return new Random(randomSeed); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sampler.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sampler.java index 396ce0a..773e818 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sampler.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/Sampler.java @@ -16,6 +16,14 @@ package pl.wavesoftware.plugs.tools.packager.sample; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + /** * Resolves a sample of a given spec. * @@ -23,17 +31,24 @@ */ public interface Sampler { - /** - * Whether this sampler supports a given spec of a sample - * - * @return a sample spec - */ - boolean supports(SampleSpec spec); + String SAMPLE_SCOPE = "sample"; /** - * Creates a sample based on a spec - * - * @return a created sample + * Creates a new sample */ - Sample create(SampleSpec spec); + T create(); + + @Target({ElementType.TYPE}) + @Retention(RetentionPolicy.RUNTIME) + @Documented + @org.springframework.stereotype.Component + @interface Component { + /** + * The value may indicate a suggestion for a logical component name, + * to be turned into a Spring bean in case of an autodetected component. + * @return the suggested component name, if any (or empty String otherwise) + */ + @AliasFor(annotation = org.springframework.stereotype.Component.class) + String value() default ""; + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerBean.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerBean.java deleted file mode 100644 index 0bd80ec..0000000 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerBean.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package pl.wavesoftware.plugs.tools.packager.sample; - -import org.springframework.core.annotation.AliasFor; -import org.springframework.stereotype.Component; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Component -public @interface SamplerBean { - /** - * The value may indicate a suggestion for a logical component name, - * to be turned into a Spring bean in case of an autodetected component. - * @return the suggested component name, if any (or empty String otherwise) - */ - @AliasFor(annotation = Component.class) - String value() default ""; -} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContext.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContext.java index 9e54972..1dbf685 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContext.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContext.java @@ -16,7 +16,12 @@ package pl.wavesoftware.plugs.tools.packager.sample; -public interface SamplerContext extends AutoCloseable { +import org.springframework.beans.factory.DisposableBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.env.Environment; + +public interface SamplerContext extends DisposableBean { /** * Gets a created sample or create one if it wasn't created before * @@ -24,7 +29,7 @@ public interface SamplerContext extends AutoCloseable { * @param a type of sample to create * @return a sample */ - T get(SampleSpec spec); + T get(Class> spec); /** * Creates a new sample, without caching it in {@link SamplerContext}. @@ -33,8 +38,31 @@ public interface SamplerContext extends AutoCloseable { * @param a type of sample to create * @return a sample */ - Sample createNew(SampleSpec spec); + T createNew(Class> spec); + + /** + * Gets a controller for this sampler context + * + * @return a controller for sampler context + */ + SamplerScopeControl controller(); + + @ComponentScan(includeFilters = @ComponentScan.Filter(Sampler.Component.class)) + class Configuration { + + @Bean + RandomSource samplerRandomSource(Environment environment) { + return new RandomSourceImpl(environment); + } + + @Bean + SamplerScope samplerScope(RandomSource randomSource) { + return new SamplerScope(randomSource); + } - @Override - void close(); + @Bean + static SamplerScopeBeanFactoryPostProcessor samplerScopeBeanFactoryPostProcessor(SamplerScope scope){ + return new SamplerScopeBeanFactoryPostProcessor(scope); + } + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScope.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScope.java new file mode 100644 index 0000000..d05c775 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScope.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.beans.factory.config.Scope; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.UUID; + +final class SamplerScope implements Scope, SamplerScopeControl, DisposableBean { + private static final Logger LOGGER = + LoggerFactory.getLogger(SamplerScope.class); + + private final Map registries = + Collections.synchronizedMap(new HashMap<>()); + private final RandomSource randomSource; + @Nullable + private UUID id; + + public SamplerScope(RandomSource randomSource) { + this.randomSource = randomSource; + } + + @Override + public Object get(String name, ObjectFactory objectFactory) { + return getRegistry().get(name, objectFactory); + } + + @Override + public Object remove(String name) { + return getRegistry().remove(name); + } + + @Override + public void registerDestructionCallback(String name, Runnable runnable) { + getRegistry().registerDestructionCallback(name, runnable); + } + + @Override + public Object resolveContextualObject(String key) { + return null; + } + + @Override + public String getConversationId() { + return ensureId().toString(); + } + + @Override + public UUID actualId() { + return ensureId(); + } + + @Override + public void newId() { + Random random = randomSource.get(); + setId(new UUID(random.nextLong(), random.nextLong())); + } + + @Override + public synchronized void setId(UUID id) { + LOGGER.info("Sampler scope: {}", id); + this.id = id; + } + + private SamplerScopeRegistry getRegistry() { + return registries.computeIfAbsent(ensureId(), ignored -> new SamplerScopeRegistry()); + } + + private UUID ensureId() { + if (id == null) { + newId(); + } + return id; + } + + @Override + public void destroy() { + for (Map.Entry entry : registries.entrySet()) { + LOGGER.info("Closing sample scope: {}", entry.getKey()); + entry.getValue().destroy(); + } + } +} diff --git a/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactoryIT.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeBeanFactoryPostProcessor.java similarity index 54% rename from tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactoryIT.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeBeanFactoryPostProcessor.java index 1e55bd8..c616d87 100644 --- a/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactoryIT.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeBeanFactoryPostProcessor.java @@ -16,25 +16,20 @@ package pl.wavesoftware.plugs.tools.packager.sample; -import org.junit.jupiter.api.Test; -import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.sample.project.Projects; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import static org.assertj.core.api.Assertions.assertThat; +final class SamplerScopeBeanFactoryPostProcessor + implements BeanFactoryPostProcessor { -class SamplerContextFactoryIT { + private final SamplerScope samplerScope; - @Test - void create() { - // given - SamplerContextFactory factory = new SamplerContextFactory(); - - // when - try (SamplerContext ctx = factory.create()) { - Project project = ctx.get(Projects.SIMPLE); + SamplerScopeBeanFactoryPostProcessor(SamplerScope samplerScope) { + this.samplerScope = samplerScope; + } - // then - assertThat(project).isNotNull(); - } + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) { + factory.registerScope(Sampler.SAMPLE_SCOPE, samplerScope); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactory.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeControl.java similarity index 75% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactory.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeControl.java index 766dee0..fc577b7 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextFactory.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeControl.java @@ -16,11 +16,10 @@ package pl.wavesoftware.plugs.tools.packager.sample; -public final class SamplerContextFactory { - public SamplerContext create() { - SamplerContext ctx = SpringSamplerContext.boot(); - Runtime.getRuntime().addShutdownHook(new Thread(ctx::close)); - return ctx; - } +import java.util.UUID; +public interface SamplerScopeControl { + UUID actualId(); + void newId(); + void setId(UUID id); } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeRegistry.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeRegistry.java new file mode 100644 index 0000000..97df70b --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerScopeRegistry.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.ObjectFactory; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +final class SamplerScopeRegistry implements DisposableBean { + + private static final Logger LOGGER = + LoggerFactory.getLogger(SamplerScopeRegistry.class); + + private final Map objects = + Collections.synchronizedMap(new HashMap<>()); + private final Map destructionCallbacks = + Collections.synchronizedMap(new HashMap<>()); + + Object get(String name, ObjectFactory objectFactory) { + return objects.computeIfAbsent(name, ignored -> objectFactory.getObject()); + } + + @Nullable + Object remove(String name) { + return objects.remove(name); + } + + void registerDestructionCallback(String name, Runnable runnable) { + destructionCallbacks.put(name, runnable); + } + + @Override + public void destroy() { + for (Map.Entry entry : destructionCallbacks.entrySet()) { + try { + entry.getValue().run(); + } catch (Exception ex) { + LOGGER.error("Error occurred while destroying " + entry.getKey(), ex); + } + } + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SpringSamplerContext.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SpringSamplerContext.java index a273ef6..2d426ba 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SpringSamplerContext.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SpringSamplerContext.java @@ -16,84 +16,31 @@ package pl.wavesoftware.plugs.tools.packager.sample; -import io.vavr.collection.HashSet; -import io.vavr.collection.Set; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.ComponentScan; -import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; -import java.util.HashMap; -import java.util.Map; +import static pl.wavesoftware.plugs.tools.packager.sample.Sampler.SAMPLE_SCOPE; -@ComponentScan(includeFilters = @ComponentScan.Filter(SamplerBean.class)) -class SpringSamplerContext implements SamplerContext { - private static final Logger LOGGER = - LoggerFactory.getLogger(SpringSamplerContext.class); - private static final Object CLOSING_MONITOR = new Object(); - private final ConfigurableApplicationContext applicationContext; - private final Map, Sample> samples = new HashMap<>(); +@Sampler.Component +@Scope(proxyMode = ScopedProxyMode.INTERFACES, value = SAMPLE_SCOPE) +class SpringSamplerContext extends AbstractSamplerContext { - private boolean closed = false; + private final ApplicationContext context; + private final SamplerScopeControl control; - SpringSamplerContext(ConfigurableApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - static SamplerContext boot() { - return new SamplerContextProxy(() -> { - LOGGER.info("Booting SamplerContext using Spring..."); - ApplicationContext ctx = - new AnnotationConfigApplicationContext(SpringSamplerContext.class); - return ctx.getBean(SamplerContext.class); - }); - } - - @Override - public T get(SampleSpec spec) { - @SuppressWarnings("unchecked") - T val = (T) samples.computeIfAbsent(spec, this::createNew).get(); - return val; + SpringSamplerContext(ApplicationContext context, SamplerScopeControl control) { + this.context = context; + this.control = control; } @Override - @SuppressWarnings({"unchecked", "rawtypes"}) - public Sample createNew(SampleSpec spec) { - Set samplers = HashSet.ofAll( - applicationContext - .getBeansOfType(Sampler.class) - .values() - ); - Sample sample = samplers - .filter(sampler -> sampler.supports(spec)) - .map(sampler -> (Sampler) sampler) - .getOrElseThrow(() -> new EidIllegalArgumentException("20190523:131128")) - .create(spec); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug( - "Created sample {} for spec {}.{}", - Integer.toHexString(sample.hashCode()), - spec.getClass().getSimpleName(), spec - ); - } - return sample; + protected Sampler getSampler(Class> spec) { + return context.getBean(spec); } @Override - public void close() { - synchronized (CLOSING_MONITOR) { - if (closed) { - return; - } - closed = true; - LOGGER.info("Closing SamplerContext..."); - samples.values().forEach(Sample::close); - samples.clear(); - applicationContext.close(); - } + public SamplerScopeControl controller() { + return control; } - } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java index f3274ef..e928bbd 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java @@ -16,35 +16,9 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; -import com.github.zafarkhaja.semver.Version; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -@SamplerBean -final class HibernateArtifact implements Sampler { +public interface HibernateArtifact extends Sampler { - private final SamplerContext context; - - HibernateArtifact(SamplerContext context) { - this.context = context; - } - - @Override - public boolean supports(SampleSpec spec) { - return Artifacts.HIBERNATE == spec; - } - - @Override - public Sample create(SampleSpec spec) { - return (Sample.Easy) () -> new MavenArtifact( - () -> context, - "hibernate-core", - "org.hibernate", - Version.valueOf("5.4.2.Final") - ); - } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java index 4edba19..3638d33 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java @@ -16,34 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; -import com.github.zafarkhaja.semver.Version; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -@SamplerBean -final class Jsr305Artifact implements Sampler { - private final SamplerContext context; - - Jsr305Artifact(SamplerContext context) { - this.context = context; - } - - @Override - public boolean supports(SampleSpec spec) { - return Artifacts.JSR305 == spec; - } - - @Override - public Sample create(SampleSpec spec) { - return (Sample.Easy) () -> new MavenArtifact( - () -> context, - "jsr305", - "com.google.code.findbugs", - Version.valueOf("3.0.2") - ); - } +public interface Jsr305Artifact extends Sampler { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java index b6272c8..61dcf73 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java @@ -16,34 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; -import com.github.zafarkhaja.semver.Version; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -@SamplerBean -final class OsgiCoreArtifact implements Sampler { - private final SamplerContext context; - - OsgiCoreArtifact(SamplerContext context) { - this.context = context; - } - - @Override - public boolean supports(SampleSpec spec) { - return Artifacts.OSGI_CORE == spec; - } - - @Override - public Sample create(SampleSpec spec) { - return (Sample.Easy) () -> new MavenArtifact( - () -> context, - "osgi.core", - "org.osgi", - Version.valueOf("7.0.0") - ); - } +public interface OsgiCoreArtifact extends Sampler { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Artifacts.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java similarity index 81% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Artifacts.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java index 0122be8..edb7acb 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Artifacts.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java @@ -17,11 +17,7 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -public enum Artifacts implements SampleSpec { - SIMPLE, - HIBERNATE, - JSR305, - OSGI_CORE +public interface SimpleProjectBuildArtifact extends Sampler { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java new file mode 100644 index 0000000..a866e31 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.github.zafarkhaja.semver.Version; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; +import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; + +@Sampler.Component +final class HibernateArtifactImpl implements HibernateArtifact { + private final SamplerContext context; + + HibernateArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenArtifact( + context, + "hibernate-core", + "org.hibernate", + Version.valueOf("5.4.2.Final") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java new file mode 100644 index 0000000..90ce775 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.github.zafarkhaja.semver.Version; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; +import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; + +@Sampler.Component +final class Jsr305ArtifactImpl implements Jsr305Artifact { + private final SamplerContext context; + + Jsr305ArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenArtifact( + context, + "jsr305", + "com.google.code.findbugs", + Version.valueOf("3.0.2") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenArtifact.java similarity index 79% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenArtifact.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenArtifact.java index 3ce970c..3dd794f 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenArtifact.java @@ -14,31 +14,25 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact; +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; import com.github.zafarkhaja.semver.Version; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -import pl.wavesoftware.plugs.tools.packager.sample.fs.Paths; +import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; import java.nio.file.Path; -import java.util.function.Supplier; final class MavenArtifact implements Artifact { - private final Supplier contextSupplier; + private final SamplerContext context; private final String name; private final String group; private final Version version; - MavenArtifact( - Supplier contextSupplier, - String name, - String group, - Version version - ) { - this.contextSupplier = contextSupplier; + MavenArtifact(SamplerContext context, String name, String group, Version version) { + this.context = context; this.name = name; this.group = group; this.version = version; @@ -56,7 +50,7 @@ public Version version() { @Override public Path path() { - Path root = contextSupplier.get().get(Paths.VIRTUAL_ROOT); + Path root = context.get(VirtualRoot.class); return root.resolve("home") .resolve("jenkins") .resolve(".m2") diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystem.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java similarity index 50% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystem.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java index 47b7014..c6b4e7f 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystem.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java @@ -14,24 +14,29 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.fs; +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; +import com.github.zafarkhaja.semver.Version; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; +import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; -import java.nio.file.Path; +@Sampler.Component +final class OsgiCoreArtifactImpl implements OsgiCoreArtifact { + private final SamplerContext context; -@SamplerBean -final class VirtualFileSystem implements Sampler { - @Override - public boolean supports(SampleSpec spec) { - return Paths.VIRTUAL_ROOT == spec; + OsgiCoreArtifactImpl(SamplerContext context) { + this.context = context; } @Override - public Sample create(SampleSpec spec) { - return new VirtualFileSystemSample(); + public Artifact create() { + return new MavenArtifact( + context, + "osgi.core", + "org.osgi", + Version.valueOf("7.0.0") + ); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java similarity index 76% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java index 2db1bca..3c13e3e 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java @@ -14,19 +14,17 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact; +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; import com.github.zafarkhaja.semver.Version; import io.vavr.Lazy; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -import pl.wavesoftware.plugs.tools.packager.sample.project.Projects; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; import java.nio.file.Files; import java.nio.file.Path; @@ -37,30 +35,26 @@ import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; -@SamplerBean -final class SimpleArtifact implements Sampler { +@Sampler.Component +final class SimpleProjectBuildArtifactImpl implements SimpleProjectBuildArtifact { private static final byte[] BYTES = {0x56, 0x45}; private static final Instant FILE_MOD_TIME = LocalDateTime.of( 2019, 5, 23, 20, 39 ).toInstant(ZoneOffset.ofHours(2)); + private final SamplerContext context; - SimpleArtifact(SamplerContext context) { + SimpleProjectBuildArtifactImpl(SamplerContext context) { this.context = context; } @Override - public boolean supports(SampleSpec spec) { - return Artifacts.SIMPLE == spec; - } - - @Override - public Sample create(SampleSpec spec) { - return (Sample.Easy) () -> new Artifact() { + public Artifact create() { + return new Artifact() { private final Lazy artifactPath = Lazy.of(() -> { - Project project = context.get(Projects.SIMPLE); + Project project = context.get(SimpleProject.class); Path path = project.outputPath().resolve(project.finalName()); tryToExecute(() -> { Files.createDirectories(path.getParent()); diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/Projects.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java similarity index 70% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/Projects.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java index 00954fe..5f811dc 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/Projects.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java @@ -14,11 +14,10 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.project; +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; -import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; -public enum Projects implements SampleSpec { - SIMPLE -} +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/Paths.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java similarity index 84% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/Paths.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java index 20a0c13..55a659c 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/Paths.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java @@ -16,10 +16,11 @@ package pl.wavesoftware.plugs.tools.packager.sample.fs; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; import java.nio.file.Path; -public enum Paths implements SampleSpec { - VIRTUAL_ROOT + +public interface VirtualRoot extends Sampler { + } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystemSample.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java similarity index 76% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystemSample.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java index 85c32f8..d0fb497 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualFileSystemSample.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java @@ -18,23 +18,23 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; +import org.springframework.beans.factory.DisposableBean; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; import java.nio.file.FileSystem; import java.nio.file.Path; -import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; - -final class VirtualFileSystemSample implements Sample { - +@Sampler.Component +final class VirtualRootImpl implements VirtualRoot, DisposableBean { private final FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); + @Override - public void close() { - tryToExecute(fs::close, "20190523:202849"); + public Path create() { + return fs.getPath("/"); } @Override - public Path get() { - return fs.getPath("/"); + public void destroy() throws Exception { + fs.close(); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java index 987a0b9..e3cb5c6 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java @@ -16,57 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.sample.project; -import io.vavr.collection.HashSet; -import io.vavr.collection.Set; -import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.sample.Sample; -import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec; import pl.wavesoftware.plugs.tools.packager.sample.Sampler; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerBean; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.Artifacts; -import pl.wavesoftware.plugs.tools.packager.sample.fs.Paths; - -import java.nio.file.Path; - -@SamplerBean -final class SimpleProject implements Sampler { - - private final SamplerContext context; - - SimpleProject(SamplerContext context) { - this.context = context; - } - - @Override - public boolean supports(SampleSpec spec) { - return spec == Projects.SIMPLE; - } - - @Override - public Sample create(SampleSpec spec) { - Path root = context.get(Paths.VIRTUAL_ROOT); - return (Sample.Easy) () -> new AbstractProject(root, "simple") { - @Override - public Artifact mainArtifact() { - return context.get(Artifacts.SIMPLE); - } - - @Override - public Set dependencies() { - return HashSet - .of(Artifacts.HIBERNATE, Artifacts.JSR305) - .map(context::get); - } - - @Override - public Set imports() { - return HashSet - .of(Artifacts.OSGI_CORE) - .map(context::get); - } - }; - } +public interface SimpleProject extends Sampler { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java new file mode 100644 index 0000000..1aedc35 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample.project; + +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.Sampler; +import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; + +import java.nio.file.Path; + +@Sampler.Component +final class SimpleProjectImpl implements SimpleProject { + + private final SamplerContext context; + + SimpleProjectImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Project create() { + Path root = context.get(VirtualRoot.class); + return new AbstractProject(root, "simple") { + @Override + public Artifact mainArtifact() { + return context.get(SimpleProjectBuildArtifact.class); + } + + @Override + public Set dependencies() { + return HashSet + .of(HibernateArtifact.class, Jsr305Artifact.class) + .map(context::get); + } + + @Override + public Set imports() { + return HashSet + .of(OsgiCoreArtifact.class) + .map(context::get); + } + }; + } + +} diff --git a/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextIT.java b/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextIT.java new file mode 100644 index 0000000..59c7638 --- /dev/null +++ b/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerContextIT.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2019 Wave Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.wavesoftware.plugs.tools.packager.sample; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = PackagerSamplerContext.class) +class SamplerContextIT { + + @Autowired + private SamplerContext samplerContext; + + @Test + void get() { + // when + Project project = samplerContext.get(SimpleProject.class); + + // then + assertThat(project).isNotNull(); + } + + @Test + void diffrentScopes() { + // when + Project project1 = samplerContext.get(SimpleProject.class); + Project project2 = samplerContext.get(SimpleProject.class); + UUID firstId = samplerContext.controller().actualId(); + samplerContext.controller().newId(); + Project project3 = samplerContext.get(SimpleProject.class); + Project project4 = samplerContext.get(SimpleProject.class); + UUID secondId = samplerContext.controller().actualId(); + samplerContext.controller().setId(firstId); + Project project5 = samplerContext.get(SimpleProject.class); + Project project6 = samplerContext.get(SimpleProject.class); + + // then + assertThat(project1).isNotNull().isSameAs(project2); + assertThat(project3).isNotNull().isSameAs(project4); + assertThat(project3).isNotSameAs(project1); + assertThat(project5).isNotNull().isSameAs(project6).isSameAs(project1); + assertThat(firstId).isNotNull().isNotEqualTo(secondId); + assertThat(secondId).isNotNull(); + } + +} diff --git a/tools/plugs-packager-testing/pom.xml b/tools/plugs-packager-testing/pom.xml deleted file mode 100644 index cc5b078..0000000 --- a/tools/plugs-packager-testing/pom.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - 4.0.0 - - plugs-tools-parent - pl.wavesoftware.plugs.tools - 0.1.0-SNAPSHOT - - - plugs-packager-testing - Plugs :: Tools :: Packager Testing - - - - pl.wavesoftware.plugs - testing - ${project.version} - compile - - - pl.wavesoftware.plugs.tools - plugs-packager-samples - ${project.version} - - - - diff --git a/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/Fresh.java b/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/Fresh.java deleted file mode 100644 index a3de909..0000000 --- a/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/Fresh.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package pl.wavesoftware.plugs.tools.packager.testing; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.PARAMETER, ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -public @interface Fresh { - boolean value() default true; -} diff --git a/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/SamplerExtension.java b/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/SamplerExtension.java deleted file mode 100644 index e7305e2..0000000 --- a/tools/plugs-packager-testing/src/main/java/pl/wavesoftware/plugs/tools/packager/testing/SamplerExtension.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package pl.wavesoftware.plugs.tools.packager.testing; - -import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ExtensionContext.Namespace; -import org.junit.jupiter.api.extension.ExtensionContext.Store; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.jupiter.api.extension.ParameterResolver; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; -import pl.wavesoftware.plugs.tools.packager.sample.SamplerContextFactory; - -import java.util.Optional; - -public final class SamplerExtension - implements ParameterResolver, AfterEachCallback { - private static final Namespace GLOBAL = - Namespace.create(SamplerExtension.class, "global"); - private static final Namespace TEST = - Namespace.create(SamplerExtension.class, "test"); - - @Override - public boolean supportsParameter( - ParameterContext parameterContext, - ExtensionContext extensionContext - ) { - return SamplerContext.class.isAssignableFrom( - parameterContext.getParameter().getType() - ); - } - - @Override - public Object resolveParameter( - ParameterContext parameterContext, - ExtensionContext extensionContext - ) { - boolean freshness = parameterContext.findAnnotation(Fresh.class) - .map(Fresh::value) - .orElse(false); - Store store = getStore(extensionContext, freshness); - return store.getOrComputeIfAbsent( - SamplerContext.class, this::create, SamplerContext.class - ); - } - - @Override - public void afterEach(ExtensionContext context) { - Store store = getStore(context, true); - Optional.ofNullable(store.get(SamplerContext.class, SamplerContext.class)) - .ifPresent(SamplerContext::close); - } - - private SamplerContext create(Class cls) { - SamplerContextFactory factory = new SamplerContextFactory(); - return factory.create(); - } - - private Store getStore(ExtensionContext extensionContext, boolean freshness) { - if (freshness) { - return extensionContext.getStore(TEST); - } else { - return extensionContext.getRoot().getStore(GLOBAL); - } - } -} diff --git a/tools/pom.xml b/tools/pom.xml index d541fa5..d28e3d4 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -40,7 +40,6 @@ plugs-packager-api plugs-packager-core plugs-packager-samples - plugs-packager-testing plugs-maven-plugin