Saturday, April 2, 2016

How to set custom screen resolution in X-Windows

This post will explain step-by-step how to configure custom screen resolutions in X-Windows. This may be useful in situaction when, for example the system is unable to automatically detect all modes supported by monitor, or when we are working on a virtual machine (VMWare, Parallels, etc.) and want to adjust the screen size to the host. To list all available modes we have to open the Terminal (Ctrl+Alt+T) and type the following command:
xrandr
The output will look similar to this:

We can then select desired mode using command:
xrandr -s [selected mode]
If the mode that we are interested in is not listed, it will be necessary to add it manually. The first step is to create a proper modeline using CVT:
cvt 960 1080 60
CVT is a built-in tool which calculates and prints out valid VESA Coordinated Video Timing modeline for given screen resolution and refresh rate.

Once that step is done, it's time to add the mode to the set of valid modes for our output:
xrandr --newmode "960x1080_60.00"   86.00  960 1024 1120 1280  1080 1083 1093 1120 -hsync +vsync
xrandr --addmode Virtual1 "960x1080_60.00"
Note that the params used for the --newmode command are copied exactly from the output of the cvt command. Also, the --addmode operation requires the name of the device the mode will be associated with. In my case it was "Virtual1".



To confirm that the mode has been successfully added, run xrandr again:



That's it. Now we can select the mode using:
xrandr -s "960x1080_60.00"
To delete it from the list use the following commands:
xrandr --delmode Virtual1 "960x1080_60.00
xrandr --rmmode "960x1080_60.00


The above steps explain how to dynamically create and set custom screen resolution. However this will only last until the system is rebooted. To make the changes persistent we will have to use X-Windows configuration files. Here's how to do it:
cd /usr/share/X11/xorg.conf.d
sudo touch 10-monitor.conf
sudo notepadqq 10-monitor.conf
I'm using notepadqq to edit text files, but any other will do as well. Here's the content of the file:
Section "Monitor"
 Identifier "Virtual Monitor"
 Modeline "960x1080_60.00" 86.00 960 1024 1120 1280 1080 1083 1093 1120 -hsync +vsync
EndSection

Section "Screen"
 Identifier "Default Screen"
 Device "Virtual1"
 Monitor "Virtual Monitor"
 DefaultDepth 24
 SubSection "Display"
  Modes "960x1080_60.00"
 EndSubSection
EndSection
/usr/share/X11/xorg.conf.d stores configuration files for the X Server. All files in the directory have specific format and contain various parameters used to configure the system.

Friday, April 17, 2015

Overloading, overriding and method resolution

Today, I'm going to shed some light on the mechanics of method resolution for overloaded and overridden methods in Java. Will start with a small piece of code:

package net.progsign.prep;

class Base {
 public void method(int... params) {     //Method #1
  System.out.println("[B] int...");
 }
  
 public void method(int param1, long param2) {  //Method #2
  System.out.println("[B] int, long");
 }
 
 public void method(int param1, Integer param2) { //Method #3
  System.out.println("[B] int, Integer");
 }
 
 public void method(int param1, Number param2) {  //Method #4
  System.out.println("[B] int, Number");
 }
 
 public void method(int param1, Object param2) {  //Method #5
  System.out.println("[B] int, Object");
 }
 
 public void method(int param1, int... param2) {  //Method #6
  System.out.println("[B] int, int...");
 }
 
 public void method(int param1, long... param2) { //Method #7
  System.out.println("[B] int, long...");
 }
 
 public void method(int param1, Integer... param2) { //Method #8
  System.out.println("[B] int, Integer...");
 }
}

public class OverloadingTest {
 public static final void main(String[] args) {
  Base base0 = new Base();
  base0.method(1, 1);
 }
}
The code, when compiled and executed, will result in the following message printed to the console:
[B] int, long  //Method #2

OK, so what exactly happend, and how did the virtual machine figure out which method should be executed? With reference to Java Language Specification for Java SE 7 Edition, there are three phases of resolving the proper method signature:

Phase #1: performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation.
During this phase the compiler will try to find an exact match (overloaded method which parameters types match the invocation). If no such method is found, the compiler then will try to upcast (widen) the argument types until the closest match is found. Below diagram shows the direction the upcasting will process for primitive types:


For non-primitive types (instances) it will follow up the inheritance tree. If no applicable method is found during this phase then processing continues to the next phase.

Phase #2: performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation.
In this phase the compiler will try to utilize the auto-boxing mechanism introduced in Java 5. If boxing (wrapping primitive type with its istance representation) occurs, then, same as before the whole class hierarchy is taken into account, however the most specific type takes precedence over the more generic types. Example:

package net.progsign.prep;

class Base {
 public void method(int param1, Integer param2) { //Method #3
  System.out.println("[B] int, Integer");
 }
 
 public void method(int param1, Number param2) {  //Method #4
  System.out.println("[B] int, Number");
 }
 
 public void method(int param1, Object param2) {  //Method #5
  System.out.println("[B] int, Object");
 }
}

public class OverloadingTest {
 public static final void main(String[] args) {
  Base base0 = new Base();
  base0.method(1, 1);  //Line #1
  base0.method(1, null);  //Line #2
 }
}
The result will be as follows:
[B] int, Integer  //Method #3
[B] int, Integer  //Method #3

In line marked as Line #1 the second argument was automatically boxed to an Integer. In Line #2 the second argument is null, but since Integer is more specific type than Number and Object, the compiler will favour Method #3 over the other two. If the compiler is still not satisfied, it continues to the third phase.

