refactor metadata client
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/7002c68d Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/7002c68d Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/7002c68d Branch: refs/heads/4.0.0-grouping Commit: 7002c68d52c078d4c384616080a8a2c7c1fbd4d5 Parents: 9075e87 Author: Udara Liyanage <[email protected]> Authored: Wed Oct 15 08:25:48 2014 +0530 Committer: Udara Liyanage <[email protected]> Committed: Wed Oct 15 08:25:48 2014 +0530 ---------------------------------------------------------------------- .../org.apache.stratos.metadata.client/pom.xml | 51 ++++++----- .../client/DefaultMetaDataServiceClient.java | 35 ++------ .../metadata/client/MetaDataServiceClient.java | 25 +++--- .../metadata/client/beans/PropertyBean.java | 23 ++--- .../client/config/MetaDataClientConfig.java | 74 +++++----------- .../data/extractor/MetaDataExtractor.java | 36 -------- .../exception/MetaDataExtractorException.java | 47 ---------- .../factory/MetaDataExtractorFactory.java | 51 ----------- .../metadata/client/pojo/DataContext.java | 88 ------------------- .../metadata/client/rest/DefaultRestClient.java | 40 +++++---- .../metadata/client/rest/RestClient.java | 6 +- .../client/sample/DefaultMetaDataExtractor.java | 45 ---------- .../sample/MetaDataServiceClientSample.java | 92 -------------------- .../client/util/MetaDataClientConstants.java | 2 - 14 files changed, 107 insertions(+), 508 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/pom.xml ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/pom.xml b/components/org.apache.stratos.metadata.client/pom.xml index fcafdaa..4f92ca7 100644 --- a/components/org.apache.stratos.metadata.client/pom.xml +++ b/components/org.apache.stratos.metadata.client/pom.xml @@ -1,5 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.apache.stratos</groupId> @@ -7,32 +7,31 @@ <version>4.1.0-SNAPSHOT</version> </parent> - <groupId>org.apache.stratos</groupId> - <artifactId>org.apache.stratos.metadata.client</artifactId> - <packaging>bundle</packaging> - <modelVersion>4.0.0</modelVersion> + <artifactId>org.apache.stratos.metadata.client</artifactId> + <packaging>bundle</packaging> + <modelVersion>4.0.0</modelVersion> - <name>Apache Stratos - Metadata Client</name> + <name>Apache Stratos - Metadata Client</name> - <dependencies> - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - <version>4.3.5</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>commons-configuration</groupId> - <artifactId>commons-configuration</artifactId> - <version>1.9</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.google.code.gson</groupId> - <artifactId>gson</artifactId> - <version>2.2.4</version> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.3.5</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>commons-configuration</groupId> + <artifactId>commons-configuration</artifactId> + <version>1.9</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.2.4</version> + </dependency> + </dependencies> <build> <plugins> http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/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 d60dfb2..6c43915 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 @@ -21,7 +21,6 @@ package org.apache.stratos.metadata.client; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.HttpResponse; import org.apache.stratos.metadata.client.beans.PropertyBean; import org.apache.stratos.metadata.client.exception.MetaDataServiceClientExeption; import org.apache.stratos.metadata.client.exception.RestClientException; @@ -34,14 +33,19 @@ import java.util.Set; public class DefaultMetaDataServiceClient implements MetaDataServiceClient { - private static Log log = LogFactory.getLog(DefaultMetaDataServiceClient.class); + private static final Log log = LogFactory.getLog(DefaultMetaDataServiceClient.class); private RestClient restClient; - private String baseUrl; + private final String baseUrl; - public DefaultMetaDataServiceClient (String baseUrl) throws RestClientException { + public DefaultMetaDataServiceClient(String baseUrl) throws RestClientException { this.baseUrl = baseUrl; - restClient = new DefaultRestClient(); + this.restClient = new DefaultRestClient(); + } + + public DefaultMetaDataServiceClient(String baseUrl, RestClient restClient) throws RestClientException { + this.baseUrl = baseUrl; + this.restClient = restClient; } public void initialize() { @@ -59,32 +63,11 @@ public class DefaultMetaDataServiceClient implements MetaDataServiceClient { } } - public void addPropertyToApplication(String appId, String propertyKey, String propertyValue) - throws MetaDataServiceClientExeption { - String applicationPath = baseUrl.concat("application/").concat(appId).concat("/property"); - PropertyBean property = new PropertyBean(propertyKey, propertyValue); - HttpResponse x; - try { - x = restClient.doPost(applicationPath, property); - } catch (RestClientException e) { - log.error(String.format("Error occurred while adding property %s", propertyKey)); - } - } - -// public void addPropertyToCluster(String appId, String propertyKey, String propertyValue) -// throws MetaDataServiceClientExeption { -// //To change body of implemented methods use File | Settings | File Templates. -// } - public Map<String, Set<String>> getProperties(String appId, String clusterId) throws MetaDataServiceClientExeption { return null; //To change body of implemented methods use File | Settings | File Templates. } -// public Map<String, Set<String>> getProperties(String appId) throws MetaDataServiceClientExeption { -// return null; //To change body of implemented methods use File | Settings | File Templates. -// } - public Set<String> getProperty(String appId, String propertyKey) throws MetaDataServiceClientExeption { return null; //To change body of implemented methods use File | Settings | File Templates. } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java index fa89f96..01ea3e3 100644 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java +++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java @@ -28,22 +28,23 @@ public interface MetaDataServiceClient { /** * Initialize the MetaDataServiceClient. Should be called once before using the MetaDataServiceClient. + * * @throws MetaDataServiceClientExeption */ - public void initialize (); + public void initialize(); /** * Adds a property key value pair for the relevant cluster of the specified app * - * @param appId Application id - * @param clusterId Cluster id - * @param propertyKey Key of the Property + * @param appId Application id + * @param clusterId Cluster id + * @param propertyKey Key of the Property * @param propertyValue Value of the Property * @throws MetaDataServiceClientExeption */ public void addPropertyToCluster(String appId, String clusterId, String propertyKey, String propertyValue) throws MetaDataServiceClientExeption; -// /** + // /** // * Adds a property key value pair for the specified app // * // * @param appId Application id @@ -61,9 +62,9 @@ public interface MetaDataServiceClient { // * @return Map of Keys and Values for the specified cluster in the relevant app. Each key can have multiple Values. // * @throws MetaDataServiceClientExeption // */ - public Map<String, Set<String>> getProperties (String appId, String clusterId) throws MetaDataServiceClientExeption; + public Map<String, Set<String>> getProperties(String appId, String clusterId) throws MetaDataServiceClientExeption; -// /** + // /** // * Retrieves the property key value pairs of the specified app // * // * @param appId Application id @@ -80,23 +81,23 @@ public interface MetaDataServiceClient { // * @return Set of Values for specified Key of the relevant app. // * @throws MetaDataServiceClientExeption // */ - public Set<String> getProperty (String appId, String propertyKey) throws MetaDataServiceClientExeption; + public Set<String> getProperty(String appId, String propertyKey) throws MetaDataServiceClientExeption; /** * Retrieves the property values for the specified key of the relevant cluster and app * - * @param appId Application id - * @param clusterId Cluster id + * @param appId Application id + * @param clusterId Cluster id * @param propertyKey Key of the Property * @return Set of Values for specified Key of the relevant cluster in the relevant app. * @throws MetaDataServiceClientExeption */ - public Set<String> getProperty (String appId, String clusterId, String propertyKey) throws MetaDataServiceClientExeption; + public Set<String> getProperty(String appId, String clusterId, String propertyKey) throws MetaDataServiceClientExeption; /** * Shutdown the MetaDataServiceClient. Should be called once after using the client. * * @throws MetaDataServiceClientExeption */ - public void terminate () throws MetaDataServiceClientExeption; + public void terminate() throws MetaDataServiceClientExeption; } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java index dd2d777..7fae6a6 100644 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java +++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java @@ -24,14 +24,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -@XmlRootElement(name="properties") +@XmlRootElement(name = "properties") public class PropertyBean { private String key; private List<String> values = new ArrayList<String>(); - public PropertyBean(){} - public PropertyBean(String key, String value){ - this.key=key; + public PropertyBean() { + } + + public PropertyBean(String key, String value) { + this.key = key; this.values.add(value); } @@ -43,22 +45,21 @@ public class PropertyBean { this.key = key; } - public String[] getValues(){ + public String[] getValues() { String[] values = new String[this.values.size()]; values = this.values.toArray(values); return values; } - public void setValues(String[] values) { - this.values.addAll(Arrays.asList(values)); - } - - public void setValues(String value) { this.values.add(value); } - public void addValue(String value){ + public void setValues(String[] values) { + this.values.addAll(Arrays.asList(values)); + } + + public void addValue(String value) { this.values.add(value); } } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/config/MetaDataClientConfig.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/config/MetaDataClientConfig.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/config/MetaDataClientConfig.java index 2596ca9..4324e5d 100644 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/config/MetaDataClientConfig.java +++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/config/MetaDataClientConfig.java @@ -21,35 +21,40 @@ package org.apache.stratos.metadata.client.config; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.stratos.metadata.client.sample.MetaDataServiceClientSample; import org.apache.stratos.metadata.client.util.MetaDataClientConstants; - import java.io.File; public class MetaDataClientConfig { - private static final Log log = LogFactory.getLog(MetaDataServiceClientSample.class); - + private static final Log log = LogFactory.getLog(MetaDataClientConfig.class); + private static volatile MetaDataClientConfig metaDataClientConfig; private String metaDataServiceBaseUrl; - - private String dataExtractorClass; - private String username; - private String password; - private XMLConfiguration config; - private static volatile MetaDataClientConfig metaDataClientConfig; - - private MetaDataClientConfig () { + private MetaDataClientConfig() { readConfig(); } - private void readConfig () throws RuntimeException{ + public static MetaDataClientConfig getInstance() { + + if (metaDataClientConfig == null) { + synchronized (MetaDataClientConfig.class) { + if (metaDataClientConfig == null) { + metaDataClientConfig = new MetaDataClientConfig(); + } + } + } + + return metaDataClientConfig; + } + + private void readConfig() throws RuntimeException { // the config file path is found from a system property String configFilePath = System.getProperty(MetaDataClientConstants.METADATA_CLIENT_CONFIG_FILE); @@ -62,7 +67,7 @@ public class MetaDataClientConfig { metaDataServiceBaseUrl = config.getString(MetaDataClientConstants.METADATA_SERVICE_BASE_URL); if (metaDataServiceBaseUrl == null) { throw new RuntimeException("Unable to find metadata service base URL [ " + - MetaDataClientConstants.METADATA_SERVICE_BASE_URL + " ] in the config file"); + MetaDataClientConstants.METADATA_SERVICE_BASE_URL + " ] in the config file"); } username = config.getString(MetaDataClientConstants.METADATA_SERVICE_USERNAME); @@ -74,56 +79,25 @@ public class MetaDataClientConfig { if (password == null) { throw new RuntimeException("Meta data service password not defined in the configuration"); } - - dataExtractorClass = config.getString(MetaDataClientConstants.METADATA_CLIENT_DATA_EXTRACTOR_CLASS); - if (dataExtractorClass == null) { - if(log.isDebugEnabled()) { - log.debug("No custom Data Extractor class detected in the configuration"); - } - } } - private void loadConfig (String configFilePath) { + private void loadConfig(String configFilePath) { + if(StringUtils.isEmpty(configFilePath)){ + throw new IllegalArgumentException("Confguration file path can not be null or empaty"); + } try { - - File confFile; - if (configFilePath != null && !configFilePath.isEmpty()) { - confFile = new File(configFilePath); - - } else { - confFile = new File(configFilePath); - } - - config = new XMLConfiguration(confFile); - + config = new XMLConfiguration(new File(configFilePath)); } catch (ConfigurationException e) { String errorMsg = "Unable to load configuration file at " + configFilePath; throw new RuntimeException(errorMsg); } } - public static MetaDataClientConfig getInstance () { - - if (metaDataClientConfig == null) { - synchronized (MetaDataClientConfig.class) { - if (metaDataClientConfig == null) { - metaDataClientConfig = new MetaDataClientConfig(); - } - } - } - - return metaDataClientConfig; - } - public String getMetaDataServiceBaseUrl() { return metaDataServiceBaseUrl; } - public String getDataExtractorClass() { - return dataExtractorClass; - } - public String getUsername() { return username; } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/data/extractor/MetaDataExtractor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/data/extractor/MetaDataExtractor.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/data/extractor/MetaDataExtractor.java deleted file mode 100644 index 13bc725..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/data/extractor/MetaDataExtractor.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.data.extractor; - -import org.apache.stratos.metadata.client.exception.MetaDataExtractorException; -import org.apache.stratos.metadata.client.pojo.DataContext; - -import java.util.Collection; - -public interface MetaDataExtractor { - - public void initialize (); - - public Collection<DataContext> getAllData (Object object) throws MetaDataExtractorException; - - public DataContext getData (Object object) throws MetaDataExtractorException; - - public void terminate () throws MetaDataExtractorException; -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/exception/MetaDataExtractorException.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/exception/MetaDataExtractorException.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/exception/MetaDataExtractorException.java deleted file mode 100644 index c1273f5..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/exception/MetaDataExtractorException.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.exception; - -public class MetaDataExtractorException extends Exception { - - private String message; - - public MetaDataExtractorException() { - super(); - } - - public MetaDataExtractorException(String message, Throwable cause) { - super(message, cause); - this.message = message; - } - - public MetaDataExtractorException(String message) { - super(message); - this.message = message; - } - - public MetaDataExtractorException(Throwable cause) { - super(cause); - } - - public String getMessage() { - return message; - } -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/factory/MetaDataExtractorFactory.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/factory/MetaDataExtractorFactory.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/factory/MetaDataExtractorFactory.java deleted file mode 100644 index 90d39d1..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/factory/MetaDataExtractorFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.factory; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.metadata.client.data.extractor.MetaDataExtractor; - -public class MetaDataExtractorFactory { - - private static final Log log = LogFactory.getLog(MetaDataExtractorFactory.class); - - public static MetaDataExtractor getMetaDataServiceClient (String clazzName) throws RuntimeException { - - try { - return (MetaDataExtractor) MetaDataExtractor.class.getClassLoader().loadClass(clazzName).newInstance(); - - } catch (InstantiationException e) { - String errorMsg = "Unable to instatiate class " + clazzName; - log.error(errorMsg, e); - throw new RuntimeException(errorMsg, e); - - } catch (IllegalAccessException e) { - String errorMsg = "Unable to instatiate class " + clazzName; - log.error(errorMsg, e); - throw new RuntimeException(errorMsg, e); - - } catch (ClassNotFoundException e) { - String errorMsg = "Unable to instatiate class " + clazzName; - log.error(errorMsg, e); - throw new RuntimeException(errorMsg, e); - } - } -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/pojo/DataContext.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/pojo/DataContext.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/pojo/DataContext.java deleted file mode 100644 index f5194c8..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/pojo/DataContext.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.pojo; - -import java.util.Set; - -public class DataContext { - - private String appId; - - private String clusterId; - - private String propertyKey; - - private Set<String> propertyValues; - - private String propertyValue; - - - public DataContext (String appId, String propertyKey, Set<String> propertyValues) { - - this.appId = appId; - this.propertyKey = propertyKey; - this.propertyValues = propertyValues; - - } - - public DataContext (String appId, String clusterId, String propertyKey, Set<String> propertyValues) { - - this.appId = appId; - this.clusterId = clusterId; - this.propertyKey = propertyKey; - this.propertyValues = propertyValues; - } - - public DataContext (String appId, String propertyKey, String propertyValue) { - - this.appId = appId; - this.propertyKey = propertyKey; - this.propertyValue = propertyValue; - } - - public DataContext (String appId, String clusterId, String propertyKey, String propertyValue) { - - this.appId = appId; - this.clusterId = clusterId; - this.propertyKey = propertyKey; - this.propertyValue = propertyValue; - } - - public String getAppId() { - return appId; - } - - public String getClusterId() { - return clusterId; - } - - public String getPropertyKey() { - return propertyKey; - } - - public Set<String> getPropertyValues() { - return propertyValues; - } - - public String getPropertyValue() { - return propertyValue; - } - -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/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 a6f0fb4..29f5415 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 @@ -48,34 +48,32 @@ 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"; - public static final int MAX_TOTAL_CONNECTIONS = 100; - public static final int DEFAULT_MAX_PER_ROUTE = 20; - - private static Log log = LogFactory.getLog(DefaultRestClient.class); + private static final Log log = LogFactory.getLog(DefaultRestClient.class); private HttpClient httpClient; - public DefaultRestClient() throws RestClientException{ + public DefaultRestClient() throws RestClientException { SSLContextBuilder builder = new SSLContextBuilder(); - SSLConnectionSocketFactory sslsf = null; + SSLConnectionSocketFactory sslConnectionFactory; try { builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); - sslsf = new SSLConnectionSocketFactory( - builder.build()); + sslConnectionFactory = new SSLConnectionSocketFactory(builder.build()); } catch (NoSuchAlgorithmException e) { - throw new RestClientException(e); + throw new RestClientException(e); } catch (KeyManagementException e) { - throw new RestClientException(e); + throw new RestClientException(e); } catch (KeyStoreException e) { - throw new RestClientException(e); + throw new RestClientException(e); } - CloseableHttpClient closableHttpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(getHttpConnectionManager()).build(); + CloseableHttpClient closableHttpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionFactory).setConnectionManager(getHttpConnectionManager()).build(); this.httpClient = closableHttpClient; } - private HttpClientConnectionManager getHttpConnectionManager(){ + private HttpClientConnectionManager getHttpConnectionManager() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // TODO: Introduce configurable variable for Max total and max per router variables. cm.setMaxTotal(MAX_TOTAL_CONNECTIONS); @@ -97,7 +95,7 @@ public class DefaultRestClient implements RestClient { String errorMsg = "Error while executing POST statement"; log.error(errorMsg, e); throw new RestClientException(errorMsg, e); - }finally { + } finally { post.releaseConnection(); } } @@ -105,7 +103,7 @@ public class DefaultRestClient implements RestClient { private void setAuthHeader(HttpPost post) { String username = getUsername(); String password = getPassword(); - String identity = username+":"+password; + String identity = username + ":" + password; String encoding = new String(Base64.encodeBase64(identity.getBytes())); post.setHeader("Authorization", "Basic " + encoding); } @@ -122,7 +120,7 @@ public class DefaultRestClient implements RestClient { GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - String payloadText = gson.toJson(payload, payload.getClass()); + String payloadText = gson.toJson(payload, payload.getClass()); addStringPayload(post, payloadText); } @@ -137,7 +135,7 @@ public class DefaultRestClient implements RestClient { String errorMsg = "Error while executing GET statement"; log.error(errorMsg, e); throw new RestClientException(errorMsg, e); - }finally { + } finally { get.releaseConnection(); } } @@ -153,20 +151,24 @@ public class DefaultRestClient implements RestClient { String errorMsg = "Error while executing DELETE statement"; log.error(errorMsg, e); throw new RestClientException(errorMsg, e); - }finally { + } finally { delete.releaseConnection(); } } private void addStringPayload(HttpPost post, String payloadText) { + if(org.apache.commons.lang.StringUtils.isEmpty(payloadText)){ + throw new IllegalArgumentException("Payload text can not be null or empty"); + } StringEntity stringEntity = null; try { stringEntity = new StringEntity(payloadText); + stringEntity.setContentType(APPLICATION_JSON); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } - stringEntity.setContentType(APPLICATION_JSON); + post.setEntity(stringEntity); } } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/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 5027735..4c8fb6e 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 @@ -24,9 +24,9 @@ import org.apache.stratos.metadata.client.exception.RestClientException; public interface RestClient { - public HttpResponse doPost (String resourcePath, Object payload) throws RestClientException; + public HttpResponse doPost(String resourcePath, Object payload) throws RestClientException; - public HttpResponse doGet (String resourcePath) throws RestClientException; + public HttpResponse doGet(String resourcePath) throws RestClientException; - public HttpResponse doDelete (String resourcePath) throws RestClientException; + public HttpResponse doDelete(String resourcePath) throws RestClientException; } http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/DefaultMetaDataExtractor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/DefaultMetaDataExtractor.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/DefaultMetaDataExtractor.java deleted file mode 100644 index 1594601..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/DefaultMetaDataExtractor.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.sample; - -import org.apache.stratos.metadata.client.data.extractor.MetaDataExtractor; -import org.apache.stratos.metadata.client.exception.MetaDataExtractorException; -import org.apache.stratos.metadata.client.pojo.DataContext; - -import java.util.Collection; - -public class DefaultMetaDataExtractor implements MetaDataExtractor { - - public void initialize() { - //To change body of implemented methods use File | Settings | File Templates. - } - - public Collection<DataContext> getAllData(Object object) throws MetaDataExtractorException { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - public DataContext getData(Object object) throws MetaDataExtractorException { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - public void terminate() throws MetaDataExtractorException { - //To change body of implemented methods use File | Settings | File Templates. - } -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java deleted file mode 100644 index 6c0215d..0000000 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.sample; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.metadata.client.DefaultMetaDataServiceClient; -import org.apache.stratos.metadata.client.MetaDataServiceClient; -import org.apache.stratos.metadata.client.config.MetaDataClientConfig; -import org.apache.stratos.metadata.client.data.extractor.MetaDataExtractor; -import org.apache.stratos.metadata.client.exception.MetaDataExtractorException; -import org.apache.stratos.metadata.client.exception.RestClientException; -import org.apache.stratos.metadata.client.factory.MetaDataExtractorFactory; -import org.apache.stratos.metadata.client.pojo.DataContext; - -import java.util.Collection; - -public class MetaDataServiceClientSample { - - private static final Log log = LogFactory.getLog(MetaDataServiceClientSample.class); - - private MetaDataServiceClient metaDataServiceClient; - - private MetaDataExtractor metaDataExtractor; - - private MetaDataClientConfig metaDataClientConfig; - - - public MetaDataServiceClientSample() throws RestClientException { - //initialize(); - } - - private void initialize() throws RestClientException { - - metaDataClientConfig = MetaDataClientConfig.getInstance(); - metaDataServiceClient = new DefaultMetaDataServiceClient(metaDataClientConfig. - getMetaDataServiceBaseUrl()); - metaDataServiceClient.initialize(); - - if (MetaDataClientConfig.getInstance().getDataExtractorClass() != null) { - metaDataExtractor = MetaDataExtractorFactory.getMetaDataServiceClient(metaDataClientConfig. - getDataExtractorClass()); - metaDataExtractor.initialize(); - } - } - - public Collection<DataContext> getAllData (Object someObj) { - - Collection<DataContext> dataContexts = null; - - try { - dataContexts = metaDataExtractor.getAllData(someObj); - - } catch (MetaDataExtractorException e) { - log.error("Unable to get extracted data", e); - } - - return dataContexts; - } - - public DataContext getData (Object someObj) { - - DataContext dataContext = null; - - try { - dataContext = metaDataExtractor.getData(someObj); - - } catch (MetaDataExtractorException e) { - log.error("Unable to get extracted data", e); - } - - return dataContext; - } - -} http://git-wip-us.apache.org/repos/asf/stratos/blob/7002c68d/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/util/MetaDataClientConstants.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/util/MetaDataClientConstants.java b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/util/MetaDataClientConstants.java index c027bed..5cd29fb 100644 --- a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/util/MetaDataClientConstants.java +++ b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/util/MetaDataClientConstants.java @@ -29,6 +29,4 @@ public class MetaDataClientConstants { public static final String METADATA_SERVICE_PASSWORD = "metadataService.password"; - public static final String METADATA_CLIENT_DATA_EXTRACTOR_CLASS = "metadataClient.dataExtractorClass"; - }
