Unable to open kernel device “\\.\vmci” solution

I found this error when trying to run a Redhat Linux VMware image using the VMware player on my Windows XP operating system.

Unable to open kernel device "\\.\vmci": 
The system cannot find the file specified. 
Did you reboot after installing VMware Player?

After some hours of investigation, I fixed the problem by disabling the VMCI (Virtual Machine Communication Interface) on my virtual machine by doing the following:
1. Open the xxxxxx.vmx file using any text editor.
2. Change the following line:

vmci0.present = "TRUE"

to the following:

vmci0.present = "FALSE"

After applying this workaround, the VMware image worked successfully.

Java getting the class path in the runtime

In some cases, you may need to know for debugging purposes from where a specific class is loaded. This happens usually if there are two different versions of the class in the class path (the one used in compilation is different from the one used in the runtime) which results in the popular java.lang.NoClassDefFoundError error).

Thanks to the ProtectionDomain, you can get this information as shown below.

try {
	Class myclass = Class.forName("org.apache.commons.logging.Log"); /*or any other class you wish*/
	System.out.println(myclass.getProtectionDomain().getCodeSource()
			.getLocation());

} catch (ClassNotFoundException e) {
	e.printStackTrace();
}

Detecting the Page Leave event in JavaScript

You may need to detect if the user is leaving your web page for displaying a warning message to the user mentioning that he may not leave the page and the form is containing data filled by him. To be able to detect this event in JavaScript, you need to deal with the "onbeforeunload" event. The following example explains the idea.

In this example, there is a simple form that contains an input text. When the user enters something in the input text, a warning message appears.

<HTML>
<HEAD>
	<TITLE>Welcome to the test Page</TITLE>
	<SCRIPT type="text/javascript">
		window.onbeforeunload = function(event) {
			
			if (hasData()) {
				return "You entered information in the page. Are you sure you want to leave now?"; // Very important
			}
		}
		
		function hasData() {
			var someThing = document.getElementById("someThing");
			
			if (someThing.value != "") {
				return true;
			}		
			
			return false;
		}
	</SCRIPT>
	
</HEAD>
<BODY>
	<H1>Welcome to the test page</H1>
	
	<FORM>
		<LABEL for="someThing">Enter something here:</LABEL>
		<INPUT type="text" id="someThing"/>
	</FORM>
</BODY>
</HTML>

The "onbeforeunload" event is called when the user tries to leave the page. As shown in the code, to display the interruption message "You entered information in the page. Are you sure you want to leave now?", you have to specify the interruption message in the return of the event. The interruption message is displayed as shown in the figure below.
IE Preview

This code works with me on IE, Firefox, and Safari. I wish that this tip can be useful for you.

Generating Keys for Google Maps API Using the APIs Console

I’m writing this post because I was asked many times how to generate keys for the web sites that are still using Google Maps APIs version 2 (or using GMaps4JSF that is still using Google Maps APIs version 2). Many users were confused because Google removed the key generation mechanism from the Google signup page and some of the users thought that it is no longer supported to generate keys for Google Maps version 2 applications. Fortunately, generating new keys for GMaps v2 applications is still supported.

Here are the steps that I follow for generating new key for my Google Maps API version 2:

  • Login to the APIs console at https://code.google.com/apis/console with your Google Account.
  • Activate the Google Maps v2 service by clicking the “Services” link of the left menu and then enabling the service as shown in the figure below:

    Figure1

  • Click on the “API Access” link of the left menu, then click on the “Create new browser key …” button, the following dialog will appear to you:

    Figure2

  • Enter the URL of your web site, and then click on the “Create” button, you will find your GMaps v2 key attached in the API access page as shown in the figure below:

    Figure3

Finally you can use this key for your GMaps v2 script include as follows:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABCDEFGHIJK&sensor=false"
            type="text/javascript"></script>

This is all about; I wish that this tip can be useful for you.

References:
https://developers.google.com/maps/documentation/javascript/v2/introduction#Obtaining_Key.

“Ajax Status Zero” Resolution