Phase #3: allows overloading to be combined with variable arity methods, boxing, and unboxing.
As you can see, varargs (short for "variable arguments", also introduced in Java 5) have the lowest priority. This is to ensure backward compatibility with legacy code that doesn't support this feature. Knowing the above rules, there should be no problem with predicting the output of the following code:

package net.progsign.prep;

class Base {
 public void method(int... params) {   //Method #1
  System.out.println("[B] int...");
 }
  
 public void method(int param1, int param2) {  //Method #2
  System.out.println("[B] int, int");
 }
 
 public void method(int param1, Integer param2) { //Method #3
  System.out.println("[B] int, Integer");
 }
}

public class OverloadingTest {
 public static final void main(String[] args) {
  Base base0 = new Base();
  base0.method(1, 1);
 }
}

Of course Method #2 will be invoked first, and in case of its absence the compiler will prefer Method #3 rather than Method #1. Simple enough. OK, what about this code:

package net.progsign.prep;

class Base {
 public void method(short... params) {   //Method #1
  System.out.println("[B] short...");
 }
 
 public void method(int param1, int param2) {  //Method #2
  System.out.println("[B] int, int");
 }
 
 public void method(short param1, double param2) { //Method #3
  System.out.println("[B] short, double");
 }
}

public class OverloadingTest {
 public static final void main(String[] args) {
  Base base0 = new Base();
  base0.method((short)2, (short)5);
 }
}

This is where things get a bit tricky. From earlier we know that Method #1 will be the last to take into consideration. But what about the other two methods? There's section 15.12.2.5. Choosing the Most Specific Method in the JLS that explains the rules of selecting the most specific method signature. Here's what is going to happen in the above code. The compiler will compare the first parameter and will find a perfect match in Method #3. Spot on! But when it gets to the second parameter it will actually lean forward Method #2 (see the upcasting chart). This ambiguity will lead to a compiler error.


Having more fun!

Hope everything makes a bit more sense now. Let's add some petrol to the fire:
package net.progsign.prep;

class Base {
 public void method(int... params) {     //Method #1
  System.out.println("[B] int...");
 }
 
 public void method(long param1, double param2) { //Method #2
  System.out.println("[B] long, double");
 }
}

class Subclass extends Base {
 public void method(int... params) {
  System.out.println("[D] int...");    //Method #S1
 }
 
 public void method(int param1, int param2) {  //Method #S2
  System.out.println("[D] int, int");
 }
 
 public void method(int param1, int... param2) {  //Method #S3
  System.out.println("[D] int, int...");
 }
}

public class OverloadingTest {
 public static final void main(String[] args) {
  Base base0 = new Base();
  Base base1 = new Subclass();
  Subclass subclass0 = new Subclass();
  Subclass subclass1 = (Subclass)base1;
  
  base0.method(1, 1);   //Line #1
  base1.method(1, 1);   //Line #2
  subclass0.method(1, 1);  //Line #3
  subclass1.method(1, 1);  //Line #4
 }
}
And the output will be...
[B] long, double
[B] long, double
[D] int, int
[D] int, int

Right as expected, isn't it? So what have we got here. Class Base declaring two overloaded methods. Also another class, Subclass, with three more overloaded (overriden?) methods.
In the main function we first instantiate Base class, and refer to it by its base type. Then we create an instance of a Subclass class, but we're using its super type as a reference. Also another class is being created and assigned to a proper reference type. And finally another reference of type Subclass is created to point to one of the previously created objects.
I believe Line #1 is clear enough. But what actually happened in Line #2? In Line #1 the compiler resolved Method #2 to be the best match of the Base class. Since we're using the same reference type in Line #2, the same Method #2 was invoked again. Please note that the only overriden method in the Subclass is Method #1 (overriden by Method #S1, so there's no polymorphism taking place in this case. Also, because Base doesn't know anything about Method #S2, which otherwise would be the best candidate, that method is not called. There's no black magic in Line #3 and Line #4.

One last thing to remember: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.

Monday, February 18, 2013

Brainfuck plugin for Eclipse IDE

Features:
  • Brainfuck interpreter
    • launch configurations
    • launch configuration shortcut
  • Brainfuck editor
    • associates with *.b & *.bf files
    • syntax highlighting
    • code hover tool tips
    • loop select on double click
Repository: hg clone https://code.google.com/p/brainfuck-eclipse-plugin/

Enjoy!

Tuesday, November 13, 2012

Local functions prototyping

A trivial piece of C++ source code:
#include <iostream>

class Foo {
    public:
        Foo(int=5);
};

Foo::Foo(int) {
    std::cout << "[Foo]";
}

int main() {
    Foo Foo0();
    Foo Foo1(1);
    return 0; //SUCCESS?
}
And a trivial question: how many times will the Foo constructor execute?

Explanation:
in the above code, line:
    Foo Foo0();

is not a declaration of a variable named Foo0 of class Foo initialized with default constructor. Instead, it is, what can be called local function prototyping. More precisely, it is a declaration of a new function, named Foo0, which returns result of type Foo.

It seems to be legacy code after nested functions, supported by GCC C Compiler. See the example:
#include <stdio.h>

int main() {
 auto int m_nested();
 int m_nested() {
  return 666;
 }

 printf("%d", m_nested());
 return 0;
}
Function m_nested() is nested within another function (main()), and its scope is limited to that surrounding function.

Getting back to the original code, any attempt to access Foo0 will result in error:
int main() {
    Foo Foo0();
    Foo Foo() { //error: a function-definition is not allowed here before '{' token
        //...
    };
    Foo0(); // error: undefined reference to 'Foo0()'
    Foo0 = *new Foo(); // error: cannot convert 'Foo' to 'Foo()' in assignment
}
The only thing, that would make sense and would actually compile is defining this function somewhere else:
//...
int main() {
    // no access to Foo0() here
    Foo Foo0();
    Foo f = Foo0();
    //.. do some stuff with 'f'
    delete &f;
    return 0;
}

