Thursday, June 26, 2014

Improved data source for Spring




Why and what about C3p0

c3p0 is an easy-to-use library for making traditional JDBC drivers "enterprise-ready" by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2. In particular, c3p0 provides several useful services:

Classes which adapt traditional DriverManager-based JDBC drivers to the newer javax.sql.DataSource scheme for acquiring database Connections.
Transparent pooling of Connection and PreparedStatements behind DataSources which can "wrap" around traditional drivers or arbitrary unpooled DataSources.
The library tries hard to get the details right:

c3p0 DataSources are both Referenceable and Serializable, and are thus suitable for binding to a wide-variety of JNDI-based naming services.
Statement and ResultSets are carefully cleaned up when pooled Connections and Statements are checked in, to prevent resource- exhaustion when clients use the lazy but common resource-management strategy of only cleaning up their Connections....
The library adopts the approach defined by the JDBC 2 and 3 specification (even where these conflict with the library author's preferences). DataSources are written in the JavaBean style, offering all the required and most of the optional properties (as well as some non-standard ones), and no-arg constructors. All JDBC-defined internal interfaces are implemented (ConnectionPoolDataSource, PooledConnection, ConnectionEvent-generating Connections, etc.) You can mix c3p0 classes with compliant third-party implementations (although not all c3p0 features will work with external implementations).
c3p0 now fully supports the JDBC4 specification.

c3p0 hopes to provide DataSource implementations more than suitable for use by high-volume "J2EE enterprise applications".


Change to maven
                  <dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>


Change to Spring config where data source is created

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="${LIBRARY_JDBC_CONNECTION_STRING}" />
<property name="user" value="${LIBRARY_DATASOURCE_USER_NAME}" />
<property name="password" value="${LIBRARY_DATASOURCE_USER_PASSWORD}" />

<property name="idleConnectionTestPeriod" value="${C3P0_POOL_IDLE_CONNECTION_TEST_PERIOD}"/> 
<property name="preferredTestQuery" value="select 1"/>

<!-- performance improvements and configuration so that you don't get above error from: http://javatech.org/2007/11/c3p0-connectionpool-configuration-rules-of-thumb/ -->
<property name="acquireIncrement" value="${C3P0_POOL_ACQUIRE_INCREMENT}"/>
<property name="maxIdleTime" value="${C3P0_POOL_MAX_IDLE_TIME}"/>
<property name="maxIdleTimeExcessConnections" value="${C3P0_POOL_MAX_IDLE_TIME_EXCESS_CONNECTIONS}"/>
<property name="maxPoolSize" value="${C3P0_POOL_MAX_POOL_SIZE}"/>
<property name="minPoolSize" value="${C3P0_POOL_MIN_POOL_SIZE}"/>
<property name="numHelperThreads" value="${C3P0_POOL_NUM_HELPER_THREADS}"/>
<property name="unreturnedConnectionTimeout" value="${C3P0_POOL_UNRETURNED_CONNECTION_TIMEOUT}"/>  
</bean>

Example  values

C3P0_POOL_ACQUIRE_INCREMENT : 1 
C3P0_POOL_IDLE_CONNECTION_TEST_PERIOD : 300 
C3P0_POOL_MAX_IDLE_TIME : 3600 
C3P0_POOL_MAX_IDLE_TIME_EXCESS_CONNECTIONS : 2 
C3P0_POOL_MAX_POOL_SIZE : 4 
C3P0_POOL_MIN_POOL_SIZE : 2 
C3P0_POOL_NUM_HELPER_THREADS :2 
C3P0_POOL_UNRETURNED_CONNECTION_TIMEOUT : 3600

References
http://www.mchange.com/projects/c3p0/


Thursday, June 19, 2014

Best way to Identifying integer value from String



Best way to Identifying integer value from String


if(stringVariable.trim().matches("^\\d*$")){
// true this is a integer
}

To Decimal number like 1.23

if(("1.23".matches("^\\d+\\.\\d{2}$")){
// this will be truw
}

Wednesday, May 28, 2014

Building a Complex Regular Expressions


I follow this way to build a very complex regular expression. But this has to be start from very small
  • /v1/user  -  /(\\w*)/(\\w*)
  • v1/203i03 -  /(\\w*)/(\\w*)
  • v1 -   /(\\w*)/*(\\w*)

  • /v1/user?name  -  /(\\w*)/(\\w*)(\\?*)(\\w*)
  • /v1/user?name=don  -  /(\\w*)/(\\w*)(\\?*)(\\w*)(\\=*)(\\w*)


Java Code to Test the regular expression


private static void getEndPointUrl(String restUrlPattern,String urlToMatch) {
Pattern pattern = Pattern.compile(restUrlPattern);
Matcher matcher = pattern.matcher(urlToMatch);
System.out.println(restUrlPattern + " -- " +urlToMatch);
if (matcher.matches()) {
System.out.println("Matches");;
} else {
System.out.println("Not Matches");;
} }

    Thursday, February 6, 2014

    Get a JSON Object from HTML form

    Found this great JS library which can be used to serialize an HTML Form to a JavaScript Object

    Example HTML form
    <form id="my-form">
      <input type="text" name="name"              value="david" />

      <!-- object -->
      <input type="text" name="address[city]"         value="Melbourne" />
      <input type="text" name="address[state][name]"  value="Victoria" />
      <input type="text" name="address[state][abbr]"  value="VIC" />
    </form>

    Java Script
    var jsonData = $('#my-form').serializeJSON();

    Return value
    {
      name: "david",

      address: {
        city: "Melbourne",
        state: {
          name: "Victoria",
          abbr: "VIC"
        }
      }
    }

    Install

    Download the jquery.serializeJSON.min.js(https://raw.github.com/marioizquierdo/jquery.serializeJSON/master/jquery.serializeJSON.min.js) script and include in your page after jQuery, for example:

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.serializeJSON.min.js"></script>

    Reference :https://github.com/marioizquierdo/jquery.serializeJSON

    Thursday, October 24, 2013

    What are the defined fields for each salesforce object

    In salesforce(www.salesforce.com) there are many objects like accounts, users, leads...Etc. When using REST API or when executing a SOQL we need to give the exact name of the field.
    Since salesforce does not support SELECT * from XXXX

    This is way I used to extract necessary information from salesforce

    1. Login to salesforce
    2. Go to Setup->Develop API
    3. Generate Partner WSDL (If you working with sandbox environment it might gives some errors but partner WSDL is sufficient)
    4. Generate stub jar file Ref :http://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_code_set_up_client.htm
    5. Use the following code sample

           ConnectorConfig sfconfig = new ConnectorConfig();
            sfconfig.setUsername("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            sfconfig.setPassword("xxxxxxxxxxxxxxxxxxxxxxQNx26Lb4PTw");        
            PartnerConnection  connection = Connector.newConnection(sfconfig);
             DescribeSObjectResult[] describeSObjectResults =  connection.describeSObjects(
                       new String[] { "account"});
            for (int i=0;i < describeSObjectResults.length; i++)
            {
                DescribeSObjectResult desObj = describeSObjectResults[i];
                String objectName = desObj.getName();
                com.sforce.soap.partner.Field[] fields = desObj.getFields();
                if (desObj.getActivateable()) System.out.println("\tActivateable");
                for(int j=0;j < fields.length; j++)       {                        
                    com.sforce.soap.partner.Field field = fields[j];
                    System.out.println("\tField: " + field.getName());
                    System.out.println("\t\tLabel: " + field.getLabel());
                    if (field.isCustom()) 
                        System.out.println("\t\tThis is a custom field.");
                    System.out.println("\t\tType: " + field.getType());
                    if (field.getLength() > 0)
                        System.out.println("\t\tLength: " + field.getLength());
                    if (field.getPrecision() > 0)
                        System.out.println("\t\tPrecision: " + field.getPrecision());
                    if (field.getType() == FieldType.picklist)
                    {                            
                      
                        PicklistEntry[] picklistValues = field.getPicklistValues();
                        if (picklistValues != null && picklistValues[0] != null)
                        {
                            System.out.println("\t\tPicklist values = ");
                            for (int k = 0; k < picklistValues.length; k++)
                            {
                                System.out.println("\t\t\tItem: " + picklistValues[k].getLabel());
                            }
                        }
                    }
                    if (field.getType() == FieldType.reference)
                    {                            
                       
                        String[] referenceTos = field.getReferenceTo();
                        if (referenceTos != null && referenceTos[0] != null)
                        {
                            System.out.println("\t\tField references the following objects:");
                            for (int k = 0; k < referenceTos.length; k++)
                            {
                                System.out.println("\t\t\t" + referenceTos[k]);
                            }
                        }
                    }
                }            
            }

    Wednesday, October 16, 2013

    Creating a new remote access application in Salesforce

    Create OAuth Token and secret from Salesforce


    If your planning to get Sales force information from REST API you will find that creating Remote Access app by following http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API is not working as describe

    Here how it goes

    Log in to Salesforce.com with your developer account, navigate to Setup ➤ Develop ➤ Remote Access, 
    and click New to create a new remote access application if you have not already done so.

    This is not working under the new Salesforce changes

    Once you click Remote Access it will say 

    Remote Access Objects have been moved to Applications. You'll be redirected to that page in five seconds, or you can click Take Me There to go now. 

    Then you will be forword to Create -> Apps section

    Go to Connected Apps and create new



    Once you save this you can get the OAuth token and secret

    Saturday, October 12, 2013

    Read and process large one line JSON file

    My initial problem was to read and process large one line JSON file. with traditional approaches it took 3 hours to read and process the JSON file. So I have done research in this area and found a way to read the JSON file chunk by chunk, now I can do the same thing in 5 min

    Bellow I have break the JSON which had 200000 elements in to 20 * 10000 chunks

    public static void jsonFileReader() throws JsonParseException, IOException{
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createJsonParser(new File("MyHugeJSonFile.json"));
    JsonToken current;

    current = jp.nextToken();
    if (current != JsonToken.START_OBJECT) {
    System.out.println("Error: root should be object: quiting.");
    return;
    }
    int i = 0;
    while (jp.nextToken() != JsonToken.END_OBJECT) {

    String fieldName = jp.getCurrentName();
    current = jp.nextToken();
    if (fieldName.equals("employees")) {

    if (current == JsonToken.START_ARRAY) {
    List<String> strings = new ArrayList<String>();
    String previousValue = "";
    while (jp.nextToken() != JsonToken.END_ARRAY) {
    JsonNode node = jp.readValueAsTree();
    String valueAsText = node.get("id").getTextValue();
    strings.add(valueAsText);
    if((strings.size() == 100)) {
    String valueAsText1 = null;
    while (jp.nextToken() != JsonToken.END_ARRAY) {
    JsonNode node1 = jp.readValueAsTree();
    valueAsText1 = node1.get("id").getTextValue();
    if(!previousValue.equals(valueAsText1)) {
    break;
    } else {
    strings.add(valueAsText1);
    }
    }
    int j =0;
    for (Iterator<String> iterator  = strings.iterator(); iterator.hasNext();) {
    i = i + 1;
    j = j + 1;
    String string = (String) iterator .next();
    System.out.println(i+" -- "+j+" --> "+string);
    strings = new ArrayList<String>();
    }
    System.out.println("-------------------------------------------------------------");
    strings.add(valueAsText1);
    }
    previousValue = valueAsText;
    }
    int j =0;
    for (Iterator<String> iterator  = strings.iterator(); iterator.hasNext();) {
    i = i + 1;
    j = j + 1;
    String string = (String) iterator .next();
    System.out.println(i+" -- "+j+" --> "+string);
    strings = new ArrayList<String>();
    }
    System.out.println("-------------------------------------------------------------");

    } else {
    System.out.println("Error: records should be an array: skipping.");
    jp.skipChildren();
    }
    } else {
    System.out.println("Unprocessed property: " + fieldName);
    jp.skipChildren();
    }
    }

    System.out.println("Total Record size " + i);
    }