mercoledì 27 gennaio 2010

Deploy scripts: ANT [ENG]

Notwithstanding its ugly XML syntax, Apache ANT is a powerful tool. It is generally used for automatic repetitive tasks (such as deploying applications). Here's one of my scripts, a little bit trimmed down (so it may not work as it is).




  

  

  
    
    
    
    

    
    
    

  
  
    
      
      
      
    
  

  
    
      
    
  

  
    
      
    
  

  
    
      
    
  

  
    
    
  
  
  
    

    
    

    
    

    
    
    
    
      
    

    
    
    

    
    
    

    
    
    
      
        
        
      
    

    
    
    
      
    

    
    
    

    
    

  


NOTE: sorry for the scrambled formatting of XML listing... I'm trying to work it out

Deploy scripts: DOS batch file [ENG]

From time to time I need to deploy a new web application to a server. There are many ways to do so, but I found this small Windows batch script mighty useful.
  1. stops the application server service
  2. copy the files from the directory ./latest to the proper destination directory
  3. starts the application server service again
  4. renames the directory ./latest with the current date and time (in case you have to rollback)
@echo off
echo Stopping "service x"...
sc \\localhost stop "service x"
echo Deploying files...
xcopy ".\latest\*.*" "destination_dir" /Y /S
echo Restarting "service x"...
sc \\localhost start "schd service"
set Filename=%Date:~-4,4%%Date:~-7,2%%Date:~-10,2%_%Time:~0,2%%Time:~3,2%_application_name
echo Renaming "latest" folder to "%Filename%"...
rename latest %Filename%
pause

JSP functions [ENG]

The usage of JSP scriptlet is discouraged, and for many good reasons. But just in case you didn't know you might add methods to the implicit class generated from the JSP. I found this legacy code:
  try
  {
    strPagina = CString.isNullReplace(request.getParameter("pic_pagina")).trim();
    if (strPagina.length() == 0)
    {
     strPagina = CString.isNullReplace(authentBean.getString("pic_pagina")).trim();
    }
    authentBean.set("pic_pagina", strPagina);
    strDatePic = CString.isNullReplace(request.getParameter("pic_date")).trim();
    if (strDatePic.length() == 0)
    {
     strDatePic = CString.isNullReplace(authentBean.getString("pic_date")).trim();
    }
    authentBean.set("pic_data", strDatePic);    
    strEventPic = CString.isNullReplace(request.getParameter("pic_event")).trim();
    if (strEventPic.length() == 0)
    {
     strEventPic = CString.isNullReplace(authentBean.getString("pic_event")).trim();
    }
    authentBean.set("pic_event", strEventPic);    
  }
  catch (Exception e)
  {
    throw new Exception(param_error);
  }
and felt compelled to refactor it:
  try
  {
    set("pic_pagina", authentBean, request);
    set("pic_date", authentBean, request);
    set("pic_event", authentBean, request);
  }
  catch (Exception e)
  {
  throw new Exception(param_error);
  }
How did I implement the set method?
<%!
    void set(String parameterName, ArrayBean authentBean, HttpServletRequest request)
    {
        String strValue = CString.isNullReplace(request.getParameter(parameterName)).trim();
     if (strValue.length() == 0)
     {
         strValue = CString.isNullReplace(authentBean.getString(parameterName)).trim();
     }
     authentBean.set(parameterName, strValue);
    }
%>
Please note the <%! opening tag.
See also JSP Tutorial

venerdì 18 dicembre 2009

Eclipse: Open Extern Plugin

Per aprire una risorsa direttamente in Explorer, o per aprire la shell:
Sito: http://code.google.com/p/openextern
Update site: http://openextern.googlecode.com/svn/trunk/openextern_update

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