Skip to content

Commit

Permalink
Reduce scope of Service Loader work to CompressionCodecs and JsonSeri…
Browse files Browse the repository at this point in the history
…alizers

Fixes: #458
  • Loading branch information
bdemers committed Oct 17, 2019
1 parent c1d72c6 commit e78acaa
Show file tree
Hide file tree
Showing 49 changed files with 486 additions and 596 deletions.
28 changes: 0 additions & 28 deletions api/src/main/java/io/jsonwebtoken/CompressionCodecFactory.java

This file was deleted.

10 changes: 5 additions & 5 deletions api/src/main/java/io/jsonwebtoken/CompressionCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.jsonwebtoken;

import io.jsonwebtoken.lang.Services;
import io.jsonwebtoken.lang.Classes;

/**
* Provides default implementations of the {@link CompressionCodec} interface.
Expand All @@ -26,16 +26,15 @@
*/
public final class CompressionCodecs {

private static final CompressionCodecFactory FACTORY = Services.loadFirst(CompressionCodecFactory.class);

private CompressionCodecs() {
} //prevent external instantiation

/**
* Codec implementing the <a href="https://tools.ietf.org/html/rfc7518">JWA</a> standard
* <a href="https://en.wikipedia.org/wiki/DEFLATE">deflate</a> compression algorithm
*/
public static final CompressionCodec DEFLATE = FACTORY.deflateCodec();
public static final CompressionCodec DEFLATE =
Classes.newInstance("io.jsonwebtoken.impl.compression.DeflateCompressionCodec");

/**
* Codec implementing the <a href="https://en.wikipedia.org/wiki/Gzip">gzip</a> compression algorithm.
Expand All @@ -44,6 +43,7 @@ private CompressionCodecs() {
* that all parties accessing the token support the gzip algorithm.</p>
* <p>If you're concerned about compatibility, the {@link #DEFLATE DEFLATE} code is JWA standards-compliant.</p>
*/
public static final CompressionCodec GZIP = FACTORY.gzipCodec();
public static final CompressionCodec GZIP =
Classes.newInstance("io.jsonwebtoken.impl.compression.GzipCompressionCodec");

}
80 changes: 0 additions & 80 deletions api/src/main/java/io/jsonwebtoken/JwtFactory.java

This file was deleted.

22 changes: 11 additions & 11 deletions api/src/main/java/io/jsonwebtoken/Jwts.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package io.jsonwebtoken;

import io.jsonwebtoken.lang.Services;
import io.jsonwebtoken.lang.Classes;

import java.util.Map;

