Wednesday, January 20, 2010

nut & ippon

It was tricky to make nut 2.4.1 work with IPPON Power PRO 1000 using USB interface.

Any driver (blazer_usb, megatec_usb) failed with the message:
Can't claim USB device [xxxx:yyyy]: could not detach kernel driver from interface 0: Operation not permitted

It turned out, that nut came with the broken configuration for udev 150 (on debian). To fix the problem, I commented out all lines in /etc/udev/rules.d/52-nut-usbups.rules, and created a new file with the name /etc/udev/rules.d/46-ippon.rules and the following content:

SUBSYSTEM=="usb", ATTR{idVendor}=="0665", ATTR{idProduct}=="5161", MODE="0664", GROUP="nut"

This helped. Required product id and vendor id are discovered from the output of lsusb program.

PS. The following alias is very useful when configuring something with udev:
alias udevinfo="udevadm info -a -n"
and it can be used like:
udevinfo /dev/sda

Thursday, November 26, 2009

Upgrade of Firefox

After apt-get dist-upgrade I got this "Could not initialize the browser's security component. The most likely cause is problems with files in your browser's profile directory. Please check that this directory has no read/write restrictions and your hard disk is not full or close to full. It is recommended that you exit the browser and fix the problem. If you continue to use this browser session, you might see incorrect browser behavior when accessing security features."

I tried many possible solutions discovered in internet, but the only thing helped was removing of secmod.db file from my profile directory of firefox. I have no idea what this file is for, but it works. If you are having the same problem, backup your ~/.mozilla directory first before trying this or other solution which involves removing of files.

Friday, October 2, 2009

Auto-mount with halevt

halevt is good piece of software but by default it mounts disks in a way very inconvenient for me - that is mount points are like '/media/disk' '/media/disk-1/ '/media/disk-2' and so on. In order to help myself find disks more easily I've reconfigured halevt and created a script. Now I have /media/Cruzer-d4ba-1 and /media/Cruzer-a5e1-1 mountpoints for my two Sandisk USB sticks. And there are SD_Reader-a6a1-1, SD_Reader-a6a1-2, SD_Reader-a6a1-3 (three partitions) for the SD card I've inserted into a reader. A mountpoint name is composed by concatenating storage model name, short version of storage serial id and partition number. This is done by the following script (~/bin/halevt-mount-helper):
#!/bin/sh

STORAGE=`hal-get-property --udi "$1" --key block.storage_device`
PARTITION=`hal-get-property --udi "$1" --key volume.partition.number`
UUID=`hal-get-property --udi "$STORAGE" --key storage.serial | md5sum | head -c 4`
MODEL=`hal-get-property --udi "$STORAGE" --key storage.model | sed 's/ /_/g' | sed 's/^USB_//g'`

MPOINT="$MODEL-$UUID-$PARTITION"

halevt-mount -u "$1" -p "$MPOINT" -o sync -m 007
The script is used by configuring halevt with the following line instead of the default "halevt:insertion" line in halevt configuration file (/etc/halevt/halevt.xml or ~/.halevt/halevt.xml):
   <halevt:insertion exec="halevt-mount-helper $hal.udi$">

More security with ZSH

Don't save commands in history while a secure device is mounted. The mounted device must have .secure file in order to disable history file while the device mounted.

zshaddhistory() {
DIRS=`cat /proc/mounts | rgrep -P '(fuse|ext3|ext2|ext4|fat)' | cut -f2 -d' '`
FLAG=`for dir in ${=DIRS} ; do test -f "$dir/.secure" && echo secure; done`
echo $FLAG | grep secure 2>/dev/null >/dev/null && return -1 || return 0
}

Saturday, August 22, 2009

Constructor annotation in Scala 2.8.0

It was really hard for me to find any information concerning annotations on default constractor. Finally I discovered that scala accepts the following syntax:
class MyClass @Annotation() (val arg : Int) {
}

It is important that an annotation is followed by (), otherwise parameters of MyClass are parsed as parameters of annotation...

Tuesday, August 18, 2009

Richfaces & JSF 1.2

The problem with Richfaces 3.3.1 and Sun JSF RI is that an exception thrown in action listener is suppressed by Richfaces. I will show why this happens. The following stacktrace shows an order of invocations that takes place when an action listener is called:

((1)) at blah.blah.blah.ActionListenerTest.test(ActionListenerTest.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
((2)) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:99)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771)
at javax.faces.component.UICommand.broadcast(UICommand.java:372)
at javax.faces.component.UIData.broadcast(UIData.java:938)
((3)) at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)

