CAMEL-9156: Add JMX api for the swagger api
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e26541ac Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e26541ac Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e26541ac Branch: refs/heads/master Commit: e26541acb06d70563551341ba105b390d4084059 Parents: cac4ae2 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Sep 24 10:59:39 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Sep 24 10:59:39 2015 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestApiEndpoint.java | 2 + .../camel/component/rest/RestEndpoint.java | 2 + .../management/mbean/ManagedRestRegistry.java | 73 +++++++++++--------- .../swagger/SwaggerRestApiProcessorFactory.java | 4 +- 4 files changed, 47 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/e26541ac/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java index 7829a7c..f2ee48d 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java @@ -21,6 +21,7 @@ import java.util.Set; import org.apache.camel.Component; import org.apache.camel.Consumer; +import org.apache.camel.ExchangePattern; import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.NoSuchBeanException; import org.apache.camel.Processor; @@ -57,6 +58,7 @@ public class RestApiEndpoint extends DefaultEndpoint { public RestApiEndpoint(String endpointUri, RestApiComponent component) { super(endpointUri, component); + setExchangePattern(ExchangePattern.InOut); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/e26541ac/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 a226d70..f17110d 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 @@ -21,6 +21,7 @@ import java.util.Set; import org.apache.camel.Component; import org.apache.camel.Consumer; +import org.apache.camel.ExchangePattern; import org.apache.camel.NoSuchBeanException; import org.apache.camel.Processor; import org.apache.camel.Producer; @@ -62,6 +63,7 @@ public class RestEndpoint extends DefaultEndpoint { public RestEndpoint(String endpointUri, RestComponent component) { super(endpointUri, component); + setExchangePattern(ExchangePattern.InOut); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/e26541ac/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 index eca3508..cbcbcd6 100644 --- 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 @@ -33,6 +33,7 @@ import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; import org.apache.camel.api.management.mbean.ManagedRestRegistryMBean; import org.apache.camel.component.rest.RestApiEndpoint; import org.apache.camel.component.rest.RestEndpoint; +import org.apache.camel.impl.ProducerCache; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestRegistry; import org.apache.camel.util.ObjectHelper; @@ -45,6 +46,7 @@ import org.apache.camel.util.ServiceHelper; public class ManagedRestRegistry extends ManagedService implements ManagedRestRegistryMBean { private final RestRegistry registry; + private transient Producer apiProducer; public ManagedRestRegistry(CamelContext context, RestRegistry registry) { super(context, registry); @@ -94,48 +96,53 @@ public class ManagedRestRegistry extends ManagedService implements ManagedRestRe @Override public String apiDocAsJson() { // see if there is a rest-api endpoint which would be the case if rest api-doc has been explicit enabled - Endpoint restApiEndpoint = null; - Endpoint restEndpoint = null; - for (Map.Entry<String, Endpoint> entry : getContext().getEndpointMap().entrySet()) { - String uri = entry.getKey(); - if (uri.startsWith("rest-api:")) { - restApiEndpoint = entry.getValue(); - break; - } else if (restEndpoint == null && uri.startsWith("rest:")) { - restEndpoint = entry.getValue(); + if (apiProducer == null) { + Endpoint restApiEndpoint = null; + Endpoint restEndpoint = null; + for (Map.Entry<String, Endpoint> entry : getContext().getEndpointMap().entrySet()) { + String uri = entry.getKey(); + if (uri.startsWith("rest-api:")) { + restApiEndpoint = entry.getValue(); + break; + } else if (restEndpoint == null && uri.startsWith("rest:")) { + restEndpoint = entry.getValue(); + } } - } - if (restApiEndpoint == null && restEndpoint != null) { - // no rest-api has been explicit enabled, then we need to create it first - RestEndpoint rest = (RestEndpoint) restEndpoint; - String componentName = rest.getComponentName(); + if (restApiEndpoint == null && restEndpoint != null) { + // no rest-api has been explicit enabled, then we need to create it first + RestEndpoint rest = (RestEndpoint) restEndpoint; + String componentName = rest.getComponentName(); - if (componentName != null) { - RestConfiguration config = getContext().getRestConfiguration(componentName, true); - String apiComponent = config.getApiComponent() != null ? config.getApiComponent() : RestApiEndpoint.DEFAULT_API_COMPONENT_NAME; - String path = config.getApiContextPath() != null ? config.getApiContextPath() : "api-doc"; - restApiEndpoint = getContext().getEndpoint(String.format("rest-api:%s/%s?componentName=%s&apiComponentName=%s&contextIdPattern=#name#", path, getCamelId(), componentName, apiComponent)); + if (componentName != null) { + RestConfiguration config = getContext().getRestConfiguration(componentName, true); + String apiComponent = config.getApiComponent() != null ? config.getApiComponent() : RestApiEndpoint.DEFAULT_API_COMPONENT_NAME; + String path = config.getApiContextPath() != null ? config.getApiContextPath() : "api-doc"; + restApiEndpoint = getContext().getEndpoint(String.format("rest-api:%s/%s?componentName=%s&apiComponentName=%s&contextIdPattern=#name#", path, getCamelId(), componentName, apiComponent)); + } } - } - try { if (restApiEndpoint != null) { - Producer producer = restApiEndpoint.createProducer(); - ServiceHelper.startService(producer); - + // reuse the producer to avoid creating it try { - Exchange dummy = restApiEndpoint.createExchange(); - producer.process(dummy); - - String json = dummy.hasOut() ? dummy.getOut().getBody(String.class) : dummy.getIn().getBody(String.class); - return json; - } finally { - ServiceHelper.stopService(producer); + apiProducer = restApiEndpoint.createProducer(); + getContext().addService(apiProducer, true); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); } } - } catch (Exception e) { - throw ObjectHelper.wrapRuntimeCamelException(e); + } + + if (apiProducer != null) { + try { + Exchange dummy = apiProducer.getEndpoint().createExchange(); + apiProducer.process(dummy); + + String json = dummy.hasOut() ? dummy.getOut().getBody(String.class) : dummy.getIn().getBody(String.class); + return json; + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } } return null; http://git-wip-us.apache.org/repos/asf/camel/blob/e26541ac/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java index 45eff06..373db45 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java @@ -31,7 +31,9 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory { RestConfiguration configuration, Map<String, Object> parameters) throws Exception { Map<String, Object> options = new HashMap<String, Object>(parameters); - options.putAll(configuration.getApiProperties()); + if (configuration.getApiProperties() != null) { + options.putAll(configuration.getApiProperties()); + } // need to include host in options String host = (String) options.get("host");