diff --git a/java/monasca-common-middleware/src/main/java/monasca/common/middleware/TokenCache.java b/java/monasca-common-middleware/src/main/java/monasca/common/middleware/TokenCache.java index 865ca0a1..e03ec1ec 100644 --- a/java/monasca-common-middleware/src/main/java/monasca/common/middleware/TokenCache.java +++ b/java/monasca-common-middleware/src/main/java/monasca/common/middleware/TokenCache.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. - * + * (C) Copyright 2014,2016 Hewlett Packard Enterprise Development LP + * * 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 @@ -42,12 +42,19 @@ public class TokenCache { String value = null; AuthClient client = null; + boolean discarded = false; try { client = factory.getClient(); value = client.validateTokenForServiceEndpointV3(key); + } catch (ClientProtocolException e) { + factory.discard(client); + logger.warn("Validate token failed with ClientProtocolException. Discarding AuthClient connection from pool to create a new connection."); + logger.warn("ClientProtocolException " + e.getMessage() + " " + e); + discarded = true; + throw e; } finally { - if (client != null) + if (client != null && !discarded) factory.recycle(client); } return value;