I've marked interesting places with ((1)), ((2)) and ((3)). The first interesting method is ((1)) - that is where we throw an unchecked exception. The exception will be catched somewhere between ((1)) and ((2)) and wrapped into the ELException class. Then this exception will be caught in ((2)) as it can be seen here (sources are taken from Sun JSF RI 1.2.12):

public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
if (actionEvent == null) {
throw new NullPointerException();
}

try {
FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
methodExpression.invoke(elContext, new Object[] {actionEvent});
} catch (ELException ee) {
Throwable eeCause = ee.getCause();
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE,
"severe.event.exception_invoking_processaction"
new Object[]{
eeCause == null ? ee.getClass().getName() : eeCause.getClass().getName(),
methodExpression.getExpressionString(),
actionEvent.getComponent().getId()
});
StringWriter writer = new StringWriter(1024);
if (eeCause == null) {
ee.printStackTrace(new PrintWriter(writer));
} else {
eeCause.printStackTrace(new PrintWriter(writer));
}
LOGGER.severe(writer.toString());
}

throw eeCause == null ? new AbortProcessingException(ee.getMessage(), ee) : new AbortProcessingException(ee.getMessage(), eeCause);
}
}
When the processActio methods catches exception it logs it and wraps exception cause into the AbortProcessingException. Here we come to place ((3)) which catches AbortProcessingException. The following is the source code from RichFaces 3.3.1.GA:

public void processEvents(FacesContext context,
EventsQueue phaseEventsQueue, boolean havePhaseEvents) {
FacesEvent event;
while (havePhaseEvents) {
try {
event = (FacesEvent) phaseEventsQueue.remove();
UIComponent source = event.getComponent();
try {
source.broadcast(event);
} catch (AbortProcessingException e) {
if (_log.isErrorEnabled()) {
UIComponent component = event.getComponent();
String id = null != component ? component
.getClientId(context) : "";
_log.error(
"Error processing faces event for the component "
+ id, e);
}
}
} catch (NoSuchElementException e) {
havePhaseEvents = false;
}
}
}
As you can see processEvents catches AbortProcessingException, logs it and does nothing! That is it!

I've tried a lot of solutions to solve this, I've tried to solve it with servlets/filters/phase-listeners/action-listeners and so on with no luck! Then I've solved it with last resort - AspectJ. The following aspect does the job perfectly:

@Aspect
public class RichfacesErrorIntercepterAspect {
@Around("call(* javax.faces.component.UIComponent.broadcast(..)) && within (org.ajax4jsf.component.AjaxViewRoot)")

public void callToBroadcast (final ProceedingJoinPoint thisJoinPoint) throws Throwable
{
try {
thisJoinPoint.proceed ();
} catch (final AbortProcessingException e) {
throw new RuntimeException ("Exception in action listener: " + e.getMessage (), e.getCause ());
}
}
}
To weave Richfaces's jar I've used the maven (I use it to build the project anyway):

<plugin>
<groupId>org.codehaus.mojo</groupId>

<artifactId>aspectj-maven-plugin</artifactId>

<configuration>
<complianceLevel>1.5</complianceLevel>
<weaveDependencies>
<weaveDependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>

<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

Sunday, August 2, 2009

Richfaces or Trinidad with Facelets

I upgraded existing project from Java 1.4 to Java 1.5, JSF 1.1 to JSF 1.2, myfaces to sun ri... and decided to leverage a framework providing AJAX for JSF. The first I tried was richfaces. But it worked unstable and I spent 2 days trying to find a reason for it. Then I tried to use trinidad. It was the same. The framework worked but very unstable. For example. I opened a jsf page, if I clicked on a ajaxfied button just immediately after page appeared, then button worked OK. But if I clicked on the button after a while, then the button didn't work, the relevant page part was not updated! It looked like a magick in work. Then I noticed a very strange message which I noticed long time ago but didn't pay any attention to it. The message was "INFO: Facelet[/page/blah.xhtml] was modified @ 14:23:24 AM, flushing component applied...". A quick investigation revealed a root of my problem! It turned out that time in a virtual machine I used as a place for application server was 3 hours less then current time! And so file creation time of files in WAR-file was greater than time in the virtual machine. It looked like facelets framework had some strange algorithm based on current time to reload modified files (and flush components tree). Having set facelets.REFRESH_PERIOD context parameter in web.xml I solved the problem. Both richfaces and trinidad worked fine after that.