Friday, March 30, 2012

Developing Android App in Scala



This is possible. The following links are useful if you plan to use Scala to develop Android app:

Use Gradle to build android app: https://github.com/jvoegele/gradle-android-plugin/wiki/ and http://www.gradle.org

Developing for ANdroid using Scala/Eclipse: http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android

Example of developing for Android: http://chneukirchen.org/blog/archive/2009/04/programming-for-android-with-scala.html



Complete list of available Archetype Catalogs

Here is a list of Maven repositories:
http://scala-tools.org/repo-releases/


Run as follows:
mvn archetype:generate -DarchetypeCatalog=http://slim3.googlecode.com/svn/trunk/repository

Here is a list of all available Maven Archetype Catalogs:
Verified list:
https://oss.sonatype.org/content/groups/scala-tools/archetype-catalog.xml
http://slim3.googlecode.com/svn/trunk/repository

http://cocoon.apache.org
http://myfaces.apache.org
http://download.java.net/maven/2
http://oss.sonatype.org/content/repositories/appfuse



Not verified list:
http://repo.fusesource.com/maven2
http://tapestry.formos.com/maven-repository
http://www.terracotta.org/download/reflector/maven2/  (is empty)




Tuesday, March 27, 2012

Why you should use NIO for scalability


The key to scalability is NON-BLOCKING IO. Most operating systems support non-blocking I/O, which actually means that when you utilize an I/O resource for reading or writing (say the streams from a socket) there is no blocking operation. So if you read from a stream your read function would immediately return regardless of whether there is data available or not. In Java, non-blocking I/O is provided by the Java New I/O (NIO) library using Selectors and perhaps the Reactor pattern [F]  [F] A nice overview of NIO is athttp://gee.cs.oswego.edu/dl/cpjslides/nio.pdf. This has a major impact on scalability because the threads are only held as long as there is work to do. Once they’re finished with the available data, they are returned to the thread pool so that they may be reused for processing other requests. In this model the threads are allocated to connections only when data is available for processing, which inherently leads to better resource utilization.

Note: This is somewhat off-topic, but if you’re looking to do a lot of work with NIO and networking, we recommend looking at the Apache MINA project at http://mina.apache.org/. MINA provides some nice abstractions for NIO that allows you use a stateful approach to developing NIO applications without having to deal with a lot of the underlying details of using NIO.

Comet or Ajax ?

Which one you should choose ? Ajax or Comet ?
Comet changes the traditional model by using a long-polling HTTP request in the background that allows the server to push data to the browser without requiring additional requests. Essentially this is like AJAX, except in the opposite direction.
While the AJAX model increases the richness of the User Experience for a single client at a time, Comet can do the same for multiple users.

Friday, March 23, 2012

Problem to display characters from other language than English


If  you are, for example, unable to display Swedish characters, this can mainly means that you probably have the language file written in UTF-8 and not in ISO-8859-1. There is no way an ISO-8859-1 environment will display characters from a different charset like UTF-8.

You may have saved data in database in UTF-8 for example. If you want to display correctly the data must be converted to a ISO-8859-1.

Wednesday, March 7, 2012

How to Local Repository path in Eclipse for Maven



If you want to change Maven Local Repository to point to another repository than those of Eclipse pre-configured, you have to do the following steps:
1) Windows->Preferences->User settings
2) Change in the c:\yourMavenDirectory\apache-maven-2.2.1\conf\settings.xml
Locate and add the following in settings.xml

<localRepository>C:\yourmavenrepository\maven\.m2\repository</localRepository>

You can use any path. This is recommended you should not have any spaces inside your path. This may work, but to avoid any further unnecessary or unexpected errors, do rust Windows will handle it gracefully.

Then Click Apply. You can as well click Update Settings. Eclipse will read the new settings and change
the path in your Local Repository to point to the new repository.



Embedded error: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path


You are not authorized to view this page.
If you have already configured the Manager application to allow access and you have used your browser's back button, used a saved book-mark or similar then you may have triggered the cross-site request forgery (CSRF) protection that has been enabled for the HTML interface of the Manager application. You will need to reset this protection by returning to the main Manager page. Once you return to this page, you will be able to continue using the Manager appliction's HTML interface normally. If you continue to see this access denied message, check that you have the necessary permissions to access this application.
If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.
For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Note that for Tomcat 6.0.30 onwards, the roles required to use the manager application were changed from the single manager role to add the following four roles. (The manager role is still available but should not be used as it avoids the CSRF protection). You will need to assign the role(s) required for the functionality you wish to access.
  • manager-gui - allows access to the HTML GUI and the status pages
  • manager-script - allows access to the text interface and the status pages
  • manager-jmx - allows access to the JMX proxy and the status pages
  • manager-status - allows access to the status pages only
The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:
  • users with the manager-gui role should not be granted either the manager-script or manager-jmx roles.
  • if the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session.
For more information - please see the Manager App HOW-TO.


You get the error message above when you try to use Maven to deploy your war-application to Tomcat or try to put http://localhost:8080/manager/deploy?path=yourWar.war in the browser. If you install Tomcat windows installer you will have the following pre-configuration in your tomcat-users.vml file ( located under C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf ). You have to change or add the following:



<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="edison" roles="manager"/>

....
</tomcat-users>





In your POM.xml you should have the following:


<!--  tomcat-server -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<server>tomcat-server</server>
<path>/arbetsformedling</path>
</configuration>
</plugin>



In order to get the maven deploy via the following at command line:
$mvn tomcat:deploy 
or
$mvn tomcat:undeploy

More useful information about this see
https://issues.apache.org/bugzilla/show_bug.cgi?id=51147



Sunday, March 4, 2012

Access sqlite within Emulator from your sell command



$ adb -s emulator-5554 shell
## sqlite3 /data/data/[Your Package Name Here]/databases/[Your Databasefile name here]
sqlite3 /data/data/ [Your Package Name Here] /databases/[Your Databasefile name here]
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
.dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE android_metadata (locale TEXT);
INSERT INTO "android_metadata" VALUES('en_US');
CREATE TABLE  [Your Databasefile name here] (_id integer primary key autoincrement,contactI
D integer, ImagePath TEXT);
INSERT INTO " [Your Databasefile name here] " VALUES(1,3,'/mnt/sdcard/rebeca.jpg');
DELETE FROM sqlite_sequence;
INSERT INTO "sqlite_sequence" VALUES(' [Your Databasefile name here] ',1);
COMMIT;
sqlite>



Using the ContactsContract in phone lookup



This is very important that you make difference between

alt 1:  int contactID = c.getColumnIndexOrThrow(ContactsContract.Contacts._ID);

alt 2: String  contactID  = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

If you use logcat to see the result you will see same contactID  if you use alt 1, but with alt 2 you will see different strings.
Be aware of this!
Sample code to lookup phone and e-mail using the latest class ContactsContract
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}

Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.CommonDataColumns.DATA));
}
emails.close();
cursor.close();

Saturday, March 3, 2012

Pitfalls in Android/Eclipse

Be aware of this pitfall. The Android/Eclipse never tell you that this is a wrong way. You will not be aable to find any reason to runtime errors. Avoid to write so!

findViewById(R.layout.main);