Wednesday, December 23, 2009

Redundant home internet with a Surf Sticks and Internet Connection Sharing

If you have a not-so-reliable ISP and an UMTS surf stick you can configure your home internet to be more redundant by setting up the surf stick as secondary gateway and DNS server for all your machines on a local network.
Takes a while to set up, but its worth it :-)
Assumption: you already have a primary internet gateway (e.g. DSL/Cable) on 192.168.0.1.
Internet host setup (sometimes connected directly through a surf stick):
  1. Setup your dial-up connection to use 'Internet Connection Sharing' (ICS) as described, for example here: http://support.microsoft.com/kb/306126
  2. Make sure that for Internet Connection Sharing 'Settings' you have enabled DNS to be provided by this new Internet host. Possibly DHCP, but not for now.
  3. Also make sure that the LAN network interface (on a laptop, that's usually 'Local Area Connection') now uses a static IP. Set it the way you would like your new internet gateway to appear on your home network. For example in my case, I already have a default gateway on192.168.0.1, so the new Internet-Host, should still use this primary gateway while it is not connected through the surf stick. The new internet host's ip is 192.168.0.100
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) 82567LM Gigabit Network Connection
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.0.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
DNS Servers . . . . . . . . . . . : 192.168.0.1
So the Internet Host (my laptop with a surf stick) will use IP and DNS from 192.168.0.1 if it currently isn't connected directly.
Client Machine Setup:
The client machines on the network 192.168.0.x must be configured to use both, the primary gateway at 192.168.0.1 and, if not available, the secondary gateway at 192.168.0.100.
On each client machine, go to 'Local Area Connection' -> Properties -> TCP/IP ->Properties and specify an "Alternate DNS Server" of 192.168.0.100 and click 'Advanced' to add a new Default Gateway. Click 'Add' and set the 'TCP/IP Gateway Address' to 192.168.0.100.
Now each client uses 192.168.0.1, while your DSL/Cable internet is available, and 192.168.0.100 otherwise.
The whole configuration with DHCP is similar, you just need to prepolute the DHCP answers with about the same settings (Default gateways and DNS: 192.168.0.1 and 192.168.0.100) .

Friday, October 30, 2009

MAC/IP lookup

I was trying to figure out more about a MAC address and found an interesting lookup mechanism:
To be concise, all lookups on one page:

Thursday, October 29, 2009

Automatic Restart Script for a Java service

This blog tries to describe a pattern on how to write a pragmatic Unix start and stop script for an automatically restarting Java service.
In many cases, you might want to write a Java service that should be up and running 24x7. Now in theory the garbage collector should deal with everything, and if programmed correctly, the process should never crash. But in praxis, things do happen. For example a web service could encounter user requests where memory use is much bigger than you ever expected.
What is a good maximum heap size anyway ?
A pragmatic approach is just to face the fact that your JRE could run out of memory and deal with it. There are sophisticated monitoring solutions out there to automatically restart processes (e.g. nagios/ganglia), but a poor-man's solution is to automatically restart the JRE from the Unix start script.
Please note, you don't want to restart in every case. A bad command line option should just stop the process and not run into the restar loop. Also, there must be a clean way to manually stop it.
Under these constraints, the best solution I could find is to create a temporary file from the Java code at exactly the point 'of no return'. If the JRE stops before this point, no restart happens. If the JRE stops after this point, automatic restart will kick in.
... parse command line options ...
 
     // register shutdown hook      Runtime.getRuntime().addShutdownHook(new ShutdownHook(...));
     // register uncaught exception handler
    Thread.currentThread().setDefaultUncaughtExceptionHandler( new UncaughtExceptionHandler() {
      public void uncaughtException(Thread t, Throwable e)
      {
        e.printStackTrace();
        if( e.getClass()==java.lang.OutOfMemoryError.class ) {
      
          System.err.println("FATAL: shutting down because of java.lang.OutOfMemoryError ...");
          System.exit(7);
        }
      }} );

    File restart = new File("webservice.restart"); // tell the shell to restart me
    restart.createNewFile();

