Skip to content

Commit

Permalink
Added a working sampler for project with artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Suszynski committed May 23, 2019
1 parent 57ffd6f commit 8047231
Show file tree
Hide file tree
Showing 32 changed files with 765 additions and 149 deletions.
20 changes: 9 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath />
</parent>
<groupId>pl.wavesoftware.plugs</groupId>
<artifactId>plugs-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
Expand Down Expand Up @@ -113,20 +119,12 @@
<sonar.skip>${skipTests}</sonar.skip>

<!-- Versions -->
<spring.boot.version>2.1.2.RELEASE</spring.boot.version>
<felix.version>6.0.2</felix.version>
<felix.version>6.0.3</felix.version>
<maven.version>3.6.0</maven.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
Expand Down Expand Up @@ -294,15 +292,15 @@

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<version>2.22.2</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
<version>2.22.2</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
Expand Down Expand Up @@ -52,7 +53,7 @@ private CollectorAppender(
CollectorManager manager,
boolean ignoreExceptions
) {
super(name, filter, layout, ignoreExceptions);
super(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
this.manager = manager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.3.RELEASE</version>
<version>2.1.5.RELEASE</version>
<relativePath />
</parent>
<groupId>org.example</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ final class ProjectDigestImpl implements ProjectDigest {
public CharSequence digest(Project project) throws IOException {
Path sourcePath = project.mainArtifact().path();
Path buildFilePath = project.buildFilePath();
checkArgument(sourcePath.toFile().isFile(), "20190131:221929");
CRC32 digest = new CRC32();
digest.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8));
digest.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8));
digest.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8));
digest.update(Long.toHexString(buildFilePath.toFile().lastModified()).getBytes(UTF_8));
return Long.toHexString(Math.abs(digest.getValue()));
checkArgument(Files.isRegularFile(sourcePath), "20190131:221929");
CRC32 digester = new CRC32();
digester.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8));
digester.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8));
digester.update(Long.toHexString(Files.getLastModifiedTime(sourcePath).toMillis()).getBytes(UTF_8));
digester.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8));
digester.update(Long.toHexString(Files.getLastModifiedTime(buildFilePath).toMillis()).getBytes(UTF_8));
return encode(digester.getValue());
}

private CharSequence encode(long digest) {
return Long.toUnsignedString(digest, 36);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pl.wavesoftware.plugs.tools.packager.core.jar.ProjectDigestImpl
pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigestImpl
pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilderImpl
pl.wavesoftware.plugs.tools.packager.core.DefaultPackagerFactory
pl.wavesoftware.plugs.tools.packager.core.DefaultRepackagingIsRequiredFactory
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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.SamplerContext;
import pl.wavesoftware.plugs.tools.packager.sample.project.ProjectSample;
import pl.wavesoftware.plugs.tools.packager.sample.project.Projects;
import pl.wavesoftware.plugs.tools.packager.testing.SamplerExtension;

import java.io.IOException;
Expand All @@ -39,12 +39,12 @@ class ProjectDigestImplTest {
void digest(SamplerContext context) throws IOException {
// given
ProjectDigest digester = new ProjectDigestImpl();
Project project = context.get(ProjectSample.SIMPLE);
Project project = context.get(Projects.SIMPLE);

// when
CharSequence digest = digester.digest(project);

// then
assertThat(digest).isEqualTo("22ssdd33");
assertThat(digest).isEqualTo("1is34vi");
}
}
5 changes: 5 additions & 0 deletions tools/plugs-packager-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.1</version>
</dependency>

<!-- Tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 java.util.function.Supplier;

public interface Sample<T> extends Supplier<T>, AutoCloseable {
@Override
void close();

interface Easy<T> extends Sample<T> {
@Override
default void close() {
// nothing here
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public interface Sampler<T> {
*
* @return a created sample
*/
T create(SampleSpec<T> spec);
Sample<T> create(SampleSpec<T> spec);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface SamplerContext extends AutoCloseable {
* @param <T> a type of sample to create
* @return a sample
*/
<T> T createNew(SampleSpec<T> spec);
<T> Sample<T> createNew(SampleSpec<T> spec);

@Override
void close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

public final class SamplerContextFactory {
public SamplerContext create() {
return new SpringSamplerContext();
SamplerContext ctx = SpringSamplerContext.boot();
Runtime.getRuntime().addShutdownHook(new Thread(ctx::close));
return ctx;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 java.util.function.Supplier;

final class SamplerContextProxy implements SamplerContext {

private final Lazy<SamplerContext> context;

SamplerContextProxy(Supplier<SamplerContext> supplier) {
this.context = Lazy.of(supplier);
}

@Override
public <T> T get(SampleSpec<T> spec) {
return context.get().get(spec);
}

@Override
public <T> Sample<T> createNew(SampleSpec<T> spec) {
return context.get().createNew(spec);
}

@Override
public void close() {
if (context.isEvaluated()) {
context.get().close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package pl.wavesoftware.plugs.tools.packager.sample;

import io.vavr.Lazy;
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;
Expand All @@ -29,50 +29,71 @@
import java.util.HashMap;
import java.util.Map;

@ComponentScan(includeFilters = @ComponentScan.Filter(SamplerBean.class))
class SpringSamplerContext implements SamplerContext {
private static final Logger LOGGER =
LoggerFactory.getLogger(SpringSamplerContext.class);
private final Lazy<ConfigurableApplicationContext> applicationContext =
Lazy.of(() -> {
LOGGER.info("Booting SpringSamplerContext...");
return new AnnotationConfigApplicationContext(SamplerConfiguration.class);
private static final Object CLOSING_MONITOR = new Object();
private final ConfigurableApplicationContext applicationContext;
private final Map<SampleSpec<?>, Sample<?>> samples = new HashMap<>();

private boolean closed = false;

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);
});
private final Map<SampleSpec<?>, Object> samples = new HashMap<>();
}

@Override
public <T> T get(SampleSpec<T> spec) {
@SuppressWarnings("unchecked")
T val = (T) samples.computeIfAbsent(spec, this::createNew);
T val = (T) samples.computeIfAbsent(spec, this::createNew).get();
return val;
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> T createNew(SampleSpec<T> spec) {
LOGGER.debug("Creating sample for spec {}", spec);
public <T> Sample<T> createNew(SampleSpec<T> spec) {
Set<Sampler> samplers = HashSet.ofAll(
applicationContext.get()
applicationContext
.getBeansOfType(Sampler.class)
.values()
);
return samplers
Sample<T> sample = samplers
.filter(sampler -> sampler.supports(spec))
.map(sampler -> (Sampler<T>) 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;
}

@Override
public void close() {
LOGGER.info("Closing SpringSamplerContext...");
samples.clear();
if (applicationContext.isEvaluated()) {
applicationContext.get().close();
synchronized (CLOSING_MONITOR) {
if (closed) {
return;
}
closed = true;
LOGGER.info("Closing SamplerContext...");
samples.values().forEach(Sample::close);
samples.clear();
applicationContext.close();
}
}

@ComponentScan(includeFilters = @ComponentScan.Filter(SamplerBean.class))
private static class SamplerConfiguration {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

import pl.wavesoftware.plugs.tools.packager.api.model.Artifact;
import pl.wavesoftware.plugs.tools.packager.sample.SampleSpec;

public enum Artifacts implements SampleSpec<Artifact> {
SIMPLE,
HIBERNATE,
JSR305,
OSGI_CORE
}
Loading

0 comments on commit 8047231

Please sign in to comment.