Monday, May 16, 2011

Log4j SMTPAppender and deadlocks - Adding Timeout support

On the post: Sending Email alerts with Log4j we saw how we can easily send mails whenever our system has an exception. But we have to remember that using Apache Log4j SMTPAppender, can be very risky if not used cautiously. In general, the Log4j SMTPAppender has 2 main problems:

  1. The part that send the mail, is synchronous. In fact, it is synchronous between all Log4j appenders. It means that when you log an error and a mail is being sent, all log commands are locked. This is quite risky. Especially when your system has many errors (for whatever reason). This may cause your whole system to get stuck. This issue can be easily handled by using: AsyncAppender. I may write about AsyncAppender in more detail in the future.
  2. The code responsible for sending the mail, which is written by Apache developers, doesn't has a Timeout. That mean, a mail being sent can be stuck forever and simply cause your entire system to go into a deadlock.
    In this post, we will improve the Apache SMTPAppender to include a Timeout property. The Timout property will make sure, that if a mail is being sent using SMTP connection for too long, it will be dropped. We may loose a report about an exception, but we will make sure our system won't be stuck indefinitely.


We will create a new class name: SMTPAppenderTimeout, that extends SMTPAppender. This new class will override the SMTPAppender method: createSession.
The new createSession method will make sure to add the following 2 properties to the mail session: 

  • mail.smtp.connectiontimeout
  • mail.smtp.timeout


These 2 properties instruct the Java mail framework to set a timeout on the SMTP connection.
Let's have a look on the SMTPAppenderTimeout class:

package com.bashan.blog; 
import org.apache.log4j.net.SMTPAppender; 
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import java.util.Properties; 
/** 
* @author Bashan 
*/ 
public class SMTPAppenderTimeout extends SMTPAppender { 
  private int timeout; 
  public int getTimeout() { 
    return timeout; 
  } 
  public void setTimeout(int timeout) { 
    this.timeout = timeout; 
  } 
  @Override 
  protected Session createSession() { 
    Properties props; 
    try { 
      props = new Properties(System.getProperties()); 
    } catch (SecurityException ex) { 
      props = new Properties(); 
    } 
    if (timeout > 0) { 
      String timeoutStr = Integer.toString(timeout); 
      props.setProperty("mail.smtp.connectiontimeout", timeoutStr); 
      props.setProperty("mail.smtp.timeout", timeoutStr); 
    } 
    if (getSMTPHost() != null) { 
      props.put("mail.smtp.host", getSMTPHost()); 
    } 
    Authenticator auth = null; 
    if (getSMTPPassword() != null && getSMTPUsername() != null) { 
      props.put("mail.smtp.auth", "true"); 
      auth = new Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
          return new PasswordAuthentication(getSMTPUsername(), getSMTPPassword()); 
        } 
      }; 
    } 
    Session session = Session.getInstance(props, auth); 
    if (getSMTPDebug()) { 
      session.setDebug(getSMTPDebug()); 
    } 
    return session; 
  } 
} 

You can also download the SMTPAppenderTimeout.



Let's see an example of a log4j.xml file which use SMTPAppenderTimeout to allow timeout of 5 seconds (5000ms):       


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	
	<appender name="RollFile" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="C:\\testlog.txt" />
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="1" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{HH:mm:ss} %-5p [%c{1}]: %m%n" />
		</layout>
	</appender>
	<appender name="Email" class="com.bashan.blog.SMTPAppenderTimeout">
		<param name="BufferSize" value="10" />
		<param name="SMTPHost" value="smtpout.secureserver.net" />
		<param name="SMTPUsername" value="test_user" />
		<param name="SMTPPassword" value="test_password" />
		<param name="Timeout" value="5000" />
		<param name="From" value="someone@mail.com" />
		<param name="To" value="bashan@mail.com" />
		<param name="Subject" value="System Error Notification" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n" />
		</layout>
	</appender>
	
	<root>
		<priority value="info" />
		<appender-ref ref="RollFile" />
		<appender-ref ref="Email" />
	</root>
</log4j:configuration>

You can also download the log4j.xml.




Sunday, April 24, 2011

Nikon D7000 vs Nikon D300 or should I wait for the Nikon D400?