Foo Foo0() {
    std::cout << "Foo0()";
    return *new Foo();
}
Such code will narrow access to Foo0() to the place where it was declared for the first time, and will end as soon as the program leaves the execution block.

Monday, February 6, 2012

java.net.URL, content handlers and HTML/XML parsing

Below is an example of using java.net.ContentHandler class while retrieving resources from URL.

The objective is to get traffic stats from a network device. The device may present its status in two ways: as an XML data or human readable HTML page. In this example will use both the sources to get the information.

My network device collects data from different interfaces. The interface may be described as follows:

public class InterfaceStatus {
    private String name;
    private long txPackets;
    private long txBytes;
    private long rxPackets;
    private long rxBytes;
    
    // getters and setters...
}

To override default behaviour of the URL.getContent() method, a custom content handler factory must be created, i.e. class that implements ContentHandlerFactory interface. There's only one method to implement in this interface: public ContentHandler createContentHandler(String mimetype).

import java.io.*;
import java.net.*;

import javax.swing.text.html.parser.ParserDelegator;

import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;

public class UrlContentHandlerFactory implements ContentHandlerFactory {

    @Override
    public ContentHandler createContentHandler(String mimetype) {
        if("application/xml".equals(mimetype)) {
            return new XmlContentHandler();
        } else
        if("text/html".equals(mimetype)) {
            return new HtmlContentHandler();
        }
        // default content handler will be selected by JVM
        return null;
    }
    // ... inner *ContentHandler classes below...
}

The mimetype value is taken from the [JAVA_HOME]\lib\content-types.properties file. Now, it's time for the concrete implementation of the ContentHandler abstract class.

HtmlContentHandler
public class UrlContentHandlerFactory implements ContentHandlerFactory {
    // ...
    protected class HtmlContentHandler extends ContentHandler {

        @Override
        public Object getContent(URLConnection urlc) throws IOException {
            HttpURLConnection conn = (HttpURLConnection) urlc;
            if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // using HTML Editor Kit API
                HtmlTrafficExtractor hte = new HtmlTrafficExtractor();
                new ParserDelegator().parse(new InputStreamReader(conn.getInputStream()), hte, true);

                return hte.getExtractedList();
            }
            return null;
        }
    } // HtmlContentHandler
}

To parse HTML pages, I've used HTMLEditorKit and ParserDelegator from javax.swing.text.html package. Why not to use XML parser? Here's the answer:

<html>
    <!-- head -->
    <body bgcolor=#00cccc>                <!-- no quotation marks around attribute value -->
        <img src="logo.gif" alt="Logo">   <!-- no closing "img" tag -->
    </body>
</html>

Although HTML pages consist of tags, tag attributes, text, etc., just as XML documents do, they don't have to conform to XML specification as illustrated in the above snippet. This would cause unnecessary exceptions being thrown.

XmlContentHandler
public class UrlContentHandlerFactory implements ContentHandlerFactory {
    // ...
    protected class XmlContentHandler extends ContentHandler {

        @Override
        public Object getContent(URLConnection urlc) throws IOException {
            HttpURLConnection conn = (HttpURLConnection) urlc;
            if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // this is where the SAX2 API kicks in
                try {
                    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                    XmlTrafficExtractor te = new XmlTrafficExtractor();
                    xmlReader.setContentHandler(te);
                    xmlReader.setErrorHandler(te);
                    xmlReader.parse(new InputSource(conn.getInputStream()));

                    return te.getExtractedList();
                } catch(SAXException saxe) {
                    System.err.println("Parsing failed due to the following error: " + saxe.getMessage());
                }
            } // if
            return null;
        }
    } // XmlContentHandler
}

The content handler for XML documents is very similar. In contrast to the previous code, it uses SAX2 parser, which is a part of Java environment.

Both APIs use callback objects to parse documents. In HTML Editor Kit, the object must extend static HTMLEditorKit.ParserCallback class, and in SAX2 it is org.xml.sax.helpers.DefaultHandler.

HtmlTrafficExtractor
import javax.swing.text.html.HTMLEditorKit;

public class HtmlTrafficExtractor extends HTMLEditorKit.ParserCallback {
    private List<InterfaceStatus> statusList;

    // overriding essential callback methods here

    public List<InterfaceStatus> getExtractedList() {
        return statusList;
    }
}

XmlTrafficExtractor
import org.xml.sax.helpers.DefaultHandler;

public class XmlTrafficExtractor extends DefaultHandler {
    private List<InterfaceStatus> statusList;
    
    // overriding essential handler's methods here
    
    public List<InterfaceStatus> getExtractedList() {
        return statusList;
    }
}

Both the callback classes provide a method to return a list of available/found interfaces.

And here's how to use the code:

public class TransferStatus {
    private static final String URL_ADDRESS_XML = "http://router/stats/traffic.xml";
    private static final String URL_ADDRESS_HTM = "http://router/stats/netstat.html";

