[jboss-user] [JBoss AOP] - Re: NullPointerException at advised._getInstanceAdvisor();

robotics80 do-not-reply at jboss.com
Fri Dec 15 05:07:45 EST 2006


I have the main application in a file FileFinder.java that use a logger object to log its operations into a file. The FileFinder build this log object in a method init() and I want to create an aspect for logging interception that is able to intercept the log object build in method init() of FileFinder class and use this object to log operations.

A part of FileFinder.java code is:

public class FileFinder {

    private SqlMapClient sqlMapClient;
    private Log log;
    private IniReader ini;
    private InstanceManager instanceManager;
    private SessionTask sessionTask;
    private int errorReq = 0;
    private int discardedReq = 0;
    
    private static final String className = "FileFinder";
   
    /**
     * @throws DeferredException 
     * 
     */
    public FileFinder() throws DeferredException {
        init();
    }

    /**
     * @throws DeferredException 
     * 
     */
    private void init() throws DeferredException {
        String proc = className + ".init()";
        try {            
            ini = new IniReader(Env.getSqtpHome() + "etc/aicagw.properties");
            log = new Log(ini);
            log.logInit("AICAGW.FILEFINDER","BATCH");
            log.setPid();
            
            logInfo(proc,"START");
            
            long startTime = System.currentTimeMillis();
            
            //creazione file gestione esecuzione singola istanza del batch
            String workingFolder = ini.getPrivateProfileString("AICAGW.COMMON","WORKINGFOLDER","/");
            String instancefileName = "fileFinder";
            logInfo(proc,"file per gestione esecuzione singola istanza del batch: " + workingFolder + "/" + instancefileName);
            instanceManager = new InstanceManager(workingFolder,instancefileName);
                        
            //creazione sessione
            sessionTask = new SessionTask(log);
            sessionTask.setSessionName(""+startTime);
            
            //accesso a db tramite iBatis
            sqlMapClient = SqlMapClientConfig.getClient("AICAGW", "BATCH");
            
            logInfo(proc,"END");
        } catch (Exception e) {
            DeferredException de = CommonDeferred.wrapException(e);
            logErr(proc, de.toString());
            throw de;
        }
    }

//... and some other methods
}

A part of LogAspect.java code is:
public class LogAspect {
	private static final String className = "LogAspect";
	private static final String DBG_ERROR = "ERROR";
	private static final String DBG_WARNING = "WARNING";
	private static final String DBG_INFO = "INFO";
	private static final String DBG_TRACE = "TRACE";
	private String propertyFile;
	private Log log;
    private IniReader ini;
    
    /*
    private void init() throws Exception {
    	String proc = className + ".init()";
    	try {
    		ini = new IniReader(Env.getSqtpHome() + "etc/" + propertyFile);
            log = new Log(ini);
            log.logInit("AICAGW.FILEFINDER","BATCH");
            log.setPid();
		} catch (IOException e) {
			logErr(proc, e.getMessage());
			throw e;
		}
    }
    */
	
    public void setPropertyFile(String propertyFile) {
		this.propertyFile = propertyFile;
	}
	
	public void setLog(Log log) {
		this.log = log;
	}
	
	public String getName() {
		return "LogAspect";
	}
	
	public Object getLogger(Invocation invocation) throws Throwable{
		try{
			System.out.println("INGRESSO nel getLogger()");
			if (invocation instanceof FieldWriteInvocation) {
				FieldWriteInvocation fieldInvocation = (FieldWriteInvocation)invocation;
				setLog((Log)fieldInvocation.getValue());
			} else 
				System.out.println("Unknown Invocation!!");
			return invocation.invokeNext();
		} finally {
			System.out.println("USCITA dal getLogger()");
		}
	}
	
	public Object trace(Invocation invocation) throws Throwable{
		String proc = className + ".trace()";
		long startTime = System.currentTimeMillis();
		logInfo(proc, "START");
		logTrace(proc, invocationTypeValue(invocation));
		Object response = null;
		try {
			response = invocation.invokeNext();
			return response;	
		}catch(Throwable t) {
			long endTime = System.currentTimeMillis();
	        long elapsedTime = endTime - startTime;
			logErr(proc, t.getMessage() + " - Tempo di esecuzione (ms): " + elapsedTime);
	        throw t;
		}
		finally {
	        long endTime = System.currentTimeMillis();
	        long elapsedTime = endTime - startTime;
	        logTrace(proc,dumpInvocationResponse(invocation, response) + " - Tempo di esecuzione (ms): " + elapsedTime);
	        logInfo(proc, "END");
		}
	}

public String dumpInvocationResponse(Invocation invocation, Object response) {
// some code
}

private String invocationTypeValue (Invocation invocation) {
// some code
}

private void logErr(String proc, String message) {
    	System.out.println(DBG_ERROR + ": " + message);
    	if (log != null) {
            log.logWrite(log.DBG_ERROR, proc, message);
        }
    }
   
    private void logWarning(String proc, String message) {
    	System.out.println(DBG_WARNING + ": " + message);
    	if (log != null) {
            log.logWrite(log.DBG_WARNING, proc, message);
        }
    }
    
    private void logInfo(String proc, String message) {
    	System.out.println(DBG_INFO + ": " + message);
    	if (log != null) {
            log.logWrite(log.DBG_INFO, proc, message);
        }
    }
    
    private void logTrace(String proc, String message) {
    	System.out.println(DBG_TRACE + ": " + message);
    	if (log != null) {
            log.logWrite(log.DBG_TRACE, proc, message);
        }
    }
}



The jboss-aop.xml file is:

<!-- Declaring pointcuts -->
    
    
    
    


<!-- Declaring Aspects -->
    

<!-- Binding pointcuts to Aspects -->
    
        
     
     
    
        
    

In this way It works fine because I use the pointcut expression set(..) and a FieldWriteInvocation to intercept the log object when it is assigned in a log variable into FileFinder class.

But if I use the pointcut expression call(..) to intercept the construction of the Log object in the FileFinder.init() method, I got the NullPointerException like this:


N.B. Remember tha I run this file with load time weaving activated with option -javagent:..... JDK 1.5 and the latest version JBoss AOP 1.5.2.GA

Is there anyone that is able to solve this problem?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3994165#3994165

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3994165



More information about the jboss-user mailing list