I was waiting for the Nikon D400 to come out for too long. It has been more than 4 years since I bought my Nikon D300. I love this camera. But it has gotten too old. I wanted something new and the Nikon D400 doesn’t seem to show any signs of coming out. So I got tired of waiting, and just bought the Nikon D7000. I wanted to share some of my feelings of my new Nikon D7000 and do a small comparison with my good and old Nikon D300, so those of you who own the Nikon D300 could have an easier decision regarding purchasing the Nikon D7000. So, before comparing the Nikon D7000 and Nikon D300, I can spare you from reading and tell you that I simply love my new Nikon D7000 and I do recommend you go and buy one. If you own a Nikon D300, you won’t regret it. Although the Nikon D7000 is less pro, it’s newer technology definitely puts it in a better place than Nikon D300 and even Nikon D300s and all for that for less price. OK, so let’s start to see what is so good on the Nikon D7000:

Focus System:

Nikon D300 has 51 focus points from which 15 are cross-type. 51 focus points are a lot, and the Nikon D300 has great focus system. It is fast, accurate and allows you to do continuous and even tracking focus. But, although the Nikon D7000 has less focus points: 39 from which 9 are cross-type, it has very fast and accurate focus system. I think even faster and better than the one of the Nikon D300. You don’t have anything to worry about, all the focus options from the Nikon D300 exist in the Nikon D7000.

Controls:

That’s one of the things I worried about when I wanted to buy the Nikon D7000. After so much time that I got used to all the fast and easy on-camera controls of the Nikon D300, I worried that I will really miss them on the Nikon D7000. Well, now that I got the Nikon D7000, I can tell you that I don’t miss those buttons much. I mean, it was nice to have these buttons there, but you can easily manage without them on the Nikon D7000. Switching the ISO (I usually choose the ISO manually), White Balance (I almost never change it from Automatic), and metering mode are all all done same as the Nikon D90/D80. But, there is a new button, placed on the focus AF/M selection switch. This new button along with the two front and back wheels allows you to quickly and easily select your desired focus mode. This new cool button, makes it easier to forget those loveable focus switches on the Nikon D300.

Viewfinder:

The Nikon D7000 has 100% viewfinder coverage. comparing to the 95% viewfinder coverage of the Nikon D300, you can see entirely what you are going to shoot. It’s the first time I am using a 100% coverage viewfinder and I like it. Better image

Quality:

The Nikon D7000 has better image quality. You have to look at the results and decide for yourself. In my opinion the results are better. Although the Nikon D7000 has 16.2 MP sensor (comparing to the Nikon D300), the image quality is better at all ISO ranges. A sensor containing more pixels, means smaller pixels. Smaller pixels mean more noise at higher ISOs. But the D7000 seems to be dealing better with noise than the D300. The Nikon D7000 has better color depth: 24bit comparing to 22bit. That’s about x2.5 more colors. In addition, the Nikon D7000 has better dynamic range: 14EV comparing to 12EV of the Nikon D300. That’s more 2 f-stops on the D7000.

Continuous Shooting:

The Nikon D300 shoots 7 frames per second. The Nikon D7000 shoots 6 frames per second. That makes the Nikon D300 a bit better when it comes to continuous shooting. There are times when continuous shooting can be a great help, but the difference between 6 to 7 frames per second is not that big. In addition, when you set the Nikon D300 D-Lightning feature, or you set your ISO to be 800 or higher (when high ISO cleaning option is turned on), the processing is too slow, so after about 8 photos, the shooting rate goes down dramatically. The D7000 doesn’t seem to suffer from this issue.

Shutter Noise:

One of the most annoying things of the Nikon D300 is it’s shutter noise. This is really an extreme noise. I don’t know how Nikon released a camera with such a strong shutter noise. Well, on the Nikon D7000 you don’t have to worry about noise. The shutter makes much less noise, even when shooting continuously. If you want even less noise you have a special “quite” mode that will reduce the noise even further.

Memory Slots:

The Nikon D7000 has 2 memory slots. Additional memory slot means more memory. More memory means you can store more photos. This is good when you shoot on RAW mode which takes considerably more space for each photo, or you shoot 1080p video which takes a lot of space. You can also use the 2 slots to function as backup (each photo is stored on 2 cards). Video: Well, I don’t shoot video. But with the Nikon D7000 you can easily shoot 1080p Video with 24 fps. You shoot the video using your good Nikon lenses, so you can get great results with shallow depth of field. The video is shown on the back screen and there is an automatic focusing system that can identify faces. It is working very nice. You can also edit the video on the camera.

Size and Weight:

The Nikon D7000 is smaller in size and lighter in weight than the Nikon D300, but it still has a strong grip and it feels good in your hands. It is not well built as the Nikon D300, but it still has a very satisfying built quality Price: Event today, after more than 4 years that the Nikon D300 exists in the market (I believe Nikon has already stopped producing it), it is sold for about more than $200 than the Nikon D7000.