    public static final void main(String[] args) {
        URLConnection.setContentHandlerFactory(new UrlContentHandlerFactory());

        try {
//          Object content = new URL(URL_ADDRESS_XML).getContent();
            Object content = new URL(URL_ADDRESS_HTM).getContent();
            if(content != null && content instanceof List<?>) {
                @SuppressWarnings("unchecked")
                List<InterfaceStatus> statusList = (List<InterfaceStatus>) content;
                for(InterfaceStatus status : statusList) {
                    // doing things with the data
                }
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

First, URLConnection.setContentHandlerFactory() static method is called to set the content handler factory. From now on, every call to URL.getContent() will ask the factory for a proper content handler (if none is found, i.e. the factory returns null, JVM will try to load default handler).
The next step is to check if the content returned is of correct type and further processing of the data.
Now, the only thing that changes in the above code is the resource URL address passed as an argument to the URL() constructor.

Friday, December 16, 2011

Initialization block as a constructor for anonymous class

Although not broadly used, [static] initialization blocks are quite interesting features in Java. They may be used to initialize static and instance fields with own default values, before a constructor kicks in. In byte-code, such blocks are represented by two special methods: void <clinit>() for static initialization block and void <init>() for instance initialization block. There may be multiple declarations of initialization blocks in one class. In such case the code from each block is combined into one of the above methods. All instructions are invoked in the same order as they were declared in source code. Example:

package net.progsign.java6;

public class InitializationBlockTest {
 
 private static char charValue;
 private boolean boolValue;
 private int intValue;
 private String stringValue;
 
 static {
  charValue = '$';
  System.out.println("[clinit] " + charValue);
 }
 
 {
  System.out.println("[init-0] " + boolValue);
  System.out.println("[init-0] " + intValue);
  System.out.println("[init-0] " + stringValue);
  System.out.println();
 }
 
 {
  boolValue = true;
  intValue = 1024;
  stringValue = "default";
 }

 public InitializationBlockTest() {
  System.out.println("[constr] " + boolValue);
  System.out.println("[constr] " + intValue);
  System.out.println("[constr] " + stringValue);
 }
 
 public static final void main(String[] args) {
  new InitializationBlockTest();
 }
}

The output of the above code will be:
[clinit] $
[init-0] false
[init-0] 0
[init-0] null

[constr] true
[constr] 1024
[constr] default

Good code design expects from us to initialize class fields in a constructor. Also, because of the characteristics of initialization blocks, it may cause some confusion when trying to understand the order in which the object is created.
However, there is a case where initialization block may be successfuly used. Examine the following code:

package net.progsign.java6;

interface IFace {
 int method();
}

public class AnonymousConstructor {
 public static void main(String[] args) {
  // anonymous class implementing IFace interface
  IFace foo = new IFace() {
   private int value;
   
   {
    value = 1024;
    init();
   }
   
   public int method() {
    return value;
   }
   
   private void init() {
    System.out.println("<init> called init()");
   }
  };
  System.out.println("[main] foo.method() = " + foo.method());
 }
}

In the above code, I declared an interface IFace and, in the main() method, I created anonymous class that implements this interface. My anonymous class has one private attribute value of type int. Because anonymous classes have no name, thus it's not possible to define own constructor (default non-argument constructor will still be created for the class by the compiler). Without initialization blocks, we would be unable to init the class field with our own values.

When compiled and run, the code will produce the following output:

<init> called init()
[main] foo.method() = 1024

Things to know about [static] initialization blocks:
  • there may be multiple initialization blocks in one class (they run in the order they occur in code)
  • they are invoked before any constructor
  • you can't call constructors (neither super() nor this()) from inside initialization block
  • same as constructors, initialization blocks may throw runtime exceptions (in such case the object will not be created)
  • you can assign default value to fields declared as final, but only if they haven't been initialized at declaration time



PS: noticed quite an interesting behaviour of initialization blocks. See the following code:

package net.progsign.java6;

public class InitializationTest {

 static {
  sfield = 1;
  //System.out.println("<clinit> " + sfield);
  //System.out.println("<clinit> sfield=" + InitializationTest.sfield);
 }
 
 {
  ifield = 2;
  //System.out.println("<init> " + ifield);
  //System.out.println("<init>   ifield=" + this.ifield);
 }
 
 static int sfield = 10;
 int ifield = 20;
 
 public static void main(String[] args) {
  InitializationTest it = new InitializationTest();
  System.out.println("[main]   sfield=" + it.sfield);
  System.out.println("[main]   ifield=" + it.ifield);
 }
}

Will the code compile? What will be the output? What will happen if you uncomment the "System.out.println(...)" lines?

Tuesday, October 11, 2011

OT: Chatting with Windows shell (VBScript)

Just a short memo on how to use WMI (Windows Management Instrumentation) and VBScript to simplify some administration tasks.

Rebooting remote machine (have to have privileges to WMI on the remote host):
Sub Reboot(host)
On Error Resume Next
    Set wmi = GetObject("winmgmts:{(Shutdown)}\\" & host & "\root\cimv2")
    If Err.Number <> 0 Then
        Exit Sub
    End If
    Set osList = wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
 
    For Each os In osList
        os.Reboot()
    Next
End Sub

Checking host availability:
Sub Ping(host)
    Set pingStatus = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus WHERE address = '" & host & "'")
    For Each pingReplay In pingStatus
       If pingReplay.StatusCode = 0 Then
            WScript.Echo "Response: OK [Time (ms)=" & pingReplay.ResponseTime & "/TTL (ms)=" & pingReplay.ResponseTimeToLive & "]"
       Else
            WScript.Echo "No response from host '" & host & "'"
       End If
    Next
End Sub

Mounting network resources:
Sub Mount(folder)
On Error Resume Next
    Set NetworkObj = CreateObject("WScript.Network")
    Set ShellObj   = CreateObject("WScript.Shell")
    NetworkObj.MapNetworkDrive "X:", folder, true', "user", "pass"
    If Err.Number = 0 Then
        ShellObj.LogEvent 0, "Network resource '" & folder & "' mounted"
    Else
        WScript.Echo "Failed (Status code: " & Err.Number & ")"
    End If
End Sub

Thursday, September 29, 2011

Getting power source info from OS X (Objective-C)

I trust any introduction is unnecessary here, so will go straight to the code.
First approach is to use power source support methods from the IOKit/ps package. Here's how to do it:
#import <Cocoa/Cocoa.h>
#import <IOKit/ps/IOPowerSources.h>
#import <IOKit/ps/IOPSKeys.h>
#import "PowerSourceInfo.h"

@interface IOKitPowerSourceInfo : NSObject {

}

- (PowerSourceInfo*) getPowerSourceInfoFor: (int) index;

@end
... and the implementation:
#import "IOKitPowerSourceInfo.h"


@implementation IOKitPowerSourceInfo

- (PowerSourceInfo*) getPowerSourceInfoFor: (int) index {
 CFTypeRef info = IOPSCopyPowerSourcesInfo();
 CFArrayRef sources = IOPSCopyPowerSourcesList(info);
 PowerSourceInfo* psi = nil;

 int numOfSources = CFArrayGetCount(sources);
 if(numOfSources == 0) {
  return nil;
 }
 CFDictionaryRef source = IOPSGetPowerSourceDescription(info, CFArrayGetValueAtIndex(sources, index));
 psi = [[PowerSourceInfo alloc] initWithDictionary:(NSDictionary*)source];

 CFRelease(sources);
 CFRelease(info);

 return psi;
}

@end

Two functions, IOPSCopyPowerSourcesInfo() and IOPSCopyPowerSourcesList() are used to get information from the system and create list of available power sources. Then, by invoking IOPSGetPowerSourceDescription() function with references to our info object and particular source passed as arguments, we get a reference to a dictionary with all information about selected power source provided by vendor.
The key values for the dictionary are stored in IOKit/ps/IOPSKeys.h file. Unfortunately the dictionary does not have to contain values for all the keys as some of them, according to documentation, are optional.

Another apprach is to read system IO registry related to particular power source. The code is as follows:

#import <Cocoa/Cocoa.h>
#import <IOKit/IOKitLib.h>
#import "PowerSourceInfo.h"

@interface IORegPowerSourceInfo : NSObject {

}

- (PowerSourceInfo*) getPowerSourceInfo;

@end
and...
#import "IORegPowerSourceInfo.h"


@implementation IORegPowerSourceInfo

- (PowerSourceInfo*) getPowerSourceInfo {
 io_object_t deviceHandle;
 kern_return_t kernReturn;
 CFMutableDictionaryRef serviceMatch, properties;
 PowerSourceInfo* psi = nil;

 serviceMatch = IOServiceMatching("IOPMPowerSource");
 deviceHandle = IOServiceGetMatchingService(kIOMasterPortDefault, serviceMatch);
 kernReturn = IORegistryEntryCreateCFProperties(deviceHandle, &properties, NULL, 0);
 if(kernReturn == kIOReturnSuccess) {
  psi = [[PowerSourceInfo alloc] initWithDictionary:(NSDictionary*)properties];
 }
 CFRelease(properties);
 IOObjectRelease(deviceHandle);
 return psi;
}

@end

First, we get a dictionary matching IOService class called "IOPMPowerSource". Then we ask the system to return first IOService related to this class. Next step is to invoke IORegistryEntryCreateCFProperties, passing the device handle we just got, address of a pointer which will refer to a dictionary with all registry values of a particular power source. The function returns status code of type kern_return_t to inform whether it succeeded or failed. Finally, we have to release the memory.

In both examples, the PowerSourceInfo class is just a custom wrapper for the returned dictionary that exposes all keys as class methods.

OS X allows us to be notified about any changes that occur in different parts of the system (including power source chanage). To listen for those changes we have to create a RunLoopSource and attach it to current RunLoop. See the code below:

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import <IOKit/ps/IOPowerSources.h>

@interface PowerSourceInfoWorker : NSObject {
 WebView* webView;
 CFRunLoopSourceRef runLoopSource;
}

@property (readonly, nonatomic) WebView* webView;

-(void) startThread;
-(void) stopThread;

void powerSourceChange(void* context);

@end

PowerSourceInfoWorker is just a simple Cocoa class that exposes two messages: "startThread" and "stopThread" which will be used to add and remove our "listener" from system loop. The most important is the void powerSourceChange(void* context). It's a regular C function, which will be the callback from the loop.

NOTE: The WebView* webView attribute in the above code will be used later on to call JavaScript functions from the callback function.

Implementation of the PowerSourceInfoWorker:

#import "PowerSourceInfoWorker.h"


@implementation PowerSourceInfoWorker
@synthesize webView;

-(id) initWithWebView:(WebView*) aWebView {
 self = [super init];
 
 if(self) {
  webView = [aWebView retain];
 }
 return self;
}

-(void) dealloc {
 [self stopThread];
 [webView release];
 [super dealloc];
}

-(void) startThread {
 runLoopSource = (CFRunLoopSourceRef)IOPSNotificationCreateRunLoopSource(powerSourceChange, self);
 if(runLoopSource) {
  CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
 }
}

-(void) stopThread {
 if(runLoopSource) {
  CFRunLoopSourceInvalidate(runLoopSource);
  CFRelease(runLoopSource);
 } 
}

void powerSourceChange(void* context) {
 NSArray* args = [NSArray arrayWithObjects: @"Power Source has changed!", nil];
 id win = [[(PowerSourceInfoWorker*)context webView] windowScriptObject];
 [win callWebScriptMethod:@"jsCallback" withArguments:args];
}

@end

In startThread new IOPS RunLoopSource is being created. The two params are our callback function and context (which in this case is "self"). Then, this newly created source is attached to currenct RunLoop in default mode (see documentation for details about available RunLoop modes).
stopThread is responsible for removing our RunLoopSource from the system loop, by invoking CFRunLoopSourceInvalidate function, and releasing resources.
The callback function simply gets WebView* from the context, which in my case is the PowerSourceInfoWorker class itself (see: startThread), and calls some WebScript method with arguments passed as an array.

And here comes the JavaScript callback function (with one argument):

function jsCallback(msg) {
    document.getElementById('status').innerHTML = msg + ' (' + new Date().toUTCString() + ')';
    //refresh view
}

The code is quite obvious, and I believe does not need any explanation.

There's one big advantage of the latter method. It returns much more information about power sources available in system than the IOPSCopyPowerSourcesInfo. I wrote a simple Cocoa application to present the differences between those two methods. For source code see the following Mercurial repository:
hg clone https://code.google.com/p/osx-battery-info-app/

Saturday, July 9, 2011

The longest class name in Java

The first thing, that I noticed while working with Spring framework (right after its simplicity, purity, good performance, etc.) is that the designers of the framework didn't bother of creating short and simple names which resulted in a set of VeryLongSelfDescriptiveClassNames.

Curious enough, went throught the core Spring API and found two candidates for the title of "the longest class name in Java", and by the longes class name I mean a class name with no package prefix.

Those two candidates are:

AbstractInterruptibleBatchPreparedStatementSetter
AbstractTransactionalDataSourceSpringContextTests
both containing impressing 50 characters.

However, with Google's help found they're not the winners. The longes class name tends to be:
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails
from Spring Security package, which is amazing 59 characters long.

Well self-described code or just a good sense of humor? Both, I guess.

Uninstalling devices and hardware reenumeration in Windows - C# in action

Recently I came across the following problem: how to re-install some device in MS Windows in the easiest possible way (assuming a target user has no knowledge of the system, whatsoever, maybe with exception of an Internet browser). The solution is to write a simple app, which does everything automatically. The idea was quite simple: uninstall the device and then rescan all hardware for changes, letting the PnP mechanism do the rest. Language: C# and SetupAPI (Windows Driver Kit) hidden behind fancy Windows Forms GUI. Here's the code.

Declarations:
using System.Runtime.InteropServices;
// ...
public const int CR_SUCCESS = 0x00000000;
public const int CM_LOCATE_DEVNODE_NORMAL = 0x00000000;
public const int DIGCF_PRESENT = 0x00000002;
public const int DIF_REMOVE = 0x00000005;

// devnode info struct
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
    public int cbSize;
    public Guid ClassGuid;
    public int DevInst;

    public ulong Reserved;
}

// importing external SetupAPI methods
[DllImport("cfgmgr32.dll")]
public static extern UInt32 CM_Locate_DevNode(ref UInt32 DevInst, string pDeviceID, UInt32 Flags);

[DllImport("cfgmgr32.dll", SetLastError = true)]
public static extern UInt32 CM_Reenumerate_DevNode(UInt32 DevInst, UInt32 Flags);

[DllImport("setupapi.dll")]
public static extern Boolean SetupDiClassGuidsFromNameA(string ClassName, ref Guid Guids, UInt32 ClassNameSize, ref UInt32 RequiredSize);

[DllImport("setupapi.dll")]
public static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndPtr, UInt32 Flags);

[DllImport("setupapi.dll")]
public static extern Boolean SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 DeviceIndex, SP_DEVINFO_DATA DeviceInfoData);

[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData);

[DllImport("setupapi.dll")]
public static extern Boolean SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
Uninstalling all devices in class:
private Int32 RemoveAllDevicesInClassName(string ClassName)
    {
        IntPtr NewDeviceInfoSet;
        UInt32 RequiredSize = 0;
        SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
        Guid[] Guids = new Guid[1];

  // getting required size to store Guids for given class name
        Boolean result = SetupDiClassGuidsFromNameA(
                                ClassName, ref Guids[0], RequiredSize, ref RequiredSize);
        if (RequiredSize == 0)
        {
            // incorrect class name
            return -4;
        }
        if (!result)
        {
            Guids = new Guid[RequiredSize];
   // getting the actual Guids
            result = SetupDiClassGuidsFromNameA(
                                ClassName, ref Guids[0], RequiredSize, ref RequiredSize);
        }
        if (!result)
        {
            // incorrect class name
            return -4;
        }
  // getting all present devnodes for given Guid
        NewDeviceInfoSet = SetupDiGetClassDevsA(ref Guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);

        if (NewDeviceInfoSet.ToInt32() == -1)
        {
            // unavailable
            return -8;
        }

  // preparing device info struct
        DeviceInfoData.cbSize = 28;
        DeviceInfoData.ClassGuid = Guid.Empty;
        DeviceInfoData.DevInst = 0;
        DeviceInfoData.Reserved = 0;

        UInt32 i;
  // for each device in class (set of devnodes pointed by NewDeviceInfoSet)
        for (i = 0; SetupDiEnumDeviceInfo(NewDeviceInfoSet, i, DeviceInfoData); i++)
        {
   // invoke class installer method with DIF_REMOVE flag
            if (!SetupDiCallClassInstaller(DIF_REMOVE, NewDeviceInfoSet, DeviceInfoData))
            {
                // failed to uninstall device
    SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
                return -16;
            }
        }
  // perform cleanup
        SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
        return CR_SUCCESS;
    }
Forcing hardware reenumeration:
private Int32 ScanForHardwareChanges()
    {
        UInt32 DevInst = 0;
        UInt32 result = CM_Locate_DevNode(ref DevInst, null, CM_LOCATE_DEVNODE_NORMAL);
        if (result != CR_SUCCESS)
        {
            // failed to get devnode
            return -1;
        }
        result = CM_Reenumerate_DevNode(DevInst, 0);

        if (result != CR_SUCCESS)
        {
            // reenumeration failed
            return -2;
        }
        return CR_SUCCESS;
    }

Method invocation:
private void btnFixme_Click(object sender, EventArgs e)
    {
        // remove all devices of class "Image" (image capturing devices)
        Int32 status = RemoveAllDevicesInClassName("Image");
        if (status == CR_SUCCESS)
        {
            status = ScanForHardwareChanges();
        }
        lblStatus.Text = status==CR_SUCCESS ? "Done!" : "Error: " + status.ToString();
    }
I hope the code is commented well enough, and doesn't require further explanation.

Thursday, February 10, 2011

#{...} is not allowed in template text

Shortly after starting my adventure with JSP 2.0 I came across the following runtime exception:

org.apache.jasper.JasperException: /index.jsp(13,27) #{...} is not allowed in template text
 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
 org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
 org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)
 ...
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)

