home / blog

Tomcat7 server in eclipse with ubuntu

Annoyingly there isn’t one “installation directory” for tomcat on ubuntu, instead it is spread across /var/log, /usr/share /var/lib etc. Eclipse expects one directory with log, conf etc. This can be fixed with a few carefully placed symlinks.

sudo apt-get install tomcat7

# fix directories
cd /usr/share/tomcat7
sudo ln -s /var/lib/tomcat6/conf conf
sudo ln -s /var/log/tomcat7 log
sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
sudo chmod -R a+rwx /usr/share/tomcat6/conf

# allow startup from inside eclipse
/etc/init.d/tomcat7 stop
Posted in geek | Leave a comment

Excel, counting multiple conditions

Say you wanted to count how many red horses there are in this data set.

Animal	Color
Horse	Red
Cat	Yellow
Frog	Green
Frog	Orange
Cat	Yellow
Horse	Red
Rabbit	Blue
Horse	Pink

COUNTIF sounds likely as it can be used to count occurrences of items in a given range, e.g. =COUNTIF(A2:B9, "=Horse") It cannot however be used to evaluate multiple conditions.

Another approach is to use array formulas. Array formulas require magic key-strokes (Ctrl-Shift-Enter) or non-standard syntax, i.e. {} in excel and different things in open office / google apps.

I prefer this solution using the lesser-known SUMPRODUCT() and –() functions. SUMPRODUCT counts numerical rows with matching values. The –() function converts Boolean true and false to 1 and 0 respectively, this allows SUMPRODUCT to do the counting.

=SUMPRODUCT(--(A2:A9="Horse"),--(B2:B9="Red"))

Posted in geek | Tagged | Leave a comment

Java class-loading woes

Another day, another OSGI headache.

JAXB within OSGI.

This won’t work by default. FYI: I’m more of a DOM / XPath fan, but some people insist on using JAXB.

JAXBContext jc = JAXBContext.newInstance("com.adamish.foo.jaxb");

This error is produced.

"com.adamish.foo.jaxb" doesn't contain ObjectFactory.class or jaxb.index

Workaround, manually pass in a class-loader to the overloaded newInstance() method.

ClassLoader cl = com.adamish.foo.jaxb.ObjectFactory.class.getClassLoader();
JAXBContext jc = JAXBContext.newInstance("com.adamish.foo.jaxb", cl);

Resource loading within OSGI.

You may have existing code which loads resources via strings referring to resource names. This will work fine when the class doing the loader is in the same “context” as the resource. With OSGI this might not be the case.

In my nightmare I have resources in one bundle, referenced by XML which are loaded by code in another bundle.

// fine most of the time, but a nightmare with OSGI
public void foo() {
  getClass().getResource("/icons/foo.png");
}

Solutions?

One: Do the resource resolution from the same bundle as the resources. Another variant on this would be to pass an InputStream to foo() instead. This might not always be possible.

// in same bundle as resources, then call foo with resolved resource
URL url = getClass().getResource("/icons/foo.png");
foo(url);

public void foo(URL url) {
   // do stuff
}

Two: Pass in an optional class-loader from the context which has access to the resources.

foo(getClass().getClassLoader());

public void foo(ClassLoader cl) {
  cl.getResource("/icons/foo.png");
}
// default for backwards compatibility
public void foo() {
  foo(getClass().getClassLoader());
}

Three: Put the resources in a fragment bundle. Set the fragment host to the bundle containing the code that does the loading. Node there can only be one fragment bundle per bundle-host.

Other class-loading funnies.

The following two lines are not equivalent, I repeat, not equivalent. The first will successfully resolve foo.png (if it’s in the classpath). The second will not.

getClass().getResource("/foo.png");
getClass().getClassLoader().getResource("/foo.png");

The second getClassLoader() is a different classloader to the one used internally in getClass().getResource(…). It will only accept relative paths. The following _will_ work. Note the removal of the leading slash.

getClass().getClassLoader().getResource("foo.png");
Posted in geek | Tagged , | Leave a comment

More random CLI

Generate random password.

dd if=/dev/random count=8 bs=1  | uuencode -m -

Find executable files that are world-writable – for local privilege escalation exploits…

find /usr -type f -perm -102

FLAC to MP3 conversion on MacOS.

# darwin ports versions of mplayer and LAME if necessary
sudo port install mplayer
sudo port install lame

# FLAC => WAV
mplayer -ao pcm:file=foo.wav foo.flac

# WAV => MP3
lame -q 2 -b 320 foo.wav
Posted in geek | Leave a comment

JNI asynchronous callbacks

If you’re using JNI, you may wish to callback a java method asynchronously. For example event handling from a win32 message pump.

package com.adamish;

public class Foo {
  public native void register();
  public void callback(int val) {
    // do stuff
  }
}

Step one is to have a “register” method which lets the C++ code obtain a reference to the java code to enable the callback.

For efficiently we cache the references to the VM, the object, and the method. This means less work per callback.

For the object reference you must convert the local reference to a global reference. Local references only exist for the lifetime of the JNI bound call. Afterwards they cannot be used, and worse than that attempts to use them fail silently – the java code is simply not called.

// cached refs for later callbacks
JavaVM * g_vm;
jobject g_obj;
jmethodID g_mid;