Summary:

I own the Nikon D7000 for only 2 days. I have taken with it only 200 photos. It may be a short period for solid opinion, but so far I am very satisfied with it. If you own a Nikon D300 or Nikon D300s I think the Nikon D7000 is a good upgrade choice for a decent price. Since the Nikon D400 is still not on the horizon (with the recent earthquake on Japan, it will probably event take longer), and it seems like it is going to be with the same sensor of the Nikon D7000 but with more focus points and stronger body, I think it is not worth the time and money waiting for the Nikon D400.

Sunday, April 10, 2011

Check if Virtual Box / VMWare can run 64bit Operating Systems

VirtualBox and VMWare are great virtualization software allowing you to easily run virtual operating systems on your machine.

Creating a new virtual machine is an easy task. You create a new virtual machine, set some parameters and you are ready to go. You get a new place to install any operating system of your choice.

Sometime we would like to install 64bit operating system. 64bit operating system, can take advantage of 64bit machine and do some of the operations almost twice faster than 32bit. Todays modern computers are mostly support 64bits. If your machine support 64bit, it means you can install 64bit operating system on it. Buy, it doesn’t mean you can install 64bit operating in a virtual environment. In order to be able to run 64bit operating system on VirtualBox and VMWare you should have a machine that support it. Running a 64bit operating system in a virtual environment requires hardware level support.

VMWare made a small utility that helps you to check if your machine supports installing 64bit operating systems.

The tool is called: Processor Check for 64-Bit Compatibility and you can find on this link.

If your machine allows you to run 64bit operating systems on virtual machines you will get this message:

vmware-1

And if your if your machine doesn’t allow you to run 64bit operating systems on virtual machines you will get this message:

vmware-2

Note that I run this utility on 2 different machines, both are 64bit capable, both have Windows 7 64bit installed. One machine allows running 64bit operating systems on virtual machines and one doesn’t allow.

Tuesday, April 5, 2011

Convert image file .img to .iso

Both “.img” and “.iso” are file formats contains data packed together and ready to be burned.
I recently had to convert “.img” file to “.iso” file. after a little search I got to this small piece of software called Magic ISO.
After a small installation process and loading the application go to “Tools” and select “Convert…” from the menu:

magiciso1


