martedì 13 novembre 2012

Maven Archetype for simple JEE6 Application on JBoss AS 7.1.1

Here is the Maven Archetype for a simple JEE6 Web Application on JBoss AS 7.1.1 http://search.maven.org/#artifactdetails%7Corg.jboss.spec.archetypes%7Cjboss-javaee6-webapp-archetype%7C7.1.1.Final%7Cmaven-archetype

giovedì 24 maggio 2012

Log user info with log4j on Spring + JSF webapp

You can easily log information about the logged user in a Spring + JSF web application by using a simple Servlet filter and log4j MDC Mapped Diagnostic Context.
This can be useful when you have to debug a web application where logs from different users are interwined.

I have created a small example, you could check it out with:

svn checkout http://lbonacina-experiments.googlecode.com/svn/trunk/SpringAuth spring-auth-read-only

It is a simple Web Application build with Spring 3.1 + Spring Authentication + JSF 2 + H2 in-memory DB; there is a login JSP page and a couple of JSF pages with some backing beas actions. There is also a Servlet Filter, it just gets information from the SecurityContext (the logged User) and add a small info (the logged User id) to the log4j MDC.

public class LogLoggedUserFilter implements Filter {

    protected Logger log = Logger.getLogger(this.getClass().getName());
    
    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig fc) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {

        SecurityContext sc = SecurityContextHolder.getContext();
        if (sc != null) {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            MDC.put("lu", user.getId());
        }
        
        fc.doFilter(sr, sr1);
    }


on the log4j ConversionPatter the pattern [%X{lu}] print out the info ('lu' is the key added to the MDC) between square brackets:

log4j.appender.logfile.layout.ConversionPattern=%d{HH:mm:ss} %-5p [%X{lu}] - %-30.30c{1} - %m%n

 if necessary a simple post processing could extract from the log the entries of a single user to simplify the debugging/tracing

venerdì 25 novembre 2011

how to show full command line of a Tomcat process

a quick way to see the full command line of a Tomcat process and view all the -D options

 /usr/ucb/ps -auxwww | grep -i <user> | grep -i java



giovedì 6 ottobre 2011

quick script to drop all objects in an Oracle 10 Schema

begin
for i in (select * from user_objects where object_type in ('TABLE')) loop
   dbms_output.put_line('drop '||i.object_type||' '||i.object_name || ' CASCADE CONSTRAINTS') ;
   execute immediate 'drop '||i.object_type||' '||i.object_name || ' CASCADE CONSTRAINTS';
end loop;
for i in (select * from user_objects where object_type in ('VIEW','SEQUENCE','DATABASE LINK','PACKAGE','PACKAGE BODY', 'TRIGGER', 'FUNCTION','PROCEDURE', 'SYNONYM')) loop
   dbms_output.put_line('drop '||i.object_type||' '||i.object_name ) ;
   execute immediate 'drop '||i.object_type||' '||i.object_name ;
end loop;
execute immediate 'purge recyclebin' ;
end;

venerdì 5 agosto 2011

HSQLDB and Oracle Compatibility

in HSQLDB 2.x you can force Oracle compatibility mode with the command SET DATABASE SQL SYNTAX ORA TRUE or with the connection property sql.syntax_ora=true.
when using Spring to create a embedded datasource you can place the SET command at the top of  the schema.sql file.
Oracle compatibility details : http://hsqldb.org/doc/2.0/guide/deployment-chapt.html#N1431F
(still no support for CUBE query)

Etichette: ,

spring + junit + mockito : automatically mock @Autowired

the idea came from this post: http://java.dzone.com/tips/automatically-inject-mocks
the only problem i've found with this setup is that i need to manually reset mock objects between tests because mocks are created only once.
but then i found out the @DirtiesContext annotation which forces the destruction and recreation of the Spring context after each test, so new mocks are created each time.


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContextTest.xml")
public class MainControllerTest {

    @Autowired
    MainController mainController ;

    MyService myService ;

    @Before
    public void setupTest() {

        myService = (MyService)ReflectionTestUtils.getField(mainController,"myService") ;
    }

    @Test
    @DirtiesContext
    public void testMock1() throws Exception {
        when(myService.getTestString()).thenReturn("MOCK!") ;
        Assert.assertEquals("MOCK!",mainController.getString()) ;
        verify(myService).doSomething("MOCK!");
    }

    @Test
    @DirtiesContext
    public void testMock2() throws Exception {
        when(myService.getTestString()).thenReturn("MOCK!") ;
        verify(myService,never()).doSomething("MOCK!");
    }
}

Etichette: , ,