Archiv für die Kategorie ‘java’

Is Duke suffering from Bulimia?

Freitag, 24. Juni 2011

a while ago it seemed that Duke was getting fat and lazy, so the Java community started pushing him to work out:

Well, it seems we overdid it… When I met Duke at Jazoon this week I was shocked. Instead of the chubby, good-natured being we know and love, I was facing this odd, shark-shaped shadow of his former self:

Jazoon 2011

Not sure wether he’s bulimic or sick, but he definitely didn’t seem to be very healthy. I was too appalled to say something to him, so I tried to put a good face on the matter, but I think we definitely need to do something about it…

Here are some additional shots to prove his unnatural transformation:

Duke bulimic

Performance Tuning in Munich… and everywhere

Donnerstag, 05. Mai 2011

Summer is coming, and at Eppleton we’re preparing for the launch of our new online training site. Right now we’re preparing everything for the upcoming training with Kirk Pepperdine in Munich next month “Become Proficient in Java Performance Tuning with Kirk Pepperdine” (June 14th-17th).

“This comprehensive four day workshop will provide you with techniques that have been proven to improve your ability to find and fix performance bottlenecks. What you won’t find are tips and tricks that will be obsoleted with the next release of Java. Instead the course focuses on a methodology that has evolved from years of experience solving performance problems found in a variety of languages running on number of different execution platforms. During the course of the seminar we will look at how Java works, tooling to expose performance bottlenecks and a methodology that helps you to decide on which tools should be used. The workshop contains more than a dozen exercises each taken from real world problems and each designed to challenge your thinking.”

This will also the first time we’ll be offering our training via BigBlueButton, the Open Source Web Conferencing platform. If you want to join us in Munich you can register here. In case you have specific questons, or you want to participate via our virtual classroom from your site please contact us directly.

NetBeans Day in Munich, June 18th

Kirk will also stay in Munich for our NetBeans Day on June 18th organized by the XING NetBeans User Group together with Eppleton. He’ll talk about performance tuning with NetBeans and extending VisualVM. Oracle Developer of the Year Adam Bien will do some live hacking showing real world Java EE 6. And, of course, we’ll have plenty of news about the NetBeans Platform as well. So, if you’re in Munich on June 18, come to this free community event to meet your peers offline and learn exciting new things about our favorite IDE and Java desktop application framework!

You can already preregister for the event here. Stay tuned for more information and a detailed schedule.

Hadoop Studio – Cool NetBeans based IDE

Freitag, 21. August 2009

Just found this announcement at Freshmeat:

“Hadoop Studio is a map-reduce development environment (IDE) based on Netbeans. It makes it easy to create, understand, and debug map-reduce applications based on Hadoop, without requiring development-time access to a map-reduce cluster. The studio provides a real-time workflow view of a map-reduce job, which displays the individual inputs, outputs, and interactions between the phases of a map-reduce job. The workflow view of a job updates in real time with the developer’s code changes. It then generates Java sources and compiles them into a binary jar file, which can be run on a normal Hadoop cluster.”

screenshot

via Hadoop Studio | freshmeat.net.

That does look cool, right?

Component creation off EDT is a bug!

Dienstag, 28. Juli 2009

Last week I asked the question wether or not Component creation off the EDT is a bug or just a bad practice:

Toni Epple » Is component creation off EDT really a bug?.

In the beginning component creation off the Event Dispatching Thread was claimed to be OK until around the release of Java 1.4. There is some information on the web, that this has changed, but a real official answer from the Swing Team was missing. Looking at the lively discussion this has spawned here and in another blog, and the different opinions it seems to be a really vital topic. And finally yesterday I received an answer from Josh Marinacci former member of the Swing Team, and now one of the most prominent JavaFX developers:

“As a former member of the Swing Team I’m going to go ahead and say that creating UI objects (not just components) off of the EDT is a bug in your program.

Until the 1.4/1.5 timeframe we said it was okay to create your UI objects off of the EDT in certain cases (main frame not initalized). This was our mistake. We should have said this because it depends on particular implementation details of Swing (the order in which things are initialized internally), and it turns out we were wrong in some cases. Therefore we now say that creating or manipulating any UI object off of the EDT is a bug. Doing so will probably work fine in 99% of the cases, but you have now introduced potential race conditions in your app that may give you strange behavior later on.

