Sihan’s Blog

Tech Talks

Displaying map from Open Street Map (OSM) inside google map api

with 6 comments

In many countries google’s map data is inadequate; it lacks street level data in many countries.

In these cases Open Street Map is a good solution. Most of the applications written using google map api use google map data for displaying map. In this cases one should find a solution to integrate OSM inside google map api.

After some RnD on google map api and OSM I found out a solution to this. Google map api has a nice provision to add custom map types. And I used this feature to implement the solution. I added a custom map type in my google map and fetch the desired tiles from osm server. Here is the JavaScript code snippet:


var map = new GMap(document.getElementById(“map”));

map.addControl(new GScaleControl());

map.addControl(new GLargeMapControl());

map.addControl(new GMapTypeControl());

//Custom function for fetchng tiles from OSM server

CustomGetTileUrl=function(a,b){

return ‘http://a.tile.openstreetmap.org/’+b+’/’+a.x+’/’+a.y+’.png’;

}

var copyright = new GCopyright(1,

new GLatLngBounds(new GLatLng(53.8136257,-3.0981445),

new GLatLng(53.8654855,-2.9663944) ),

17, “”);

var copyrightCollection = new GCopyrightCollection(”);

copyrightCollection.addCopyright(copyright);

var tilelayers = [new GTileLayer(copyrightCollection,1,17)];

tilelayers[0].getTileUrl = CustomGetTileUrl;

var osmmap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), “O.S.M.”);

map.addMapType(osmmap);

map.setCenter(new GLatLng(34.6891,33.0182), 14, osmmap);

This will create an extra map type labeled “O. S.M.” besides the standard map types of google map api.

Written by sihan

January 6, 2009 at 10:34 PM

Load Balancing Tomcat with Apache

with 4 comments

Recently I was working on server scalability issue of one of my projects. So I had to went through some RnD on load balancing and finally was able to configure it. I used tomcat as servlet container and apache as load balancer. I used mod_proxy and mod_proxy_balancer module of apache for load balancing between two tomcat instances running on port 8080 and 3333.

Here is the details:

Server used:
Tomcat 6.0.16 as servlet container x 2
Apache 2.2.6 as load balancer x 1

Testing Tool:

Jakarta JMeter 2.6.1

Apache Configuration:

Here, I created a cluster named mycluster using 2 tomcat server and sticksession named as JSESSIONID. My test application myapp was running on both of the tomcat server.

I used loadfactor 50 on both of the tomcat server (meaning both of them will be sharing equal load). I

1. Enable mod_proxy: In httpd.conf enable/add

LoadModule proxy_module modules/mod_proxy.so

2. Enable mod_proxy_balancer: In httpd.conf

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

3. Configure the load balancer on the cluster and balancer manager:

In httpd.conf add these lines:

ProxyPass /myapp balancer://mycluster stickysession=JSESSIONID

<Proxy balancer://mycluster>

BalancerMember http://localhost:8080/myapp route=tomcat1 loadfactor=50
BalancerMember http://localhost:3333/myapp route=tomcat2 loadfactor=50

</Proxy>

<Location /balancer-manager>
SetHandler balancer-manager
</Location>

After restarting the server and hitting http://localhost/balancer-manager in a browser it showed me the configurations of the cluster.

Tomcat Configuration:

1. In server.xml file of tomcat running on 8080 port I configured the jvm route as tomcat1 as :

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

By editing the engine tag.

2. In server.xml file of tomcat running on 3333 port I configured the jvm route as tomcat1 the same way:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

The testing application:

I used a single servlet for the test application myapp.

Here is the code snippet of the servlet named myservlet:

ServletContext sc = this.getServletContext();
String servername = sc.getInitParameter(
"servername");
out.print(
"This is from ::" + servername);

In web.xml of the application I added a context parameter named servername which value was server1 while deploying into tomcat1(on port 8080) and server2 on tomcat2(running on 3333 port).

Testing:

With JMeter I created a script for hiitting the url http://localhost/myapp/myservlet using 400 concurrent thread in 20 loops to see the effect.

Links of toturials for mysql performance tuning

leave a comment »

Written by sihan

August 14, 2008 at 10:44 PM

Sending array in GET method in Java Servlet

with 2 comments

