miércoles, 7 de julio de 2010

Buenos videos de Uncle Bob (Robert Martin, autor de Clean Code)

http://www.objectmentor.com/videos/video_index.html

lunes, 5 de julio de 2010

Exceptions de jetty para retornar http status codes

Usando jetty, puedes arrojar un status code, con mensaje incluido, de la sgte manera:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
throw new HttpException(404, "not found jeje");
}

jueves, 1 de julio de 2010

Wrap your Exception as a RuntimeException when it is not of your method's business

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.


package exceptions;

public class RuntimeExceptionTest {

public class TestException extends Exception {

}

public static void main(String[] args) {
RuntimeExceptionTest test = new RuntimeExceptionTest();
try {
test.throwWrappedTestExceptionAsRuntimeException();
} catch (Exception e) {
System.out.println("exception catched:\n");
e.printStackTrace();
}
}

private void throwTestException() throws TestException {
throw new TestException();
}


public void throwWrappedTestExceptionAsRuntimeException() {
try {
throwTestException();
} catch (TestException e) {
throw new RuntimeException(e);
}
}
}

Notification rather than logging

¿Te ha pasado que el log ensucia?:

Location location = tracker.getCurrentLocation();
for (Filter filter : filters) {
filter. selectFor(location);
if (logger. isInfoEnabled()) {
logger. info("Filter " + filter.getName() + ", " + filter.getDate()
+ " selected for " + location.getName()
+ ", is current: " + tracker.isCurrent(location));
}
}


Solución:

Location location = tracker.getCurrentLocation();
for (Filter filter : filters) {
filter. selectFor(location);
support.notifyFiltering(tracker, location, filter);
}
Otros beneficios:
  • Mientras programas, no te preocupas de escribir un string concatenando objetos, déjalo para el final.
  • El traspaso del software al área de soporte de tu empresa se complementará dándoles la clase Support, en ella están todos los monitoreos o alarmas que deben capturar.
  • No debes inicializar un Logger de log4j en cada clase que creas (sino sólo en caso de que quieras hacer un log.debug mientras programas)
  • Los logs dejan de ser un string que se escribe "a la rápida", pues deben ser pensados realmente como solución a un requerimiento implícito del departamento de operaciones de tu empresa.
  • En cada método de support no sólo puedes poner un logger en disco, sino también un registro en BD o cualquier otra cosa, usa la encapsulación.

Ref:

lunes, 21 de junio de 2010

Cómo convertir encoding en java tipo iconv, ej: UTF-8 a latin1


import org.apache.commons.io.IOUtils;

public class Iconv {

public static void iconv(InputStream in, OutputStream out, Charset inCharset, Charset outCharset) throws IOException {
InputStreamReader reader = new InputStreamReader(in,inCharset);
OutputStreamWriter writer = new OutputStreamWriter(out,outCharset);
IOUtils.copy(reader,writer);
writer.close();
}



Cómo se usa:


public static void main(String[] args) throws Exception {
utf8ToLatin1();
latin1ToUtf8();
}

private static void latin1ToUtf8() throws Exception {
FileInputStream in = new FileInputStream("/home/pablo/Desktop/workspace/test/files/textoLatin1.txt");
FileOutputStream out = new FileOutputStream("/tmp/textoLatin1ToUTF8.txt");
iconv(in, out, Charset.forName("latin1"), Charset.forName("UTF-8"));
out.close();
}

private static void utf8ToLatin1() throws Exception {
FileInputStream in = new FileInputStream("/home/pablo/Desktop/workspace/test/files/textoUTF8.txt");
FileOutputStream out = new FileOutputStream("/tmp/textoUTF8ToLatin1.txt");
iconv(in, out, Charset.forName("UTF-8"), Charset.forName("latin1"));
out.close();
}


Entrada:


pablo@pablo-desktop:~/Desktop/workspace/test/files$ file --mime texto*
textoLatin1.txt: text/plain; charset=iso-8859-1
textoUTF8.txt: text/plain; charset=utf-8


Resultado:

pablo@pablo-desktop:~/Desktop/workspace/test/files$ file --mime /tmp/texto*
/tmp/textoLatin1ToUTF8.txt: text/plain; charset=utf-8
/tmp/textoUTF8ToLatin1.txt: text/plain; charset=iso-8859-1

martes, 15 de junio de 2010

Cómo ver el contenido de un jar para saber donde esta una clase que se deba incluir

pablo@pablo-desktop:~/bea/user_projects/workspaces/workSpaceStudio/TimerService$ jar -tf /home/pablo/bea/wlserver_10.0/server/lib/weblogic.jar | grep ApplicationException
com/sun/java/xml/ns/javaee/ApplicationExceptionType$1.class
com/sun/java/xml/ns/javaee/ApplicationExceptionType$Factory.class
com/sun/java/xml/ns/javaee/ApplicationExceptionType.class
com/sun/java/xml/ns/javaee/impl/ApplicationExceptionTypeImpl.class
schemacom_bea_xml/javaname/com/sun/java/xml/ns/javaee/ApplicationExceptionType.xsb
weblogic/application/ApplicationException.class
weblogic/j2ee/descriptor/ApplicationExceptionBean.class
weblogic/j2ee/descriptor/ApplicationExceptionBeanImpl$Helper.class
weblogic/j2ee/descriptor/ApplicationExceptionBeanImpl$SchemaHelper.class
weblogic/j2ee/descriptor/ApplicationExceptionBeanImpl$SchemaHelper2.class
weblogic/j2ee/descriptor/ApplicationExceptionBeanImpl.class
weblogic/j2ee/descriptor/ApplicationExceptionBeanImplBeanInfo.class
weblogic/management/ApplicationException.class
pablo@pablo-desktop:~/bea/user_projects/workspaces/workSpaceStudio/TimerService$