One more detail, and this is important. As we work to further improve startup time of the VM and the Swing infrastructure, we will likely change the internals of Swing. This may increase the likely hood that code which used to work fine off of the GUI thread will now break. Therefore it is *very* important that you stay on the GUI thread; and that’s why I say: yes, it’s a bug!

I’m really very glad to finally have a semi-official answer to this. I think this statement should be part of the Swing API Docs. Lots of legacy applications are using component creation off the EDT, and it will be a lot of work to replace this. Most of the time it works fine, but in the long run it might be something that should be changed (also in NetBeans), especially if, like Josh says, it’s going to get worse.

Thanks to everyone who participated in this discussion. It was good to see the different aspects of this, and it’s also good to see that people inside Sun are listening and answering to our concerns.

Is component creation off EDT really a bug?

Donnerstag, 23. Juli 2009

At JavaOne I had some discussions with NetBeans Platform users, whether the way the UI is created in NetBeans is OK or not. The topic came up again when I tried to use substance 5.2 as the LAF for NetBeans. Substance throws an exception when it finds components that are created outside the EDT, while NetBeans does this quite extensively and also intentionally. Obviously, when you create a very large and modular UI, component creation off the EDT is something rather useful. But the rules seem to have changed at one point in time, and if it’s to be considered a bug now, it would have to be discussed if it can be fixed. Everything pointed to that direction and it seemed quite clear, but after I had some discussions with Wade from the Dream Team and later investigated further how this new rule entered the world of Swing, I’m not so sure anymore. So: Let’s get this sorted!

Swing documentation is lacking a clear statement if component creation off the EDT is to be considered a bug. In the beginning the official statement was:

“Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.”

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

So component creation off the EDT was officially considered OK. Only when the component is realized, you had to make sure to only access it from EDT. At one point all the Swing examples were changed to also do component creation on the EDT. Obviously because one of the examples had issues:

“Out of all the demos in the Swing Tutorial, we encountered a problem only in ComponentEventDemo. In that case, sometimes when you launched the demo, it would not come up because it would deadlock when updating the text area if the text area had not yet been realized, while other times it would come up without incident.”
http://web.archive.org/web/20040413005634/http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

I don’t have the old ComponentEventDemo lying around, so I can’t check, but it would be interesting to see what the actual problem was… The above link is not an official document, but sits somewhere in the archives. Still now it’s recommended in the Swing tutorial to create components in EDT:

“Why does not the initial thread simply create the GUI itself? Because almost all code that creates or interacts with Swing components must run on the event dispatch thread. This restriction is discussed further in the next section. ” ( bold text formatting by me. by the way, there is not really a discussion in the next section )

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html

And also in the API Doc the example does that:

http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading

While these statements are recommendations in my eyes, In the meantime this has widely been interpreted as an official statement by sun with the effect, that most Swing developers consider component creation outside EDT as a bug. There’s a huge difference between a recommendation to avoid threading issues and a bug. But today this is even a question asked in Job interviews. And there are tools helping to find this bug. There are even popular libraries, like Substance Look & Feel enforcing component creation on EDT by throwing Exceptions otherwise:

http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=5
http://www.pushing-pixels.org/?p=368

Also people inside the Swing team seem to have adopted this thought, and officially close bugs with this explanation:

“This is not expected to work since about 2003. The rules for threading in Swing were changed to say that all object contruction must be done on the event dispatching thread (EDT). Prior to then object contruction was allowed before the top level(Frame) was realised, but that is no longer the case. Almost all code in Swing assumes it is running on the EDT in a single threaded model, Nimbus LAF is no different. There is documented in the Swing Tutorial at http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html .Also the history is exmplained at http://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/”

http://bugs.sun.com/view_bug.do?bug_id=6718641

Now: is it a bad practice or a bug? We need a clear official statement if component creation off the EDT is a bug or not. Large swing projects like NetBeans are using component creation off the EDT extensively and it would cost a lot of effort to change this:

