Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding GenericDiscoveryContext to ease the effort it takes to impleme…
…nt discovery server custom payloads. Migrated MapDiscoveryContext to extend GenericDiscoveryContext. Added corresponding unit tests.
- Loading branch information
Showing
7 changed files
with
397 additions
and
66 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
86 changes: 86 additions & 0 deletions
86
...rc/main/java/com/netflix/curator/x/discovery/server/contexts/GenericDiscoveryContext.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,86 @@ | ||
package com.netflix.curator.x.discovery.server.contexts; | ||
|
||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.codehaus.jackson.JsonNode; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.codehaus.jackson.node.ObjectNode; | ||
|
||
import com.netflix.curator.x.discovery.ProviderStrategy; | ||
import com.netflix.curator.x.discovery.ServiceDiscovery; | ||
import com.netflix.curator.x.discovery.server.rest.DiscoveryContext; | ||
|
||
/** | ||
* For convenience, a version of {@link DiscoveryContext} that allows a user to | ||
* specify their own custom type as the payload. | ||
* | ||
*/ | ||
@Provider | ||
public class GenericDiscoveryContext<T> implements DiscoveryContext<T>, ContextResolver<DiscoveryContext<T>> | ||
{ | ||
private final ServiceDiscovery<T> serviceDiscovery; | ||
private final ProviderStrategy<T> providerStrategy; | ||
private final int instanceRefreshMs; | ||
|
||
/** | ||
* payloadType is wildcard Class<?> it could be Class<T> except when dealing with situations like | ||
* Map<String,String> which cannot be referenced explicitly and instead can only be referenced as Map.class. | ||
*/ | ||
private final Class<?> payloadType; | ||
|
||
public GenericDiscoveryContext(ServiceDiscovery<T> serviceDiscovery, ProviderStrategy<T> providerStrategy, int instanceRefreshMs, Class<?> payloadType) | ||
{ | ||
this.serviceDiscovery = serviceDiscovery; | ||
this.providerStrategy = providerStrategy; | ||
this.instanceRefreshMs = instanceRefreshMs; | ||
this.payloadType = payloadType; | ||
} | ||
|
||
@Override | ||
public ProviderStrategy<T> getProviderStrategy() | ||
{ | ||
return providerStrategy; | ||
} | ||
|
||
@Override | ||
public int getInstanceRefreshMs() | ||
{ | ||
return instanceRefreshMs; | ||
} | ||
|
||
@Override | ||
public ServiceDiscovery<T> getServiceDiscovery() | ||
{ | ||
return serviceDiscovery; | ||
} | ||
|
||
@SuppressWarnings("unchecked") // because payloadType is Class<?> instead of Class<T> because of generic generics like Map<String,String> | ||
@Override | ||
public void marshallJson(ObjectNode node, String fieldName, T payload) throws Exception | ||
{ | ||
if ( payload == null ) | ||
{ | ||
payload = (T) payloadType.newInstance(); | ||
} | ||
|
||
node.putPOJO(fieldName, payload); | ||
|
||
} | ||
|
||
@SuppressWarnings("unchecked") // | ||
@Override | ||
public T unMarshallJson(JsonNode node) throws Exception | ||
{ | ||
T payload; | ||
ObjectMapper mapper = new ObjectMapper(); | ||
payload = (T) mapper.readValue(node, payloadType); | ||
return payload; | ||
} | ||
|
||
@Override | ||
public DiscoveryContext<T> getContext(Class<?> type) | ||
{ | ||
return this; | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
...ver/src/test/java/com/netflix/curator/x/discovery/server/jetty_jersey/ServiceDetails.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,73 @@ | ||
package com.netflix.curator.x.discovery.server.jetty_jersey; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.codehaus.jackson.map.annotate.JsonRootName; | ||
|
||
/** | ||
* Service payload describing details of a service. | ||
*/ | ||
@JsonRootName("details") | ||
public class ServiceDetails { | ||
|
||
private Map<String, String> data; | ||
|
||
private String description; | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public ServiceDetails() { | ||
this(new HashMap<String, String>()); | ||
} | ||
|
||
public ServiceDetails(Map<String, String> data) { | ||
this.data = data; | ||
} | ||
|
||
public void setData(Map<String, String> data) { | ||
this.data = data; | ||
} | ||
|
||
public Map<String, String> getData() { | ||
return data; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((data == null) ? 0 : data.hashCode()); | ||
result = prime * result | ||
+ ((description == null) ? 0 : description.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
ServiceDetails other = (ServiceDetails) obj; | ||
if (data == null) { | ||
if (other.data != null) | ||
return false; | ||
} else if (!data.equals(other.data)) | ||
return false; | ||
if (description == null) { | ||
if (other.description != null) | ||
return false; | ||
} else if (!description.equals(other.description)) | ||
return false; | ||
return true; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...a/com/netflix/curator/x/discovery/server/jetty_jersey/ServiceDetailsDiscoveryContext.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,40 @@ | ||
/* | ||
* | ||
* Copyright 2011 Netflix, 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 com.netflix.curator.x.discovery.server.jetty_jersey; | ||
|
||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import com.netflix.curator.x.discovery.ProviderStrategy; | ||
import com.netflix.curator.x.discovery.ServiceDiscovery; | ||
import com.netflix.curator.x.discovery.server.contexts.GenericDiscoveryContext; | ||
import com.netflix.curator.x.discovery.server.rest.DiscoveryContext; | ||
|
||
/** | ||
* A DiscoveryContext that maps a concrete payload object of ServiceDetails | ||
*/ | ||
@Provider | ||
public class ServiceDetailsDiscoveryContext extends GenericDiscoveryContext<ServiceDetails> implements DiscoveryContext<ServiceDetails>, ContextResolver<DiscoveryContext<ServiceDetails>> | ||
{ | ||
public ServiceDetailsDiscoveryContext(ServiceDiscovery<ServiceDetails> serviceDiscovery, ProviderStrategy<ServiceDetails> providerStrategy, int instanceRefreshMs) | ||
{ | ||
super(serviceDiscovery, providerStrategy, instanceRefreshMs, ServiceDetails.class); | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../com/netflix/curator/x/discovery/server/jetty_jersey/ServiceDetailsDiscoveryResource.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,35 @@ | ||
/* | ||
* | ||
* Copyright 2011 Netflix, 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 com.netflix.curator.x.discovery.server.jetty_jersey; | ||
|
||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.ext.ContextResolver; | ||
|
||
import com.netflix.curator.x.discovery.server.rest.DiscoveryContext; | ||
import com.netflix.curator.x.discovery.server.rest.DiscoveryResource; | ||
|
||
@Path("/") | ||
public class ServiceDetailsDiscoveryResource extends DiscoveryResource<ServiceDetails> | ||
{ | ||
public ServiceDetailsDiscoveryResource(@Context ContextResolver<DiscoveryContext<ServiceDetails>> resolver) | ||
{ | ||
super(resolver.getContext(DiscoveryContext.class)); | ||
} | ||
} |
Oops, something went wrong.