Archive for the ‘Guide’ Category

Guide: Google Maps V2 for Android : Creating your Google Map Application:

If you already got your Google Maps Android API V2 key then you are ready to create
your map application. If you don’t head to my Guide: Google Maps V2 for Android :
Getting the API key
post to read how to get it.

So lets start:
1. Open Eclipse and create a new Android project.
The first thing that we will handle is the import of Google Map classes.
To get the Google Maps files we need to download the last version of
Google Play Services via the Android SDK Manager.

2. After you downloaded the Google Play Services, restart Eclipse and
in the Package Explorer Right-Click –> Import….
In the opened windows choose “Existing Android Code into Workspace” and click “Next”.
Click the “Browse…” Button and head to the location of your SDK folder.
in it find the following folder:

\extras\google\google_play_services\libproject\google-play-services_lib

and press “OK”, check the V next to it in the window and press the “Finish” button.

3. Now you added Google Play Services to your work space, we have to create a reference
from our project to this library. Right-Click your project and choose “Properties” go
to the Android section, in the lower part press the “Add…” button and add a reference
to that library. Your result should be as in the screen shot bellow:

adding google play services library

Note: If you try to reference google-play-service library and you receive a red X next to this
reference, what you should do is to move the .jar file to a location where it’s path will be shorter
and then reference it again.

4. Another import we have to make in order to make our application work on
Operation system prior to API11 is to import the support library this can be done very easily
using Eclipse: Right-Click you project and choose “Android Tools” and then choose
“Add Support Library…”:

supportlibrary

When you finish those import you should have the following libraries (Red) in the
Android Dependencies folder (Green) of your project:
Untitled

5. We are now ready for some codding: First of all open the Android Manifest file:
add the following permissions:

<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Important: Replace your application package instead of the current “your.application.package” string.

As mentioned in the comments by @Keilaron it looks like since the Google Play Services
3.1.59 update the MAPS_RECEIVE permissions are completely unnecessary and as a result
they can be removed.

6. Next, Google Maps uses OpenGL so we have to add OpenGL support to our application
by adding this to the Manifest file:

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

7. Finally add your key to you application right before you close your “application” node
in the Manifest file:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your Google Maps API V2 Key" />

8. Now create an Activity that extends from “FragmentActivity”:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MapActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
    }
}

9. Finally for map_layout, XML layout file that was set
as a content view of the map activity write the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

10. UPDATE: The last update for Google Play Services library revision 13 introduced
a new meta-data tag that should be added as well to your Android Manifest file.
So go ahead and add this right next to your API key meta-data code:

<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" />

And that’s it, run the application and you should see a full screen map:

map

Remember that if you want to run the application in the emulator you should install
Google Play Services first.

Enjoy and stay tuned.

Guide: Google Maps V2 for Android : Getting the API key

Since I have been working on an Android application that uses Google map
services I have decided to write a couple of posts on this topic.
The application will be posted here as well in the near future.

The first thing you have to know when implementing Google Maps for android is
that you have to produce your own Google Map Android API V2 key.
Now there is a reason that I’m marking this name, take a careful look that you turning on
the right Key in you API Console and that you have chosen the API for Android
and not the Google Maps API V2 or V3 for example.

So how do we get an API key, you ask? Well it’s a few steps task:
1. First of all we need to find our SHA1 key. For debugging
purposes we will use the debug.keystore file to get our SHA1 key. to do that
we will use a tool named ‘Keytool‘ that come with Java installation.
so open you command prompt, head to the following location:

C:\Program Files\Java\<your JDK or JRE instllation>\bin>

and run the next command:

keytool -list -v -keystore C:\Users\<your user name>\.android\debug.keystore -storepass android -keypass android

You will recive the following output:

SHA1 key pic
Copy the SHA1 key by right-clicking the command prompt choosing ‘Mark’, then
mark the all key and right-click again on the marked text to copy it.

2. Now that we have the SHA1 key we can head to the Google API’s Console,
sign-in with our Google account and create a new project by clicking the drop box
in the upper-left corner of the screen and choosing the ‘Create…’ option:

create in google api

Give a name to your project and create it.

3. Once you have done that head to the Services section and turn on the Google Map Android API V2:

api service

4. Now open the API Access section and press the Create new Android key… button.

In the window that opens you will have to enter you SHA1 key you got in the 1. section adding
to it your application package name after a semicolon as in the example:

45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example.your_project_name

After pressing the Create button you will recieve you own Google Maps API V2 key for Android.

My next post will explain how to use this key to create a Google map in your application.

Guide: c3p0 – JDBC3 Connection and Statement Pooling

After I uploaded my JavaEE course project to Openshift hosting service as you
could do using the Guide: Introduction to Openshift hosting Apache Tomcat, I thought
that my work with this project is done. But strangely for me I started to encounter
database connectivity issues. I done a little research and realized that the reason for
those problems is the default MySQL configuration for closing inactive connections to
the database. MySQL automatically closes any inactive connections to the database
after the default 8 hours. Unfortunately Hibernate ins’t aware of that and still thinks
that the connection is open when trying access the database to perform a query.
Of course Hibernate will fail to perform that task and we will be given the following exception:

[org.hibernate.exception.JDBCConnectionException: could not execute query] with root cause java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

To fix this issue as it’s done in production environments it is important to implement
some sort of connection pool mechanism that will always keep open shared connections
to the database for all application users. Here I’m going to show you how to implement the
c3p0 – JDBC3 Connection and Statement Pooling mechanism in your project with
Hibernate already implemented:

1. So clearly the first thing that needs to be done is to download the c3p0 project files.
Use the following link to download the latest version from Source-forge.

2. Extract the files from the zip archive and copy the c3p0-0.9.2-pre7.jar
and mchange-commons-java-0.2.3.2.jar to the lib folder of your project.

3. Edit your hibernate.cfg.xml and add the following infomation under the
<session-factory> hive:

<!-- important to specify c3p0 as your connection provider -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

<!-- configuration for connection pool via c3p0 -->
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.timeout">14400</property>
<property name="c3p0.idle_test_period">3600</property>

It is really important to specify the “connection.provider_class” property as:
org.hibernate.connection.C3P0ConnectionProvider or hibernate will continue using it’s
own default mechanism. Other properties may be customized to your liking.
Eventually your hibernate configuration file should look like this one:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
        <!-- default hibernate connection settings -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/database</property>
		<property name="connection.username">user</property>
		<property name="connection.password">password</property>
		<!-- for MySQL users specify the following dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<!-- inmportant to specify c3p0 as your connection provider -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- configuration for connection pool via c3p0 -->
		<property name="c3p0.min_size">3</property>
		<property name="c3p0.max_size">20</property>
		<property name="c3p0.acquire_increment">1</property>
		<property name="c3p0.max_statements">50</property>
		<property name="c3p0.timeout">14400</property>
		<property name="c3p0.idle_test_period">3600</property>
		<!-- end of configuration -->
		<mapping class="com.eadesign.hibernate.CouponDetails"/>
		<mapping class="com.eadesign.hibernate.Company"/>
		<mapping class="com.eadesign.dto.User"/>
		<mapping class="com.eadesign.dto.Administrator"/>
		<mapping class="com.eadesign.hibernate.Feedback"/>	
	</session-factory>
</hibernate-configuratio

And that’s it, now you have connection pool in your project.

more information can be found in the c3p0 manual.
As this Appendix C: Hibernate-specific notes:

Appendix C-Hibernate of c3p0

December 19, 2012Emil Adjiev 1 Comment »
FILED UNDER :Guide , Guide - Java Development

Guide: WordPress hosting on OpenShift

Actually in my opinion this is one of the simplest ways to get your WordPress blog/site
up and running in no time. in just few simple commands you have a Blog in the air that
you can customize and edit to you liking. I am counting that you already have created your
own OpenShift account and installed the rhc Client Tools. If not please head to my
Guide: Introduction to Openshift hosting Apache Tomcat post and follow the first two
paragraphs to open the account and install the tools. I would recommend to read all of it
as it holds great information regarding tools that will simplify your work with OpenShift.

Now you are ready to create our own WordPress application container on OpenShift.
Open your command prompt and head to the location were you want your git repository
for this project to be created and enter the following command:

rhc app create -a <name for your container> -t php-5.3

Name your application container as you wish. This name will be part of the URL for accessing your application.
So now we allocated resources to ourselves in Red Hut’s cloud with php 5.3 application
already installed. WordPress needs a database to store it’s data and can’t live without
it so we will add MySQL database to this configuration using the next command:

rhc cartridge add -a <name of your container> -c mysql-5.1

And finally I always add the phpMyAdmin application to make it easier on me to manage my
databases. To do that run the following command:

rhc cartridge add -a <name of your container> -c phpmyadmin-3.4

Save your database credentials produced at the end of this process.

Now we are ready to install WordPress. There are two ways you can do that:

1. The first and the easy way to get a WordPress installation is to pull it from a git repository.
Head to your application folder using the next command:

cd <name of your container>

Now set the remote git repository to pull it’s stream from it:

git remote add upstream -m master git://github.com/openshift/wordpress-example.git

Pull the stream/content:

git pull -s recursive -X theirs upstream master

And push it to your repository on the cloud:

git push

To access your WordPress application use the default credentials:

User name: admin
Password: OpenShiftAdmin

The problem is that the version of WordPress now hosted on this repository is 3.4 and there
is a new 3.5 WordPress installation available. You could upgrade your WordPress version
using it’s Dashboard interface, but now you will have 2 different versions of WordPress
on your local git repository (3.4) and the clouded one (3.5). I will explain how to fix this
problem in the update section.

2. Alternatively you could push you own WordPress installation to your cloud repository.
Head to www.WordPress.org and download the latest version of WordPress.
Copy the downloaded content to the php folder in your local repository and open the
command prompt. Head to your local git repository folder and enter the following command:

git add .

For letting git know of all the changes that were made to that folder, then:

git commit -m "added wordpress3.5"

Committing the changes to the local repository. And finally Pushing the changes to the cloud:

git push

And that’s it, now you can access your new WordPress application using the URL
you are provided on OpenShift and start customize it.
 