http://www.netbeans.org/issues/show_bug.cgi?id=168779

So Swing team, what’s the answer to this? Once and for all: Let’s get this sorted!

Update: I had a ( very lively :-) ) discussion with Jonathan Giles on his blog, who has linked to this article, and Josh Marinacci, former member of the Swing Team has commented:

“As a former member of the Swing Team I’m going to go ahead and say that creating UI objects (not just components) off of the EDT is a bug in your program.

Until the 1.4/1.5 timeframe we said it was okay to create your UI objects off of the EDT in certain cases (main frame not initalized). This was our mistake. We should have said this because it depends on particular implementation details of Swing (the order in which things are initialized internally), and it turns out we were wrong in some cases. Therefore we now say that creating or manipulating any UI object off of the EDT is a bug. Doing so will probably work fine in 99% of the cases, but you have now introduced potential race conditions in your app that may give you strange behavior later on.

One more detail, and this is important. As we work to further improve startup time of the VM and the Swing infrastructure, we will likely change the internals of Swing. This may increase the likely hood that code which used to work fine off of the GUI thread will now break. Therefore it is *very* important that you stay on the GUI thread; and that’s why I say: yes, it’s a bug!

Thanks a lot Josh! Not only did you give a clear answer, but also an explanation as to why It’s a bug and not only a bad practice. I really do appreciate this!

Warning: Issue fixed in NetBeans 6.1 :-)

Sonntag, 25. Mai 2008

An issue that really could be quite annoying for NetBeans Platform developers, Issue 96711, has been fixed in 6.1. Sounds like great news, but better have a look at your code so you don’t get in trouble!

The bug, or feature as some might see it, was that some classes were intentionally not loaded from the rt.jar in NetBeans platform applications although they are part of the standard JRE. That can be a good idea in some cases, where there are already newer implementations or your application depends on a specific one (JAXB is a good example). But if you didn’t know about this bug/feature it could be quite strange to have the correct jre in place but the classes simply wouldn’t get loaded. As soon as I had found out why, I liked it as a feature and used it for my Webservice clients. I provided a library wrapper module containing exactly the version of JAX-WS that I needed, and I didn’t have to care which version of java 6 my users had (or torture them with endorsed dirs ).

I just found that out that the bug has been fixed a few days ago when I tried to port an application from 6.0 to 6.1. Suddenly my Webservice clients didn’t work any more, complaining that they need JAXB 2.1 instead of 2.0 which was loaded from the bootstrap classloader. “bootstrap????? what the …?”. Then -again- I tried to find out what went wrong and -again- I had to search a lot until I finally found out that 96711 got me again.

Luckily it’s very easy to get everything to work as intended again. The attachment Jesse Glick has added to the issue shows how to prevent loading the packages via bootstrap:

OpenIDE-Module-Hide-Classpath-Packages: javax.jws.**, javax.xml.bind.**, javax.xml.stream.**, javax.xml.ws.**, javax.xml.soap.**, javax.annotation.**

When I knew the property name I also found some more information here. You simpy need to add this to your library wrapper and all dependent modules will use the classes you provided in the library wrapper instead of the standard ones. Great solution, now it’s definitely a feature and not a bug!
But what would have happened if I had used the latest version of Java 6 to test? The latest edition contains all the jars and I would probably never have recognized that my application suddenly has a bug and I would have shipped the broken version. So watch out and check your code if you have used the same workaround as I did!

Creating Extension Points in NetBeans Platform Applications

Sonntag, 25. Mai 2008

One of the main benefits of the NetBeans platform is the module system. Regardless of which module system is best ( my guess is there will soon be a version of NeBeans that can also run as OSGi bundles ) it’s important to have a system that enables you to create a modular architecture for your application. It’s an invitation to create a clean and maintainable architecture with defined dependencies and to create nice APIs with clearly defined and easy to use extension points. If you follow these principles others can extend your application easily. Maybe the easiest way to provide extension points in NetBeans is via the layer.xml.

