Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #110 from SalesforceEng/2.0
Browse files Browse the repository at this point in the history
Merging changes from 2.0 to master
  • Loading branch information
dilipdevaraj-sfdc authored Aug 24, 2016
2 parents 11f79ac + 3de47ed commit 94b75b8
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

import com.salesforce.dva.argus.service.CollectionService;
import com.salesforce.dva.argus.service.MonitorService;
import com.salesforce.dva.argus.service.MonitorService.Counter;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand Down Expand Up @@ -73,6 +75,7 @@ public void run() {

if (count > 0) {
LOGGER.info(MessageFormat.format("Committed {0} metrics for schema records creation.", count));
monitorService.modifyCounter(Counter.SCHEMACOMMIT_CLIENT_METRIC_WRITES, count, new HashMap<String,String>());
jobCounter.incrementAndGet();
}
Thread.sleep(POLL_INTERVAL_MS);
Expand All @@ -84,8 +87,9 @@ public void run() {
LOGGER.info("Error occured while committing metrics for schema records creation.", ex);
}
}
LOGGER.warn("Ending Schema Committer.");
LOGGER.warn(MessageFormat.format("Schema committer thread interrupted. {} metrics committed by this thread.", jobCounter.get()));
collectionService.dispose();
monitorService.dispose();
}
}
/* Copyright (c) 2016, Salesforce.com, Inc. All rights reserved. */
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public static enum Counter {
DATAPOINT_WRITES("argus.core", "datapoint.writes"),
UNIQUE_USERS("argus.core", "users.unique"),
COMMIT_CLIENT_DATAPOINT_WRITES("argus.core", "commit.client.datapoint.writes"),
COMMIT_CLIENT_METRIC_WRITES("argus.core", "commit.client.metric.writes");
COMMIT_CLIENT_METRIC_WRITES("argus.core", "commit.client.metric.writes"),
SCHEMACOMMIT_CLIENT_METRIC_WRITES("argus.core", "schemacommit.client.metric.writes");

private final String _scope;
private final String _metric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public class GusNotifier extends AuditNotifier {
*
* @author Tom Valine ([email protected])
*/
private static final SystemConfiguration _sysConfig = new SystemConfiguration(new Properties());

//~ Instance fields ******************************************************************************************************************************

@SLF4JTypeListener.InjectLogger
Expand Down Expand Up @@ -190,8 +188,9 @@ private void postToGus(Set<String> to, String feed) {
HttpPost chatterIt = new HttpPost();

try {
String gusPost = MessageFormat.format("{0}&subjectId={1}&text={2}", Endpoint.POST_URL.getUrl(), groupId,
URLEncoder.encode(feed.toString(), "UTF-8"));
String gusPost = MessageFormat.format("{0}&subjectId={1}&text={2}",
_config.getValue(Property.POST_ENDPOINT.getName(), Property.POST_ENDPOINT.getDefaultValue()), groupId,
URLEncoder.encode(feed.toString(), "UTF-8"));

chatterIt = new HttpPost(gusPost);
chatterIt.setHeader("Authorization", "Bearer " + generateAccessToken());
Expand All @@ -201,7 +200,7 @@ private void postToGus(Set<String> to, String feed) {

httpclient.execute(chatterIt);
} catch (Exception e) {
_logger.debug("Throws Exception when posting to gus group {} with subject {}", groupId, feed);
_logger.error("Throws Exception when posting to gus group {} with subject {}.", groupId, feed);
} finally {
chatterIt.releaseConnection();
}
Expand All @@ -220,7 +219,7 @@ private String generateAccessToken() {

try {
// Send a post request to the OAuth URL.
oauthPost = new HttpPost(Endpoint.CALLBACK_URL.getUrl());
oauthPost = new HttpPost(_config.getValue(Property.GUS_ENDPOINT.getName(), Property.GUS_ENDPOINT.getDefaultValue()));

// generate the request body
List<String> paraList = Arrays.asList("grant_type", "username", "password", "client_id", "client_secret");
Expand All @@ -237,7 +236,7 @@ private String generateAccessToken() {

return accessToken;
} catch (RuntimeException | IOException e) {
_logger.debug("Encoding Exception when generating access token", CredentialPair.ARGUS_GUS_USER.getValue());
_logger.error("Encoding Exception when generating access token {}", Property.ARGUS_GUS_USER.getDefaultValue());
} finally {
oauthPost.releaseConnection();
}
Expand Down Expand Up @@ -270,19 +269,17 @@ public List<BasicNameValuePair> generateParameterBody(List<String> paraList) {
* @throws UnsupportedOperationException If the specified parameter doesn't have a corresponding GUS property.
*/
public BasicNameValuePair generateNameValuePair(String paraName) {
CredentialPair credentialPara = CredentialPair.fromString(paraName);

switch (credentialPara) {
case GRANT_TYPE_PWD:
return new BasicNameValuePair(CredentialPair.GRANT_TYPE_PWD.getName(), CredentialPair.GRANT_TYPE_PWD.getValue());
case ARGUS_GUS_USER:
return new BasicNameValuePair(CredentialPair.ARGUS_GUS_USER.getName(), CredentialPair.ARGUS_GUS_USER.getValue());
case ARGUS_GUS_PWD:
return new BasicNameValuePair(CredentialPair.ARGUS_GUS_PWD.getName(), CredentialPair.ARGUS_GUS_PWD.getValue());
case GUS_CLIENT_ID:
return new BasicNameValuePair(CredentialPair.GUS_CLIENT_ID.getName(), CredentialPair.GUS_CLIENT_ID.getValue());
case GUS_CLIENT_SECRET:
return new BasicNameValuePair(CredentialPair.GUS_CLIENT_SECRET.getName(), CredentialPair.GUS_CLIENT_SECRET.getValue());
switch (paraName) {
case "grant_type":
return new BasicNameValuePair(paraName, _config.getValue(Property.GRANT_TYPE_PWD.getName(), Property.GRANT_TYPE_PWD.getDefaultValue()));
case "username":
return new BasicNameValuePair(paraName, _config.getValue(Property.ARGUS_GUS_USER.getName(), Property.ARGUS_GUS_USER.getDefaultValue()));
case "password":
return new BasicNameValuePair(paraName, _config.getValue(Property.ARGUS_GUS_PWD.getName(), Property.ARGUS_GUS_PWD.getDefaultValue()));
case "client_id":
return new BasicNameValuePair(paraName, _config.getValue(Property.GUS_CLIENT_ID.getName(), Property.GUS_CLIENT_ID.getDefaultValue()));
case "client_secret":
return new BasicNameValuePair(paraName, _config.getValue(Property.GUS_CLIENT_SECRET.getName(), Property.GUS_CLIENT_SECRET.getDefaultValue()));
default:
throw new UnsupportedOperationException(paraName);
}
Expand All @@ -298,93 +295,6 @@ public Properties getNotifierProperties() {
return result;
}

//~ Enums ****************************************************************************************************************************************

/**
* CredentialPair stores name/value pair in the request.
*
* @author Tom Valine ([email protected])
*/
public enum CredentialPair {

GRANT_TYPE_PWD("grant_type", _sysConfig.getValue(Property.GRANT_TYPE_PWD.getName(), Property.GRANT_TYPE_PWD.getDefaultValue())),
ARGUS_GUS_USER("username", _sysConfig.getValue(Property.ARGUS_GUS_USER.getName(), Property.ARGUS_GUS_USER.getDefaultValue())),
ARGUS_GUS_PWD("password", _sysConfig.getValue(Property.ARGUS_GUS_PWD.getName(), Property.ARGUS_GUS_PWD.getDefaultValue())),
GUS_CLIENT_ID("client_id", _sysConfig.getValue(Property.GUS_CLIENT_ID.getName(), Property.GUS_CLIENT_ID.getDefaultValue())),
GUS_CLIENT_SECRET("client_secret", _sysConfig.getValue(Property.GUS_CLIENT_SECRET.getName(), Property.GUS_CLIENT_SECRET.getDefaultValue()));

private final String _name, _value;

private CredentialPair(String name, String value) {
_name = name;
_value = value;
}

/**
* Returns the credential parameter name corresponding to the input string.
*
* @param paraName name The name of the credential parameter.
*
* @return The corresponding parameter name.
*
* @throws IllegalArgumentException If no element exists for the name.
*/
public static CredentialPair fromString(String paraName) {
if (paraName != null) {
for (CredentialPair para : CredentialPair.values()) {
if (paraName.equalsIgnoreCase(para.getName())) {
return para;
}
}
}
throw new IllegalArgumentException(paraName);
}

/**
* Returns the credential parameter name.
*
* @return The credential parameter name.
*/
public String getName() {
return _name;
}

/**
* Returns the credential parameter value.
*
* @return The credential parameter value.
*/
public String getValue() {
return _value;
}
}

/**
* Endpoint stors callback url and post url.
*
* @author Tom Valine ([email protected])
*/
public enum Endpoint {

CALLBACK_URL(_sysConfig.getValue(Property.GUS_ENDPOINT.getName(), Property.GUS_ENDPOINT.getDefaultValue())),
POST_URL(_sysConfig.getValue(Property.POST_ENDPOINT.getName(), Property.POST_ENDPOINT.getDefaultValue()));

private final String _url;

private Endpoint(String url) {
_url = url;
}

/**
* Returns the url from config.
*
* @return The url from config.
*/
public String getUrl() {
return _url;
}
}

public enum Property {
/** The GUS grant type password. */
GRANT_TYPE_PWD("notifier.property.alert.grant_type_pwd", "password"),
Expand Down Expand Up @@ -428,4 +338,4 @@ public String getDefaultValue() {
}
}
}
/* Copyright (c) 2016, Salesforce.com, Inc. All rights reserved. */
/* Copyright (c) 2016, Salesforce.com, Inc. All rights reserved. */
Loading

0 comments on commit 94b75b8

Please sign in to comment.