Thursday, July 15, 2010

Low Level Android app fundamentals: Rookie/Novice/fresher...

APPLICATION COMPONENTS:
1. Activity(ACT)

activity, window(W), view(V)
Activity.setContentView()



2. Service(SVC)-

3.  Broadcast Receiver(BR)- 

4. Content Provider(CP)- 
ContentProvider(CP) and ContentResolver(CR)

ACTIVATING COMPONENTS USING INTENT

ACT:

SVC:

BR:
onReceive()









SHUTTING DOWN COMPONENTS


ACT:
finish()
finishActivity(). (startActivityForResult())


SVC:
stopSelf() 
Context.stopService().







THE MANIFEST FILE












INTENT FILTERS




ACTIVITIES AND TASKS





The principal Intent flags are:



FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP
The principal <activity> attributes are:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch 






High Level Android app fundamentals: Rookie/Novice/fresher...

http://developer.android.com/guide/topics/fundamentals.html - nothing better

Wednesday, July 14, 2010

Clean URLs with Drupal-UrlRewriteFilter-Quercus-Jboss

http://www.brianshowalter.com/blog/running_drupal_on_quercus - There's a nice RewriteRule-RewriteMatch combo classes for UrlRewriteFilter posted at this blog.
But it's designed  for tomcat and didn't work on following JBoss configuration right away.
The JBoss config:
1. JBoss-4.2.2
2. Deploying the Quercus web-app(with Drupal inside) as exploded war (Folder name= "myapp.war").
3. Quercus 403
4. Drupal-6.17.
5. UrlRewriteFilter-320
Steps that worked for me:
1. Follow  the above blog at brianshowalter.com.
2. Download UrlRewriteFilter source code from http://code.google.com/p/urlrewritefilter/
(http://code.google.com/p/urlrewritefilter/downloads/detail?name=urlrewritefilter-3.2.0-src.zip&can=2&q=)
3. You must have already downloaded drupalrewrite_0.1 in step1.
4. Couldn't find a link for posting comments on above blog. Hence posting the modified source code here. Kindly setup UrlRewriteFilter and drupalrewrite_0.1 sources with all required libs in eclipse etc. and copy the compiled classes to your myapp.war/WEB-INF/classes.
5. Checkout updated sources for drupalrewrite from http://drupalrewritefilter.googlecode.com (http://code.google.com/p/drupalrewritefilter/source/checkout) and compile/jar and copy to WEB-INF/lib(jar) or WEB-INF/classes(class) as you wish.
6. Good to go.

Thank you.

Monday, June 21, 2010

Removing HTML tags from a String using Regex

result = data.replaceAll("\\\\n","<br/>"). //replace \n with <br/>, 
replaceAll("\\\\", ""). //remove stray "\"s
replaceAll("\\\"", "\\\\\""). // and escape double quotes
replaceAll("<[\\p{Alnum}\\p{Space}\\.\\-=/:\\\"\\\\;]*>"," "). //remove all opening html tags
replaceAll("</[\\p{Alnum}]*>"," "); //remove all closing html tags

Tuesday, June 15, 2010

High Level Hadoop MapReduce: Rookie/Novice/fresher...

MapReduce borrows a lot from functional programming(Lisp/ML/Scheme). Func. prog. expects to process lists of data frequently, hence they have a lot of inbuilt iterator-mechanisms and higher-level functions called list-comprehensions that are operators over lists. Two of these operators are map and reduce. Like map operator, Mapper takes a record (assumed to be a key-value pair) but can emit multiple key-value pairs(map operator does only one). A characteristic of MapRed paradigm is that mapper should be processing individual records in isolation of one-another.(One Record = up to you- depends on the class that loads the data from the block into mapper as k-v pair records).
Reducer takes a key and list of values and can emit none, one or multiple key-value pairs.

It's a general notion to ignore the key that's input to a map task. They could be byte offset to a chunk of data.

People also skip mapper or more often reducer, if it suffices for the app.
Reducers don't start until all mappers complete. They run on the same nodes as mappers(after mappers complete).
All values with same keys are collected from all mappers(that emitted that key) and sent to same reducer. This involves network communication. If multiples keys are processed by one reducer, they are presented to it in sorted order(No assumptions about the values of those keys). This is called "sort and shuffle" phase handled by the underlying framework. A single reduce task processes single key(it takes one key and list of values as input).

Within sort and shuffle, there can be a user-defined combine task that's run on the mapper's intermediate results. If app-logic allows it could be same as reducer code(eg: if reducer is commutative and associative). Combining phase is disabled by default. It runs on the mapper machine. It's solely for reducing network data-load and load on reducer. Don't perform any data-specific operation in combine phase. It may run zero, one or more times.(depends on size of data). Example is the word-count map reduce jobs. Mapper emits [, 1] . Combiner aggragates that mapper's results to [, n](no. of times the word occurred in the input blocks local to that machine). Then comes the reducers(mappers die).

Thank You.

Friday, June 11, 2010

Hibernate JDBCConnectionException: could not execute query- Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

"org.quartz.JobExecutionException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query [See nested exception: org.springframework.dao.DataAccessResourceFailureException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query]
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 13,768,115 milliseconds ago.  The last packet sent successfully to the server was 13,768,115 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem."

This problem occurred in a Grails application having a cron job that requests some data from an external server, processes it and persists some results. However the data is very large and the app requires 2+ Hrs. to process all of it before persisting a small result. The jdbc connection remains idle during this time.
Setting 'autoReconnect=true' or max_idle_time is certainly not a reliable solution for this.

What worked for me:
http://sacharya.com/grails-dbcp-stale-connections/: This was my exact problem. It says "By default, DBCP holds the pooled connections open for infinite time. But a database connection is essentially a socket connection, and it doesn’t come for free. The host OS, database host, and firewall have to allocate a certain amount of memory and other resources for each socket connection. It makes sense to those devices not to hold onto idle connections for ever. So the idea is to make sure that you don’t have stale connections in your pool that would otherwise be silently dropped by OS or firewall."

Modified my Datasource.groovy for working out this solution.
Had to use the following syntax from: http://stackoverflow.com/questions/376544/grails-mysql-maxpoolsize
Modified my Datasource.groovy:

dataSource {
    pooled = true
    dbCreate = "update"
    url = "jdbc:mysql://localhost/yourDB"
    driverClassName = "com.mysql.jdbc.Driver"
    username = "yourUser"
    password = "yourPassword"
    properties {
        maxActive = 50
        maxIdle = 25
        minIdle = 5
        initialSize = 5
        minEvictableIdleTimeMillis = 60000
        timeBetweenEvictionRunsMillis = 60000
        maxWait = 10000     
    }   }


Other relevant links:

http://drglennn.blogspot.com/2009/05/javasqlsqlexception-communication-link.html
http://commons.apache.org/dbcp/configuration.html
http://www.grails.org/DataSources+New
http://www.grails.org/1.2+Release+Notes