In NetBeans modules the layer file is the central configuration file. NetBeans IDE uses the layer a lot to provide extension points for APIs. Objects can be created declaratively there and you can use the lookup to listen for changes. You will use it whenever you create new Actions or TopComponents. This quick tutorial shows how you can provide your own extension points via the layer:

Prerequisites:

  1. NetBeans (I’m using 6.1, but this will also work with older versions)
  2. Create a new ModuleSuite :
    1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Plug-in Modules. Under projects, select “NetBeans Platform Application” or ( “Module Suite Project” on older versions ) and click Next.
    2. In the Name and Location panel, type “layerextensionpoints” in Project Name. Change the Project Location to any directory on your computer, such as c:\mymodules. Click Finish.
  3. Now create four modules inside the suite “extensionpointinterface”, “messagereader”, “messageprovider1″ and “messageprovider2″:
    1. Choose File > New Project (Ctrl-Shift-N) again. Under Categories, select NetBeans Plug-in Modules. Under projects, select Module Project and click Next.
    2. In the Name and Location panel, type the name in Project Name. The default in the wizard should be to create the module underneath the directory where you just created the suite, which is fine. Click Next.
    3. In the Basic Module Configuration panel, replace the Code Name Base with de.eppleton.<modulename>. Click Finish.

Create a Service interface

We will use module “extensionpointinterface” to define an interface that will be used by the Service Providers as well as by the module that uses the extension point. Inside that module create interface “MessageProviderInterface” with the single method getMessage() that returns a String:

[sourcecode language='java']

public interface MessageProviderInterface {

public String getMessage();

}

[/sourcecode]

To make this part of the public API right click the project node, select API Versioning and select the check box of de.eppleton.extensionpointinterface. Now this package is accessible by other modules.

Create Service Provider implementations

Now we need some modules that implement the ExtensionPointInterface. We will use the modules “messageprovider1″ and “messageprovider2″ to do that. To implement the interface they both need a dependency on module “extensionpointinterface”. For each of them do the following:

  1. Right click the project node, select the “Libraries” category.
  2. Click “Add Dependency”.
  3. Select “extensionpointinterface”, and click “OK”.

Now that we have access to the interface in our modules we can implement it. Again for both modules do the following:

  1. Select “New” > “Java Class”.
  2. In the Wizard type “MessageProvider1″ or “MessageProvider2″ in the name field respectively.
  3. Implement the interface. Each of them should provide a different String e.g. “Hello ” in MessageProvider1 and “World!” in MessageProvider2:

8<———–MessageProvider1——————->8

[sourcecode language='java']

import de.eppleton.extensionpointinterface.MessageProviderInterface;

public class MessageProvider1 implements MessageProviderInterface {

public String getMessage() {

return “Hello “;

}

}

[/sourcecode]

8<——————————>8

8<———–MessageProvider2——————->8

[sourcecode language='java']

import de.eppleton.extensionpointinterface.MessageProviderInterface;

public class MessageProvider2 implements MessageProviderInterface {

public String getMessage() {

return “World!”;

}

}

[/sourcecode]

8&lt;——————————&gt;8

In order to make our MessageProviders available as services add these entries in the layer of the two modules. In 6.0 and earlier version the layer should be there anyway. In 6.1 you will need to create the layer.xml yourself:

<ol>

<li>In “Important files” open “Module Manifest”.</li>

<li>In the manifest add this line: OpenIDE-Module-Layer: de/eppleton/messageprovider2/layer.xml or OpenIDE-Module-Layer: de/eppleton/messageprovider1/layer.xml respectively.

That indicates the location where you put the layer.xml later on.</li>

<li>Create the layer.xml files in their respective package. (“New” &gt; “XML” &gt; “XML Document”) , use “layer” in the name field and add:</li>

</ol>

[sourcecode language='xml']

[/sourcecode]

In the layer files <filesystem> add:

[sourcecode language='xml']

[/sourcecode]

and

[sourcecode language='xml']

[/sourcecode]

respectively. This will create an instance of our MessageProviders using the standard constructor. The trick is that those instances will be accessible from outside via the SystemFileSystem. The next step shows how you can access these Services without a module dependency.

<h3 id=”section-VisualDatabaseExplorer-CreateACookieAction”>Find and use Service Providers</h3>

