English
Java ME (J2ME) JSON Implementation Tutorial/Sample
by jimmy on Mar.03, 2010, under English, Java, Programming
JSON is one of the most popular format used for communicating between (Java ME) J2ME client and App Server. Strangely it’s hard to find JSON implementation sample in J2ME. So I decide to write a sample.
Blackberry sending delivered notification email
by jimmy on Jun.26, 2009, under English, My Life, Technology
I just realize that when sending to blackberry device when the email received on blackberry and the sender request delivered notification, it will automatically send delivered notification back to the sender.
And there’s no option to disable this.
Maybe it’s like a ‘feature’, but in my opinion it’s suck to cannot select whether we want to sent this delivered notification or not.
For spammer this could be good ‘feature’ since they will know which email still actively receiving their spam.
In my blackberry curve there’s only option to disable ‘read notification’, which located at message – options – email settings.
But read notification is not delivered notification, when I disable sending read notification still the blackberry sending delivered notification automatically.
This is sucks, anyone know how to solve this issue?
Solving Pidgin cannot connect to Yahoo Messenger (YM)
by jimmy on Jun.19, 2009, under English, Linux
Starting June 18 I started having problem connecting to YM with Pidgin.
Somehow it’s random I can connect or not.
I google and several article/forum writing that need to flush dns cache and also about several YM server cannot be accessed from Pidgin.
I try my friend advice to change YM server configuration to IP rather than domain name.
And it’s worked..
You will need to use cn.scs.msg.yahoo.com as Pager server since June 24, or you can update to pidgin 2.5.7 to solve this problem
The steps: (continue reading…)
Solving failed starting mysql service with non default datadir (Ubuntu)
by jimmy on Jun.19, 2009, under English, Linux
I used to put my mysql data dir to /home/mysql, just to separated the partition so when something goes wrong or upgrading the OS I won’t lose my mysql data just because I forgot to backup it.
After I upgrade from Ubuntu 8.04 to Ubuntu 9.04 suddenly my mysql failed to start.
The strange thing the only data dir seems working is putting it on /var/lib/mysql which is the default directory for mysql data dir.
After browsing around I found out that it is apparmor that prevent mysql datadir on different directory.
So the easy way to solve this is to remove apparmor : apt-get –purge remove apparmor
Unknown winmail.dat attachment in email
by jimmy on Jul.29, 2008, under English, Life, Technology
From my colleague I received an email but with unknown type : winmail.dat
He should send me office document.
I asked him to sent it again, he forwarded the email again but I still received winmail.dat in attachment.
After googling about this I found out that winmail.dat is generated by Outlook (I using thunderbird).
From About.com :
It’s Outlook’s fault, in a way. Or the recipient’s email client’s. If Outlook sends a message using the RTF format (which is not very common outside Outlook) for bold text and other text enhancements, it includes the formatting commands in the winmail.dat file. Receiving email clients that do not understand the code therein display it as a stale attachment. To make matters worse, Outlook may also pack other, regular file attachments in the winmail.dat file.Fortunately, you can get rid of winmail.dat altogether by making sure Outlook does not even try to send mail using RTF.
And there’s also a way to disable this feature on Outlook. You can open it for more detail.
I would focus on how non-Outlook user can handle this format.
I know I can not ask all Outlook user who sent me attachment to turn off this feature.
So I search a program to open this content type “application/ms-tnef”.
I encounter this good console program : WMdecode
You just need to extract this. When running the program it’ll search winmail.dat in current folder and extract it.
The program working great although it just console application, no GUI.
Other tool I search is add on for Thunderbird.
I found : LookOut
This add on will automatically decode the winmail.dat
Also working great but it won’t decode winmail.dat in emails that you received before installing the add-on.
So I think it’ll only decode winmail.dat on new email received.
Cheers
Java Application – Make sure only single/one instance running – with File Lock and ShutdownHook
by jimmy on Jul.21, 2008, under English, Java, Programming
Often we want to make sure that only 1 instance our application running.
Because something terrible could happen when more than 1 instance running (for example the whole server would exploded and that would make you fired
)
Nevertheless what the reason, one of the way to make this happen is by creating a lock file as a sign that an instance is currently running.
So you application will have this flow:
- Check if the lock file exists.
- Try to delete the lock file to check if there’s really a running process lock the file.
- Get the file lock.
- If failed to get the lock file, show error that there’s already an instance running –> the end
- If successfully get the lock then go on with your application want to do.
- When application ended/closed release the lock and delete the file lock.
package test.jimmy.filelock; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class MyApp { private static File f; private static FileChannel channel; private static FileLock lock; public static void main(String[] args) { try { f = new File("RingOnRequest.lock"); // Check if the lock exist if (f.exists()) { // if exist try to delete it f.delete(); } // Try to get the lock channel = new RandomAccessFile(f, "rw").getChannel(); lock = channel.tryLock(); if(lock == null) { // File is lock by other application channel.close(); throw new RuntimeException("Only 1 instance of MyApp can run."); } // Add shutdown hook to release lock when application shutdown ShutdownHook shutdownHook = new ShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownHook); //Your application tasks here.. System.out.println("Running"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } catch(IOException e) { throw new RuntimeException("Could not start process.", e); } } public static void unlockFile() { // release and delete file lock try { if(lock != null) lock.release(); channel.close(); f.delete(); } catch(IOException e) { e.printStackTrace(); } } static class ShutdownHook extends Thread { public void run() { unlockFile(); } } }
Technorati Tags: java, file lock, shutdownhook, lock, single, one, intance
Microsoft + Yahoo = ?
by jimmy on Feb.05, 2008, under English, Technology
Microsoft planning to buy Yahoo for $44.6 billion. Until now Yahoo hasn’t give final answer it.
I’m wondering why do Microsoft want to buy Yahoo, is it for fighting Google?
Or Microsoft just want to kill one of his rival.
Let’s see
IM : Microsoft has Windows Live Messenger, Yahoo has Yahoo Messenger.
Search Engine : Microsoft has Live Search, Yahoo has it’s own search engine.
Email : Microsoft has Windows Live Hotmail, Yahoo has Yahoo Mail.
Let’s wait for Yahoo’s final answer.
And how will Google respond about this
NetBeans 6 T-shirt
by jimmy on Dec.28, 2007, under English, My Life
After waiting for 16 days finally the NetBeans T-shirt arrived.
They sent me this t-shirt because my participation in NetBeans Community Docs program although I only contribute 1 article (this) :p
Thanks to Sun and James Branam (NetBeans Community Docs Manager) for this cool t-shirt ![]()
It’s a great pleasure to be part of the NetBeans community & I would love to contribute more.
Using mock object with jmock 2
by jimmy on Dec.26, 2007, under English, Java, Programming
Okay, finally you decide it’s time to create a unit testing for your project (after a long ad hoc programming life :p).
When you create a sample unit testing, it’s all seem so simple.. you fascinate by the junit easiness, how it can do reflection and make it simple.
When you want to create a real unit testing for your project, you realize that a ‘real’ method is not like just adding 2 int argument and return a result. It’s more complex and using interface as parameter is common…
Now a thought cross your mind “do I have to create all stupid classes to implement all the interface I need?”, you starting to think that creating unit testing is really waste of time & you don’t want to do it anymore :p
It’s time mock object framework come to rescue… before you fall to the darkness of untested code ![]()
There’re several mock object framework like jMock, easymock, etc
Here’s an example creatin HttpMethod mock-object with jMock 2 & JUnit 3
[sourcecode language='java']
public class sampleJMockTest extends MockObjectTestCase {
public void testCreateHTTPMethod() {
final HttpMethod httpMethod = mock(HttpMethod.class);
checking(new Expectations() {
{
allowing(httpMethod).getResponseBodyAsString();
will(returnValue(”sample response”));
}
});
SomeObject someObject = new SomeObject();
someObject.someMethod(httpMethod);
}
}[/sourcecode]
This sample will create an instance of HttpMethod (which is an interface) and when this mock object’s ‘getResponseBodyAsString’ method called it’ll return “sample response”.
So now we can easily create all interface implementation we need.Of course there’re more in jMock than just this simple feature, check it more at jMock Cookbook
Java – Adding new Classpath at Runtime
by jimmy on Dec.19, 2007, under English, Java, Programming
There are cases where we need to add classpath at runtime. For example adding jar to classpath, but depends on configuration or some logic at runtime.
The class who responsible for handling classpath list is URLClassloader.
You can get current system URLClassLoader with:
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
If you check javadoc about this class you’ll see:
protected void addURL(URL url)
Appends the specified URL to the list of URLs to search for classes and resources.
So you’ll just need to call the addUrl method to add new path/jar file.
But wait a minute, the method is protected…
do I need to create a subclass or put the caller class on same package?
Creating a subclass is one way to do it, but there’s simpler way (I’ll create article about creating URLClassLoader subclass next time :p)
Here come the savior : Reflection
By using reflection we can break OOP concept about encapsulating method. It’s feel good to break the rules right (warning : use this at your own risk, you know you’re breaking the OO rule. So I won’t responsible if your computer explode or you’ll fighting with OO fans because of this)
Here a sample how to called it with reflection:
[sourcecode language='java']
public static void addPath(String s) throws Exception {
File f = new File(s);
URL u = f.toURL();
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod(”addURL”, new Class[]{URL.class});
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u});
}[/sourcecode]
Happy reflecting..

