Archive for February, 2007

System.out and System.err over log4j

Originally posted on April 14, 2005 on another blog, here is how to redirect all java’s System.out/err over the log4j stream.

Download the latest log4j library. Actually here is only one form of packaging that includes both binaries and sources.

Unpack the archive obtained and you’ll get a “contribs/JimMoore” directory inside the directory created by extracting it.

Inside your source project, where you should already have log4j configured and running, create the org.apache.log4j.contribs package and place here the contribs/JimMoore/LoggingOutputStream.java file.

Then, where you need to redirect the out/err stream, here is a sample code:

PrintStream oldOut = System.out;
PrintStream oldErr = System.err;

//redirecting System.out/err to log4j appender
System.setOut(new PrintStream(new LoggingOutputStream(logger,Level.INFO),true));
System.setErr(new PrintStream(new LoggingOutputStream(logger,Level.ERROR),true));

//...

//restoring original System.out/err
System.setOut(oldOut);
System.setErr(oldErr);

Comments (2)

Ftp and Java

Originally posted on April 17th, 2005 on another blog, here is a brief example on how to use the jvftp library for sending files via ftp in the java language.

PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
Ftp client = new Ftp();
FtpContext context = client.getContext();

//redirecting System.out/err to log4j appender
System.setOut(new PrintStream(new LoggingOutputStream(logger,Level.INFO),true));
System.setErr(new PrintStream(new LoggingOutputStream(logger,Level.ERROR),true));

context.setFileTransferMode('A');

try{
   client.connect(connectionParams.getAddress(),Ftp.PORT);
   client.login(connectionParams.getUser(),connectionParams.getPassword());
   client.command(”site recfm=FB lrecl=256″);

   CoFile from = new LocalFile(connectionParams.getFile());
   CoFile to = new FtpFile(”‘” + connectionParams.getFile() + “‘”,client);
   CoLoad.copy(to,from);
}catch(IOException ioe){
   logger.error(”save() IOException”,ioe);
   throw new ExecuteCommandException(ioe.getMessage());
}finally{
client.disconnect();
}

//restoring original System.out/err
System.setOut(oldOut);
System.setErr(oldErr);

logger.debug("done");

Comments

Mp3 2 Wav

Sometimes ago, I asked my self how can I convert an entire directory of mp3 files into wave files. So, after a little search of something ad hoc on google, I opted to use the swiss knife of the multimedia: mplayer

for i in *.mp3; do mplayer -ao pcm:file="/home/your-user/Desktop/${i}.wav" "$i" ; done

This will generate the wave files in the directory specified. The filename will be mysongs.mp3.wav. If you would like you can make it a bit better by using sed in order to remove the .mp3 extension.

Comments

Morse code

It was the June 22th, 2004 when I posted this code on another blog. It was made to spend some time in the hot italian summer, not to be useful. However here it is :)

import java.io.IOException;
import java.util.HashMap;

/**
* Class that write a sentence in Morse Code
* @author
* @version 1.0.0
*
* Copyright (C) 2004
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Licence details at http://www.gnu.org/licenses/gpl.txt
*/
public class Morse {
private HashMap transcode = new HashMap();

public Morse(){
//initializing Transcode
transcode.put(”A”,”.-”);
transcode.put(”B”,”-…”);
transcode.put(”C”,”-.-.”);
transcode.put(”D”,”-..”);
transcode.put(”E”,”.”);
transcode.put(”F”,”..-.”);
transcode.put(”G”,”–.”);
transcode.put(”H”,”….”);
transcode.put(”I”,”..”);
transcode.put(”J”,”.—”);
transcode.put(”K”,”-.-”);
transcode.put(”L”,”.-..”);
transcode.put(”M”,”–”);
transcode.put(”N”,”-.”);
transcode.put(”O”,”—”);
transcode.put(”P”,”.–.”);
transcode.put(”Q”,”–.-”);
transcode.put(”R”,”.-.”);
transcode.put(”S”,”…”);
transcode.put(”T”,”-”);
transcode.put(”U”,”..-”);
transcode.put(”V”,”…-”);
transcode.put(”W”,”.–”);
transcode.put(”X”,”-..-”);
transcode.put(”Y”,”-.–”);
transcode.put(”Z”,”–..”);
transcode.put(”1″,”.—-”);
transcode.put(”2″,”..—”);
transcode.put(”3″,”…–”);
transcode.put(”4″,”….-”);
transcode.put(”5″,”…..”);
transcode.put(”6″,”-….”);
transcode.put(”7″,”–…”);
transcode.put(”8″,”—..”);
transcode.put(”9″,”—-.”);
transcode.put(”0″,”—–”);
}

/**
* Method that encode a message in morse code
* @param s: the clean message
* @return the message encoded
*/
public String encode(String s){
String ret = “”;
String elem = “”;

s = s.toUpperCase();
for(int i=0; i<s.length();i++){
elem = (String) transcode.get(String.valueOf(s.charAt(i)));
if(elem == null)
ret += String.valueOf(s.charAt(i));
else
ret += elem;
}
return ret;
}

public static void main(String[] args) {
Morse morse = new Morse();
String message = “”;
char lineSeparator = System.getProperty(”line.separator”,”n”).toCharArray()[0];
int in = 0;

System.out.print("Insert a phrase to encode (return to end) ");
try{
while((in = System.in.read()) != lineSeparator){
message += String.valueOf((char)in);
}
}catch(IOException ioe){
System.err.println(”error input the message”);
ioe.printStackTrace();
System.exit(1);
}
System.out.println(message + “= ” + morse.encode(message));
}
}