... start your service ...
The Unix start script below will restart the JRE if, and only if, the temporary file (webservice.restart) exists. This could go like this:
#!/bin/sh

#try to start service once
${JAVA_HOME}/bin/java -DREPLAYWEB ${JOPT} com.codestreet.replay.jms.shell.web.ReplayWeb $*

# restart again (until webservice.restart file was removed)
while [ -f webservice.restart ]; do
echo "### RESTART ###"
/bin/rm -f webservice.restart # let Java decide if we really want to restart
${JAVA_HOME}/bin/java -DREPLAYWEB ${JOPT} com.codestreet.replay.jms.shell.web.ReplayWeb $*
done

So Java decides if the service should be restarted and the shell actually performs the restart.
An alternative would have been to simply use return codes from System.exit() ?
But then the question would be: what's the return code with a not yet known exception ?
If someone else uses kill-9 on the jre, a shutdown hook wouldn't be invoked. And to manually stop the restarting you would have to kill the start script as well as the JRE.
With this file-based approach, the stop script is pretty simple: remove the temporary file and kill the JRE. Please note that finding the proper Java process is not as simple as it seems since the classpath is usually very long and 'ps -f' potentially won't show the classname anymore because the line gets too long. On Linux you can use the --col option to see a longer output, but that doesn't work on Solaris :-(
So a little trick around this is to use a dummy JRE property, -DREPLAYWEB in this case. This mock up property has no meaning except that it will show up in ps before the classpath and you can make it unique enough to identify only this instance of JRE.
The stop script would then perform these steps:
  1. get the 'ps line' that contains the dummy JRE property (REPLAYWEB)
  2. get the task id of that process (awk is good enough)
  3. remove the temporary restart file so the start script won't restart automatically
  4. kill the process
#!/bin/sh -f

psline=`/bin/ps -aef | /bin/grep "REPLAYWEB" | /bin/grep -v grep`
echo $psline
pid=`echo $psline | /bin/awk '{ print $2}'`
if [ $pid ]
then
 /bin/rm -f webservice.restart
 echo "stop_webservice: killing Web Service with pid=$pid"
 kill $pid
else
 echo "stop_webservice: Web Service was not running"
fi

Thursday, October 15, 2009

JAX-WS inside Jetty

Jetty is a nice and easy to use Web Server (see previous post), but it can it handle an open standards, heavy weight JAX-WS Web Service ?
With a bit of luck and glue code, it can.
So first we need a Service Provider Implementation (SPI) from Jetty: J2se6HttpServerSPI
This will make the JAX-WS endpoint use the Jetty server instead its default Sun HttpServer.
To plugin different SPI's you would define this new service in a META-INF/services file, but you can also set a system property, as described here. You can even do this in code which reduces the number of files you need to worry about when refactoring.
So here's an example of a Jetty Server handling a JAX-WS endpoint in combination with a File system directory.
      Server jettyServer = new Server(port);
     HandlerCollection handlerCollection = new HandlerCollection();
     jettyServer.setHandler(handlerCollection);
    
     /** 1) Publish WebService (JettyHttpServerProvider) */ 
     String context = "/web/ws";
             
     // 1.1) register THIS Jetty server with the JettyHttpServerProvider
     new JettyHttpServerProvider().setServer(jettyServer);
      
     // 1.2) make sure JAX-WS endpoint.publish will use our new service provider: JettyHttpServerProvider
     System.setProperty("com.sun.net.httpserver.HttpServerProvider",
           "org.mortbay.jetty.j2se6.JettyHttpServerProvider");
      
     // 1.3) add an empty HandlerCollection to by setup by this provider
     handlerCollection.addHandler( new HandlerCollection() );
      
     // 1.4) use JAX-WS API to publish the endpoint (will use a JettyHttpServerProvider)
     Endpoint endpoint = Endpoint.create(replayServiceImpl);
     endpoint.publish("http://localhost:" + port + "/web/ws", replayServiceImpl);
    
     /** 2) Publish WebGUI (Jetty) */       
     String context = "/gui";

     // 2.1) configure File Resource Handler
     ResourceHandler fileResourceHandler=new ResourceHandler();
     fileResourceHandler.setWelcomeFiles(new String[]{"index.html"});
     fileResourceHandler.setResourceBase(guiPath); // start here
       
     // 2.2) configure 'gui' Context
     ContextHandler guiHandler = new ContextHandler();
     guiHandler.setContextPath(context);
     guiHandler.setResourceBase(".");
     guiHandler.setClassLoader(Thread.currentThread().getContextClassLoader());
     guiHandler.setHandler(fileResourceHandler);
      
     // 2.3) add this context handler
     handlerCollection.addHandler(guiHandler);
 
     /** 3) start JETTY server */
     jettyServer.start();
     jettyServer.join();

Embedding an HTTP daemon in Java

Apache Tomcat, the well-known WebServer is about 84MB after installation. Other contenders are even bigger. Does it have to be so complicated ? The following blog is about the search for a 'right size' HTTP daemon embedable in Java.
Now, what's a Web Server anyway ?
To my mind, its just a piece of software that opens a socket (port 8080) where you send requests to (as defined by the HTTP protocol) and it returns data, in most cases something from the file system (HTML pages and such). This doesn't sound too complicated. In fact, there are examples on the net of HTTP daemons that only take 10 lines of source code (compare the C++ obfuscation contest).
But besides this little niche, there is also NanoHTTPD which already handles some of the ugliness of HTTP. So in order to serve up a system directory, which could contain fairly complicated AJAX/Javascript/JSP files, you don't need more than this single file of Java source code.
If serving a file directory is all you need, go back to the previous chapter.
Unfortunately, things are not that easy ... sometimes you also need to publish a WebService or a Servlet or an already packaged Web application (a war file). Then you need something slightly bigger. The best thing I could find in this category is Jetty. It gives up a nice stand-alone WebServer with all kinds of configuration choices and extension points, but most importantly, it is very easy to embed. By using three jar files (less then 1 MB in total) and writing very little code you again have a complete HTTP daemon. Oh, and also, Jetty is already used inside Eclipse and GWT.

Publishing a JAX-WS Endpoint not just to localhost

Interestingly enough, publishing the JAX-WS Endpoint with endpoint.publish(url) as described in a previous post, publishes the WSDL to http://localhost:8080 but this socket is not accessible from any other host ! Strange default, but there you go ...
So in order to really make a JAX-WS Endpoint public to the world, you can revert to the slightly more cumbersome API of com.sun.net.httpserver.HttpServer (at least on Sun's JDK):

HttpServer server = HttpServer.create(new InetSocketAddress(port), 3 ); // backlog
server.start();
  
endpoint.setExecutor( Executors.newFixedThreadPool(10) );
  
// publish WebService
HttpContext wsContext = server.createContext( "/" + service );
  
//wsContext.setAuthenticator(new TestBasicAuthenticator("test"));
endpoint.publish( wsContext );

Sunday, October 4, 2009

Quarkbase

Ever wondered who are the key people behind a website ?
Check out http://www.quarkbase.com/ and enter the URL to check.
Not always perfect, though ... ;-)

Monday, September 7, 2009

60 seconds on SOAP-based Web Services

According to this book, there are 74 distinct initiatives trying to define what a Web Service should look like. And I bet, there are at least 74 different abbreviations around to describe different parts of it. So it's surprising how simple it is to create a Web Service with JDK 1.6. First you define an interface with a bunch of annotations:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
// use RPC or 'wrapped' document-style
@SOAPBinding(style = Style.RPC)
//@SOAPBinding(style = Style.DOCUMENT,
// parameterStyle = SOAPBinding.ParameterStyle.WRAPPED )
public interface Example
{
 @WebMethod public String getGreeting( @WebParam(name="myname") String myname);
}
And then implement the service itself. Use a thread-pool if your application is multi-threaded.

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(endpointInterface = "simple.Example")
public class ExampleImpl implements Example
{
 @Override
 public String getGreeting(String myname)
 {
   return "hello, " + myname;
 }

 public static void main(String[] args)
 {
   String url = "http://127.0.0.1:9876/example";
   System.out.println("Starting WebService at: " + url);
   System.out.println("WSDL available at:      " + url + "?wsdl");

   Endpoint endpoint = Endpoint.create(new ExampleImpl());
   //endpoint.setExecutor( Executors.newFixedThreadPool(10) );
   endpoint.publish(url);
 }
}
The resulting WSDL definition of this service can be looked at http://127.0.0.1:9876/example?wsdl (use Firefox or IE, not Chrome!). Any SOAP-based client (such as the XML Test Utility in General Interface, see previous post) can now invoke this service.
REST-style would be harder to implement, but easier to test. In the REST case, you can simply do: http://127.0.0.1:9876/rs?name="Pete" for invocations. The difference to the previous solution is that with REST-style, the client uses an HTTP GET to invoke a method and the return value can be any kind of XML, not just a SOAP envelope.
The code above implements the main 'standard', but its also the most bloated possibilty. More efficient, but less standardized, is a message encoding of JSON instead of XML, or better, use the Google Web Toolkit. If you are in full control of the server and the client and don't need to follow standards, GWT seems an excellent choice right now.

Friday, September 4, 2009

General Interface Primer

There's now an open-source Ajax GUI builder called General Interface which looks quite promising. A bunch of components that are somewhat familiar to Swing developers. Of course, all logic is Javascript :-(
After installation, here some tips that could come in handy:
  1. Look at the video tutorials, the best one for tables(matrix) isn't actually on that page, but here.
  2. Defining Javascript functions doesn't always work, even after Save/Reload. Try to restart and reload the page before giving up.
  3. You might sneeze at the package names, but trust me, in Javascript conflicts are easy to create. Here's a simple example on a function called jmsw.doAction(index) :
    
    // name of the package to create: jmsw
    jsx3.lang.Package.definePackage(  "jmsw", function(jmsw) {
     jmsw.doAction = function(index) // define doAction(index)
     {
       ...
     };
    });
    
  4. Do Control+Click on a component (such as a button) to locate its definition in the 'Component Hierachy'.
  5. Float a Palette to get more space or to move it to the bottom panel: Upper Right Corner of a Palette View, click on 'Floating'. Use Menu->Palettes to enable/disable a palette.
  6. The 'XML Mapping Tool' always starts with the same default of ..Address.wsdl, but this will not be the one you will be working with. To change the default of this initial URI, edit C:\TIBCO\gi-3.8-max\GI_Builder\plugins\jsx3.ide.mapping\components\Inputs\wsdl.xml and search for 'Address, comment it out and add your own WebService URI:
    
           <!-- use different default for XML Mapping Tool:
           <strings jsxname="jsx_schema_wsdlurl" jsxvalue="jsxplugin://jsx3.ide.mapping/samples/Address.wsdl" jsxwidth="100%"/>
           -->
    
           <strings jsxname="jsx_schema_wsdlurl" jsxvalue="http://127.0.0.1:9876/rs?wsdl" jsxwidth="100%"/>
    

Monday, August 31, 2009

Java Bi-directional Maps: Apache commons or Google collections ?

In case you need a bi-directional Map in Java, you can, of course, easily roll your own.
But this is a common problem, so others have done this work already.
Here's a simple how-to on bi-directional maps with Apache and Google commons.
Apache commons collections offers a BidiMap interface with various implementations.
For example:
public class TestBidiMap
{
static BidiMap bimap = new DualHashBidiMap();
public static void main(String[] args)
{
bimap.put("D", 3);
System.out.println("D -> " + bimap.get("D") );
System.out.println("3 -> " + bimap.inverseBidiMap().get(3) );
}
}
Nice and easy, but unfortunately this version of BidiMap doesn't use Java generics, which I have learned to like over time (bimpap.get(3) will not show a compile error) !
Google collections has an alternative that uses generics: BiMap
public class TestBiMap
{
static BiMap<String,Integer> bimap = HashBiMap.create();
public static void main(String[] args)
{
 bimap.put("D", 3);
 System.out.println("D -> " + bimap.get("D") );
 System.out.println("3 -> " + bimap.inverse().get(3) );
}
}
Also nice and easy, what's interesting is the factory method HashBiMap.create(). This removes the need to duplicate the generics type specification of String, Integer. Kiss rules, so two points for Google collections here.
Summary: any mid-size Java project probably shouldn't live without Apache commons, but as a useful addition, Google collections is definitely worth a look.

Monday, August 17, 2009

Amazon S3 intro

Started to look into Amazon EC2/S3 recently (technology you should know about).
Here's a short S3 intro, one with lots of screenshots and therefore easy to digest:

Monday, August 10, 2009

Spring and Pipes&Filters

Finally an IoC container that almost everyone agrees on: Spring.
After many inhouse, half-baked attempts and 'framework wars' a default solution. TIBCO folks might remember the days of TAF (1996), GAF, XXTAF or PushBeans. Also
openadaptor.org should be noted here.

Now Spring certainly is nice and generic, but for building EAI adapters or 'message processing pipelines' it is not ideal. In case of these pipelines, the message path is very structured: from a source bean through several transformation beans until it finally reaches one (or many) sinks. XML already imposes an 'order of things' so an ideal configuration language of a little adapter might look something like this:
<pipelines> <pipe name="Jdbc2JMSPipe"> <jdbc dbref="db1" querysql="select * from table1"> <concatenate method1="getFirstName" method2="getLastName" result="setName"> <jmsObject2MapMessage> <jmsPublish topic="PERSON"> </pipe> </pipelines> This example configures a processing pipeline to get rows from a JDBC connection, invoke the concatenation filter, convert an object to a JMS MapMessage and finally publish the result to a JMS topic named 'T.PERSON'.

The configuration above is easy to understand and manipulate. One can imagine extensions with for-loops, if-clauses etc.  For example:<pipelines>
<pipe name="Jdbc2JMSPipe"> <jdbc dbref="db1" querysql="select * from table1"> <concatenate method1="getFirstName" method2="getLastName" result="setName"> <jmsObject2MapMessage> <if method="getName" startswith="a-z"> <jmsPublish topic="PERSON.LOWERCASE"> <else> <jmsPublish topic="PERSON.UPPERCASE"> </else> </if> </pipe> </pipelines> Configuring the first example pipeline with Spring could look like this: <beans> <bean id="1" class="com.abc.jdbc" dbRef="db1" querySql="select * from table1"> <property name="next"> <ref bean="2"> </property> </bean> <bean id="2" class="com.abc.concatenate" method1="getFirstName" method2="getLastName" result="setName"> <property name="next"> <ref bean="3"> </property> </bean> <bean id="3" class="com.abc.jmsObject2MapMessage> <property name="next"> <ref bean="4"> </property> </bean> <bean id="4" class="com.abc.jmsPublish" topic="T.PERSON"/> </beans> The order of beans is defined explicitly although XML already provides an order.
A nasty user of this system could even reorder the beans in a different order :-(
So maybe some XSLT to transform from a 'pipes&filters' language to a Spring configuration ?
Or extend Spring with this kind of processing step ?
Or how about a XML-aware bean reference: <beans> <bean id="1" class="com.abc.jdbc" dbRef="db1" querySql="select * from table1"> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="2" class="com.abc.concatenate" method1="getFirstName" method2="getLastName" result="setName"> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="3" class="com.abc.jmsObject2MapMessage> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="4" class="com.abc.jmsPublish" topic="T.PERSON"/> </beans>
Currently, I think XSLT would be the best solution since it keeps the clarity of the pipes&filters language.

Good books on EAI and Agile Programming

  • Head First "Design Patterns" - 26 pages on the Decorator ? Finally the space it deserves ! If you found the "Consequences, Forces, Solutions, Implementation, Related Patterns"structure of other Patterns book too rigid. This one is for you. Finally an entertaining read on Patterns. "Patterns in Java" and even the GoFs books loose their glory after reading this one.
  • "Client/Server Survival Guide" - Very old, but I liked the writing. (use Steven's "Unix Network Programming" as a reference)
  • "Extreme Programming Explained" - there you go, little Agile programmer. Also some good tips on how to convince upper management of the benefits.
  • "Java Extreme Programming Cookbook" - nice intro to a all the good technologies (as of 2005).
  • "Java Message Service" (Monson-Haefel&Chappel) - no thrills, but all you really need. Writing concise books is a great virtue.

Ajax: Xpath-enabled Json queries

In the Ajax world, there is a big discussion about XML versus Jsonas a means of transporting data from the Server to the Browser. After looking at the first examples of using a DOM API in Javascript, I knew, I'll not be using XML in Javascript. Using Json instead, feels very nice and natural. But Json is just a means of transport. Nothing else. The simplisticclasses work, but no query language to select the interesting data before sending it over. If you structure your Web-Gui well, the resulting data should be simple. Hence no need for the complex XML structure. XML offers however, two goodies, I wouldn't want to miss: Object to XML serializers (e.g. XStream) and XPath to select parts of the XML. So my idea is this: why not marry both approaches ? Transform the Java object into XML, apply an XPath query to select what you really want and then transform the result to Json to send it over. Even more flexible, support a 'class' and 'method' (and an optional 'args') parameter to select which data to get. Here is how it works: 1) Ajax Request: On the Browser, request a Json object with parameters class,method, xpath:
requestList( "class=TibjmsAdmin&method=getTopics&xpath=/*/*/name",
"name",
isAsync,
topicList );
2) Java Method call: On the Server-side, this request will execute the method TibjmsAdmin.getTopics() and create a Java object with some code similar to this:

if( className.equals("TibjmsAdmin") ) { // TODO: use ClassLoader (security?)
 Object invokee = jmsAdmin;
}
... others ...

// get specified method on specified class
className = invokee.getClass().getName();
Class c = Class.forName(className); // className is TibjmsAdmin
Method m = c.getMethod(method,argClasses); // method is getTopics, argClasses is null

// invoke method and get return object
  returnObj = m.invoke(invokee, argObs);
3) Java to XML: the next step is now to serialize this Java object to XML. I like to use XStream for this purpose because its extremly simple and straight-forward.