“Ajax status is zero” is one of the hardest errors to debug especially when the Ajax readyState is 4. If we looked at the XMLHTTPRequest documentation, we will find that the readyState attribute can have one of the following values:

  • 0 “The object has been constructed.”
  • 1 “The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.”
  • 2 “All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.”
  • 3 “The response entity body is being received.”
  • 4 “The data transfer has been completed.”

This means that although the data transfer is completed, the status is not OK (not 200).

After some hours of investigation, I found that the XHR (XMLHTTPRequest) zero status means that there is a permission problem of the Ajax request. In other words, the Ajax request is trying to access a domain that the request is not authorized to access.

The actual problem was that my JavaScript JsTestDriver asynchronous test case was running on the JsTestDriver server which is using a different port other than the one that is used by the application server which hosts the Ajax servlet.

The other interesting point to mention is that I only faced this issue on Firefox 10 while in Internet Explorer 8, the Ajax request worked without any problems.

To solve this problem, I authorized the Ajax request that comes from the JsTestDriver server to access the application server by setting the "Access-Control-Allow-Origin" header from my servlet as follows.

response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:42442");

The "http://127.0.0.1:42442" is the JsTestDriver server address from which the Ajax request is constructed and sent to the application server.

After making this workaround the scenario worked fine on all the browsers, I wish that this tip can be helpful for you.

Java Converting String to InputStream

This is the short tip of the day, this tip can be helpful to you if you are wishing, for example, to parse an XML String using the Java DOM APIs or in general if you are working with an API that does not expose an interface to accept the content as a String and it exposes an interface that accepts an input stream.

Using the java.io.ByteArrayInputStream class you can convert a String object to a byte array input stream as follows:

InputStream istream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

Generating Test Reports using JsTestDriver

JsTestDriver is one of the most powerful JavaScript unit testing tools. I wrote an article in developerWorks about efficient JavaScript unit testing using JsTestDriver, please read first it before reading this post.

In this post, I will show you how to generate test coverage reports using the JsTestDriver. First of all, Let’s understand what is meant by “Test Coverage”. “Test Coverage” is one of the measures of software testing. It describes how much the source code of the program is tested.

The coverage criteria can be on the function level (Function coverage), on the statement level (Statement coverage), and on the code branch level (Branch coverage).

JsTestDriver has a plugin that can generate code coverage for the JavaScript code. To configure this plugin, you should:

  • Download the “coverage.jar” from http://code.google.com/p/js-test-driver/downloads/list
  • Add the coverage plugin declaration to the configuration file (jsTestDriver.conf file) as follows:

    ...
    plugin: 
    - name: "coverage" 
      jar: "plugins/coverage.jar" 
      module: "com.google.jstestdriver.coverage.CoverageModule"
    
  • Specify the --testOutput [output_folder] flag in the test running command. This will get the test coverage files in the output_folder you specified.

Currently, The JsTestDriver generates the test coverage files in LCOV format. Unfortunately, JsTestDriver does not generate HTML reports directly as shown in the figure below.
Genarated LCOV files

To generate the HTML reports from the LCOV files, you can use the LCOV visualizer, check it here: http://ltp.sourceforge.net/coverage/lcov.php.

The LCOV visualizer tool is working on Linux Redhat environment. To generate the report from the tool, execute this command on the jsTestDriver.conf-coverage.dat file:

genhtml jsTestDriver.conf-coverage.dat

For more information about how to install the LCOV visualizer on Linux Redhat environment, and converting the LCOV files to HTML reports:
http://www.technicaladvices.com/2012/05/02/converting-the-jstestdriver-coverage-files-to-html-reports/.

Finally for your reference, You can download the project from here, and the LCOV test coverage files from here.

I wish that this tip can be useful.

How to Create a Custom Dojo Component

I published an article about how to create custom Dojo components in Wazi. This article shows you how to create a custom Dojo component from scratch. It illustrates very important concepts like the dojo attach point, the widget structure, and the widget template. I wish you will enjoy this article: http://olex.openlogic.com/wazi/2012/how-to-create-a-custom-dojo-component/.