emacs strip blank lines

Spiral-Bound Pad

Spiral-Bound Pad (by incurable_hippie)

Here is a quick way to strip (remove) blank lines from a file with emacs.

  1. position on top of file: M-<
  2. Call a query-regex-replace: M-C-%
  3. input the regex ^ C-q C-j that stand for match all lines that consist in only a carriage return.
  4. RET-RET. The first one confirm the regex and the last one is for “replace with nothing”.
  5. When asked use ! to replace-all.

Summing all commands:

M-< C-M-% ^ C-q C-j RET RET !

here you are :)

SSL and Java (ciak 2)

credits: Vagamundos (from flickr)

credits: Vagamundos (from flickr)

Last time we spoke about accessing a site via HTTPS using the pure Sun way.

Today, we see the same problem, solved with the apache HttpClient libraries. This library require a keystore specified in the code. In order to generate a keystore, you will have to download a certification file as described in the previous post and then create a keystore using the keytool program. I don’t remember well how to create a new keystore with the required certificate via keytool (I did’t write it down), but reading the help of the program it should be something like:

keytool -importcert -alias <my_alias_certificate> -file <path_to_the_cer_file> -keystore myKeystore.ks

where myKeystore.ks is the name of the file containing the keystore. The pros of using this approach is that you can provide a .ks file among with the program, in a location desired and the program will use it, avoiding post-installation procedures to register the certificate on each jvm.

So, assuming to have our keystore into a sub-directory certs the code for using the site is in the pdf as usual.

SSL and java

credits: ph0t0 (from flickr)

credits: ph0t0 (from flickr)

Sooner or later it happens that you have to access an https via Java. Accessing a generic http (non ssl) site is quite easy using the URL and URLConnection objects.

When you try to access a site through SSL the main problem is that most sites does not have a registration in the java’s Certificate Autorithy (CA), causing the framework to refuse connection to that site. So the main solution is to register the certificate of that site in the java’s CA.

So, the things to do are:

  1. download the site certificate
  2. register it in the java’s CA
  3. accessing the site

Downloading the certificate

In order to download the certificate we are gonna use Firefox. With firefox navigate to the site and if required accept the certificate. Double click on the icon of the certificate (1), in the screen that appear choose view certificate (2), on details tab (3) use the export button (4) and save it wherever you want (5).

screen

Register the certificate

Once downloaded the .cer file (X 509 Certificate DER), you can import it into the CA using the a command line program deployed with the j2se installation: keytool.

Java’s CA and keytool are available in the $JAVA_HOME/lib/security/ and an example of using the keytool (under windows) is the following

C:\Program Files\Java\jre6\lib\security>keytool -import -trustcacerts -alias fonsai -keystore cacerts -file fonsai.cer

where fonsai.cer the file name of the certificate just downloaded.

Maybe if you are using eclipse, you will have to restart the workbench in order to make the registration visible to the jvm.

The main counterpart of using this approach is that you have to register the certificate for each jvm running the program and since certificate can expire you should have to do this operation more than one time. I advise to automate the registration operation with a shell script to be distributed among with the program.

Accessing the site

Once registered the site’s certificate, you can access it as usual using the URL and URLConnection objects. It should be enough to tell (via System.properties) an SSL provider. Here is a PDF with the code.

rounding double in java

Credits: Gabri Le Cabri (from flickr)

Credits: Gabri Le Cabri (from flickr)

Rounding a double in java is very simple. If you have to round it to the nearest integer you could use the Math.round(double) method, but what about if you have to round it with a desired number of decimals?

Quite easy too. Using the BigDecimal.setScale(int,int) method you can choose how to round it. Following a quick example:

BigDecimal bd = new BigDecimal(123.45);
bd = bd.setScale(1,BigDecimal.ROUND_HALF_UP);
System.out.println(bd.doubleValue()); //should output 123.5

MySQL add 3 years to a date

It may happens if you are silly like me, that you just inserted a lot of records and then realized that you have inserted a mistaken date. In my case the date was a 3 years earlier than the required.

In order to fix it rapidly with mysql you can use the date_add function. Following a quick ad self explaining statement

update <table> set <field>=date_add(<field>,interval 3 year) where id>3289;

Debian, eclipse and exit code 127

Just after installing eclipse from lenny repository, it crashed me up with the error JVM terminated. Exit code=127. After a bit of searching and watching at what a console execution told me, I tried execute eclipse from command line with a simple java -jar /usr/lib/eclipse/startup.jar. Well, in this way it starts up successfully without any problem. Then I tried again a startup from gnome menu and everything worked fine.

Now there is still the failing about integrated browser and the fact that eclipse shipped with lenny is the very obsolete 3.2.

execute shell command via java

It happened to me that working via an extranet on a citrix environment in a WSAD workspace, that I needed to remove the attribute to at least 200 files in different directories.

Since do it one by one via WSAD is unthinkable, and I didn’t have time to going to the client, loggin into the net in order to have a shell and I dind’t have a shell by my hands, I did it via java.

The environment is Windows machine and here is the source

package tests.runners;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class ShellCommandRunner {
	public static void main(String[] args) throws IOException{
		OutputStream stdin = null;
		BufferedReader br = null;
		String line = null;
		String command = null;

		command = "cmd.exe /C attrib -r /S /D X:\\workspaces\\tuc-workspace\\*.*";

		System.out.println(command);
		Process p = Runtime.getRuntime().exec(command);
		stdin = p.getOutputStream(); //use this to push commands

		//processing stdout
		br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		while((line=br.readLine())!=null) System.out.println(line);

		//processing stderr
		br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		while((line=br.readLine())!=null) System.err.println(line);

		System.out.println("done!");
	}
}

[bash] format date

It happens often a script I have the needs to append a simple timestamp in the format of YYYYMMDDHHMMSS to a generated file.

It’s very easy to get it to work in pure bash, without perl script and similia. Just use the native date command with his format option. Easy as drinking a glass of water, the command is (for the format above)

$ date +”%Y%m%d%H%M%S”

for the full detail of the format arguments, refer to the man page.

A simple usage in script is

#!/bin/bash
set -e

NOW=`date +”%Y%m%d%H%M%S”`
FILE=my-cool-file-${NOW}.txt

echo $FILE

[eclipse-svn] branching, tagging and merging

branching, tagging and merging are foundamental concepts of the SVN operativity. For their means I redirect you to the Chapter 4 in the SVN book.

Here we will discuss the merging operativity. The case presented is the one in which I would like to merge (align) the latest fixes/development in the trunk in a specific branch (say 2.2).

What I like to do is download a new project, as said in the preceding post, but pointing to the desired branch instead of the trunk. Then when asked, specify a different project name, in order to avoid naming conflicts.

When done, on the root of the branch project, select with a right click the Team->Merge.

Now in the URL field specify the url of the source from which you would like to take the latest version. Then in the Start Revision specify the version from which you would like to start search for editing and in Stop Revision specify the version to stop for searches. Generally the last one is the number beside the trunk project in the repository (if you want to align to the latest version). Here is how the form should end up after compilation.

Before proceeding I prefer take a quick preview with the suitable button. When assured that everything is ok, click on the OK button and wait for the end of the process.

When process complete it should be safe to give a Commit of everything but maybe it’s better to give a full recompile and a full test :)

edivad-suite 3.0.0 is out!

edivad-suite, a suite of script-fu add-ons for automating some
operations in The Gimp has reached the 3.0.0 milestone which
accomplish requirements for running under gimp 2.4. Go to the download
page
for getting it.

Next Page »