XStream xstream = new XStream();
String xml = xstream.toXML(returnObj);
If the returned object does not produce the required results, you can do some magic by plugging in an XStream 'Converter' (not shown here). 4) Apply Xpath: Now that that we have the XML, we can apply the specified XPath to fiddle out the data that is really interesting for us. The following code will create a list of selected XML nodes.
    NodeList nl = XmlUtils.selectNodeList(docBuilder,xmlString,xpath);
StringBuffer sb = new StringBuffer("");
for( int i=0;i<nl.getLength();i++ ) {

  Node node = nl.item(i);
  sb.append("<" + node.getNodeName() + ">" );
  sb.append( node.getTextContent() );
  sb.append("" );
}
sb.append("\n");
5) Return Json result: finally return the Json object back to the Browser using the Json tools.
JSONObject json = XML.toJSONObject(xml);
  response.setContentType("text/plain");
if( out.equals("pretty") ) // pretty JSON
  response.getWriter().print( json.toString(2) );
else // or not
  response.getWriter().print( json.toString() );

Windows NT Service configuration

To debug current Windows Service configuration, try 'sc' on a command console.

For example 'sc query' pretty much shows the same configuration information as the registry tree which defines Windows services:

My Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

There's an interesting Windows Resource Kist that includes service configuration tools.

