Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Stapler's Guava dependency #213

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.github.ben-manes.caffeine:caffeine</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>org.kohsuke.stapler.shaded.com.github.benmanes.caffeine</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>com.github.ben-manes.caffeine:caffeine</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -88,9 +123,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>11.0.1</version>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/kohsuke/stapler/AbstractTearOff.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

package org.kohsuke.stapler;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.File;
Expand Down Expand Up @@ -58,7 +58,7 @@ private static final class ExpirableCacheHit<S> {
this.script = script;
}
}
private final Cache<URL, ExpirableCacheHit<S>> cachedScripts = CacheBuilder.newBuilder().softValues().build();
private final Cache<URL, ExpirableCacheHit<S>> cachedScripts = Caffeine.newBuilder().softValues().build();

protected AbstractTearOff(MetaClass owner, Class<CLT> cltClass) {
this.owner = owner;
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/kohsuke/stapler/CachingScriptLoader.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.kohsuke.stapler;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.LoadingCache;

import java.net.URL;

Expand All @@ -25,7 +25,7 @@ public abstract class CachingScriptLoader<S, E extends Exception> {
*
* {@link Optional} is used as Google Collection doesn't allow null values in a map.
*/
private final LoadingCache<String,Optional<S>> scripts = CacheBuilder.newBuilder().softValues().build(new CacheLoader<String, Optional<S>>() {
private final LoadingCache<String,Optional<S>> scripts = Caffeine.newBuilder().softValues().build(new CacheLoader<String, Optional<S>>() {
public Optional<S> load(String from) {
try {
return Optional.create(loadScript(from));
Expand Down Expand Up @@ -59,7 +59,7 @@ public S findScript(String name) throws E {
if (MetaClass.NO_CACHE)
return loadScript(name);
else
return scripts.getUnchecked(name).get();
return scripts.get(name).get();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/kohsuke/stapler/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

package org.kohsuke.stapler;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.interceptor.Interceptor;
import org.kohsuke.stapler.interceptor.InterceptorAnnotation;
Expand Down Expand Up @@ -195,7 +195,7 @@ Object bindAndInvoke(Object o, StaplerRequest req, StaplerResponse rsp, Object..
}

// if the databinding method is provided, call that
Function binder = PARSE_METHODS.getUnchecked(t);
Function binder = PARSE_METHODS.get(t);
if (binder!=RETURN_NULL) {
arguments[i] = binder.bindAndInvoke(null,req,rsp);
continue;
Expand Down Expand Up @@ -226,7 +226,7 @@ Object bindAndInvoke(Object o, StaplerRequest req, StaplerResponse rsp, Object..
throw new AssertionError(e); // impossible
}

PARSE_METHODS = CacheBuilder.newBuilder().weakKeys().build(new CacheLoader<Class,Function>() {
PARSE_METHODS = Caffeine.newBuilder().weakKeys().build(new CacheLoader<Class,Function>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this ever worked to begin with. See https://github.com/jglick/biweak-class-map

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this ever worked to begin with

This would not have worked in the case of a class with a fromStapler method, because then the value would be a MethodFunction whose Method m has a Class<?> clazz that refers to the key of the map.

But it would have worked in the case of a class without a fromStapler method, because then the value would be a StaticFunction whose Method m has a Class<?> clazz that refers to org.kohsuke.stapler.Function rather than the key of the map.

public Function load(Class from) {
// MethdFunction for invoking a static method as a static method
FunctionList methods = new ClassDescriptor(from).methods.name("fromStapler");
Expand Down
79 changes: 79 additions & 0 deletions core/src/main/java/org/kohsuke/stapler/export/Iterators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2007 The Guava Authors
*
* 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 org.kohsuke.stapler.export;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* This class contains static utility methods that operate on or return objects of type {@link
* Iterator}.
*
* <p><i>Performance notes:</i> Unless otherwise noted, all of the iterators produced in this class
* are <i>lazy</i>, which means that they only advance the backing iteration when absolutely
* necessary.
*
* <p>See the Guava User Guide section on <a href=
* "https://github.com/google/guava/wiki/CollectionUtilitiesExplained#iterables"> {@code
* Iterators}</a>.
*
* @author Kevin Bourrillion
* @author Jared Levy
*/
final class Iterators {
private Iterators() {}

/**
* Returns a view containing the first {@code limitSize} elements of {@code iterator}. If {@code
* iterator} contains fewer than {@code limitSize} elements, the returned view contains all of its
* elements. The returned iterator supports {@code remove()} if {@code iterator} does.
*
* @param iterator the iterator to limit
* @param limitSize the maximum number of elements in the returned iterator
* @throws IllegalArgumentException if {@code limitSize} is negative
*/
static <T> Iterator<T> limit(final Iterator<T> iterator, final int limitSize) {
if (iterator == null) {
throw new NullPointerException();
}
if (limitSize < 0) {
throw new IllegalArgumentException("limit is negative");
}
return new Iterator<T>() {
private int count;

@Override
public boolean hasNext() {
return count < limitSize && iterator.hasNext();
}

@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
count++;
return iterator.next();
}

@Override
public void remove() {
iterator.remove();
}
};
}
}
2 changes: 0 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/export/Range.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.kohsuke.stapler.export;

import com.google.common.collect.Iterators;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

package org.kohsuke.stapler.jelly;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.TagLibrary;
Expand All @@ -35,6 +32,8 @@

import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* {@link MetaClassLoader} tear-off for Jelly support.
Expand All @@ -47,7 +46,7 @@ public class JellyClassLoaderTearOff {
/**
* See {@link JellyClassTearOff#scripts} for why we use {@link WeakReference} here.
*/
private volatile WeakReference<LoadingCache<String,TagLibrary>> taglibs;
private volatile WeakReference<ConcurrentMap<String,TagLibrary>> taglibs;

static ExpressionFactory EXPRESSION_FACTORY = new JexlExpressionFactory();

Expand All @@ -56,46 +55,43 @@ public JellyClassLoaderTearOff(MetaClassLoader owner) {
}

public TagLibrary getTagLibrary(String nsUri) {
LoadingCache<String,TagLibrary> m=null;
ConcurrentMap<String,TagLibrary> m=null;
if(taglibs!=null)
m = taglibs.get();
if(m==null) {
m = CacheBuilder.newBuilder().build(new CacheLoader<String,TagLibrary>() {
public TagLibrary load(String nsUri) {
m = new ConcurrentHashMap<>();
taglibs = new WeakReference<>(m);
}
TagLibrary tl = m.computeIfAbsent(nsUri, key -> {
if(owner.parent!=null) {
// parent first
TagLibrary tl = owner.parent.loadTearOff(JellyClassLoaderTearOff.class).getTagLibrary(nsUri);
if(tl!=null) return tl;
TagLibrary taglib = owner.parent.loadTearOff(JellyClassLoaderTearOff.class).getTagLibrary(key);
if(taglib!=null) return taglib;
jglick marked this conversation as resolved.
Show resolved Hide resolved
}

String taglibBasePath = trimHeadSlash(nsUri);
String taglibBasePath = trimHeadSlash(key);
try {
URL res = owner.loader.getResource(taglibBasePath +"/taglib");
if(res!=null)
return new CustomTagLibrary(createContext(),owner.loader,nsUri,taglibBasePath);
return new CustomTagLibrary(createContext(),owner.loader,key,taglibBasePath);
} catch (IllegalArgumentException e) {
// if taglibBasePath doesn't even look like an URL, getResource throws IllegalArgumentException.
// see http://old.nabble.com/bug-1.331-to26145963.html
}

// support URIs like "this:it" or "this:instance". Note that "this" URI itself is registered elsewhere
if (nsUri.startsWith("this:"))
if (key.startsWith("this:"))
try {
return new ThisTagLibrary(EXPRESSION_FACTORY.createExpression(nsUri.substring(5)));
return new ThisTagLibrary(EXPRESSION_FACTORY.createExpression(key.substring(5)));
} catch (JellyException e) {
throw new IllegalArgumentException("Illegal expression in the URI: "+nsUri,e);
throw new IllegalArgumentException("Illegal expression in the URI: "+key,e);
}

if (nsUri.equals("jelly:stapler"))
if (key.equals("jelly:stapler"))
return new StaplerTagLibrary();

return NO_SUCH_TAGLIBRARY; // "not found" is also cached.
}
});
taglibs = new WeakReference<LoadingCache<String,TagLibrary>>(m);
}

TagLibrary tl = m.getUnchecked(nsUri);
});
if (tl==NO_SUCH_TAGLIBRARY) return null;
return tl;
}
Expand Down