My first question was: WTF? All my libs are where they should be, no compilation time errs/warnings, syntax seems to be OK as well. OK, I don't have the jsf/core & jsf/html taglibs imported, but I'm using JSF 2.0, so I don't need the <@ taglib uri="..." prefix="..."/> any more, right? Not exactly.
Quick introduction: as of JSF 2.0 Oracle introduces new technology called Facelets. It's a page declaration language used to build views & templates for JSF-based web applications. Facelets are usually created with conformance to XHTML Transitional DTD, thus the default file extension is *.xhtml. Now, how does this apply to the aforementioned error? One of the advantages of Facelets is that they use XML namespace declaration to import tag libraries. Similar mechanism is used in JSP pages with XML syntax. Example:

Facelets (XHTML) - index.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html">
 <h:head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <title>Facelet (JSF 2.0)</title>
 </h:head>
 <h:body>
  Name: <h:outputText value="#{personDetails.name}"/>
 </h:body>
</html>

In the above example, both namespace declarations in <html> tag instruct the container that the page is using standard JSF tag libraries http://java.sun.com/jsf/core & http://java.sun.com/jsf/html, and so they should be automatically imported. Another advantage of the Facelets is that they define their own component tree, so there's no more need to wrap your JSF code into <f:view></f:view> tags, which was mandatory in previous versions of the framework.
So, why did I get the error? Because my file was a *.jsp file (i.e. JSP page) and not an *.xhtml file (default for Facelets), and the container kindly ignored my xmlns declarations.

