Managing remote users with .ssh/config

Specifying user names with ssh command can sometimes be tedious.

[tak@Z0MG0002R:~]$ ssh john.doe@remote-server-1
[tak@Z0MG0002R:~]$ ssh john.doe@remote-server-2

 

Using “.ssh/config”

These commands can be shortened by specifying server-specific setting in the “.ssh/config” file.

Host remote-server-1
User john.doe

Host remote-server-2
User john.doe

With this configured, user name doesn’t need to be specified anymore.

[tak@Z0MG0002R:~]$ ssh remote-server-1
[tak@Z0MG0002R:~]$ ssh remote-server-2

 

Wild-cards

Wild-cards are allowed in case if there’s dozens of servers with common naming convention.

Host remote-server-*
User john.doe

 

Nickname

Or give a nickname to servers for even shorter commands.

Host foo
HostName remote-server-1
User john.doe

Host bar
HostName remote-server-2
User john.doe

Now we have much less to type.

[tak@Z0MG0002R:~]$ ssh foo
[tak@Z0MG0002R:~]$ ssh bar

 

Project Coins

Project coins is a set of small language specification changes, which makes our everyday programming better. So far in jdk 7, 6 changes has been made, and I’ll leave some sample snippets below.  The detailed spec and documents can be found in the official page.

http://openjdk.java.net/projects/coin/

 

Binary integral literals

Binary literals allow you to define a number in 0/1 style. All you need is to add “0b” prefix. This will give you the clearer view of the number structure in a case where you are dealing with bits.

int a = 0b00000111; // 7
int b = 0b01110000; // 112

System.out.println(a << 4 == b); // true

 

Underscores in numeric literals

Underscore can be used like a comma.

long billion = 1_000_000_000;

System.out.printf("Instagram for Sale! Only $%,3d now!\n", billion);

 

Strings in switch

String can be used as the switch statement condition. (Be careful for null string!!!)

String value = "foo";

switch (value) {
case "foo":
System.out.println("This is foo");
break;
case "bar":
System.out.println("This is bar");
break;
}
String value = null;

switch (value) { // NullPointerException !!!
case "foo":
System.out.println("This is foo");
break;
case "bar":
System.out.println("This is bar");
break;
}

Internally, hashcode is used as a String’s substitute, so it would look something this if you decompile the String switch statement.

String value = "foo";

switch (value.hashCode()) {
case 97299:
if (value.equals("bar")) {
System.out.println("This is bar");
}
break;
case 101574:
if (value.equals("foo")) {
System.out.println("This is foo");
}
break;
}

 

Multi-catch and more precise rethrow

You can throw superclass of an exception, but still declare the method to throw subclass exception. (ReflectiveOperationException was added too.)

public static void main(String[] args) throws NoSuchMethodException, SecurityException {

try {

Constructor<BigDecimal> c = BigDecimal.class.getConstructor(Long.class);

System.out.println(c);

} catch (ReflectiveOperationException e) {

throw e;

}

}

… or, you can catch all the exceptions in a single catch block.

public static void main(String[] args) throws NoSuchMethodException, SecurityException {

try {

Constructor<BigDecimal> c = BigDecimal.class.getConstructor(Long.class);

System.out.println(c);

} catch (NoSuchMethodException | SecurityException e) {

throw e;

}

}

 

Improved type inference for generic instance creation (diamond)

Abbreviate vervose generics declarations.

Map<String, Map<String, Set<BigDecimal>>> map = new HashMap<>();

 

try-with-resources statement

Let instance of “Autocloseable” (superclass of Closeable interface) be closed automatically.

Socket socket = new Socket();

try (Socket s = socket) {

// throws SocketException
socket.getOutputStream();

} catch (IOException e) {

System.out.println(socket.isClosed()); // true

}

 

Simplified varargs method invocation

You don’t have to suppress the following compile time warning any more.

public static void main(String[] args) {

Comparable<String> comparable = "hoge";

// JDK6 wants '@SuppressWarnings("unchecked")' here
List<Comparable<String>> list = asList(comparable);

System.out.println(list.size());

}

// JDK7 allows annotation to mark this safe
@SafeVarargs
public static <T> List<T> asList(T... args) {

List<T> list = new ArrayList<T>();

for (T arg : args) {
list.add(arg);
}

return list;

}

 

Hello world!

To start off my first post, I’d like to leave a short memo of installing JDK 1.7 onto my MacBook Air (OSX Lion). Since there isn’t a official release of 1.7 for Mac yet at the time of writing, I’ll be using the OpenJDK distribution in this post .
 
 

1. Download

Download OpenJDK installer. Mount the downloaded  image file and copy the “1.7.0.jdk” icon into “DRAG HERE”.

 

http://code.google.com/p/openjdk-osx-build/
=> OpenJDK-OSX-1.7-universal-jdk-b222-20111220.dmg

 

Installing jdk

 

2. Run

Done! To check the installation version, execute the following command in the terminal.

Z0MG0002R:~ tak$ /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/bin/java -version
openjdk version "1.7.0-b222"
OpenJDK Runtime Environment (build 1.7.0-b222-20111220)
OpenJDK 64-Bit Server VM (build 21.0-b17, mixed mode)
Z0MG0002R:~ tak$
 

3. Edit login script

… but since our life is too short to type in the full path of the jdk every time, I’m going to setup my environment variable so that this jdk will be used on default without explicitly specifying. I’ll use the .bash_profile file, so create one if you don’t have one yet.

Z0MG0002R:~ tak$ cd ~
Z0MG0002R:~ tak$ touch .bash_profile
Z0MG0002R:~ tak$ vim .bash_profile
 

4. Add JAVA_HOME

Add JAVA_HOME variable and add it to the PATH.   (~/.bash_profile)

export JAVA_HOME="/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home"
PATH=$PATH:$JAVA_HOME/bin
export $PATH
 

5. Reload

Reload variables and check to see if is set correctly.

Z0MG0002R:~ tak$ source .bash_profile
Z0MG0002R:~ tak$ java -version
openjdk version "1.7.0-b222"
OpenJDK Runtime Environment (build 1.7.0-b222-20111220)
OpenJDK 64-Bit Server VM (build 21.0-b17, mixed mode)
Z0MG0002R:~ tak$
 

6. Write

Write THE code. (Hello.java)

public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello world!");
   }
}
 

7. Say hello

Compile and run :)

Z0MG0002R:~ tak$ javac Hello.java
Z0MG0002R:~ tak$ java Hello
Hello world!
Z0MG0002R:~ tak$