Sunday, February 13, 2011

Hibernate Mode on Macbook Pro with OptiBay

Recently i bought an OptiBay Drive for my Macbook Pro. The OptiBay Drive allows you to replace your Superdrive with another hard disk or SSD.

I used the OptiBay to install an Intel X25 SSD on my Computer. I put the os on the SSD. After that, the system really works fast. This article has a video comparing an old macbook pro with hard disk to an old macbook pro with SSD. The article is in german, but you will understand the included video without knowledge of german.

But after the installation of the os on the SSD i had a problem: when i put the machine on standby it froze. I had to power of the machine and restart it. After some research if found out, that by default, mac os uses the "sleep and hibernate" mode for standby.

In sleep mode, the ram content is maintained. Of course this works only as long as there is power to maintain the ram contents. Waking up the machine from sleep mode is very fast.

In hibernate mode, the ram content is written to the hard disk. The content can be restored even if the battery fully died.

"Sleep and hibernate" mode means the machine goes to sleep mode but for safety reasons the disk content is also written to the hard disk.

For some reasons, entering hibernate mode only works if the ram content is written to the primary disk. But since i installed the os on the (secondary) SSD, the system tries to write the ram contents to the secondary disk. When doing this the system freezes.

First i tried to move the hibernate file to another disk. This is possible, the file is written when the system goes to standby. But when you try to wake up the system after a power loss, the system boots from scratch and does not restore the state which was written to the hibernate file. The reason for this is that the hibernate file must be on the root volume.

I solved the problem by changing the hibernate mode from "sleep and hybernate" to "sleep". This can be done with the pmset command:
sudo pmset hibernatemode 0
Now the system can be put in standby mode again!. The only drawback of this solution is that the current state can not be restored after a power loss (e.g. battery was empty).

As a last step you can recover some disk space on your ssd and remove the hibernate file. It is not used anymore.
sudo rm /var/vm/sleepimage

Monday, December 27, 2010

How To Create An Empty Java Project With Maven

To create an empty maven-managed java project simply type:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

You will be asked for the groupId and artifactId for your project, the project version and the main package for your code.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : cloudobjects
Define value for property 'artifactId': : demoProject
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  cloudobjects: : de.cloudobjects
Confirm properties configuration:
groupId: cloudobjects
artifactId: demoProject
version: 1.0-SNAPSHOT
package: de.cloudobjects
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: cloudobjects
[INFO] Parameter: packageName, Value: de.cloudobjects
[INFO] Parameter: package, Value: de.cloudobjects
[INFO] Parameter: artifactId, Value: demoProject
[INFO] Parameter: basedir, Value: /Users/helmut
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /Users/helmut/demoProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.293s
[INFO] Finished at: Mon Dec 27 15:08:58 CET 2010
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------


Now you have a simple maven-managed java project.

If you are using eclipse you have (at least) two options:

To create an eclipse project from your maven project cd into the project-directory and type:

$ mvn eclipse:eclipse

If you are using the M2Eclipse-Plugin, you can import the maven project directly into eclipse.

To import a maven project into eclipse choose "File -> Import" and select "Maven -> Existing Maven Projects":


In the next step select the newly created project folder and the projects pom.xml:


Now you have an eclipse-project with full maven dependency management.

More information about the M2Eclipse-Plugin can be found in the sonatype M2Eclipse manual.

Tuesday, December 7, 2010

Fixing Ruby Gems On Mac OS Snow Leopard

Today I tried to install the ruby client library for cassandra. The gem install command failed with the following error message:

helmut$ sudo gem install cassandra
ERROR:  could not find gem cassandra locally or in a repository 

Running the same command with the verbose flag produced the following output:

helmut$ sudo gem install cassandra -V
HEAD 302 Found: http://gems.rubyforge.org/latest_specs.4.8
Error fetching remote data:  bad response Found 302 (http://gems.rubyforge.org/latest_specs.4.8)
Falling back to local-only install
ERROR:  could not find gem cassandra locally or in a repository


This output shows that the gem command tries to download the file http://gems.rubyforge.org/latest_specs.4.8 but cannot find it. In the next step it tried to download this file with curl. This worked. Downloading the file with the verbose flag produced the following output:

helmut$ curl -v http://rubygems.org/latest_specs.4.8
* About to connect() to rubygems.org port 80 (#0)
*   Trying 72.4.120.124... connected
* Connected to rubygems.org (72.4.120.124) port 80 (#0)
> GET /latest_specs.4.8 HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: rubygems.org
> Accept: */*
> 
< HTTP/1.1 302 Found
< Date: Tue, 07 Dec 2010 08:18:46 GMT
< Server: Apache/2.2.3 (Red Hat) mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5 Phusion_Passenger/3.0.0
< X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0
< X-UA-Compatible: IE=Edge,chrome=1
< X-Runtime: 0.001094
< Location: http://production.s3.rubygems.org/latest_specs.4.8
< Content-Length: 0
< Status: 302
< Vary: Accept-Encoding
< Content-Type: text/html
< 
* Connection #0 to host rubygems.org left intact
* Closing connection #0
This output shows, that the request was redirected to http://production.s3.rubygems.org/latest_specs.4.8. Obviously the location of the ruby gems repository has changed. To fix the above problem, add the new repository url and remove the old one:
helmut$ sudo gem sources -a http://production.s3.rubygems.org/
helmut$ sudo gem sources -r http://gems.rubyforge.org/
After this fix the installation works without errors:
helmut$ sudo gem install cassandra
Successfully installed cassandra-0.8.2
1 gem installed
Installing ri documentation for cassandra-0.8.2...
Installing RDoc documentation for cassandra-0.8.2...

Friday, November 26, 2010

Running hadoop jobs using the eclipse hadoop plugin

The hadoop distribution contains an eclipse plugin that allows you to browse the hadoop filesystem and to run MapReduce-jobs on a hadoop cluster.

As i tried to use the plugin bundled with the current stable hadoop version 0.20.2 i found out that this version of the plugin is not compatible with eclipse 3.4 and above. It allows you to browse hdfs-shares, but you cannot start hadoop-jobs. If you try to start a job nothing happens. The error log contains the following message:

Error
Fri Nov 26 10:13:43 CET 2010
Plug-in org.apache.hadoop.eclipse was unable to load class 
org.apache.hadoop.eclipse.launch.HadoopApplicationLaunchShortcut.

java.lang.NoClassDefFoundError: 
org/eclipse/jdt/internal/debug/ui/launcher/JavaApplicationLaunchShortcut

After some google searches, i found out, that this is a known issue that has already been fixed in the jira ticket MAPREDUCE-1280.

Unfortunately there is no hadoop release that contains this bugfix. Because of this I compiled the plugin from the current hadoop 0.20 svn branch. I attached the compiled plugin to the jira ticket. You can download it from here

This version of the plugin works for me on Mac OS 10.6 with hadoop 0.20.2 and eclipse 3.5.2 as well as eclipse 3.6.1.

To install the plugin, simply delete any old version of the plugin from your eclipse installation and put the jar file into the dropins-folder of your eclipse installation.

If you had an older version of the plugin installed, you need to start eclipse with the "-clean" command line switch. Help on running eclipse with command line switches can be found here.

If you have any trouble getting this to work feel free to ask for support here.

Tuesday, October 26, 2010

Is Apple Going To Discontinue Java Support For Mac OS?

Yesterday Apple released Java for Mac OS X 10.6 Update 3. The release notes for this update state:

"As of the release of Java for Mac OS X 10.6 Update 3, the version of Java that is ported by Apple, and that ships with Mac OS X, is deprecated.

This means that the Apple-produced runtime will not be maintained at the same level, and may be removed from future versions of Mac OS X."

What does this mean for the future of Java on Mac OS? At the moment there is no official alternative to the Apple-produced JVM on Mac OS.


The link to the release notes document was found on osnews.
The release notes document can be found here.

Update:
There is an online petition of people who would like to see Apple to contribute the source code of its JDK to the OpenJDK project. The petition can be found here.

Update #2
Apple and Oracle announced the OpenJDK-Project for Mac OS. Apple will donate most of it's code to the OpenJDK-Project. Read more about this here.

Tuesday, October 19, 2010

Scala Tutorial Part 2 - Defining And Calling Functions

Scala is a hybrid language that combines features of object oriented languages with features of functional languages. In this third part of my scala tutorial series i am going to show how to define and call functions in scala. All code snippets shown here can be run using the interactive scala shell or the scala interpreter as shown in the first part of this series.

Functions Without Parameters

The following snippet defines a function that takes no parameters and does not return a value:
scala> def sayHello() { 
     | println("hello")
     | }
sayHello: ()Unit
The scala shell determines that sayHello is a parameterless function that has the return type Unit. The type Unit indicates, that a function does not return a value. For now you can think of Unit as an equivalent of Java's void.
A function definition starts with the keyword def. This is followed by the function name and empty parentheses. Function arguments would go between the parentheses. This is followed by the function body which is surrounded by square brackets.

Functions that return a value have an equals sign between the parameter list and the method body:
def giveUniversalAnswer() = {
     | 42
     | }
giveUniversalAnswer: ()Int     
A function always returns the value of the last statement in the function body. You can mark this by using the return-statement, but this is not usual in scala. As you can see the type inference system determined that this function returns a value of type int. Sometimes the type inference algorithm is unable to infer the return type of a method (e.g. the scala compiler cannot determine the return type of recursive methods). In this case you have to declare the return type explicitly. And of course in some cases it makes sense to declare the return type of a function for the sake of readability.
The return type has to be declared in front of the equals sign:
def giveUniversalAnswer(): Int = {
     | 42
     | }
giveUniversalAnswer: ()Int     
If you omit the equals sign after the parameter list, the function does not return a value. This is even true for functions that have a return-statement.


Command Query Separation

It is good practice to differentiate between functions that have side effects and functions that are free of side effects. A side effect is an operation that changes the state of a program, for instance by changing a global variable or a type field. The practice of differentiating between these function types was introduced by Bertrand Meyer in his book "Object Oriented Software Construction". He introduces the term command query separation for this differentiation.
The basic idea of command query separation is do differentiate between these two function types:
  • queries: functions that compute values but are free of side effects
  • commands: functions that change the state of the system but do not return values.

// Side note:
// A more in depth description of command query separation can be found
// in Martin Fowlers Bliki.

In scala you can use functions without return type to implement commands and functions with return type to implement queries. This way you have a syntax for declaring queries and a syntax for declaring commands.


Functions With Parameters

The following snippet defines a function that takes two arguments:
scala> def registerPerson(name: String, password: String) {                    
     | printf("User '%s' added to database with password '%s'", name, password)
     | }                                                                       
registerPerson: (name: String,password: String)Unit
The parameters of a function go between the parentheses after the function name. Unlike the return type of a function, the type of function parameters has to be declared. The function parameters are constants that cannot be modified inside the function body. The following code will not compile:
scala> def registerPerson(name: String, password: String) {                    
     | name = "not allowed"
     | }
:6: error: reassignment to val
       name = "not allowed"

Calling Functions
Functions are called by the function name followed by the parameter list. If the parameter list is empty you are allowed to omit the empty parentheses. The following code shows two legal ways to call the function sayHello defined above:
scala> sayHello()
hello

scala> sayHello 
hello


// Side note:
// As we will see later, the parentheses can also be omitted
// when calling type members that have only one parameter.

When calling a function that takes parameters, the parameters are passed to the function in parentheses. To call the function registerPerson type
scala> registerPerson("Helmut", "secret")                  
User 'Helmut' added to database with password 'secret'
To make this code easier to read, scala 2.8 introduced the notion of named parameters. These allow you to specify the name of the parameter in the function call. This makes the function call above easier to read and less error prone. Since the name of the parameters is specified you are even allowed to change the order of the parameters passed. The following code demonstrates this:
scala> registerPerson(password="secret", name="Helmut")
User 'Helmut' added to database with password 'secret'


Uniform Access

When declaring functions without parameters, the parentheses indicating the empty parameter list can be omitted. This makes it possible to make the access to a computed value look like the access to a property. When the parentheses for the empty arguments are omitted in the function definition you also have to omit them when calling the function:
scala> def universalAnswer = {     
     | 40 + 2
     | }
universalAnswer: Int

scala> var x = universalAnswer * 2
x: Int = 84
This example does not make a lot of sense. But as we will see later, this feature allows you to define type members that perform a computation but look like fields for clients.


Summary

In this post I gave a short introduction to defining functions in scala. Below is a summary of rules for the use of functions in scala:

  • omit the equals sign in front of the function body if the function does not return a value
  • use an equals sign for functions that return a value, otherwise the return-value is being ignored
  • use command query separation
  • function arguments are unmodifiable constants
  • you can omit the parentheses when calling functions with zero arguments
  • if the parentheses for an empty parameter list is omitted in the function definition, you have to omit it when accessing the function

I hope this article gave you an insight in using functions in scala. Comments, suggestions for improvement or questions are greatly appreciated.

Scala Tutorial Part 1 - Three Reasons To Learn Scala

In this second part of the scala tutorial series I am going to show you three features that make scala an extremely powerful language.
These features are presented using simple one-liners. The code shown in this post can be run using the scala shell. Running scala code snippets using the scala shell is described in the first part of this scala tutorial series.

To get started fire up a scala console:

$scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

Static Typing

In the first step we simply define a variable:
scala> var i = -3
i: Int = -3 
Even in this simple statement you can see one reason why scala is such a powerful language: it has type inference and static typing.
You defined a variable named i with the value -3. The scala console shows you the result of this expression: a variable i with value -3 of type Int was created. The type inference system automatically determined that -3 is an integer. The scala compiler uses this information to ensure that only int values are assigned to the variable i. Let's try this out:
scala> i = "The static type system will not allow this" 
:6: error: type mismatch;
 found   : java.lang.String("The static type system will not allow this")
 required: Int
       i = "The static type system will not allow this"
In the above statement i tried to assign a String to our Int variable. As you can see, this results in an error.

Sometimes the code can be more readable if you include the type of a variable in the variable definition. Of course you can do this, but you don't have to. You could also have written:
scala> var i: Int = -3
i: Int = -3 
As you can see, the type of a variable can be declared after the name of the variable. The type declaration consists of a colon and the type name.


// A little side note:
// the book "Object-Oriented Type Systems" by Jens Palsberg
// and Michael Schwartzbach provides excellent background
// information on the topic type inference.


Concise Syntax

One more thing you can see in the above example is the conciseness of scala code. The scala syntax allows you to reduce the amount of boilerplate code you have to write.

You already learned that you can omit the type declaration. But there is one more thing to see in the above statements: You do not have to terminate statements by a semicolon. At least this is true for most cases - in some cases the compiler cannot infer by itself where a statement ends. In this case you have to help the compiler. But in most cases you are allowed to terminate statements by a semicolon.

As you learn more features of the scala language you will learn to appreciate the conciseness of scala. Here is a little teaser:

To define a Map with some values in scala you can use the following code:
scala> var capitals = Map("Italy" -> "Rome", "Germany" -> "Berlin")
To achieve the same in java you have to write the following code:
Map capitals = new HashMap();
capitals.put("Italy", "Rome");
capitals.put("Germany", "Berlin");
In Java you have to define the type of this map twice! Even if you use third party libraries like google collections from the guava project the java code will still be more verbose than the equivalent scala code.


Scala Is Purely Object Oriented

The third and last power-feature of scala i want to show you in this post can be seen in the following code snippet:
scala> 15.toHexString
res0: String = f
What is happing here? You invoke the method toHexString on the number 15. This is possible because everything in scala is an object. Scala is completely object oriented. It does not have simple types like int. Because of this, the literal 15 defines an object of type Int. Of course you can invoke methods on this object.

Conclusion

This post presented three extremely powerful features of the scala languages:
  • static typing and type inference
  • concise code
  • pure object-orientation

Hopefully I got you interested in scala. Stay tuned for the next article in this series!