Skip to content

Commit

Permalink
Merge pull request Netflix#131 from alexpanov/1.x
Browse files Browse the repository at this point in the history
Fix issue Netflix#129, bump groovy-all version

Looks fine to me.
  • Loading branch information
mikeycohen committed Jul 1, 2015
2 parents 51f23c1 + 442ec65 commit 85ae0cc
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 61 deletions.
2 changes: 1 addition & 1 deletion zuul-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'groovy'

dependencies {
compile 'commons-io:commons-io:2.4'
compile 'org.codehaus.groovy:groovy-all:2.3.1'
compile 'org.codehaus.groovy:groovy-all:2.3.6'
compile 'org.mockito:mockito-all:1.9.5'
compile 'org.slf4j:slf4j-api:1.7.6'

Expand Down
84 changes: 84 additions & 0 deletions zuul-netflix/src/main/java/com/netflix/zuul/FilterId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.netflix.zuul;

import java.util.UUID;

import org.junit.After;
import org.junit.Test;

import net.jcip.annotations.Immutable;
import net.jcip.annotations.ThreadSafe;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Immutable
@ThreadSafe
public final class FilterId {

private String value;

private FilterId(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}

public static class Builder {
private String applicationName = ZuulApplicationInfo.getApplicationName();
private String filterType;
private String filterName;

public Builder applicationName(String applicationName) {
this.applicationName = applicationName;
return this;
}

public Builder filterType(String filterType) {
this.filterType = filterType;
return this;
}

public Builder filterName(String filterName) {
this.filterName = filterName;
return this;
}

public FilterId build() {
return new FilterId(applicationName + ":" + filterName + ":" + filterType);
}
}


public static class UnitTest {

String zuulAppName = ZuulApplicationInfo.getApplicationName();

@After
public void setZuulAppNameBack() {
ZuulApplicationInfo.setApplicationName(zuulAppName);
}

@Test
public void filterId() {
FilterId filterId = new Builder().applicationName("app")
.filterType("com.acme.None")
.filterName("none")
.build();
assertThat(filterId.toString(), is("app:none:com.acme.None"));
}

@Test
public void defaultApplicationName() {
String applicationName = UUID.randomUUID().toString();
ZuulApplicationInfo.setApplicationName(applicationName);

FilterId filterId = new Builder().filterType("com.acme.None")
.filterName("none")
.build();
assertThat(filterId.toString(), is(applicationName + ":none:com.acme.None"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
*/
package com.netflix.zuul.scriptManager;

import net.jcip.annotations.ThreadSafe;

import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;

import com.netflix.zuul.FilterId;
import com.netflix.zuul.ZuulApplicationInfo;
import com.netflix.zuul.ZuulFilter;

import org.junit.Test;

import net.jcip.annotations.ThreadSafe;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Representation of a ZuulFilter for representing and storing in a database
*/

@ThreadSafe
public class FilterInfo implements Comparable<FilterInfo>{

Expand All @@ -34,20 +44,14 @@ public class FilterInfo implements Comparable<FilterInfo>{
private final String filter_order;
private final String application_name;
private int revision;
private Date creationDate;
private Date creationDate = new Date();

/* using AtomicBoolean so we can pass it into EndpointScriptMonitor */
private final AtomicBoolean isActive = new AtomicBoolean();
private final AtomicBoolean isCanary = new AtomicBoolean();

/**
* Constructor
* @param filter_id
* @param filter_code
* @param filter_type
* @param filter_name
* @param disablePropertyName
* @param filter_order
* @param application_name
* Constructors
*/
public FilterInfo(String filter_id, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
Expand All @@ -61,6 +65,32 @@ public FilterInfo(String filter_id, String filter_code, String filter_type, Stri
isCanary.set(false);
}

public FilterInfo(String filterCode, String filterName, ZuulFilter filter) {
this.filter_code = filterCode;
this.filter_type = filter.filterType();
this.filter_name = filterName;
this.filter_disablePropertyName = filter.disablePropertyName();
this.filter_order = "" + filter.filterOrder();
this.application_name = ZuulApplicationInfo.getApplicationName();
isActive.set(false);
isCanary.set(false);
this.filter_id = buildFilterId();
}

public FilterInfo(String filter_id, int revision, Date creationDate, boolean isActive, boolean isCanary, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
this.revision = revision;
this.creationDate = new Date(creationDate.getTime());
this.isActive.set(isActive);
this.isCanary.set(isCanary);
this.filter_code = filter_code;
this.filter_name = filter_name;
this.filter_type = filter_type;
this.filter_order = filter_order;
this.filter_disablePropertyName = disablePropertyName;
this.application_name = application_name;
}

/**
*
* @return the filter name; the class name of the filter
Expand Down Expand Up @@ -94,7 +124,6 @@ public String getFilterType() {
return filter_type;
}


@Override
public String toString() {
return "FilterInfo{" +
Expand All @@ -117,35 +146,6 @@ public String getApplication_name() {
return application_name;
}

/**
*
* @param filter_id
* @param revision
* @param creationDate
* @param isActive
* @param isCanary
* @param filter_code
* @param filter_type
* @param filter_name
* @param disablePropertyName
* @param filter_order
* @param application_name
*/
public FilterInfo(String filter_id, int revision, Date creationDate, boolean isActive, boolean isCanary, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
this.revision = revision;
this.creationDate = creationDate;
this.isActive.set(isActive);
this.isCanary.set(isCanary);
this.filter_code = filter_code;
this.filter_name = filter_name;
this.filter_type = filter_type;
this.filter_order = filter_order;
this.filter_disablePropertyName = disablePropertyName;
this.application_name = application_name;

}

/**
*
* @return the revision of this filter
Expand All @@ -159,7 +159,7 @@ public int getRevision() {
* @return creation date
*/
public Date getCreationDate() {
return creationDate;
return new Date(creationDate.getTime());
}

/**
Expand All @@ -179,7 +179,6 @@ public boolean isCanary() {
return isCanary.get();
}


/**
*
* @return unique key for the filter
Expand All @@ -204,7 +203,15 @@ public String getFilterOrder() {
* @return key is application_name:filter_name:filter_type
*/
public static String buildFilterID(String application_name, String filter_type, String filter_name) {
return application_name + ":" + filter_name + ":" + filter_type;
return new FilterId.Builder().applicationName(application_name)
.filterType(filter_type)
.filterName(filter_name)
.build()
.toString();
}

public String buildFilterId() {
return buildFilterID(application_name, filter_type, filter_name);
}

@Override
Expand Down Expand Up @@ -247,4 +254,32 @@ public int compareTo(FilterInfo filterInfo) {
return filterInfo.getFilterName().compareTo(this.getFilterName());
}

public static class UnitTest {

@Test
public void verifyFilterId() {
FilterInfo filterInfo = new FilterInfo("", "", "", "", "", "", "");
long originalCreationTime = filterInfo.getCreationDate().getTime();
filterInfo.getCreationDate().setTime(0);
assertThat(filterInfo.getCreationDate().getTime(), is(originalCreationTime));
}

@Test
public void creationDateIsCopiedInGetter() {
FilterInfo filterInfo = new FilterInfo("", "", "", "", "", "", "");
long originalCreationTime = filterInfo.getCreationDate().getTime();
filterInfo.getCreationDate().setTime(0);
assertThat(filterInfo.getCreationDate().getTime(), is(originalCreationTime));
}

@Test
public void creationDateIsCopiedInConstructor() {
Date date = new Date();
long originalCreationTime = date.getTime();
FilterInfo filterInfo =
new FilterInfo("", 1, date, false, false, "", "", "", "", "", "");
date.setTime(0);
assertThat(filterInfo.getCreationDate().getTime(), is(originalCreationTime));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,18 @@ public static FilterVerifier getInstance() {
*
* @param sFilterCode
* @return a FilterInfo object representing that code
* @throws org.codehaus.groovy.control.CompilationFailedException
* @throws CompilationFailedException
*
* @throws IllegalAccessException
* @throws InstantiationException
*/
public FilterInfo verifyFilter(String sFilterCode) throws org.codehaus.groovy.control.CompilationFailedException, IllegalAccessException, InstantiationException {
public FilterInfo verifyFilter(String sFilterCode) throws CompilationFailedException, IllegalAccessException, InstantiationException {
Class groovyClass = compileGroovy(sFilterCode);
Object instance = instanciateClass(groovyClass);
checkZuulFilterInstance(instance);
ZuulFilter filter = (ZuulFilter) instance;


String filter_id = FilterInfo.buildFilterID(ZuulApplicationInfo.getApplicationName(), filter.filterType(), groovyClass.getSimpleName());

return new FilterInfo(filter_id, sFilterCode, filter.filterType(), groovyClass.getSimpleName(), filter.disablePropertyName(), "" + filter.filterOrder(), ZuulApplicationInfo.getApplicationName());
return new FilterInfo(sFilterCode, groovyClass.getSimpleName(), filter);
}

Object instanciateClass(Class groovyClass) throws InstantiationException, IllegalAccessException {
Expand All @@ -79,10 +76,10 @@ void checkZuulFilterInstance(Object zuulFilter) throws InstantiationException {
*
* @param sFilterCode
* @return
* @throws org.codehaus.groovy.control.CompilationFailedException
* @throws CompilationFailedException
*
*/
public Class compileGroovy(String sFilterCode) throws org.codehaus.groovy.control.CompilationFailedException {
public Class compileGroovy(String sFilterCode) throws CompilationFailedException {
GroovyClassLoader loader = new GroovyClassLoader();
return loader.parseClass(sFilterCode);
}
Expand Down Expand Up @@ -277,5 +274,3 @@ public void testVerify() {
}

}


Loading

0 comments on commit 85ae0cc

Please sign in to comment.