/**
* Factory class useful for creating instances of JWT interfaces. Using this factory class can be a good
* alternative to tightly coupling your code to implementation classes.
*
* @since 0.11
* @since 0.1
*/
public final class Jwts {

private static final JwtFactory FACTORY = Services.loadFirst(JwtFactory.class);
private static final Class[] MAP_ARG = new Class[]{Map.class};

private Jwts() {
}
Expand All @@ -40,7 +40,7 @@ private Jwts() {
* @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
*/
public static Header header() {
return FACTORY.header();
return Classes.newInstance("io.jsonwebtoken.impl.DefaultHeader");
}

/**
Expand All @@ -51,7 +51,7 @@ public static Header header() {
* @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
*/
public static Header header(Map<String, Object> header) {
return FACTORY.header(header);
return Classes.newInstance("io.jsonwebtoken.impl.DefaultHeader", MAP_ARG, header);
}

/**
Expand All @@ -61,7 +61,7 @@ public static Header header(Map<String, Object> header) {
* @see JwtBuilder#setHeader(Header)
*/
public static JwsHeader jwsHeader() {
return FACTORY.jwsHeader();
return Classes.newInstance("io.jsonwebtoken.impl.DefaultJwsHeader");
}

/**
Expand All @@ -73,7 +73,7 @@ public static JwsHeader jwsHeader() {
* @see JwtBuilder#setHeader(Header)
*/
public static JwsHeader jwsHeader(Map<String, Object> header) {
return FACTORY.jwsHeader(header);
return Classes.newInstance("io.jsonwebtoken.impl.DefaultJwsHeader", MAP_ARG, header);
}

/**
Expand All @@ -82,7 +82,7 @@ public static JwsHeader jwsHeader(Map<String, Object> header) {
* @return a new {@link Claims} instance to be used as a JWT body.
*/
public static Claims claims() {
return FACTORY.claims();
return Classes.newInstance("io.jsonwebtoken.impl.DefaultClaims");
}

/**
Expand All @@ -92,7 +92,7 @@ public static Claims claims() {
* @return a new {@link Claims} instance populated with the specified name/value pairs.
*/
public static Claims claims(Map<String, Object> claims) {
return FACTORY.claims(claims);
return Classes.newInstance("io.jsonwebtoken.impl.DefaultClaims", MAP_ARG, claims);
}

/**
Expand All @@ -118,7 +118,7 @@ public static Claims claims(Map<String, Object> claims) {
*/
@Deprecated
public static JwtParser parser() {
return FACTORY.parser();
return Classes.newInstance("io.jsonwebtoken.impl.DefaultJwtParser");
}

/**
Expand All @@ -138,6 +138,6 @@ public static JwtParserBuilder parserBuilder() {
* strings.
*/
public static JwtBuilder builder() {
return FACTORY.builder();
return Classes.newInstance("io.jsonwebtoken.impl.DefaultJwtBuilder");
}
}

This file was deleted.

21 changes: 18 additions & 3 deletions api/src/main/java/io/jsonwebtoken/lang/Services.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (C) 2019 jsonwebtoken.io
*
* 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 io.jsonwebtoken.lang;

import java.util.ArrayList;
Expand All @@ -19,7 +34,7 @@ public final class Services {
* @return An unmodifiable list with an instance of all available implementations of the SPI. No guarantee is given
* on the order of implementations, if more than one.
*/
public static <T> List<T> loadAllAvailableImplementations(Class<T> spi) {
public static <T> List<T> loadAll(Class<T> spi) {
ServiceLoader<T> serviceLoader = ServiceLoader.load(spi);

List<T> implementations = new ArrayList<>();
Expand All @@ -38,14 +53,14 @@ public static <T> List<T> loadAllAvailableImplementations(Class<T> spi) {
* @param spi The class of the Service Provider Interface
* @param <T> The type of the SPI
* @return A new instance of the service.
* @throws ImplementationNotFoundException When no implementation the SPI is available on the classpath.
* @throws UnavailableImplementationException When no implementation the SPI is available on the classpath.
*/
public static <T> T loadFirst(Class<T> spi) {
ServiceLoader<T> serviceLoader = ServiceLoader.load(spi);
if (serviceLoader.iterator().hasNext()) {
return serviceLoader.iterator().next();
} else {
throw new ImplementationNotFoundException("No implementation of " + spi.getName() + " found on the classpath. Make sure to include an implementation of jjwt-api.");
throw new UnavailableImplementationException(spi);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 jsonwebtoken.io
*
* 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 io.jsonwebtoken.lang;

/**
* Exception indicating that no implementation of an jjwt-api SPI was found on the classpath.
*/
public class UnavailableImplementationException extends RuntimeException {

private static final String DEFAULT_NOT_FOUND_MESSAGE = "Unable to find an implementation for %s using java.util.ServiceLoader. Ensure you include a backing implementation .jar in the classpath, for example jjwt-impl.jar, or your own .jar for custom implementations.";

public UnavailableImplementationException(final Class klass) {
super(String.format(DEFAULT_NOT_FOUND_MESSAGE, klass));
}
}
19 changes: 0 additions & 19 deletions api/src/main/java/io/jsonwebtoken/security/KeyGenerator.java

This file was deleted.

19 changes: 0 additions & 19 deletions api/src/main/java/io/jsonwebtoken/security/KeyPairGenerator.java

This file was deleted.

Loading

0 comments on commit e78acaa

Please sign in to comment.