You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2018/05/30 10:04:55 UTC
[camel] 03/20: CAMEL-12485: camel cloud : create camel-service
component
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit e2834c97c22547e21e04f5ebf10e7be9683a7fc7
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu May 10 22:31:49 2018 +0200
CAMEL-12485: camel cloud : create camel-service component
---
.../apache/camel/cloud/DiscoverableService.java | 6 +-
.../org/apache/camel/cloud/ServiceDefinition.java | 2 +
.../camel/impl/cloud/DefaultServiceDefinition.java | 61 ++++++++++-
.../impl/cloud/ServiceRegistrationRoutePolicy.java | 8 +-
.../cloud/ConsulServiceRegistrationTestBase.java | 15 ++-
...viceRegistrationWithRoutePolicyFactoryTest.java | 2 +-
...nsulServiceRegistrationWithRoutePolicyTest.java | 2 +-
...erviceRegistrationWithServiceComponentTest.java | 13 ++-
.../camel/http/common/HttpCommonEndpoint.java | 17 ++-
.../camel/component/service/ServiceComponent.java | 37 +++----
.../camel/component/service/ServiceEndpoint.java | 32 +++---
.../camel/component/service/ServiceParameters.java | 114 ---------------------
12 files changed, 131 insertions(+), 178 deletions(-)
diff --git a/camel-core/src/main/java/org/apache/camel/cloud/DiscoverableService.java b/camel-core/src/main/java/org/apache/camel/cloud/DiscoverableService.java
index 90710ad..6d1946c 100644
--- a/camel-core/src/main/java/org/apache/camel/cloud/DiscoverableService.java
+++ b/camel-core/src/main/java/org/apache/camel/cloud/DiscoverableService.java
@@ -16,9 +16,11 @@
*/
package org.apache.camel.cloud;
+import java.util.Map;
+
public interface DiscoverableService {
/**
- * Get the service definition.
+ * Get the service properties.
*/
- ServiceDefinition getServiceDefinition();
+ Map<String, Object> getServiceProperties();
}
diff --git a/camel-core/src/main/java/org/apache/camel/cloud/ServiceDefinition.java b/camel-core/src/main/java/org/apache/camel/cloud/ServiceDefinition.java
index 3007e8c..8a0a3f6 100644
--- a/camel-core/src/main/java/org/apache/camel/cloud/ServiceDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/cloud/ServiceDefinition.java
@@ -33,7 +33,9 @@ public interface ServiceDefinition {
// default service meta-data keys
String SERVICE_META_ID = "service.id";
String SERVICE_META_NAME = "service.name";
+ String SERVICE_META_HOST = "service.host";
String SERVICE_META_PORT = "service.port";
+ String SERVICE_META_ZONE = "service.zone";
String SERVICE_META_PROTOCOL= "service.protocol";
String SERVICE_META_PATH = "service.path";
diff --git a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceDefinition.java b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceDefinition.java
index ba4708c..8deace6 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceDefinition.java
@@ -175,7 +175,7 @@ public class DefaultServiceDefinition implements ServiceDefinition {
private String id;
private String name;
private String host;
- private int port;
+ private Integer port;
private Map<String, String> meta;
private ServiceHealth health;
@@ -190,6 +190,51 @@ public class DefaultServiceDefinition implements ServiceDefinition {
return this;
}
+ public Builder from(Map<String, Object> properties) {
+ Map<String, Object> options = new HashMap<>(properties);
+ Object val = null;
+
+ val = options.remove(ServiceDefinition.SERVICE_META_ID);
+ if (val != null && val instanceof String) {
+ withId((String)val);
+ }
+
+ val = options.remove(ServiceDefinition.SERVICE_META_NAME);
+ if (val != null && val instanceof String) {
+ withName((String)val);
+ }
+
+ val = options.remove(ServiceDefinition.SERVICE_META_HOST);
+ if (val != null && val instanceof String) {
+ withHost((String)val);
+ }
+
+ val = options.remove(ServiceDefinition.SERVICE_META_PORT);
+ if (val != null && val instanceof String) {
+ withPort((String)val);
+ }
+ if (val != null && val instanceof Integer) {
+ withPort((Integer)val);
+ }
+
+ val = options.remove(ServiceDefinition.SERVICE_META_HOST);
+ if (val != null && val instanceof String) {
+ withHost((String)val);
+ }
+
+ for (Map.Entry<String, Object> entry : options.entrySet()) {
+ if (!entry.getKey().startsWith(ServiceDefinition.SERVICE_META_PREFIX)) {
+ continue;
+ }
+
+ if (entry.getValue() instanceof String) {
+ addMeta(entry.getKey(), (String)entry.getValue());
+ }
+ }
+
+ return this;
+ }
+
public Builder withId(String id) {
this.id = id;
return this;
@@ -217,12 +262,20 @@ public class DefaultServiceDefinition implements ServiceDefinition {
return host;
}
- public Builder withPort(int port) {
+ public Builder withPort(Integer port) {
this.port = port;
return this;
}
- public int port() {
+ public Builder withPort(String port) {
+ if (port != null) {
+ withPort(Integer.parseInt(port));
+ }
+
+ return this;
+ }
+
+ public Integer port() {
return port;
}
@@ -266,7 +319,7 @@ public class DefaultServiceDefinition implements ServiceDefinition {
}
public ServiceDefinition build() {
- return new DefaultServiceDefinition(id, name, host, port, meta, health);
+ return new DefaultServiceDefinition(id, name, host, port != null ? port : -1, meta, health);
}
}
}
\ No newline at end of file
diff --git a/camel-core/src/main/java/org/apache/camel/impl/cloud/ServiceRegistrationRoutePolicy.java b/camel-core/src/main/java/org/apache/camel/impl/cloud/ServiceRegistrationRoutePolicy.java
index ce05a03..c432fa7 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/cloud/ServiceRegistrationRoutePolicy.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/cloud/ServiceRegistrationRoutePolicy.java
@@ -125,7 +125,7 @@ public class ServiceRegistrationRoutePolicy extends RoutePolicySupport implement
if (endpoint instanceof DiscoverableService) {
final DiscoverableService service = (DiscoverableService)endpoint;
- final ServiceDefinition definition = service.getServiceDefinition();
+ final Map<String, Object> properties = service.getServiceProperties();
// try to get the service id from route properties
String serviceId = (String)route.getProperties().get(ServiceDefinition.SERVICE_META_ID);
@@ -137,7 +137,7 @@ public class ServiceRegistrationRoutePolicy extends RoutePolicySupport implement
}
if (serviceId == null) {
// finally get the id from the DiscoverableService
- serviceId = definition.getId();
+ serviceId = (String)properties.get(ServiceDefinition.SERVICE_META_ID);
}
// try to get the service name from route properties
@@ -148,7 +148,7 @@ public class ServiceRegistrationRoutePolicy extends RoutePolicySupport implement
}
if (serviceName == null) {
// finally get the name from the DiscoverableService
- serviceName = definition.getName();
+ serviceName = (String)properties.get(ServiceDefinition.SERVICE_META_NAME);
}
ObjectHelper.notNull(serviceId, "Service ID");
@@ -157,7 +157,7 @@ public class ServiceRegistrationRoutePolicy extends RoutePolicySupport implement
// Build the final resource definition from bits collected from the
// endpoint and the route.
DefaultServiceDefinition.Builder builder = DefaultServiceDefinition.builder()
- .from(definition)
+ .from(properties)
.withId(serviceId)
.withName(serviceName);
diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java
index a6ef5c9..c1a97e4 100644
--- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java
+++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java
@@ -16,7 +16,9 @@
*/
package org.apache.camel.component.consul.cloud;
+import java.util.Collections;
import java.util.List;
+import java.util.Map;
import java.util.UUID;
import com.orbitz.consul.CatalogClient;
@@ -35,6 +37,10 @@ public abstract class ConsulServiceRegistrationTestBase extends ConsulTestSuppor
protected final static String SERVICE_HOST = "localhost";
protected final static int SERVICE_PORT = SocketUtils.findAvailableTcpPort();
+ protected Map<String, String> getMetadata() {
+ return Collections.emptyMap();
+ }
+
@Override
protected CamelContext createCamelContext() throws Exception {
final CamelContext context = super.createCamelContext();
@@ -68,8 +74,13 @@ public abstract class ConsulServiceRegistrationTestBase extends ConsulTestSuppor
assertEquals(SERVICE_PORT, services.get(0).getServicePort());
assertEquals("localhost", services.get(0).getServiceAddress());
assertTrue(services.get(0).getServiceTags().contains(ServiceDefinition.SERVICE_META_PROTOCOL + "=http"));
- assertTrue(services.get(0).getServiceTags().contains(ServiceDefinition.SERVICE_META_PATH + "=/service/endpoint/"));
- assertTrue(services.get(0).getServiceTags().contains(ServiceDefinition.SERVICE_META_PORT + "=" + SERVICE_PORT));
+ assertTrue(services.get(0).getServiceTags().contains(ServiceDefinition.SERVICE_META_PATH + "=/service/endpoint"));
+
+ getMetadata().forEach(
+ (k, v) -> {
+ assertTrue(services.get(0).getServiceTags().contains(k + "=" + v));
+ }
+ );
List<ServiceHealth> checks = health.getHealthyServiceInstances(SERVICE_NAME).getResponse();
assertEquals(1, checks.size());
diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyFactoryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyFactoryTest.java
index cb2c51a..65981fc 100644
--- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyFactoryTest.java
+++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyFactoryTest.java
@@ -35,7 +35,7 @@ public class ConsulServiceRegistrationWithRoutePolicyFactoryTest extends ConsulS
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- fromF("jetty:http://0.0.0.0:%d/service/endpoint/", SERVICE_PORT)
+ fromF("jetty:http://0.0.0.0:%d/service/endpoint", SERVICE_PORT)
.routeId(SERVICE_ID)
.routeGroup(SERVICE_NAME)
.noAutoStartup()
diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyTest.java
index d79c306..6e18de2 100644
--- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyTest.java
+++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithRoutePolicyTest.java
@@ -26,7 +26,7 @@ public class ConsulServiceRegistrationWithRoutePolicyTest extends ConsulServiceR
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- fromF("jetty:http://0.0.0.0:%d/service/endpoint/", SERVICE_PORT)
+ fromF("jetty:http://0.0.0.0:%d/service/endpoint", SERVICE_PORT)
.routeId(SERVICE_ID)
.routeGroup(SERVICE_NAME)
.routePolicy(new ServiceRegistrationRoutePolicy())
diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithServiceComponentTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithServiceComponentTest.java
index 6738c12..d12dce3 100644
--- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithServiceComponentTest.java
+++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationWithServiceComponentTest.java
@@ -16,12 +16,23 @@
*/
package org.apache.camel.component.consul.cloud;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.service.ServiceComponent;
import org.apache.camel.impl.JndiRegistry;
public class ConsulServiceRegistrationWithServiceComponentTest extends ConsulServiceRegistrationTestBase {
+
+ protected Map<String, String> getMetadata() {
+ return new HashMap<String, String>() {{
+ put("service.type", "consul");
+ put("service.zone", "US");
+ }};
+ }
+
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
@@ -35,7 +46,7 @@ public class ConsulServiceRegistrationWithServiceComponentTest extends ConsulSer
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- fromF("service:%s:jetty:http://0.0.0.0:%d/service/endpoint/", SERVICE_NAME, SERVICE_PORT)
+ fromF("service:%s:jetty:http://0.0.0.0:%d/service/endpoint?service.type=consul&service.zone=US", SERVICE_NAME, SERVICE_PORT)
.routeId(SERVICE_ID)
.routeGroup(SERVICE_NAME)
.noAutoStartup()
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
index 759dc50..9e9e196 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
@@ -18,12 +18,13 @@ package org.apache.camel.http.common;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.camel.cloud.DiscoverableService;
import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.http.common.cookie.CookieHandler;
import org.apache.camel.impl.DefaultEndpoint;
-import org.apache.camel.impl.cloud.DefaultServiceDefinition;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
import org.apache.camel.spi.Metadata;
@@ -200,14 +201,12 @@ public abstract class HttpCommonEndpoint extends DefaultEndpoint implements Head
//-------------------------------------------------------------------------
@Override
- public ServiceDefinition getServiceDefinition() {
- // Returns a partial
- return DefaultServiceDefinition.builder()
- .withPort(getPort())
- .addMeta(ServiceDefinition.SERVICE_META_PORT, Integer.toString(getPort()))
- .addMeta(ServiceDefinition.SERVICE_META_PATH, getPath())
- .addMeta(ServiceDefinition.SERVICE_META_PROTOCOL, getProtocol())
- .build();
+ public Map<String, Object> getServiceProperties() {
+ return new HashMap<String, Object>() {{
+ put(ServiceDefinition.SERVICE_META_PORT, getPort());
+ put(ServiceDefinition.SERVICE_META_PATH, getPath());
+ put(ServiceDefinition.SERVICE_META_PROTOCOL, getProtocol());
+ }};
}
// Properties
diff --git a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceComponent.java b/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceComponent.java
index 0f8d748..b7e249b 100644
--- a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceComponent.java
+++ b/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceComponent.java
@@ -17,16 +17,17 @@
package org.apache.camel.component.service;
+import java.util.HashMap;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
+import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.cloud.ServiceRegistry;
import org.apache.camel.impl.DefaultComponent;
import org.apache.camel.impl.cloud.ServiceRegistryHelper;
import org.apache.camel.impl.cloud.ServiceRegistrySelectors;
import org.apache.camel.spi.Metadata;
-import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.URISupport;
@@ -56,7 +57,7 @@ public class ServiceComponent extends DefaultComponent {
ObjectHelper.notNull(delegateUri, "Delegate URI");
// add service name to the parameters
- parameters.put("serviceName", serviceName);
+ parameters.put(ServiceDefinition.SERVICE_META_NAME, serviceName);
// Lookup the service registry, this may be a static selected service
// or dynamically selected one through a ServiceRegistry.Selector
@@ -64,7 +65,18 @@ public class ServiceComponent extends DefaultComponent {
// Compute service definition from parameters, this is used as default
// definition
- final ServiceParameters params = computeServiceParameters(parameters);
+ final Map<String, Object> params = new HashMap<>();
+ parameters.forEach(
+ (k, v) -> {
+ if (k.startsWith(ServiceDefinition.SERVICE_META_PREFIX)) {
+ params.put(k, v);
+ }
+ }
+ );
+
+ // remove all the service related options so the underlying component
+ // does not fail because of unknown parameters
+ parameters.keySet().removeAll(parameters.keySet());
return new ServiceEndpoint(
uri,
@@ -111,23 +123,4 @@ public class ServiceComponent extends DefaultComponent {
return service;
}
-
- @SuppressWarnings("unchecked")
- private ServiceParameters computeServiceParameters(Map<String, Object> parameters) {
- // Extract service definition related parameter from uri
- final String serviceId = getAndRemoveParameter(parameters, "serviceId", String.class);
- final String serviceName = getAndRemoveParameter(parameters, "serviceName", String.class);
- final String serviceHost = getAndRemoveParameter(parameters, "serviceHost", String.class);
- final String servicePort = getAndRemoveParameter(parameters, "servicePort", String.class);
- final Map<String, Object> serviceMeta = IntrospectionSupport.extractProperties(parameters, "serviceMeta.", true);
-
- ServiceParameters params = new ServiceParameters();
- ObjectHelper.ifNotEmpty(serviceId, params::setId);
- ObjectHelper.ifNotEmpty(serviceName, params::setName);
- ObjectHelper.ifNotEmpty(serviceHost, params::setHost);
- ObjectHelper.ifNotEmpty(servicePort, params::setPort);
- ObjectHelper.ifNotEmpty(serviceMeta, meta -> params.setMeta(Map.class.cast(meta)));
-
- return params;
- }
}
diff --git a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceEndpoint.java b/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceEndpoint.java
index b99c490..f4bcd55 100644
--- a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceEndpoint.java
+++ b/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceEndpoint.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.component.service;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.camel.CamelContext;
import org.apache.camel.Consumer;
import org.apache.camel.DelegateEndpoint;
@@ -29,6 +32,7 @@ import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.cloud.ServiceRegistry;
import org.apache.camel.cluster.CamelClusterView;
import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.impl.cloud.DefaultServiceDefinition;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriPath;
@@ -50,14 +54,14 @@ import org.apache.camel.spi.UriPath;
public class ServiceEndpoint extends DefaultEndpoint implements DelegateEndpoint {
private final Endpoint delegateEndpoint;
private final ServiceRegistry serviceRegistry;
- private final ServiceParameters serviceParameters;
+ private final Map<String, Object> serviceParameters;
private final ServiceDefinition serviceDefinition;
@UriPath(description = "The endpoint uri to expose as service")
@Metadata(required = "true")
private final String delegateUri;
- public ServiceEndpoint(String uri, ServiceComponent component, ServiceRegistry serviceRegistry, ServiceParameters serviceParameters, String delegateUri) {
+ public ServiceEndpoint(String uri, ServiceComponent component, ServiceRegistry serviceRegistry, Map<String, Object> serviceParameters, String delegateUri) {
super(uri, component);
this.serviceRegistry = serviceRegistry;
@@ -65,20 +69,9 @@ public class ServiceEndpoint extends DefaultEndpoint implements DelegateEndpoint
this.delegateUri = delegateUri;
this.delegateEndpoint = getCamelContext().getEndpoint(delegateUri);
- // The service definition built from uri parameter may override parameter
- // of service definition generated by an endpoint that implement DiscoverableService
+ // The service properties set on uri override parameter provided by a
+ // an endpoint of type DiscoverableService.
this.serviceDefinition = computeServiceDefinition(component.getCamelContext(), delegateEndpoint);
-
- // Perform some basic validation, service id an name and port are mandatory,
- if (serviceDefinition.getId() == null) {
- throw new IllegalArgumentException("Service ID si required");
- }
- if (serviceDefinition.getName() == null) {
- throw new IllegalArgumentException("Service Name si required");
- }
- if (serviceDefinition.getPort() <= 0) {
- throw new IllegalArgumentException("Service Port should be greater than 0");
- }
}
@ManagedAttribute(description = "The consumer endpoint to expose as a service", mask = true)
@@ -108,12 +101,15 @@ public class ServiceEndpoint extends DefaultEndpoint implements DelegateEndpoint
}
private ServiceDefinition computeServiceDefinition(CamelContext context, Endpoint delegateEndpoint) {
- ServiceDefinition definition = null;
+ Map<String, Object> parameters = new HashMap<>();
if (delegateEndpoint instanceof DiscoverableService) {
- definition = ((DiscoverableService)delegateEndpoint).getServiceDefinition();
+ parameters.putAll(((DiscoverableService)delegateEndpoint).getServiceProperties());
}
- return serviceParameters.enrich(context, definition);
+ parameters.putAll(serviceParameters);
+ parameters.computeIfAbsent(ServiceDefinition.SERVICE_META_ID, k -> context.getUuidGenerator().generateUuid());
+
+ return DefaultServiceDefinition.builder().from(parameters).build();
}
}
diff --git a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceParameters.java b/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceParameters.java
deleted file mode 100644
index 2bbaca4..0000000
--- a/components/camel-service/src/main/java/org/apache/camel/component/service/ServiceParameters.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.apache.camel.component.service;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.cloud.ServiceDefinition;
-import org.apache.camel.impl.cloud.DefaultServiceDefinition;
-import org.apache.camel.util.ObjectHelper;
-
-public class ServiceParameters {
- private String id;
- private String name;
- private String host;
- private int port;
- private Map<String, String> meta;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getHost() {
- return host;
- }
-
- public void setHost(String host) {
- this.host = host;
- }
-
- public int getPort() {
- return port;
- }
-
- public void setPort(int port) {
- this.port = port;
- }
-
- public void setPort(String port) {
- setPort(Integer.parseInt(port));
- }
-
- public Map<String, String> getMeta() {
- return meta;
- }
-
- public void setMeta(Map<String, String> meta) {
- this.meta = meta;
- }
-
- public void addMeta(String key, String value) {
- if (this.meta == null) {
- this.meta = new HashMap<>();
- }
-
- this.meta.put(key, value);
- }
-
- public void addAllMeta(Map<String, String> meta) {
- if (this.meta == null) {
- this.meta = new HashMap<>();
- }
-
- this.meta.putAll(meta);
- }
-
- public ServiceDefinition enrich(CamelContext context, ServiceDefinition definition) {
- final DefaultServiceDefinition.Builder builder = DefaultServiceDefinition.builder();
-
- ObjectHelper.ifNotEmpty(definition, builder::from);
- ObjectHelper.ifNotEmpty(id, builder::withId);
- ObjectHelper.ifNotEmpty(name, builder::withName);
- ObjectHelper.ifNotEmpty(meta, builder::addAllMeta);
- ObjectHelper.ifNotEmpty(host, builder::withHost);
-
- if (port > 0) {
- builder.withPort(port);
- }
-
- // if the service does not have an id, we can auto-generate it
- if (builder.id() == null) {
- builder.withId(context.getUuidGenerator().generateUuid());
- }
-
- return builder.build();
- }
-}
--
To stop receiving notification emails like this one, please contact
lburgazzoli@apache.org.