JNIEXPORT jboolean JNICALL Java_com_adamish_Foo_register
	(JNIEnv * env, jobject obj, jlong hwnd) {
                bool returnValue = true;
		// convert local to global reference
                // (local will die after this method call)
		g_obj = env->NewGlobalRef(obj);

		// save refs for callback
		jclass g_clazz = env->GetObjectClass(g_obj);
		if (g_clazz == NULL) {
			std::cout << "Failed to find class" << std::endl;
		}

		g_mid = env->GetMethodID(g_clazz, "callback", "(I)V");
		if (g_mid == NULL) {
			std::cout << "Unable to get method ref" << std::endl;
		}

		return (jboolean)returnValue;
}

Now, the actual callback using our cached references from the register() method. There’s quite a bit going on here. Since the callback is on another thread the VM context must be attached to the current thread.


void callback(int val) {
	JNIEnv * g_env;
	// double check it's all ok
	int getEnvStat = g_vm->GetEnv((void **)&g_env, JNI_VERSION_1_6);
	if (getEnvStat == JNI_EDETACHED) {
		std::cout << "GetEnv: not attached" << std::endl;
		if (g_vm->AttachCurrentThread((void **) &g_env, NULL) != 0) {
			std::cout << "Failed to attach" << std::endl;
		}
	} else if (getEnvStat == JNI_OK) {
		//
	} else if (getEnvStat == JNI_EVERSION) {
		std::cout << "GetEnv: version not supported" << std::endl;
	}

	g_env->CallVoidMethod(g_obj, g_mid, val);

	if (g_env->ExceptionCheck()) {
		g_env->ExceptionDescribe();
	}

	g_vm->DetachCurrentThread();
}
Posted in geek | Tagged , | Leave a comment

JavaFX hidden VM args

I’ve been trying to find a workaround for a NullPointerException in the Quantum render thread (RT-18645). During the process I looked at what VM args are read. I added a conditional breakpoint on System.getProperty(String) with code “System.out.println(arg0); return false”.

Here are the values I found, sorted and with non-JavaFX options removed.

com.sun.scenario.animation.adaptivepulse
com.sun.scenario.animation.AnimationMBean.enabled
com.sun.scenario.animation.nogaps
decora.purgatory
glass.platform
javafx.animation.framerate
javafx.animation.fullspeed
javafx.animation.pulse
javafx.debug
javafx.embed.isEventThread
javafx.sg.warn
javafx.toolkit
javafx.verbose
javafx.version
nativewindow.ws.name
prism.debug
prism.device
prism.dirtyregioncount
prism.disableBadDriverWarning
prism.forcerepaint
prism.multisample
prism.noFallback
prism.order
prism.printallocs
prism.reftype
prism.showcull
prism.showdirty
prism.shutdownHook
prism.tess
prism.tessaa
prism.trace
prism.verbose
quantum.debug
quantum.pulsedebug
quantum.verbose
Posted in geek | Tagged , | Leave a comment

Static IP addresses with Ubuntu 11.10 regression

Static IP addresses. Things have changed slightly, before (11.04 and earlier) you could have a static IP address AND the DNS entry from your DHCP. Now if you have a static interface in /etc/network/interfaces the /etc/resolve.conf file get clobbered by NetworkManager with no content (apart from comment).

# Generated by NetworkManager

This can be solved by using the dns-nameservers directive. Example

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.20
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameservers 192.168.1.254

Now when the machine comes up the /etc/resolve.conf has the correct content:

# Generated by NetworkManager
nameserver 192.168.1.254
Posted in geek | Leave a comment

Ubuntu 11.10 update – assorted fixes

Waiting for network configuration hang at startup. Caused by directories not being created in setup.

sudo mkdir /run
sudo mkdir /run/lock
sudo mv /var/run/* /run
sudo mv /var/lock/* /run/lock
sudo rm -rf /var/run
sudo rm -rf /var/lock
sudo ln -s /run /var/run
sudo ln -s /run/lock /var/lock

Flashing backlight on Samsung NC10 netbook… More details at samsung-tools

sudo add-apt-repository ppa:voria/ppa
sudo app-get update
sudo apt-get install samsung-backlight

Reduce font size (now much larger by default).

apt-get install gnome-tweak-tool
gnome-tweak-tool

Auto login – now the display manager is light-DM instead of GDM the configuration is different. Also possible via the configuration manager GUI but always more fun at the CLI.

emacs -nw /etc/lightdm/lightdm.conf
[SeatDefaults]
autologin-user=<username>
autologin-user-timeout=0
user-session=ubuntu
greeter-session=unity-greeter

NVidia drivers – seems to have defaulted to the open source version, rather than the proprietary ones. Just run restricted-drivers app to update.

Posted in geek | Tagged , | Leave a comment

Microsoft office programs crashing + wordpad

Bizarre issue at work today which meant that word crashed on startup, and strangely wordpad too, but not excel… More strangely it effected various people at random around the office… It took me + google 5 mins to figure it out but our wonderful IT department at least 2 hours.

The cause? If you’re default printer was a network printer that was switched off.

The solution. Change your default printer.

What IT did? Reinstall office, and started rebuilding peoples profiles!!!

Posted in geek | Leave a comment

Manually starting SAMBA function on OPlay HD2

The “NAS” SAMBA server on the O!Play HD2 can only normally be run from a menu via the remote control, and not used whilst a film is playing. However the server can be manually started as follows allowing transfer of files whilst a film is playing.

Trying 192.168.1.65...
Connected to 192.168.1.65.
Escape character is '^]'.
OPLAYF3 login: root
Password:
/usr/local/bin/idealbt/smbd -D
Posted in geek | Leave a comment