Comments

File permission

Originally posted on Decembre 1st, 2005 on another blog of mine.

The question was: how can I change permission of a tree having the directories all in 777 and files in 664

chmod -R 777 theDir && find theDir/. ! -type d -exec chmod 664 '{}' \;

Comments

~/.emacs

Here is my .emacs file (so I wont have to rewrite it each time I install emacs on a new computer)

;;disable splash screen and startup message
(setq inhibit-startup-message t)
(setq initial-scratch-message nil)

;;enable syntax highlight by default
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)

;;disable line wrapping
(setq-default truncate-lines t)

;;disable autosave and backup
(setq auto-save-default nil)
(setq make-backup-files nil)

;; Force to use spaces for tabs and to be 3 spaces
(setq-default indent-tabs-mode nil)
(setq-default tab-width 3)

;;if you like aspell (http://aspell.net/win32/) instead of ispell,
;;as said in a comment on blog
;;http://edivad.wordpress.com/2007/03/20/emacs-widows-and-ispell/#comment-160
;;correct the path in order to point to your aspell location
;;(setq-default ispell-program-name "c:\\Program Files\\Aspell\\bin\\aspell.exe")

;; fonts fixing for Debian etch (testing) bug
;;(set-default-font "-Adobe-Courier-Medium-R-Normal-14-100-100-100-M-90-ISO8859-1")
;;(set-face-font 'menu "-adobe-courier-medium-r-normal-14-100-100-100-m-90-iso8859-1")
;;(set-face-font 'tooltip "-adobe-courier-medium-r-normal-14-100-100-100-m-90-iso8859-1")

;;Move emacs a bit down and on the right in order to avoid a possible ugly
;;windows bug. If you like to have the taskbar on top of the screen, sometimes
;;emacs starts with the title bar under it.
(setq initial-frame-alist '((left . 50) (top . 50)))

;;default attributes for text-mode
(setq text-mode-hook '(lambda()
            (auto-fill-mode t) ;;physical line break
            (flyspell-mode t) ;;spellchek on the fly
            (ispell-change-dictionary "italiano" nil)
            )
)

;;default attributes for mail-mode
(setq mail-mode-hook '(lambda()
            (auto-fill-mode t) ;;physical line break
            (flyspell-mode t) ;;spellchek on the fly
            (ispell-change-dictionary "italiano" nil)
            )
)

;;sets the file name as window title (for graphics emacs)
(set 'frame-title-format '(myltiple-frames "%b" ("" "%b")))

;;php-mode.el
(require 'php-mode)

;;html-helper-mode
(autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
(setq auto-mode-alist (cons '("\\.jsp$" . html-helper-mode) auto-mode-alist)) ;;hhm for jsp files
(setq html-helper-never-indent t)
;;disable auto-fill-mode and fly-spell-mode for hhm
(setq html-helper-mode-hook '(lambda()
                               (auto-fill-mode nil)
                               (flyspell-mode nil)
                               (local-set-key "\t" 'self-insert-command)
                             )
)

Comments (6)