CAMEL-7620: Rest DSL. Enlist rest services in RestRegistry and JMX.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/93ae72f6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/93ae72f6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/93ae72f6 Branch: refs/heads/master Commit: 93ae72f608f67cd95508c15942f69eddfb5487cf Parents: a22461a Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jul 30 16:16:07 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jul 30 16:16:07 2014 +0200 ---------------------------------------------------------------------- .../java/org/apache/camel/CamelContext.java | 13 ++- .../management/mbean/CamelOpenMBeanTypes.java | 12 +++ .../mbean/ManagedRestRegistryMBean.java | 32 ++++++ .../camel/component/rest/RestEndpoint.java | 4 + .../apache/camel/impl/DefaultCamelContext.java | 11 ++ .../apache/camel/impl/DefaultRestRegistry.java | 106 +++++++++++++++++++ .../DefaultManagementLifecycleStrategy.java | 4 + .../management/mbean/ManagedRestRegistry.java | 76 +++++++++++++ .../java/org/apache/camel/spi/RestRegistry.java | 54 ++++++++++ 9 files changed, 311 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/CamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java index 25e1239..5297031 100644 --- a/camel-core/src/main/java/org/apache/camel/CamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java @@ -52,6 +52,7 @@ import org.apache.camel.spi.PackageScanClassResolver; import org.apache.camel.spi.ProcessorFactory; import org.apache.camel.spi.Registry; import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestRegistry; import org.apache.camel.spi.RouteStartupOrder; import org.apache.camel.spi.RuntimeEndpointRegistry; import org.apache.camel.spi.ServicePool; @@ -1357,4 +1358,14 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration { */ void setRuntimeEndpointRegistry(RuntimeEndpointRegistry runtimeEndpointRegistry); -} + /** + * Gets the {@link org.apache.camel.spi.RestRegistry} to use + */ + public RestRegistry getRestRegistry(); + + /** + * Sets a custom {@link org.apache.camel.spi.RestRegistry} to use. + */ + public void setRestRegistry(RestRegistry restRegistry); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java index 6cbdb30..d5736bc 100644 --- a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java +++ b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java @@ -39,4 +39,16 @@ public final class CamelOpenMBeanTypes { return new CompositeType("types", "From/To types", new String[]{"from", "to"}, new String[]{"From type", "To type"}, new OpenType[]{SimpleType.STRING, SimpleType.STRING}); } + + public static TabularType listRestServicesTabularType() throws OpenDataException { + CompositeType ct = listRestServicesCompositeType(); + return new TabularType("listRestServices", "Lists all the rest services in the registry", ct, new String[]{"url", "path", "verb", "consumes", "produces"}); + } + + public static CompositeType listRestServicesCompositeType() throws OpenDataException { + return new CompositeType("types", "types", new String[]{"url", "path", "verb", "consumes", "produces"}, + new String[]{"Url", "Path", "Verb", "Consumes", "Produces"}, new OpenType[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}); + } + + } http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRestRegistryMBean.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRestRegistryMBean.java b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRestRegistryMBean.java new file mode 100644 index 0000000..804d294 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRestRegistryMBean.java @@ -0,0 +1,32 @@ +/** + * 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.api.management.mbean; + +import javax.management.openmbean.TabularData; + +import org.apache.camel.api.management.ManagedAttribute; +import org.apache.camel.api.management.ManagedOperation; + +public interface ManagedRestRegistryMBean extends ManagedServiceMBean { + + @ManagedAttribute(description = "Number of rest services in the registry") + int getNumberOfRestServices(); + + @ManagedOperation(description = "Lists all the Rest services in the registry (url, path, verb, consumes, produces)") + TabularData listRestServices(); + +} http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java index 1d9195d..10d366c 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java @@ -153,6 +153,10 @@ public class RestEndpoint extends DefaultEndpoint { if (factory != null) { Consumer consumer = factory.createConsumer(getCamelContext(), processor, getVerb(), getPath(), getConsumes(), getProduces(), getParameters()); configureConsumer(consumer); + + // add to rest registry so we can keep track of them, we will remove from the registry when the consumer is removed + getCamelContext().getRestRegistry().addRestService(consumer, null, getPath(), getVerb(), getConsumes(), getProduces()); + return consumer; } else { throw new IllegalStateException("Cannot find RestConsumerFactory in Registry or as a Component to use"); http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 15987fc..fa4d673 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -116,6 +116,7 @@ import org.apache.camel.spi.PackageScanClassResolver; import org.apache.camel.spi.ProcessorFactory; import org.apache.camel.spi.Registry; import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestRegistry; import org.apache.camel.spi.RouteContext; import org.apache.camel.spi.RouteStartupOrder; import org.apache.camel.spi.RuntimeEndpointRegistry; @@ -175,6 +176,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon private final List<RouteDefinition> routeDefinitions = new ArrayList<RouteDefinition>(); private final List<RestDefinition> restDefinitions = new ArrayList<RestDefinition>(); private RestConfiguration restConfiguration = new RestConfiguration(); + private RestRegistry restRegistry = new DefaultRestRegistry(); private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>(); // special flags to control the first startup which can are special @@ -1834,6 +1836,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon addService(inflightRepository); addService(shutdownStrategy); addService(packageScanClassResolver); + addService(restRegistry); if (runtimeEndpointRegistry != null) { if (runtimeEndpointRegistry instanceof EventNotifier) { @@ -2888,6 +2891,14 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon this.streamCachingStrategy = streamCachingStrategy; } + public RestRegistry getRestRegistry() { + return restRegistry; + } + + public void setRestRegistry(RestRegistry restRegistry) { + this.restRegistry = restRegistry; + } + @Override public String getProperty(String name) { String value = getProperties().get(name); http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java new file mode 100644 index 0000000..612e9ed --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java @@ -0,0 +1,106 @@ +/** + * 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.impl; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.Consumer; +import org.apache.camel.StaticService; +import org.apache.camel.spi.RestRegistry; + +public class DefaultRestRegistry extends ServiceSupport implements StaticService, RestRegistry { + + private final Map<Consumer, RestService> registry = new LinkedHashMap<Consumer, RestService>(); + + public void addRestService(Consumer consumer, String url, String path, String verb, String consumes, String produces) { + RestServiceEntry entry = new RestServiceEntry(consumer, url, path, verb, consumes, produces); + registry.put(consumer, entry); + } + + public void removeRestService(Consumer consumer) { + registry.remove(consumer); + } + + @Override + public List<RestRegistry.RestService> listAllRestServices() { + return new ArrayList<RestService>(registry.values()); + } + + @Override + public int size() { + return registry.size(); + } + + @Override + protected void doStart() throws Exception { + // noop + } + + @Override + protected void doStop() throws Exception { + registry.clear(); + } + + /** + * Represents a rest service + */ + private final class RestServiceEntry implements RestService { + + private final Consumer consumer; + private final String url; + private final String path; + private final String verb; + private final String consumes; + private final String produces; + + private RestServiceEntry(Consumer consumer, String url, String path, String verb, String consumes, String produces) { + this.consumer = consumer; + this.url = url; + this.path = path; + this.verb = verb; + this.consumes = consumes; + this.produces = produces; + } + + public Consumer getConsumer() { + return consumer; + } + + public String getUrl() { + return url; + } + + public String getPath() { + return path; + } + + public String getVerb() { + return verb; + } + + public String getConsumes() { + return consumes; + } + + public String getProduces() { + return produces; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java b/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java index 223a987..7eb1452 100644 --- a/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java +++ b/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java @@ -58,6 +58,7 @@ import org.apache.camel.management.mbean.ManagedConsumerCache; import org.apache.camel.management.mbean.ManagedEndpoint; import org.apache.camel.management.mbean.ManagedEndpointRegistry; import org.apache.camel.management.mbean.ManagedProducerCache; +import org.apache.camel.management.mbean.ManagedRestRegistry; import org.apache.camel.management.mbean.ManagedRoute; import org.apache.camel.management.mbean.ManagedRuntimeEndpointRegistry; import org.apache.camel.management.mbean.ManagedService; @@ -84,6 +85,7 @@ import org.apache.camel.spi.ManagementAware; import org.apache.camel.spi.ManagementNameStrategy; import org.apache.camel.spi.ManagementObjectStrategy; import org.apache.camel.spi.ManagementStrategy; +import org.apache.camel.spi.RestRegistry; import org.apache.camel.spi.RouteContext; import org.apache.camel.spi.RuntimeEndpointRegistry; import org.apache.camel.spi.StreamCachingStrategy; @@ -462,6 +464,8 @@ public class DefaultManagementLifecycleStrategy extends ServiceSupport implement answer = new ManagedEndpointRegistry(context, (EndpointRegistry) service); } else if (service instanceof TypeConverterRegistry) { answer = new ManagedTypeConverterRegistry(context, (TypeConverterRegistry) service); + } else if (service instanceof RestRegistry) { + answer = new ManagedRestRegistry(context, (RestRegistry) service); } else if (service instanceof RuntimeEndpointRegistry) { answer = new ManagedRuntimeEndpointRegistry(context, (RuntimeEndpointRegistry) service); } else if (service instanceof StreamCachingStrategy) { http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java new file mode 100644 index 0000000..99bf3f5 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java @@ -0,0 +1,76 @@ +/** + * 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.management.mbean; + +import java.util.List; +import javax.management.openmbean.CompositeData; +import javax.management.openmbean.CompositeDataSupport; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.TabularData; +import javax.management.openmbean.TabularDataSupport; + +import org.apache.camel.CamelContext; +import org.apache.camel.api.management.ManagedResource; +import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; +import org.apache.camel.api.management.mbean.ManagedRestRegistryMBean; +import org.apache.camel.spi.RestRegistry; +import org.apache.camel.util.ObjectHelper; + +/** + * + */ +@ManagedResource(description = "Managed RestRegistry") +public class ManagedRestRegistry extends ManagedService implements ManagedRestRegistryMBean { + + private final RestRegistry registry; + + public ManagedRestRegistry(CamelContext context, RestRegistry registry) { + super(context, registry); + this.registry = registry; + } + + public RestRegistry getRegistry() { + return registry; + } + + @Override + public int getNumberOfRestServices() { + return registry.size(); + } + + @Override + public TabularData listRestServices() { + try { + TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listRestServicesTabularType()); + List<RestRegistry.RestService> services = registry.listAllRestServices(); + for (RestRegistry.RestService entry : services) { + CompositeType ct = CamelOpenMBeanTypes.listRestServicesCompositeType(); + String url = entry.getUrl(); + String path = entry.getPath(); + String verb = entry.getVerb(); + String consumes = entry.getConsumes(); + String produces = entry.getProduces(); + CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "path", "verb", "consumes", "produces"}, + new Object[]{url, path, verb, consumes, produces}); + answer.put(data); + } + return answer; + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/93ae72f6/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java b/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java new file mode 100644 index 0000000..3509b6d --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java @@ -0,0 +1,54 @@ +/** + * 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.spi; + +import java.util.List; + +import org.apache.camel.Consumer; +import org.apache.camel.Service; + +public interface RestRegistry extends Service { + + interface RestService { + + Consumer getConsumer(); + + String getUrl(); + + String getPath(); + + String getVerb(); + + String getConsumes(); + + String getProduces(); + } + + void addRestService(Consumer consumer, String url, String path, String verb, String consumes, String produces); + + void removeRestService(Consumer consumer); + + List<RestService> listAllRestServices(); + + /** + * Number of rest services in the registry. + * + * @return number of rest services in the registry. + */ + int size(); + +}