forked from airlift/airlift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dynamic properties in HTTP announcement
Extend discovery binder's bindHttpAnnouncement's capabilities by allowing bindings of non-constant properties that require Guice injection to be calculated.
- Loading branch information
Showing
6 changed files
with
297 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
discovery/src/main/java/io/airlift/discovery/client/HttpAnnouncement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.airlift.discovery.client; | ||
|
||
import com.google.inject.BindingAnnotation; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Retention(RUNTIME) | ||
@Target({FIELD, PARAMETER, METHOD}) | ||
@BindingAnnotation | ||
@interface HttpAnnouncement | ||
{ | ||
String announcementId(); | ||
} |
65 changes: 65 additions & 0 deletions
65
discovery/src/main/java/io/airlift/discovery/client/HttpAnnouncementImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2010 Proofpoint, Inc. | ||
* | ||
* 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.airlift.discovery.client; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
class HttpAnnouncementImpl | ||
implements HttpAnnouncement | ||
{ | ||
private final String announcementId; | ||
|
||
public HttpAnnouncementImpl(String announcementId) | ||
{ | ||
this.announcementId = requireNonNull(announcementId, "announcementId is null"); | ||
} | ||
|
||
public String announcementId() | ||
{ | ||
return announcementId; | ||
} | ||
|
||
public String toString() | ||
{ | ||
return format("@%s(announcementId=\"%s\")", annotationType().getName(), announcementId.replace("\"", "\\\"")); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (!(o instanceof HttpAnnouncement that)) { | ||
return false; | ||
} | ||
return announcementId.equals(that.announcementId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
// see Annotation.hashCode() | ||
int result = 0; | ||
result += ((127 * "announcementId".hashCode()) ^ announcementId.hashCode()); | ||
return result; | ||
} | ||
|
||
public Class<? extends Annotation> annotationType() | ||
{ | ||
return HttpAnnouncement.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
discovery/src/test/java/io/airlift/discovery/client/TestHttpAnnouncementImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2010 Proofpoint, Inc. | ||
* | ||
* 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.airlift.discovery.client; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static io.airlift.testing.EquivalenceTester.equivalenceTester; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestHttpAnnouncementImpl | ||
{ | ||
@HttpAnnouncement(announcementId = "apple") | ||
private final HttpAnnouncement appleHttpAnnouncement; | ||
|
||
@HttpAnnouncement(announcementId = "banana") | ||
private final HttpAnnouncement bananaHttpAnnouncement; | ||
|
||
@HttpAnnouncement(announcementId = "quot\"ation-and-\\backslash") | ||
private final HttpAnnouncement httpAnnouncementWithCharacters; | ||
|
||
public TestHttpAnnouncementImpl() | ||
{ | ||
try { | ||
this.appleHttpAnnouncement = getClass().getDeclaredField("appleHttpAnnouncement").getAnnotation(HttpAnnouncement.class); | ||
this.bananaHttpAnnouncement = getClass().getDeclaredField("bananaHttpAnnouncement").getAnnotation(HttpAnnouncement.class); | ||
this.httpAnnouncementWithCharacters = getClass().getDeclaredField("httpAnnouncementWithCharacters").getAnnotation(HttpAnnouncement.class); | ||
} | ||
catch (NoSuchFieldException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAnnouncementId() | ||
{ | ||
assertEquals(new HttpAnnouncementImpl("type A").announcementId(), "type A"); | ||
} | ||
|
||
@Test | ||
public void testAnnotationType() | ||
{ | ||
assertEquals(new HttpAnnouncementImpl("apple").annotationType(), HttpAnnouncement.class); | ||
assertEquals(new HttpAnnouncementImpl("apple").annotationType(), appleHttpAnnouncement.annotationType()); | ||
} | ||
|
||
@Test | ||
public void testEquivalence() | ||
{ | ||
equivalenceTester() | ||
.addEquivalentGroup(appleHttpAnnouncement, new HttpAnnouncementImpl("apple")) | ||
.addEquivalentGroup(bananaHttpAnnouncement, new HttpAnnouncementImpl("banana")) | ||
.check(); | ||
} | ||
} |