Categories
Java

Synchronizing Reader and Writer Threads

Here are two functions that you should use when you want two threads, producer and consumer, to be synchronized. I used these functions mainly to ensure that the reader will stop until an object is ready to read. An advantage is that you can control how many objects are in memory at the same time.

 
protected List<Object> availableObjects = new ArrayList<Object>();
 
/**
 * Delivers the next object.
 * Used by the reader/consumer thread.
 */
public synchronized Object next() {
	Object rc;
 
	while (availableObjects.isEmpty()) {
		try {
			wait();
		} catch (InterruptedException e) { }
	}
	rc = availableObjects.remove(0);
 
	notify();
 
	return rc;
}
 
/**
 * Adds a new object to the list of available objects.
 * Used by the writer/producer thread.
 */
public synchronized void addObject(Object o) {
	while (availableObjects.size() >= 20) {
		try {
			wait();
		} catch (InterruptedException e) {}
	}
	availableObjects.add(o);
 
	notify();
}

The main idea was taken from Silberschatz’ book about Operating Systems. You have to make sure that you never call the next() method when the last object was read. So be careful when your number of objects produced is limited.

Categories
Linux

Firefox 3 crash on Linux with Flash

Since I installed the latest Firefox version on my openSuSE 11 box, it started to crash constantly on some websites, e.g. Jon Stewarts Full Episode player or the German news channel of Tagesschau. A lot of Google research did not reveal anything useful. Some users reported advance when de-installing AdBlock. That didn’t work, though.

At some forum I read that there are conflicts with pulseaudio installation together with flashplayer. So I deinstalled all pulseaudio packages. I had to restart my Gnome Desktop Manager (gdm). But finally it worked out. All flash videos now play perfectly, Jon Stewart as well as Tagesschau.

So you might want to give that a trial.

Categories
Linux

VMWare with USB devices on Linux

VMWare Server depends on USBFS information to recognize USB devices and forward its communication to the virtual machine. OpenSuse switched off this feature by default. In order to re-enable it, you just need to make a slight change in your /etc/fstab file:

usbdevfs /proc/bus/usb usbfs auto 0 0

Usually the line already exists with noauto. Just change it as displayed above. This will mount the filesystem automatically at system start. If you don’t wanna reboot, you can mount the filesystem immediately with:

mount usbfs

PS: This will propably work on every Linux distribution. A detailed description on this topic can be found at old openSuse’s Wiki.

Categories
Applications

Firefox 3

It is finally there. You can download the browser’s latest version at Mozilla Homepage. There is also a very good introduction to the new features of Firefox 3.

Categories
Linux

Laptop Touchpad Configuration

I am usually annoyed by the tap mechanism of my Lenovo Thinkpad. Luckily, there is a solution to switch that feature off. All you need is described here: http://wiki.gentoo.org/wiki/Synaptics

Categories
Miscellaneous

Airline Codes

Another list from my daily work. All IATA and ICAO airline codes along with the official airline’s name can be found in this CSV list.

Categories
Miscellaneous

Firefox: Enabling local links

By default, Firefox has disabled local links in websites. So no network site can link a file on your local filesystem. However, sometimes you want to enable that cause files are shared among project groups on shared folders mounted on your local PC. Here is how you enable these links to open.

Go to your Firefox profile directory. Usually this is at

Documents and Settings\myloginname\Application Data\Mozilla\Firefox\Profiles\somehash

and open user.js. You might need to create this file. Append or change the following lines accordingly:

1
2
3
user_pref("capability.policy.policynames", "localfilelinks");
user_pref("capability.policy.localfilelinks.sites", "http://webaddress1.com http://webaddress2.com");
user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");

Line 1 defines a name for the policy. You can actually set any name you want. localfilelinks is a good choice though.

Line 2 defines the websites that you want to enable to have local links enabled. Be careful to include only sites that you really trust. Multiple sites are separated by spaces.

Line 3 is the actual policy setting that allows these sites to load all URIs.

Categories
Typo3

Replacing TYPO3 Marker by Page Title

This short TYPO3 script snippet will replace a marker in your template by the current page’s title:

page.10.marks.MYMARKER = TEXT
page.10.marks.MYMARKER.field = title
Categories
Java

Configuring logging in a Java web application

Here is a short HOWTO for making some initial configuring when using Commons logging or log4j. Additionally this post will describe how to set the default locale in a servlet environment.

First you will need to create a new class derived from HttpServlet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package mypackage;
 
import java.util.Locale;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
 
/**
 * Initializes log4j so it reads its configuration from WEB-INF directory and sets default locale.
 * @author Ralph Schuster
 *
 */
public class LogConfiguratorServlet extends HttpServlet {
 
   /**
    * Serial ID.
    */
   private static final long serialVersionUID = -5756062055681369027L;
 
   private static final String DEFAULT_FILE = "WEB-INF/log4j.properties";
 
   private static final String DEFAULT_LOCALE = "en_US"; // You can use "en", too.
 
   /**
    * Initializes log4j and sets default locale.
    */
   public void init() {
      // Do the log4j configuration
      String prefix =  getServletContext().getRealPath("/");
      String file = getInitParameter("config-file");
      // if the config-file is not set, then no point in trying
      String s = null;
      if (file != null) {
         s = prefix+file;
      } else {
         s = prefix+DEFAULT_FILE;
      }
      PropertyConfigurator.configure(s);
      LogFactory.getLog(getClass()).debug("log4j configuration file: "+s);
 
      // Do the locale configuration
      s = getInitParameter("locale");
      if (s == null) s = DEFAULT_LOCALE;
      Locale available[] = Locale.getAvailableLocales();
      for (int i=0; i&lt;available.length; i++) {
         if (available[i].toString().equals(s)) {
            Locale.setDefault(available[i]);
         }
      }
      LogFactory.getLog(getClass()).debug("Default locale set to: "+Locale.getDefault());
   }
 
   /**
    * Does nothing.
    */
   public void doGet(HttpServletRequest req, HttpServletResponse res) {
   }
 
}

You then need to deploy the class in your servlet container and adjust the web.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   &lt;servlet&gt;
      &lt;/servlet-name&gt;log4j-init&lt;/servlet-name&gt;
      &lt;servlet-class&gt;mypackage.LogConfiguratorServlet&lt;/servlet-class&gt;
 
      &lt;init-param&gt;
         &lt;param-name&gt;config-file&lt;/param-name&gt;
         &lt;param-value&gt;WEB-INF/log4j.properties&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
         &lt;param-name&gt;locale&lt;/param-name&gt;
         &lt;param-value&gt;de_DE&lt;/param-value&gt;
      &lt;/init-param&gt;
 
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;
Categories
Protocols

HTTP headers for binary file downloads

In case you need to send files from a webserver, use these HTTP headers:

Content-Type:application/octet-stream
Content-Disposition:attachment;filename=filename
Content-Length:100

Of course, you need to set your own content type, filename and file length. 🙂