For example instsrv.exe to run ANY other .exe application as Windows service

C:\Program Files\Windows Resource Kits\Tools>instsrv.exe AAA "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"

The service was successfuly added!

C:\Program Files\Windows Resource Kits\Tools>instsrv.exe AA REMOVE

Consistent Subversion icons in Windows and Eclipse

With the latest version of Tortoise SVN (1.6.x) the default icon set changed to be different from the icons in Eclipse (subclipse).
To justify that again, so SVN icons look the same in Eclipse and in Windows folders, choose a TortoiseSVN directory, right mouse-click, TortoiseSVN -> Settings and choose the Subclipse icon set as shown below.

Debugging Visual C++ applications

Sometimes, the simplest trick is the best. I'm sure you all know this, but to debug a Windows application (for exampe a Windows NT Service), call 'DebugBreak'. In many situations this can be more handy than setting a breakpoint.

int main(int argc, char** argv)

{

DebugBreak();

How to spawn a command shell under windows 'system' user

This trick can be useful if you want to debug user permission issues with a Windows NT service that logs on under "Local System account" (the default).

First, figure out your current system time:

C:\>time

The current time is: 6:15:18.57

Second, use the 'at' command to start an interactive command shell when the next minute starts:

C:\>at 6:16 /interactive cmd

Now inside this new command shell, try your command which is run under this new user.

Windows DLL load order

So when you work with Windows NT services, you might want to make sure the right DLLs are loaded at the proper time. When debugging issues on NT service startup time, this becomes important. On Windows, the order of DLL loading is as follows (see also Visual C++'s help on LoadLibrary()):

  1. The directories listed in the App Path registry key (if any)
  2. The directory where the executable module for the current process is located.
  3. The current directory.
  4. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
  5. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
  6. The directories listed in the PATH environment variable.
To set an 'App Path' for your application (text.exe), set the (Default) value of
KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\test.exe
to the full path of your executable, for example:
C:\Program Files\Test\test.exe

Add a sub-key named Path, set it's value to the full path of the DLL, for example:

C:\Program Files\Test\lib
Use regedit see examples of other applications, and how they setup their App Path keys.

Wednesday, July 22, 2009

How to display a non-scrolling background for a Java JScrollPane

To add a background text (or image) to the back of a JTextArea that is inside a JScrollPane, you can use the following code. The advantage here is that the background text/image does NOT scroll with the text in the JTextArea. See screenshot below.
Here's the code:
public static void main(String[] args) {


JFrame frame = new JFrame("Overlay test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scroll = new JScrollPane();
scroll.getViewport().setBackground(Color.WHITE);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize( new Dimension(400,100));
frame.add(scroll);

final String text = "English";

final JTextArea ta = new JTextArea() {

 {setOpaque(false);}  // instance initializer

 public void paintComponent (Graphics g) {
   g.setFont(new Font("Verdana",Font.ITALIC,20));
   g.setColor(Color.LIGHT_GRAY);

   Rectangle rect = getVisibleRect();
   int x = rect.width + rect.x - 14*text.length();
   int y = rect.y + 20; // approx. height of the text
   g.drawString(text, x, y);
   super.paintComponent(g);
 }
};

ta.setText("This text area contains an overlayed text that is BEHIND this text and does not scroll");
ta.setPreferredSize( new Dimension(600,100));
scroll.setViewportView(ta);
frame.pack();
frame.setVisible(true);
}

Friday, July 17, 2009

InstallShield Setup Prerequisites for PIA 2003, PIA 2007, VSOT, .NET2.0

Now, if you are writing an "InstallShield X" package for something like an Excel Addin, you are dependent on a number of things:
  • .NET Framework (2.0, for example, dotnetfx2.0.exe)
  • Office 2007 Primary Interop Assemblies (o2007pia.msi)
  • Visual Studio Tools for Office (vstor2005.exe)
One way to do this is setting up a "Basic MSI Project" (NOTE: this doesn't work with a standard InstallShield project as pre-requisites are not supported there !!)
and add prerequisites (Tools->Prerequisite Editor) for each of these dependencies.
Now the crucial question is, which condition to set for each of these packages ?
Here's my best solution, so far (Conditions tab in Preqrequisites):
  • .NET Framework 2.0: "A File does or does not exist" [WindowsFolder]\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe --- Run setup if: Not Found
  • PIA Office 2007: "A registry key does or does not exist" HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Windows\CurrentVersion\Uninstall\{50120000-1105-0000-0000-0000000FF1CE} --- Run setup if: Not Found
  • PIA Office 2003: "A registry key does or does not exist" HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Windows\CurrentVersion\Uninstall\{91490409-6000-11D3-8CFE-0150048383C9} --- Run setup if: Not Found
  • Visual Studio 2005 Office Tools: "A registry key does or does not exist" HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Windows\CurrentVersion\Uninstall\{388E4B09-3E71-4649-8921-F44A3A2954A7} --- Run setup if: Not Found
Took me a long time to figure these out, thanks Anthony ! Would be nice if this helps someone :-)

10 Seconds on Windows Assemblies

So ... Windows assemblies are these little packages a .NET application can be made of.
Kind of a way around the 'DLL hell'. Check out C:\WINDOWS\assembly to see what I mean.
Interesting to note is that the same thing looks quite different in the command shell, try:
dir C:\Windows\assembly
Details are described here:
Here's a tool to list and possibly remove assemblies (can be very handy after a bad install):

Wednesday, July 15, 2009

InstallShield and Windows Services: ServiceExistsService doesn't work

So for me, ServiceExistsService didn't work at all.
There are some permission issues apparently (see http://community.acresso.com/archive/index.php?t-169135.html)
but the solution proposed there is a tad too heavy-weight for me.
Here's an alternative, use the Registry entry of a service to figure out if the service is installed or not:
// Indicates if the service with this key (e.g. myNewService) has been installed
function BOOL windowsServiceExists(serviceKey)
string szKey,szServiceName;
number nRootKey;
begin
nRootKey = HKEY_LOCAL_MACHINE;
if (RegDBSetDefaultRoot (nRootKey) <>
MessageBox ("Unable to set HKEY_LOCAL_MACHINE.", SEVERE);
return FALSE;
endif;
szKey = "SYSTEM\\CurrentControlSet\\Services\\" + serviceKey;
//MessageBox("Testing for service: " + serviceKey, INFORMATION);
if (RegDBKeyExist (szKey) = 1 )
then
//MessageBox("found service:" + szServiceName, INFORMATION);
return TRUE;
endif;
return FALSE;
end;

How to suppress InstallShield warning W7503

If you are like me and don't like duplication you might have something like a common.rul file that contains common methods. But with the InstallShield (7.x and higher ?) compiler you get this warning if you didn't use a method:
W7503: function defined but never called
Bad thing about this is the fact that you can't easily spot real warnings from this 'warning to ignore'.
Here's how to avoid this:
1) define a dummy method where you call all your offending methods
// dummy function to supress warning 'function defined but never called'
prototype dummySuppressWarningW7503();
function dummySuppressWarningW7503()
begin
setRegistryValue("","",0,"");
end;
2) call this method someplace that is definitely executed inside an if( FALSE ):
// a little trick to stop the compiler from showing warnings
if( FALSE ) then
dummySuppressWarningW7503();
endif;
Tada ! The compiler thinks its called, but it never is.

void main()

Ok, here it goes ... instead of collecting my private Tips&Tricks, why not publish them online?
Makes it easier for me to search and maybe someone out there finds a little trick that solves his/her problem. I owe you people out there a lot of good ideas.
This blog will be about Java, Groovy, C++ (if I can't avoid it), Design Patterns, Agile Methods, possibly Berlin or, more generally, some 'Eureka' moments when things just start working again.
Enjoy !