Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

December 28, 2009

Call Stack Details in Java

As I get more into Android and back into Java, I wanted to know if there was a way to find out where a method was called from. It seems that the StackTarceElement class can be used to get details about the call stack. I found a great article on this blog showing me how to to do this.

I made one addition to the code found on this blog:


public static String getCallStack(int skip)
{
StringBuilder sb = new StringBuilder();

StackTraceElement[] stackTraceElements =
Thread.currentThread().getStackTrace();

for (int i = 3 + skip ; i<stackTraceElements.length; i++)
{
StackTraceElement ste = stackTraceElements[i];
String classname = ste.getClassName();
String methodName = ste.getMethodName();
int lineNumber = ste.getLineNumber();

sb.append(classname + "." + methodName + ":" + lineNumber + "\r\n");
}

return sb.toString();
}

December 14, 2009

Java and POP3 with Hotmail

A simple example as we learn more on the mail protocols:

package email;

import javax.mail.*;

public class MailUtils {

public static Store getStore(Account account) throws MessagingException {
return getStore(account.Email, account.Password);
}

public static Store getStore(String email, String password) throws MessagingException {
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.socketFactory.port", "995");

javax.mail.Session session = Session.getDefaultInstance(props);

Store store = session.getStore("pop3");
store.connect("pop3.live.com", email, password);

return store;
}

public static Folder[] getFolders(Store store) throws MessagingException
{
return store.getDefaultFolder().list("*");
}

public static Folder getFolderByUrl(Store store, String url) throws MessagingException
{
return store.getFolder(new URLName(url));
}

public static Message[] getMessages(Folder folder) throws MessagingException {
try {
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();

for(Message m : messages) {
String s = m.getSubject();
System.out.print(s);
}

return messages;
}
finally {
//folder.close(false);
}
}

}