stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ud...@apache.org
Subject [4/5] git commit: making transactionmanager singleton
Date Wed, 15 Oct 2014 08:32:15 GMT
making transactionmanager singleton


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/f5ae1b93
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/f5ae1b93
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/f5ae1b93

Branch: refs/heads/4.0.0-grouping
Commit: f5ae1b93df20ef78687a26aecd846117ae97f392
Parents: 7002c68
Author: Udara Liyanage <udara@wso2.com>
Authored: Wed Oct 15 13:47:31 2014 +0530
Committer: Udara Liyanage <udara@wso2.com>
Committed: Wed Oct 15 13:47:31 2014 +0530

----------------------------------------------------------------------
 .../client/DefaultMetaDataServiceClient.java    |  8 +++-
 .../metadata/client/rest/DefaultRestClient.java | 31 +++++--------
 .../client/rest/HTTPConnectionManager.java      | 47 ++++++++++++++++++++
 .../metadata/client/rest/RestClient.java        | 20 ++++++++-
 4 files changed, 83 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/f5ae1b93/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
index 6c43915..d509533 100644
--- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
+++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
@@ -22,6 +22,7 @@ package org.apache.stratos.metadata.client;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.metadata.client.beans.PropertyBean;
+import org.apache.stratos.metadata.client.config.MetaDataClientConfig;
 import org.apache.stratos.metadata.client.exception.MetaDataServiceClientExeption;
 import org.apache.stratos.metadata.client.exception.RestClientException;
 import org.apache.stratos.metadata.client.rest.DefaultRestClient;
@@ -39,8 +40,11 @@ public class DefaultMetaDataServiceClient implements MetaDataServiceClient
{
     private final String baseUrl;
 
     public DefaultMetaDataServiceClient(String baseUrl) throws RestClientException {
-        this.baseUrl = baseUrl;
-        this.restClient = new DefaultRestClient();
+        MetaDataClientConfig metaDataClientConfig = MetaDataClientConfig.getInstance();
+        this.baseUrl = metaDataClientConfig.getMetaDataServiceBaseUrl();
+        String username = metaDataClientConfig.getUsername();
+        String password = metaDataClientConfig.getPassword();
+        this.restClient = new DefaultRestClient(username, password);
     }
 
     public DefaultMetaDataServiceClient(String baseUrl, RestClient restClient) throws RestClientException
{

http://git-wip-us.apache.org/repos/asf/stratos/blob/f5ae1b93/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
index 29f5415..b671219 100644
--- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
+++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
@@ -29,14 +29,13 @@ import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.conn.ssl.SSLContextBuilder;
 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.apache.stratos.metadata.client.config.MetaDataClientConfig;
 import org.apache.stratos.metadata.client.exception.RestClientException;
 
@@ -48,16 +47,19 @@ import java.security.NoSuchAlgorithmException;
 
 public class DefaultRestClient implements RestClient {
 
-    private static final int MAX_TOTAL_CONNECTIONS = 100;
-    private static final int DEFAULT_MAX_PER_ROUTE = 20;
     private static final String APPLICATION_JSON = "application/json";
     private static final Log log = LogFactory.getLog(DefaultRestClient.class);
 
+    private final String username;
+    private final String password;
+
     private HttpClient httpClient;
 
-    public DefaultRestClient() throws RestClientException {
-        SSLContextBuilder builder = new SSLContextBuilder();
+    public DefaultRestClient(String username, String password) throws RestClientException
{
+        this.username  =username;
+        this.password=password;
 
+        SSLContextBuilder builder = new SSLContextBuilder();
         SSLConnectionSocketFactory sslConnectionFactory;
         try {
             builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
@@ -69,20 +71,11 @@ public class DefaultRestClient implements RestClient {
         } catch (KeyStoreException e) {
             throw new RestClientException(e);
         }
-        CloseableHttpClient closableHttpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionFactory).setConnectionManager(getHttpConnectionManager()).build();
+        CloseableHttpClient closableHttpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionFactory)
+                .setConnectionManager(HTTPConnectionManager.getInstance().getHttpConnectionManager()).build();
         this.httpClient = closableHttpClient;
     }
 
-    private HttpClientConnectionManager getHttpConnectionManager() {
-        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
-        // TODO: Introduce configurable variable for Max total and max per router variables.
-        cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
-        cm.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
-        //HttpHost localhost = new HttpHost("locahost", 80);
-        //cm.setMaxPerRoute(new HttpRoute(localhost), 50);
-        return cm;
-    }
-
     public HttpResponse doPost(String resourcePath, Object payload) throws RestClientException
{
 
         HttpPost post = new HttpPost(resourcePath);
@@ -100,9 +93,7 @@ public class DefaultRestClient implements RestClient {
         }
     }
 
-    private void setAuthHeader(HttpPost post) {
-        String username = getUsername();
-        String password = getPassword();
+    private void setAuthHeader(HttpRequestBase post) {
         String identity = username + ":" + password;
         String encoding = new String(Base64.encodeBase64(identity.getBytes()));
         post.setHeader("Authorization", "Basic " + encoding);

http://git-wip-us.apache.org/repos/asf/stratos/blob/f5ae1b93/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/HTTPConnectionManager.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/HTTPConnectionManager.java
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/HTTPConnectionManager.java
new file mode 100644
index 0000000..aadfa49
--- /dev/null
+++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/HTTPConnectionManager.java
@@ -0,0 +1,47 @@
+/*
+ * 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.stratos.metadata.client.rest;
+
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+
+public class HTTPConnectionManager {
+
+    private static final int MAX_TOTAL_CONNECTIONS = 100;
+    private static final int DEFAULT_MAX_PER_ROUTE = 20;
+
+    private static class InstanceHolder {
+        public static HTTPConnectionManager instance = new HTTPConnectionManager();
+    }
+
+    public static HTTPConnectionManager getInstance() {
+        return InstanceHolder.instance;
+    }
+
+    public HttpClientConnectionManager getHttpConnectionManager(){
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
+        // TODO: Introduce configurable variable for Max total and max per router variables.
+        cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
+        cm.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
+        //HttpHost localhost = new HttpHost("locahost", 80);
+        //cm.setMaxPerRoute(new HttpRoute(localhost), 50);
+        return cm;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/f5ae1b93/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/RestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/RestClient.java
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/RestClient.java
index 4c8fb6e..3189218 100644
--- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/RestClient.java
+++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/RestClient.java
@@ -23,10 +23,28 @@ import org.apache.http.HttpResponse;
 import org.apache.stratos.metadata.client.exception.RestClientException;
 
 public interface RestClient {
-
+    /**
+     * Perform a HTTP POST request to @resourcePath with request body containing @payload
converted to JSON.
+     * @param resourcePath Resource path to where is request is sent.
+     * @param payload request message body.
+     * @return Response of the request.
+     * @throws RestClientException
+     */
     public HttpResponse doPost(String resourcePath, Object payload) throws RestClientException;
 
+    /**
+     * Perform a HTTP GET request to @resourcePath.
+     * @param resourcePath
+     * @return Response of the request.
+     * @throws RestClientException
+     */
     public HttpResponse doGet(String resourcePath) throws RestClientException;
 
+    /**
+     * Perform a HTTP DELETE request to @resourcePath.
+     * @param resourcePath
+     * @return Response of the request.
+     * @throws RestClientException
+     */
     public HttpResponse doDelete(String resourcePath) throws RestClientException;
 }


Mime
View raw message