Suppose I want to send an array of names from any http client in get method to a servlet like,

name[0] = “sihan”

name[1] = “rasel”

name[2] = “siam”

name[3] = “shamim”

All I have to do build the query string like this :

http://server_url:server_port/servletname?name=sihan&name=rasel&name=siam&name=shamim

And in the servlet we can get the array like this:

String[] names = request.getParameterValues("name");

Then namesarray will contain all the four values passed in ‘name‘ parameter.

Written by sihan

August 14, 2008 at 9:18 PM

MMAPI workarounds in different mobile devices: part 2

with 2 comments

Today, I was working with “Samsung SGH-U700” device and found some interesting things. I found the following result after some benchmark test:

  • microedition.media.version :: 1.1
  • supports.mixing :: false
  • supports.audio.capture :: true
  • supports.video.capture :: false
  • supports.recording :: true
  • audio.encodings :: encoding=audio/amr
  • video.encodings :: encoding=video/H263
  • video.snapshot.encodings :: encoding=jpeg encoding-png
  • streamable.contents :: video.mpeg

And previously I found (source http://developers.sun.com/mobility/apis/articles/mmapioptions/ ):

  • microedition.media.version returns a string representing the version of MMAPI implemented, "1.0" or "1.1" if MMAPI is supported, or null if it isn’t.
  • supports.mixing returns true if mixing is supported, false if it isn’t.
  • supports.audio.capture returns true if audio capture is supported, false if it isn’t.
  • supports.video.capture returns true if video capture is supported, false if it isn’t.
  • supports.recording returns true if recording is supported, false if it isn’t.
  • audio.encodings returns a string representing the supported audio capture formats, or null if audio capture isn’t supported.
  • video.encodings returns a string representing the supported video capture formats, or null if video capture isn’t supported.
  • video.snapshot.encodings returns a string representing the supported image capture formats, or null if video snapshot isn’t supported.
  • streamable.contents returns a string representing the supported streamable content types, in MIME syntax.

Now have a look at my benchmark result:

  1. it shows “supports.video.capture” as “false” meaning that it does not support video capture but shows “encoding=video/H263as "video.encodings", representing the supported video capture format as video/H263. Also it shows “video.snapshot.encodings” as “encoding=jpeg encoding-png” which means its supported image capture formats are “jpeg” and “png”. These things are self cotradictory. Ideally video.encodings,video.snapshot.encodings should be null.
  2. In my benchmark I found supports.recording as true” meaning that recording is supported. At first glance I was astonished thinking that “this handset does not support video capture but supports recording !(I was thinking of video recording as recording)  “ . After some googling I came to know that

You may encounter some confusion when trying to reconcile some of the property values mentioned above with what you observe on a real device. For example: the Nokia 3230 and the Nokia 6600 both return true for both the supports.video.capture and the supports.recording properties. So it seems that both devices will support video recording. But there is a catch. If supports.recording returns true, you can record media using at least one player type — at least one, but not necessarily all. The Nokia 6600 supports the recording of audio, but not the recording of video. But the Nokia 3230 also supports video recording.

(source : http://www.javaworld.com/javaworld/jw-09-2007/jw-09-mobilevideo1.html?page=2)

The case is the same for Nokia-6600 and Samsung-SGH-U700 handsets.

And running my video/image capturing program I always get “javax.microedition.media.MediaException:Not Support Camera”. Then I became sure that Neither video recording nor snap shot is possible on this device with MMAPI.

Written by sihan

August 7, 2008 at 11:33 AM

Simple RSS reader using PHP

with 14 comments

RSS is a basic XML format for publishing syndicated content. The root element of the document is rss, which contains a single channel element. The channel element contains metadata about the feed, including its title, language, and URL. It also contains various stories enclosed in item elements. Each item has a link element containing a URL and either a title or a description (usually both) that contain plain text.
Here is an example feed from google news:


<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<generator>NFE/1.0</generator>
<title>Google News - Sci/Tech</title>
<link>http://news.google.com/?ned=us&amp;topic=t</link>
<language>en</language>
<webMaster>news-feedback@google.com</webMaster>
<copyright>&amp;copy;2008 Google</copyright>
<pubDate>Wed, 06 Aug 2008 07:13:30 GMT</pubDate>
<lastBuildDate>Wed, 06 Aug 2008 07:13:30 GMT</lastBuildDate>
<image>
<title>Google News</title>
<url>http://news.google.com/intl/en_us/images/news_res.gif</url>
<link>http://news.google.com/</link>
</image>
<item>
<title>Delta to Offer Wi-Fi on US Flights - New York Times</title>
<link>http://news.google.com/news/url?sa=T&amp;ct=us/0-0&amp;fd=R&amp;url=http://www.nytimes.com/2008/08/06/technology/06delta.html%3Fref%3Dbusiness&amp;cid=1233786045&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNHnUSPm9sCPP4k2gWPBDboZAiYtCw
</link>
<guid isPermaLink="false">tag:news.google.com,2005:cluster=498a14bd
</guid>
<pubDate>Wed, 06 Aug 2008 01:10:42 GMT</pubDate>
<description>&lt;table border=0 width= valign=top cellpadding=2 cellspacing=7&gt;&lt;tr&gt;&lt;td width=80 align=center valign=top&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=T&amp;ct=us/0i-0&amp;fd=R&amp;url=http://www.geekzone.co.nz/content.asp%3Fcontentid%3D7864&amp;cid=1233786045&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNF4sPd1YWYzXRyLVYtIYVl_ndmW4g&quot;&gt; .......
</description>

</item>
<item>
<title>LiMo lands seven new phones - Register</title>
<link>http://news.google.com/news/url?sa=T&amp;ct=us/19-0&amp;fd=R&amp;url=http://www.theregister.co.uk/2008/08/05/limo_lands_seven_new_phones/&amp;cid=1233302260&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNEILOMvAvv5fwcFWpYLWWJVs7vc5Q</link>
<guid isPermaLink="false">tag:news.google.com,2005:cluster=4982b2f4
</guid>
<pubDate>Tue, 05 Aug 2008 01:00:06 GMT</pubDate>
<description>&lt;table border=0 width= valign=top cellpadding=2 cellspacing=7&gt;&lt;tr&gt;&lt;td width=80 align=center valign=top&gt;&lt;font style=&quot;font-size:85%;limo-platform/&amp;cid=1233302260&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNHS1CWmT1jipl0N-Q-VEOnxM-TmRg&quot;&gt;&lt;img src=http://news.google.com/news?imgefp=hOtr_7v_dyoJ&amp;....</description>
</item>
</channel>
</rss>

Using PHP’s SimpleXML(introduced in PHP5) api it is very easy to access different nodes of the xml above. Like, if we want to get the title of the rss at google news we can write,

$url = "http://news.google.com/?ned=us&topic=t&output=rss"; // url of google news
$xml = simplexml_load_file($url); //loading the document
$title = $xml->channel->title; //gets the title of the document.

The beauty of the function "simplexml_load_file()" is : it does not only load and parse the document but also points to the root element of the document (which is 'rss' here).

Using ‘Simple XML api’ the following code snippet prints news from google news rss :

<?php
$url = "http://news.google.com/?ned=us&topic=t&output=rss";
$rss = simplexml_load_file($url);
if($rss)
{
echo '<h1>'.$rss->channel->title.'</h1>';
echo '<li>'.$rss->channel->pubDate.'</li>';
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
echo '<h3><a href="'.$link.'">'.$title.'</a></h3>';
echo '<span>('.$published_on.')</span>';
echo '<p>'.$description.'</p>';
}
}
?>

Written by sihan

August 6, 2008 at 12:36 PM

MMAPI workarounds in different mobile devices : part 1

with 4 comments

I have been working with mmapi in j2me for the last few days and found some interesting things on different devices.

Set: Nokia-6300

1. Locator string for capturing should be different for capturing image and video

For image it should be,

“capture://image”

For video it should be,

“capture://video”

2. Shows device(camera) unavailable when battery charge is low.

3. png encoding in getSnapShot() method is not supported. Using null as parameter works fine.

Set: Sony Ericsson W850i

1. Problem: Got black screen while capturing more than one time(in canvas).

Solution: Probably caused by paint overwrite problem in this device. Used form display to show camera and it solved the problem.

2. Locator string for capturing is “capture://audio_video” for capturing image and video

3. “png” encoding in getSnapShot() method is not supported. Also custom height, width parameter does not work. Using null as parameter works fine.

4. Video rotates 90 degree counter clockwise after capturing: yet to find a solution.

Note:

1. null in the getSnapShot() method means default parameter of the set which is “encoding=png” and “dimension=480×640” in case of SE 850i

2. In Other sets of Nokia N series(95,93,90,82,76,73,70) the locator string “capture://video” works fine; also png encoding and custom dimensions works fine.

Here is some important system properties to check while working with MMAPI:

  • “microedition.amms.version”
  • “microedition.media.version”
  • “supports.mixing”
  • “supports.audio.capture”
  • “supports.video.capture”
  • “supports.recording”
  • “audio.encodings”
  • “video.encodings”
  • “video.snapshot.encodings”
  • “streamable.contents”
The value of these properties can be found calling System.getProperty() method.

Written by sihan

July 17, 2008 at 12:03 PM

Encoding URL in j2me

with 7 comments

In j2se we can use built-in URLEncoder for encoding urls. But j2me has no built-in URLEncoder support. We need to implement our own method to encode url.

Here is a sample code for such an URLEncoder:

public class URLEncoder {
public static String encode(String s, String enc) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DataOutputStream dOut = new DataOutputStream(bOut);
StringBuffer ret = new StringBuffer(); //return value
dOut.writeUTF(s);
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
bIn.read();
bIn.read();
int c = bIn.read();
while (c >= 0) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*' || c == '_') {
ret.append((char) c);
} else if (c == ' ') {
ret.append('+');
} else {
if (c < 128 ) {
appendHex(c, ret);
} else if (c < 224) {
appendHex(c, ret);
appendHex(bIn.read(), ret);
} else if (c < 240) {
appendHex(c, ret);
appendHex(bIn.read(), ret);
appendHex(bIn.read(), ret);
}
}
c = bIn.read();
}
return ret.toString();
}
private static void appendHex(int arg0, StringBuffer buff) {
buff.append('%');
if (arg0 < 16) {
buff.append('0');
}
buff.append(Integer.toHexString(arg0));
}
}