We will use module “messagereader” to display messages from all MessageProviders. To do so we will create a TopComponent:

<ol>

<li>Set a dependency on “extensionpointinterface” as shown above.</li>

<li>Click “New” &gt; “WindowComponent”. “Output” is ok for the location. Make it show on startup by ticking the box.</li>

<li>Enter “MessageReader” for the class name prefix and click “Finish”.</li>

<li>The TopComponent class will open in Design View. Drop a JScrollPane from the palette in the window, make it fill the whole area and add a JTextPane to it.</li>

<li>In the source view add this to the end of the constructor:</li>

</ol>

[sourcecode language='java']

Lookup lkp = Lookups.forPath(“MessageProviders”);

Collection &lt;MessageProviderInterface&gt; coll = (Collection&lt;MessageProviderInterface&gt;) lkp.lookupAll(MessageProviderInterface.class);

for (Iterator&lt;MessageProviderInterface&gt; it = coll.iterator(); it.hasNext();) {

MessageProviderInterface messageProviderInterface = it.next();

jTextArea1.append(messageProviderInterface.getMessage());

}

[/sourcecode]

This will lookup the folder you created via the layer file ( Lookups.forPath(“MessageProviders”) ), search for classes implementing the interface ( lookupAll(MessageProviderInterface.class) ) and call the interface method on all instances. Let’s try it out:

Run the ModuleSuite. You will see the window either displaying “Hello World!” or “World!Hello “. As we can see in this very simple example, the order in which the ServiceProviders are called can be important for the result. So in the next step I will show you a trick to guarantee the correct order:

Sort Service Providers

NetBeans provides a way to sort layer entries. This mechanism is e. g. used to provide an order for actions in menus and toolbars. Since 6.0 (I think) this is done via the position attribute. So this probably won’t work in older versions:

In the layer of the two modules add these entries:

[sourcecode language='xml']

[/sourcecode]

and

[sourcecode language='xml']

[/sourcecode]

respectively.

Now the messages will always be displayed in the correct order “Hello world!”. Simply swap these values to reverse the order of the messages. When you do something like this make sure that you choose your values big enough to add Services in between the initial ones so there’s room for your application to grow. Try what happens when you set the same value for both attributes!

Summary

The intention of this tutorial is to illustrate how simple it is to implement loose coupling via the layer.xml in a NetBeans Platform application. Note that the module that uses the services has no dependencies on the modules that provide the services. And not only does NetBeans provide a very simple mechanism to provide and lookup Services, but also an easy way to declaratively order them.

RefactorIt open sourced – NB6.X Plugin

Dienstag, 08. April 2008

Developers who joined us for the NUGM meeting in munich with the SQE team have already heard the rumours, but now it’s official: RefactorIT has been open sourced.

Sven Reimers just sent me the news: NetBeans Adventures, Java and more: RefactorIt open sourced. As I posted earlier the freeware version of RefactorIT only had a plugin for NB 5.5. Project SQE is now looking for volunteers to help with porting the cool metrics and audits to NB 6.x. Thanks to Sven for working on this great project!

Android: IMAP Inbox Part 3 – Display Message Text

Samstag, 16. Februar 2008

message.PNG

In the last blog entry of this series I explored how to connect to an imap server and show the message subjects in alist. Here’s how to read a selected message. As a prerequisite you’ll need to add the java.awt.datatransfer package to your sources as described here.

Then create a new Activity to show the Message. I named mine “MessageDisplay” and registered it in the AndroidManifest.xml:

8<————————————————->8

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”de.eppleton.mail.imap”>
<application android:icon=”@drawable/icon”>
<activity class=”.ShowInbox” android:label=”@string/app_name”>
<intent-filter>
<action android:value=”android.intent.action.MAIN” />
<category android:value=”android.intent.category.LAUNCHER” />
</intent-filter>

</activity>
<activity class=”.MessageDisplay” android:label=”message”></activity>
</application>
</manifest>

8<————————————————->8

Then implement the class:

8<————————————————->8