What about the old JSP pages? We need to import manually all required taglibs. Some examples below (note the *.jsp file extension):

JSP (XHTML, JSP syntax) - index.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <title>Insert title here</title>
 </head>
 <body>
  <f:view>
   Name: <h:outputText value="#{personDetails.name}"/>
  </f:view>
 </body>
</html>
JSP (XHTML, XML syntax) - index.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root
 xmlns:jsp="http://java.sun.com/JSP/Page"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html" version="2.0">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <title>JSP (XHTML, XML syntax)</title>
 </head>
 <body>
  <f:view>
   Name: <h:outputText value="#{personDetails.name}"/>
  </f:view>
 </body>
</html>

A little explanation to the last snippet. As mentioned before, JSP pages with an XML syntax use the xmlns namespace declarations of the <jsp:root> tag while importing required tag libraries. What differs from Facelets, is that we still need to specify scope of our JSF code by declaring <f:view></f:view> tags in our JSP file.

Friday, November 12, 2010

Dynamic proxy vs. AOP - simple cache

I'm not going to explain what dynamic proxy (introduced in Java 1.3) and Aspect Oriented Programming (AspectJ implementation) are, as there's a lot of places on the Internet where you can find information about both. I'll rather focus on how to use these two technologies to apply some basic cache mechanism to our project - which will be a simple calculator. Dynamic Proxy
As Dynamic Proxy requires proxied class to implement at least one interface, we will start from creating one:

package net.progsign.proxy;

public interface Calculator {
 public long factorial(int n);
}
and the implementation:
package net.progsign.proxy;

public class CalculatorImpl implements Calculator {
 
 @Override
 public long factorial(int n) {
  return n==0 ? 1 : n * factorial(n-1);
 }
}

Nothing special in the above code snippets. Next, the main part, our InvocationHandler, which will be passed as a parameter to the Proxy.newProxyInstance(InvocationHandler) method. It may look like this:

package net.progsign.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CalculatorCacheHandler implements InvocationHandler {
 private static final Log log = LogFactory.getLog(CalculatorCacheHandler.class);
 private Map<Integer, Long> cache;
 private Object target;
 
 public CalculatorCacheHandler(Object target) {
  cache = new HashMap<Integer, Long>();
  this.target = target;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  int value = (Integer)args[0];
  long computed = 0;
  if(cache.containsKey(value)) {
   log.debug(String.format("[proxy] - hit against cache (%d)", value));
   computed = cache.get(value);
  } else {
   computed = (Long) method.invoke(target, args);
   cache.put(value, computed);
   log.debug(String.format(
    "[proxy] - cached new result (%d/%d)", value, computed));
  }
  return computed;
 }
}

Code explanation: our cache is implemented as a simple map which matches a value to a computed result. If the cache has already got a result for the value that was passed to our method, we simply return that result. Otherwise, if the result hasn't been computed yet, we do it now by invoking calculator's method, then cache the result and finally return it to user. Test application:

package net.progsign.proxy.sample;

import java.lang.reflect.Proxy;

import net.progsign.proxy.Calculator;
import net.progsign.proxy.dynamic.CalculatorCacheHandler;
import net.progsign.proxy.dynamic.CalculatorProxy;

public class ProxyTest {
 public static final void main(String[] args) {
  CalculatorProxy calculatorProxy = new CalculatorProxy();
  Calculator proxy = (Calculator)Proxy.newProxyInstance(
    calculatorProxy.getClass().getClassLoader(),
    calculatorProxy.getClass().getInterfaces(),
    new CalculatorCacheHandler(calculatorProxy));
  calculatorProxy.setProxy(proxy);
  
  System.out.println(proxy.factorial(4));
  System.out.println(proxy.factorial(7));
 }
}
Aspect Oriented Programming (AspectJ)

If Eclipse is your IDE, there are two methods you can incorporate AOP to your project. You can either install AspectJ plug-in for Eclipse (i.e. "Eclipse AspectJ Development Tools") or you can simply add required libraries to your project, configure AspectJ through XML configuration file and finally instruct JVM to use java agent, provided with AspectJ, to weave in your aspects. I'll use the second approach in this example. We start from the Calculator class (notice it doesn't implement any interface):

package net.progsign.proxy;

public class Calculator {
 
 public long factorial(int n) {
  return n==0 ? 1 : n * factorial(n-1);
 }
}
And here's the AOP part:
package net.progsign.proxy.aop;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class CalculatorCacheAspect {
 private static final Log log = LogFactory.getLog(CalculatorCacheAspect.class);
 private Map<Integer, Long> cache;
 
 public CalculatorCacheAspect() {
  cache = new HashMap<Integer, Long>();
 }

 @Pointcut("call(public * net.progsign.proxy.Calculator.factorial(int)) && args(value)")
 public void cacheCheckPointcut(int value) {}
 
 @Around("cacheCheckPointcut(value)")
 public Object wrapCalculation(ProceedingJoinPoint pjp, int value) throws Throwable {
  if(cache.containsKey(value)) {
   log.debug(String.format("[aspect] - hit against cache (%d)", value));
   return cache.get(value);
  }
  return pjp.proceed();
 }
 
 @AfterReturning(value="cacheCheckPointcut(value)", returning="result")
 public void updateCache(int value, long result) {
  cache.put(value, result);
  log.debug(String.format("[aspect] - cached new result (%d/%d)", value, result));
 }
}

In the above code we have one pointcut which translates to every call to public method Calculator.factorial(int) which accepts one parameter of type int. We will use value of this parameter later on in our example. Next, we define two advices:

  • public Object wrapCalculation(ProceedingJoinPoint pjp, int value) - this advice is executed around the method invocation. We first check if value that was passed as an argument to our woven method already exists in the cache. If it does, we simply return pre-calculated result from the cache. Otherwise we let the calculator's method to execute and return whatever that method returns.
    Here, the ProceedingJoinPoint is a reference to our join point. By invoking the proceed() method we say, that the actual method that was advised, which in this case is Calculator.factorial(int value), should be executed. The second parameter is the parameter defined in the pointcut and it refers to the argument of the Calculator.factorial(int value) method.
  • public void updateCache(int value, long result) - this advice method is invoked after successfully returning from our pointcut. We take both, value passed as an argument to the advised method and the result it returns (no matter whether it was computed or taken directly from the cache) and put this pair in to our cache.

The last thing is the configuration XML for the application (META-INF/aop.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
 <aspects>
  <aspect name="net.progsign.proxy.aop.CalculatorCacheAspect"/>
 </aspects>
 <weaver option="-verbose">
  <include within="net.progsign.proxy..*"/>
 </weaver>
</aspectj>

I only defined one aspect, by specifying its fully qualified name, net.progsign.proxy.aop.CalculatorcacheAspect and joint points in the application where the aspect will be woven in, which in this case is all classes within package net.progsign.proxy and all its sub-packages. The test application is as simple as:

package net.progsign.proxy.sample;

import net.progsign.proxy.Calculator;

public class ProxyTest {
 public static final void main(String[] args) {
  Calculator impl = new Calculator();
  System.out.println(impl.factorial(4));
  System.out.println(impl.factorial(7));
 }
}

There's one last thing to do. To weave in the aspect to our project we also have to instruct JVM to use AspectJ agent:

-javaagent:${project_loc}/lib/aspectjweaver.jar
Conclusions

As you probably already know both Dynamic Proxy and AOP are widely used in many frameworks, e.g. IoC in Spring Framework or logging purposes in business applications. Dynamic Proxy is not very hard to implement in simple applications, but may be cumbersome in more complex systems. Sometimes even impossible. For example, there's no simple way to create dynamic proxy for recursive methods. Such method has to call its proxied version, thus be proxy aware and this may lead to design issues.
Another disadvantage is the way we implement it in our project. All methods must be called through proxy instance in order to being intercepted by the InvocationHandler and this means we have to modify our code. This, of course, may be time consuming and error prone. AOP lets us avoid these problems, because it doesn't affect the source code directly. It weaves in the new functionality into byte code and switching it on and off can be as easy as:

<aspectj>
 <weaver option="-verbose">
  <include within="net.progsign.proxy..*"/>
  <exclude within="*" />
 </weaver>
</aspectj>

Another advantage of AOP is that our classes don't have to implement any interfaces. We just choose which joint points we want to advise. It can be method calls, method executions, class attribute access, constructors, etc. Both Dynamic Proxy and AOP may be a good solution in real projects, although the latter one is really worth giving a try.