venerdì 6 novembre 2009

Java: reading & writing files [ENG]

Very quick and naive:
private String readFile(String fileName)
  {
    if (fileName==null || "".equals(fileName))
    {
      return null;
    }
    try
    {
      FileReader reader = new FileReader(fileName);
      int next = -1;
      StringBuffer s = new StringBuffer();
      do 
      {
          next = reader.read();  
          if (next != -1) 
          {
              s.append((char)next); 
          }

      } while (next != -1);

      reader.close();  
      return s.toString();
    }
    catch (Exception e)
    {
      log(e.getMessage(), Project.MSG_ERR);
      throw new BuildException(e);
    }
  }
  
  private void writeFile(String content, String fileName)
  {
    try
    {
      FileWriter writer = new FileWriter(fileName, false);
      writer.write(content);
      writer.close();
    }
    catch (Exception e)
    {
      log(e.getMessage(), Project.MSG_ERR);
      throw new BuildException(e);
    }
  }

venerdì 16 ottobre 2009

Sorting files into folders by date [ENG]

If you happen to have a folder ridden with tons of files and want to split them in folders by arrival date (think log files, or automatic downloads, or... ehm... p0rn, maybe?) these batch script may lend a hand:

move_file_to_date_dir.bat
@echo off
set tempo=%~t1
if NOT "%tempo%" EQU "" goto do_it
echo Error processing file "%~f1": time was not set
goto :exit_batch
:do_it
set tempo_formatted=%tempo:~6,4%%tempo:~3,2%%tempo:~0,2%
echo Moving "%~f1" to "%tempo_formatted%"...
if exist "%tempo_formatted%" goto move_file
mkdir "%tempo_formatted%"
:move_file
move "%~f1" "%tempo_formatted%"
:exit_batch
move_files_to_date_dir.bat
@echo off
for %%v in (%1) do (
call "move_file_to_date_dir.bat" "%%v"
)
With little tweaks these can be used to group files for month or year.

martedì 13 ottobre 2009

Why slow? [ENG]

Yahoo! YSlow: A tool which analyzes web pages and suggests ways to improve their performance
Gzip: how to enable GZip compression in Tomcat

(Thanks to Leonardo A.)

martedì 1 settembre 2009

Liferay: Hook [ENG]

So someone in your management (or in your customer's management) heard that Liferay is cool and decided that you are the one who is going to make it work. You install it, toy with it a little, build your community, translate it in your language, start fiddling with the themes, user permissions and such.
Then one day you have to write your first hook. A hook is a class which handles events or changes in the underlying model structure. For example you might want to do something when a new user is added to the database.
You google around a bit and find a lovely tutorial. Lovely but, alas, outdated, because, you know, to stir things up a bit someone thought to change hooks configuration between version 5.1 and 5.2.
Basically you have to move the hook declaration in a property file. Just a heads up, since I struggled with this a bit.

Liferay: Lost In Translation

Sicuramente avrete provato a installare Liferay 5.2 e vi sarete divertiti a selezionare la lingua italiana dalla pagina "Il mio account". Avrete certamente incontrato perle come "Aggiunga l'entrata della lima" ("Add File Entry"), o "Aggiunga il soddisfare di fotoricettore" ("Add Web Content"), o "Vada al dispositivo di piegatura" ("Go to folder"). Chiari sintomi dell'intervento di un traduttore automatico (configurato anche abbastanza male). Se volete limitare la confusione degli utenti (generalmente abbastanza confusi per loro conto) potete:
  1. estrarre da ${catalina.home}/webapps/ROOT/WEB-INF/libportal-impl.jar il file content/language_it.properties
  2. rinominare il file in language-ext_it.properties
  3. modificarlo a piacimento
  4. copiarlo in ${catalina.home}/webapps/ROOT/WEB-INF/classes/content
credits: forum ufficiale di Liferay in italiano

giovedì 20 agosto 2009

Quotes: the science of hunches [ENG]

We have hunches, certainly about danger, that are wrong all the time. It’s basic survival logic – if you have two creatures, one who is a little paranoid and worries about things that often don’t happen, and one that is totally carefree and fears nothing, the former has higher odds of survival.
Scott Berkun, blog

domenica 24 maggio 2009

Java: Jar classpath [ENG]

So you have these jars in your c:\tmp\ directory:
main.jar
lib1.jar
lib2.jar
and you issue the command:
c:\tmp\> java -cp lib1.jar;lib2.jar -jar main.jar
Right?
Wrong! All you get is the dreaded java.lang.NoClassDefFound
exception. As I recently found out (much to my chagrin: I'm supposed to be a veteran Java developer) the -jar option is mutually exclusive of -classpath.
Why did Sun made it this way? Why java.exe does not complain and provide help in that case? Beats me. How you work this around? You have to specify the classpath in your MANIFEST.MF file packaged with your jar.
So to make the jar work in the previous example:
java -jar main.jar
and, in your MANIFEST.MF:
Class-Path: lib1.jar lib2.jar

mercoledì 20 maggio 2009

Hibernate: generate toString() and equals() [ENG]

If you want Hibernate Tools to generate toString() and equals() methods picking some fields from .hbm.xml mapping files you may use these meta-tags:
<id name="id" type="long">
        <meta attribute="use-in-tostring">true</meta>
        <meta attribute="use-in-equals">true</meta>
        <column name="id" />
</id>
<property name="number" type="string" unique-key="UK_t_table1">
        <meta attribute="use-in-tostring">true</meta>
        <column name="number" length="5" />
</property>

giovedì 14 maggio 2009

Unit Testing Sucks! [ENG]

Not surprisingly, not everyone loves unit testing. Here's one.
Though not everything in this post is to be agreed with, there's a whole lot of brain food in here.

venerdì 17 aprile 2009

Java: top 10 unused features [ENG]

Interesting post on Javalobby: Top 10 unused Java features.