package de.eppleton.mail.imap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MessageDisplay extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView resultView = new TextView(this);
Bundle extras = getIntent().getExtras();
String value = “empty”;
if (extras != null) {
value = extras.getString(“message”);
}

resultView.setText(value);
setContentView(resultView);
}
}

8<————————————————->8

It simply displays the message String in a TextView. The trickiest part is to get data from your Main Activity to the subactivity. This is done via the Intent. Data is added in the main Activity to the Intent by calling putExtra(String key, Object value), and retrieved as shown above (in boldface).

Now we can use this view from the main activity (Changed part in boldface):

8<————————————————>8

package de.eppleton.mail.imap;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.mail.Message;
import javax.mail.MessagingException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ShowInbox extends ListActivity {
private static final int ACTIVITY_CREATE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Message[] messages = new Message[] {};
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = getItem(position);
TextView resultView = null;
if (null == convertView || !(convertView instanceof TextView)) {
resultView = new TextView(super.getContext());
}
try {
resultView.setText(message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
return resultView;
}

});
}

protected void onListItemClick(ListView l, View v, int position, long id) {
Message message = (Message) l.getAdapter().getItem(position);
String messageString = readMessage(message);
showMessage(messageString);
}

private void showMessage(String messageString) {
Intent i = new Intent(this, MessageDisplay.class);
i.putExtra(“message”, messageString);
startSubActivity(i, ACTIVITY_CREATE);
}

private String readMessage(Message message) {
String messageString = “”;
String contentType;
try {
contentType = message.getContentType();
if (contentType.startsWith(“text/plain”)
|| contentType.startsWith(“text/html”)) {
Object content = message.getContent();
if (content instanceof String) {
messageString = (String) message.getContent();
} else if (content instanceof InputStream) {
BufferedReader in = new BufferedReader(
new InputStreamReader((InputStream) content));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append(“\n”);
}
messageString = buffer.toString();
} else {
messageString = “Can’t read this!”;
}
} else
messageString = contentType;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return messageString;
}

}

8<————————————————>8

The only interesting thing here is how to receive the event from the list -that is done via overriding onListItemClick-, and how to start the MessageDisplay activity – that is done in showMessage. First the Intent is created, then the message is added to the Bundle via putExtra, and startSubActivity launches MessageDisplay. Actually startActivity should work equally well here, since we don’t implement onActivityResult() yet.

Now you can launch the Application and click the subjects to display the message text.



Android: IMAP Inbox Part 2 – Customizing the ListView

Sonntag, 10. Februar 2008

In a recent posting I showed how to read messages from an imap server and show them in a ListView. So far the ListView only showed the subject line from a String Array. There is no easy way back to the message object, so we need to change this.

I’ve improved this a little bit now after reading this article on how to customize an ArrayAdapter. The getMessages() method now returns the Message [], which is wrapped in the ArrayAdapter. To show the subject line in the list we simply override the getView method:

8<———————————————->8

package de.eppleton.mail;

import javax.mail.Message;
import javax.mail.MessagingException;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ShowInbox extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
Message[] messages = new Message[] { };
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages){
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
Message message =getItem(position);
TextView resultView = null;
if (null == convertView ||!(convertView instanceof TextView)){
resultView = new TextView(super.getContext());
}
try {
resultView.setText(message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
return resultView;
}

});
}
}

8<———————————————->8

And the ImapClient looks like this:

8<———————————————->8

package de.eppleton.mail.imap;

import java.net.ConnectException;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class ImapClient {
static {
Security
.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
static Session session;
static Store store;

public static Message[] getMail() throws MessagingException {
connect();
Folder folder = store.getFolder(“INBOX”);
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
return messages;
}

private static void connect() throws MessagingException {
if (null != session)
return;
java.util.Properties props = new java.util.Properties();
props.setProperty(“mail.imap.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
props.setProperty(“mail.imap.socketFactory.fallback”, “false”);
props.setProperty(“mail.imap.socketFactory.port”, “993″);
session = Session.getDefaultInstance(props);
store = session.getStore(“imap”);
store.connect(“mailserver”,”userid”,”password”);
}
}

8<———————————————->8

Well, the result looks almost the same, except multiline subbjects are supported:

inbox2.PNG