Note:

Encode each parameter name and its value with the encoder not the whole URL.

Like, to encode the parameter “user name=j2me & test|” use:

URLEncoder.encode(“user name”) as parameter and URLEncoder.encode(“j2me & test|”) and thus build the whole query string.

Written by sihan

July 16, 2008 at 1:20 PM

Jad/Jar installing problem from server on Mobile device

leave a comment »

I had to face a strange problem while downloading jad/jar from my server into mobile device. It was not installing by downloading from the server but I could install and run the same jad/jar transferring by bluetooth device. After some googling I came to know that the problem was in my server mime type settings for .jad extension and I could solve this thing. I had no mime type defined in my server for the extension and mobile browser could not recognize the .jad file.

So, I added the following line in my Apache server’s ‘mime.types’ configuaration file:

text/vnd.sun.j2me.app-descriptor                jad

And it worked like a charm.

For apache tomcat server it should be added in the “web.xml” file like this:

  <mime-mapping>

        <extension>jad</extension>

        <mime-type>text/vnd.sun.j2me.app-descriptor</mime-type>

  </mime-mapping>

Written by sihan

July 16, 2008 at 10:17 AM

Posted in J2ee, J2me

Tagged with ,

Capturing website thumbnail

leave a comment »

Some months before I had to capture and show the thumbnail of website dynamically and I found an excellent tool named ‘IECapt’. It is a sourceforge project.

IECapt is a simple command line utility that uses the Internet Explorer rendering engine to render a web page into a GIF/JPEG/BMP/PNG image file” (Quoted from sourceforge.net). 

It is a written in C++, licensed under GPL and runs in 32 Bit Windows XP/NT/2000. It can be found here.

Sample command:

From windows command line type the following command:

IECapt.exe https://sihantech.wordpress.com thumb.png

This will create an image ‘thumb.png’ in local disk by capturing a screenshot of the given url.

Now we can execute this by PHP using exec() function or by java using Runtime.getRuntime().exec() method.

Written by sihan

July 16, 2008 at 9:24 AM

Posted in Miscellaneous

Tagged with ,