Repository: camel Updated Branches: refs/heads/master b188ac9c7 -> bd898d8ce
CAMEL-7773 Created proxy wrapper class to optimize reading of shared OData Edm Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bd898d8c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bd898d8c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bd898d8c Branch: refs/heads/master Commit: bd898d8ce6aa77e0632f6ca60eef6f45551fc9ba Parents: b188ac9 Author: Dhiraj Bokde <dhira...@yahoo.com> Authored: Tue Sep 2 13:44:23 2014 -0700 Committer: Dhiraj Bokde <dhira...@yahoo.com> Committed: Tue Sep 2 13:44:23 2014 -0700 ---------------------------------------------------------------------- .../component/olingo2/Olingo2AppWrapper.java | 108 +++++++++++++++++++ .../component/olingo2/Olingo2Component.java | 16 ++- .../component/olingo2/Olingo2Endpoint.java | 74 +------------ 3 files changed, 118 insertions(+), 80 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bd898d8c/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2AppWrapper.java ---------------------------------------------------------------------- diff --git a/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2AppWrapper.java b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2AppWrapper.java new file mode 100644 index 0000000..63f7f97 --- /dev/null +++ b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2AppWrapper.java @@ -0,0 +1,108 @@ +/** + * 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.olingo2; + +import java.util.concurrent.CountDownLatch; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.olingo2.api.Olingo2App; +import org.apache.camel.component.olingo2.api.Olingo2ResponseHandler; +import org.apache.camel.util.ObjectHelper; +import org.apache.olingo.odata2.api.edm.Edm; + +/** + * Holder class for {@link org.apache.camel.component.olingo2.api.Olingo2App} + * and its lazily read {@link org.apache.olingo.odata2.api.edm.Edm}. + */ +public class Olingo2AppWrapper { + + private final Olingo2App olingo2App; + private volatile Edm edm; + + public Olingo2AppWrapper(Olingo2App olingo2App) { + ObjectHelper.notNull(olingo2App, "olingo2App"); + this.olingo2App = olingo2App; + } + + public Olingo2App getOlingo2App() { + return olingo2App; + } + + public void close() { + olingo2App.close(); + } + + // double checked locking based singleton Edm reader + public Edm getEdm() throws RuntimeCamelException { + Edm localEdm = edm; + if (localEdm == null) { + + synchronized (this) { + + localEdm = edm; + if (localEdm == null) { + + final CountDownLatch latch = new CountDownLatch(1); + final Exception[] error = new Exception[1]; + olingo2App.read(null, "$metadata", null, new Olingo2ResponseHandler<Edm>() { + + @Override + public void onResponse(Edm response) { + edm = response; + latch.countDown(); + } + + @Override + public void onException(Exception ex) { + error[0] = ex; + latch.countDown(); + } + + @Override + public void onCanceled() { + error[0] = new RuntimeCamelException("OData HTTP request cancelled!"); + latch.countDown(); + } + }); + + try { + // wait until response or timeout + latch.await(); + + final Exception ex = error[0]; + if (ex != null) { + if (ex instanceof RuntimeCamelException) { + throw (RuntimeCamelException) ex; + } else { + final String message = ex.getMessage() != null + ? ex.getMessage() : ex.getClass().getName(); + throw new RuntimeCamelException("Error reading EDM: " + message, ex); + } + } + + } catch (InterruptedException e) { + throw new RuntimeCamelException(e.getMessage(), e); + } + + localEdm = edm; + } + } + } + + return localEdm; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/bd898d8c/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Component.java ---------------------------------------------------------------------- diff --git a/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Component.java b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Component.java index 771a3ec..27e2d91 100644 --- a/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Component.java +++ b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Component.java @@ -17,12 +17,10 @@ package org.apache.camel.component.olingo2; import java.util.Map; - import javax.net.ssl.SSLContext; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; -import org.apache.camel.component.olingo2.api.Olingo2App; import org.apache.camel.component.olingo2.api.impl.Olingo2AppImpl; import org.apache.camel.component.olingo2.internal.Olingo2ApiCollection; import org.apache.camel.component.olingo2.internal.Olingo2ApiName; @@ -39,7 +37,7 @@ import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; public class Olingo2Component extends AbstractApiComponent<Olingo2ApiName, Olingo2Configuration, Olingo2ApiCollection> { // component level shared proxy - private Olingo2App apiProxy; + private Olingo2AppWrapper apiProxy; public Olingo2Component() { super(Olingo2Endpoint.class, Olingo2ApiName.class, Olingo2ApiCollection.getCollection()); @@ -90,8 +88,8 @@ public class Olingo2Component extends AbstractApiComponent<Olingo2ApiName, Oling return new Olingo2Endpoint(uri, this, apiName, methodName, endpointConfiguration); } - public Olingo2App createApiProxy(Olingo2Configuration endpointConfiguration) { - final Olingo2App result; + public Olingo2AppWrapper createApiProxy(Olingo2Configuration endpointConfiguration) { + final Olingo2AppWrapper result; if (endpointConfiguration.equals(this.configuration)) { synchronized (this) { if (apiProxy == null) { @@ -105,7 +103,7 @@ public class Olingo2Component extends AbstractApiComponent<Olingo2ApiName, Oling return result; } - private Olingo2App createOlingo2App(Olingo2Configuration configuration) { + private Olingo2AppWrapper createOlingo2App(Olingo2Configuration configuration) { HttpAsyncClientBuilder clientBuilder = configuration.getHttpAsyncClientBuilder(); if (clientBuilder == null) { @@ -130,13 +128,13 @@ public class Olingo2Component extends AbstractApiComponent<Olingo2ApiName, Oling } } - apiProxy = new Olingo2AppImpl(configuration.getServiceUri(), clientBuilder); - apiProxy.setContentType(configuration.getContentType()); + apiProxy = new Olingo2AppWrapper(new Olingo2AppImpl(configuration.getServiceUri(), clientBuilder)); + apiProxy.getOlingo2App().setContentType(configuration.getContentType()); return apiProxy; } - public void closeApiProxy(Olingo2App apiProxy) { + public void closeApiProxy(Olingo2AppWrapper apiProxy) { if (this.apiProxy != apiProxy) { // not a shared proxy apiProxy.close(); http://git-wip-us.apache.org/repos/asf/camel/blob/bd898d8c/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Endpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Endpoint.java b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Endpoint.java index d2d52d4..f77f764 100644 --- a/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Endpoint.java +++ b/components/camel-olingo2/camel-olingo2-component/src/main/java/org/apache/camel/component/olingo2/Olingo2Endpoint.java @@ -21,14 +21,10 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; -import java.util.concurrent.CountDownLatch; import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.component.olingo2.api.Olingo2App; -import org.apache.camel.component.olingo2.api.Olingo2ResponseHandler; import org.apache.camel.component.olingo2.internal.Olingo2ApiCollection; import org.apache.camel.component.olingo2.internal.Olingo2ApiName; import org.apache.camel.component.olingo2.internal.Olingo2Constants; @@ -37,7 +33,6 @@ import org.apache.camel.spi.UriEndpoint; import org.apache.camel.util.component.AbstractApiEndpoint; import org.apache.camel.util.component.ApiMethod; import org.apache.camel.util.component.ApiMethodPropertiesHelper; -import org.apache.olingo.odata2.api.edm.Edm; /** * Represents a Olingo2 endpoint. @@ -58,9 +53,7 @@ public class Olingo2Endpoint extends AbstractApiEndpoint<Olingo2ApiName, Olingo2 private final Set<String> endpointPropertyNames; - private Olingo2App apiProxy; - - private volatile Edm edm; + private Olingo2AppWrapper apiProxy; public Olingo2Endpoint(String uri, Olingo2Component component, Olingo2ApiName apiName, String methodName, Olingo2Configuration endpointConfiguration) { @@ -120,7 +113,7 @@ public class Olingo2Endpoint extends AbstractApiEndpoint<Olingo2ApiName, Olingo2 @Override public synchronized Object getApiProxy(ApiMethod method, Map<String, Object> args) { - return apiProxy; + return apiProxy.getOlingo2App(); } @Override @@ -158,7 +151,7 @@ public class Olingo2Endpoint extends AbstractApiEndpoint<Olingo2ApiName, Olingo2 public void interceptProperties(Map<String, Object> properties) { // read Edm if not set yet - properties.put(EDM_PROPERTY, readEdm()); + properties.put(EDM_PROPERTY, apiProxy.getEdm()); // handle keyPredicate final String keyPredicate = (String) properties.get(KEY_PREDICATE_PROPERTY); @@ -219,65 +212,4 @@ public class Olingo2Endpoint extends AbstractApiEndpoint<Olingo2ApiName, Olingo2 } } - - /// double checked locking based singleton Edm reader - private Edm readEdm() { - - Edm localEdm = edm; - if (localEdm == null) { - - synchronized (this) { - - localEdm = edm; - if (localEdm == null) { - - final CountDownLatch latch = new CountDownLatch(1); - final Exception[] error = new Exception[1]; - apiProxy.read(null, "$metadata", null, new Olingo2ResponseHandler<Edm>() { - - @Override - public void onResponse(Edm response) { - edm = response; - latch.countDown(); - } - - @Override - public void onException(Exception ex) { - error[0] = ex; - latch.countDown(); - } - - @Override - public void onCanceled() { - error[0] = new RuntimeCamelException("OData HTTP request cancelled!"); - latch.countDown(); - } - }); - - try { - // wait until response or timeout - latch.await(); - - final Exception ex = error[0]; - if (ex != null) { - if (ex instanceof RuntimeCamelException) { - throw (RuntimeCamelException) ex; - } else { - final String message = ex.getMessage() != null - ? ex.getMessage() : ex.getClass().getName(); - throw new RuntimeCamelException("Error reading EDM: " + message, ex); - } - } - - } catch (InterruptedException e) { - throw new RuntimeCamelException(e.getMessage(), e); - } - - localEdm = edm; - } - } - } - - return localEdm; - } }