The dialog is open, simply select the “.img” file you would like to convert and set a new directory to locate the outputed “.iso” file (only if you would like a different directory than the one of the “.img” file:

magiciso2

Wednesday, March 30, 2011

Automatically delete Tomcat access log old files

On the post Enable Tomcat acess log we saw how Tomcat can be easily configured to log all HTTP requests coming to our server.
For some reason, Tomcat doesn't have a mechanism that also makes sure to delete old access log files. This is a bit weird, because I assume that Tomcat is using Log4j in order to log the requests, and Log4j has a built in mechanism for rotating and deleting old log files.
Anyway, since Tomcat doesn’t take care of all these access log files that are being added, and since a typical production server may produce access log files of several giga per day, your disk might get out of space pretty quick.
we will build a small class that will handle old access log files deletion.
First, we need to get the following information:
  • Tomcat access log files directory.
  • Access log file structure. The default structure is: localhost_access_log.yyyy-MM-dd.txt
  • Access log date format: The default structure is: yyyy-MM-dd
  • Number of access log files we would like to keep as backup. For example, if we choose: 10 it means that we would like to keep the last 10 access log files.
The idea is very simply:
  • We get all access log files on the directory.
  • We extract the date of each log file.
  • We sort all the files on ascending order.
  • We delete the oldest files making sure to keep the requested number of backups.
Let’s have a look at our access log files deletion class. It’s code is quite simple:
package com.bashan.blog.log;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FilenameFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Bashan
*/
public class TomcatAccessLogCleaner extends TimerTask {
  private static final Log log = LogFactory.getLog(TomcatAccessLogCleaner.class);
  private static final String DEFAULT_LOG_FILE_PATTERN = "localhost_access_log\\.yyyy-MM-dd\\.txt";
  private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
  private String dateFormat;
  private Pattern logFilePat;
  private File logFileDir;
  private int numBackups;
 
  public TomcatAccessLogCleaner(File logFileDir, int numBackups) {
    this(logFileDir, DEFAULT_LOG_FILE_PATTERN, DEFAULT_DATE_FORMAT, numBackups);
}
 
  public TomcatAccessLogCleaner(File logFileDir, String logFilePattern, String dateFormat, int numBackups) {
    this.dateFormat = dateFormat;
    this.logFileDir = logFileDir;
String pat = logFilePattern.replace(dateFormat, "(.+?)");
logFilePat = Pattern.compile(pat);
    this.numBackups = numBackups;
}
 
  public void clean() {
log.info("Starting to clean old Tomcat access logs. Number of backups to keep: " + numBackups);
File[] files = logFileDir.listFiles(new FilenameFilter() {
      public boolean accept(File dir, String file) {
        return logFilePat.matcher(file).matches();
}
});
List<LogFile> logFiles = new ArrayList<LogFile>(files.length);
    for (File file : files) {
      try {
LogFile logFile = new LogFile(file, logFilePat, dateFormat);
logFiles.add(logFile);
}  
      catch (ParseException pe) {
}
} 
 
Collections.sort(logFiles, new Comparator<LogFile>() { 
@Override
      public int compare(LogFile o1, LogFile o2) {
        return o1.getLogDate().compareTo(o2.getLogDate());
}
});
 
    int numFilesToClean = logFiles.size() - numBackups;
    int removed = 0;
    for (int i = 0; i < numFilesToClean; i++) {
LogFile logFile = logFiles.get(i);
log.debug("Deleting access log file: " + logFile);
      if (!logFile.getFile().delete()) {
log.warn("Failed deleting log file");
}
}
log.info("Finished cleaning old Tomcat access logs. Total log files: " +
logFiles.size() + ". Deleted: " + removed + " of " + Math.max(0, numFilesToClean));
}
 
public static class LogFile {
  private File file;
  private Date logDate;
  public LogFile(File file, Pattern pattern, String dateFormat) throws ParseException {
Matcher matcher = pattern.matcher(file.getName());
    if (matcher.find()) {
String dateStr = matcher.group(1);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
logDate = sdf.parse(dateStr);
      this.file = file;
}
}
 
  public File getFile() {
    return file;
}
 
  public void setFile(File file) {
    this.file = file;
} 
 
  public Date getLogDate() {
    return logDate;
}
 
  public void setLogDate(Date logDate) {
    this.logDate = logDate;
}
} 
 
  public void run() {
clean();
}
}
Note that TomcatAccessLogCleaner extends TimeTask. This enables easily using this class with a timer, allowing it to run every fix interval, and clean Tomcat access log files.
You can download this class here.
Let’s have a look of an example, showing how this class can be used in a servlet. The class will be scheduled to run automatically every 24 hours and clean old access log files. The class will be scheduled once when servlet loads. Don’t forget that you have to map servlets on your web.xml:
package com.bashan.blog.log;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.File;
import java.util.Timer;
/**
* @author Bashan
*/
public class TomcatAccessLogCleanerServlet extends HttpServlet {
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
Timer time = new Timer();
TomcatAccessLogCleaner tomcatAccessLogCleaner = new TomcatAccessLogCleaner(new File("c:\\tomcat access log dir"), 10);
time.scheduleAtFixedRate(tomcatAccessLogCleaner, 0, 1000 * 60 * 60 * 24);
}
}
You can download the servlet here.

Monday, March 28, 2011

List of Mobile Devices and Brands as SQL inserts

I compiled a list of all mobile vendor (brands like Apple, Nokia) and models (mobile devices like iPhone, Galaxy S) as SQL inserts.
This list is a good place to start, but mobile devices are being added all the time. If you have a web application that needs to show list of mobile devices this list can help you. Note that the table contains both vendor and model, so it is best to show data in two drop downs: one for vendors and one for models. The drop down of the vendors is used as a filter to the drop down of the models.
This is the structure of the devices table (mySQL style):
CREATE TABLE `device` (
`vendor` varchar(100) NOT NULL,
`model` varchar(100) NOT NULL,
PRIMARY KEY (`vendor`,`model`)
) ENGINE=InnoDB
The list of the inserts if very long. You can download it by using this link.

Tuesday, March 22, 2011

How to Jump to an Anchor using JavaScript

Just like links/urls are a great way of moving between pages, anchors are great way of moving between locations on the same page.
Suppose we would like to move to a specific location on a page above some <div> tag. All we have to do is simply put an <a> tag above that div and give it some name. For example:
<a name="example"></a>
<div>
...
...
...
</div>

How do we jump to this specific location (named: example)? By putting an <a> on the place from which we would like to jump to this location. For example, somewhere else on the page we put:

<a href="#example">Press here to show example</a>

Note, that the name of the anchor for which we would like to jump contains the hash sign (#) as a prefix.

Now, suppose we would like to jump to some location by clicking something other than <a> (anchor) tag. For example by clicking a simple button. How can we jump to that location? Since we are not using anchor tag, we can no longer be directed to that location automatically.

We can use JavaScript in order to accomplish exactly the same behavior. Let’s see how it can be easily done with our button and by setting the “window.location.hash” property :

<input type="button" value="Press here to show example" onclick="window.location.hash = 'example';"/>

Note that on the JavaScript code we no longer need to use the hash (#) prefix.

Monday, March 21, 2011

Automatically Add “www” to your site on Tomcat or: Canonical Hostnames

Sometimes we would like that our website domain will always contain “www”.

I can this of two good reasons (beside of making all your site’s urls neat and unified) for adding “www” to your site urls:

  • To avoid cookie issues: Cookies are being stored for domains. www.example.com and example.com are 2 different domains when it comes to cookies. So if you would like to store some data in a cookie without wondering where did it disappear. Unify all the urls of your site to contain “www” and you are always working on the same domain.
  • To avoid security issues: Sometimes we would like to do some JavaScript coding on our site that may require us to be on the same domain (for example: running some JavaScript code from an iFrame on the parent window). Since url with “www” and url without “www” is considered to be a different domain, by making sure all of our urls contain the “www” prefix, we make sure we will not fall in all kind of cross domain security traps.

So, how do we make sure all our urls will always contain the “www” prefix" on Tomcat?

Luckily for us there is a great Java open source project named tuckey that can easily help us to accomplish that task. Tuckey is Url rewrite filter. It rewrites our urls according to set of predefined rules. As you noticed tuckey is doing much more than just adding “www” to our urls, but this is not for the scope of this post. You can learn more about tuckey and download it from this web site: http://www.tuckey.org/.

After you download and install tuckey on your web application (it is well details on the tuckey web site how this thing can be done), you simply have to add this rule to you urlrewrite.xml file:

<rule>
  <name>Canonical Hostnames</name>
  <condition name="host" operator="notequal">^www.mydomain.com</condition>
  <condition name="host" operator="notequal">^$</condition>
  <from>^/(.*)</from>
  <to type="redirect" last="true">http://www.mydomain.com/$1</to>
</rule>

Make sure that this rule is the first rule on your urlrewrite.xml file.

Tuesday, March 15, 2011

iPhone udid Structure

udid is Unique Device Identifier. Every iPhone device has it's own udid.
Other smart phones (Android, Blackberry) also has an id that uniquely identifies the device.
udid value can be easily taken from the class UIDevice by using this simple objective C line:
NSString* udid = [UIDevice currentDevice].uniqueIdentifier;
I am currently working on a mobile installation tracking process. In order to track installations you must compare between the udid of the device when the installed application is first launched.

While conducting tests with udid, I noticed that iPhone udid structure is different when running on X-Code simulator comparing to running on a real device.
Where running on X-Code simulator the value contains the following pattern:

HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH
where "H" stands for Hex number. Note that if there are letters (A-F) they are capital.
For example:
79550EDC-2A86-5BDF-8EE2-219B7FCBD223
When running on a real device the udid value is 40 hex chars. The letters are small. Note that there are no minus ("-") characters used.
For example:

ce43446816952ff4513e1389c5e1d3ac3a856141
I think that the udid is different in the simulator and on a real device because the simulator simply return the udid of the Mac machine and the Mac machine udid string is different than the iPhone device udid.

Getting radio button value from radio group using jQuery

Getting radio button value from a group of radio buttons is an easy task using jQuery. All you have to do is to apply the correct selector. If we have a group of radio buttons named: radioGroup all if we have to do is:
$('input[name=radioGroup]:checked').val();
Let's have a look how it looks on a real HTML page containing 3 radio buttons and a check button. By clicking the “Check Value” button we will open an alert popup showing the selected radio value:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> TEST </title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>  
  <input type="radio" id="inpSmall" name="radioGroup" value="small" checked><label for="inpSmall">Small</label><br>
  <input type="radio" id="inpMedium" name="radioGroup" value="medium"><label for="inpMedium">Medium</label><br>
  <input type="radio" id="inpLarge" name="radioGroup" value="large"><label for="inpLarge">Large</label><br><br>
  <input value="Check Value" type="button" onclick="alert('Radio group value: ' + $('input[name=radioGroup]:checked').val());">
</body>
</html>