Updating your local repositry with the changes made by WordPress in the cloud:

When you will update you WordPress installation in the future or maybe you would want to
change the looks of your blog/site by adding a sleek Theme or maybe a Widget and you’ll do
that using the Dashboard interface, you would have to update your local repository with
those changes. In that case what you need to do is to take a snapshot of your machine on
OpenShift using the next command:

rhc snapshot save -a <name of your container>

this will download a <name of your container>.tar.gz to you local git repository
containing a snapshot of your files from the clouded repository. Extract the files to a folder
with the same name and head to:
<name of your container>\<your OpenShift ID>\app-root\repo\
Copy the content of this folder to your local git repository folder and commit the changes
using the following commands:

git add .
git commit -m "wordpress updated"
git push

That way the files on your local repository will be identical to those in the cloud.

December 15, 2012Emil Adjiev No Comments »
FILED UNDER :Guide , Guide – Web Development

Guide: Introduction to Openshift hosting Apache Tomcat

To present my Java EE course project in this blog I had to find another
Apache Tomcat hosting service since the one we got during education was
not responding anymore. That’s how my search for a free Apache Tomcat
hosting service started. I contacted with my hosting service where i host
this blog and I was quickly explained that this kind of service is not
supported there. Next i went to the web (Goggled) to search for such free
service just to find out that there are not that many of them at all.
And then I saw that Red Hat offering Apache Tomcat service and much more
with their OpenShift PaaS – Platform as a Service. They basically give you
a platform (clouded resources) to deploy your applications and this platform
supports Node.js, Ruby, Python, PHP, Perl, and Java technologies and much more.

So how is it done?
1. The first thing is to go to http://openshift.com and create an account.
Once you have created an account you can publish 3 application for free.
During the creation of your account you will have to specify you Namespace
that latter on will be used for your application URL’s.

2. Install the OpenShift client tools. For that you need to install Git for Windows
and a Ruby and configure them. This is explained in the following link.
During the configurations of Git you will create an ssh key for connecting
to your repository on Red Hat.

3. Now that you installed the client tools you are ready to create your first
application container on OpenShift. Actually this can be done using their web
interface but it’s faster to do that with the command line. To create your
application container with Apache Tomcat already installed you need to run to
following commands in the command prompt:

rhc app create -a tomcat -t diy-0.1

this will create for you your DIY (do it yourself) application container on
Red Hat’s servers with the name “tomcat” and a local Git repository in your
current location at the command prompt with the same name. now all you need
to do is to edit your local repository and to push changed to the server
uisng git.

Enter your repository folder by entering:

cd tomcat

and then enter the following commands:

1.git remote add upstream -m master git://github.com/openshift/openshift-tomcat-quickstart.git
2.git pull -s recursive -X theirs upstream master
3.git push

what we are doing here is configuring a remote upstream and we pull it’s content from it
to our local git repository. finally we push this changes to our own application container.
at the end of this process we will have a DIY application with Apache Tomcat 7 installed.

4. Now we need to connect to our application container to copy there our application files.
There are 3 way to do that: we can copy the files to our local git repository and push the
changes to the server, we can use an SSH terminal (like PUTTY) and edit the files using
commands from the command line or we can use an SSH based FTP tool (like WinSCP)
to manage files as you would do in a commander interface.

you can download PUTTY from here. You should download the zipped file for all PUTTY package.
you can download WinSCP from here.

putting your project into the webapps folder of your local application repository and pushing
it to the server will put your files in the following location:

/var/lib/openshift/<your openshift userID>/app-root/
runtime/repo/diy/tomcat/webapps

and this is not the right webapps folder for the installed Apache Tomcat.
That is the reason I prefer to use the WinSCP application. So lets open WinSCP and
the first window you will receive is the WinSCP Login window:

enter the following information:

  1. Your full application container URL – you can get it from OpenShift.
  2. Your Username – again you can find it on your application page on OpenShift under the “GIT REPOSITORY” section.
  3. Your OpenShift password.
  4. You have to supply your ssh key, you can create it using puttygen.exe application that you can download from the PUTTY link. Open puttygen.exe:

  1. Click the “Load” button and go to your C:\Users\<your windows username>\.ssh folder. Chose the id_rsa file (without extension) and press Open. you will be notified that this is a security key.
  2. Click the “Save private key” button to save a new private key (.ppk) file.

5. Now you can supply your security key and connect via WinSCP to your machine on Openshift.
When you connected you will receive a commander window. The right pane connected to
your OpenShift repository at the following location:

/var/lib/openshift/<your openshift userID>/

Head to:

/var/lib/openshift/<your openshift userID>/app-root/
data/tomcat/webapps

and copy your project files here.
Head to:

/var/lib/openshift/<your openshift userID>/app-root/
data/tomcat/lib

and copy your project resource files (your Jars necessary for the project).

6. Finally open command prompt again and run:

rhc app restart tomcat

to restart your container and let the changes take affect. now you can access you project
using the application URL you can get on OpenShift.

December 11, 2012Emil Adjiev 2 Comments »
FILED UNDER :Guide , Guide - Java Development