Results 1 to 9 of 9

Thread: Using Around Advice for error handling

  1. #1
    Join Date
    Apr 2008
    Posts
    23

    Default Using Around Advice for error handling

    Hi. I have the following

    Code:
    public void validateSoapMessage() throws SAXException, IOException{
    	
    		SchemaFactory factory = SchemaFactory
    				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    		Source schemaFile = new StreamSource(new File("d:\\request.xsd"));
    		Schema schema = factory.newSchema(schemaFile);
    		Validator validator = schema.newValidator();
    			
    	    BufferedReader in = new BufferedReader(new FileReader("infilename"));
    	    String str;
    	    while ((str = in.readLine()) != null) {
                     System.out.print(str);
               }
    
    	    in.close();
    }
    Code:
    @Around("ExceptionPointcuts.validateSoapMessage()")
    	  public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
    	    // start stopwatch
    		
    		Object retVal = null;
    	    try{
    	    	retVal = pjp.proceed();
    	    }catch(SAXException ex){
    			logException(ex);
    			retVal = pjp.proceed();
    		}catch(IOException ex){
    			logException(ex);
                            retVal = pjp.proceed();
    		}
    	    
    	    return retVal;
    	  }
    Now when I step through in debug 'Validator validator = schema.newValidator();' throws an exception which gets caught in advice, I then call pjp.proceed for second time, but what this does is execute the first line of validateSoapMessage() when I want it to execute from BufferedReader in = ...

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    proceed doesn't mean proceed from where you are proceed. Proceed is basically stating continue with the actual method call. It is basicly calling the validateSoapMessage method not resuming. What you want isn't possible...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2008
    Posts
    23

    Default

    There is no way of making this possible???

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Not with aspects.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2008
    Posts
    23

    Default

    I guess I will have to use try catch in validateSoapMessage(). thanks

  6. #6
    Join Date
    Apr 2008
    Posts
    23

    Default

    Is it possible to access local variables from within the advice method. For example I have the following:

    Code:
    BufferedReader in = null;
    		try{
    	    	in = new BufferedReader(new FileReader("infilename"));
    	    
    		    String str;
    		    while ((str = in.readLine()) != null) {
    	            System.out.print(str);
    	        }
    	    }catch(IOException ex){}
    	    finally{
    	    	try {
    				in.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	    }
    Could I access the 'in' variable from within the advice method so I can perform the code within the finally block within a @AfterThrowing method?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    No. You cannot access the internal variables...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Apr 2008
    Posts
    23

    Default

    Quote Originally Posted by Marten Deinum View Post
    No. You cannot access the internal variables...
    How does after finally advice work then? It is said this type of advice is typically used for releasing resources. If my understanding is correct then within advice you can only access the method parameters (of the method being intercepted) and the return object. Hence if you wished to release a resource this resource would have to be one of the method parameters or the return object. Is this correct?

  9. #9
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by vikrant1 View Post
    How does after finally advice work then? It is said this type of advice is typically used for releasing resources. If my understanding is correct then within advice you can only access the method parameters (of the method being intercepted) and the return object. Hence if you wished to release a resource this resource would have to be one of the method parameters or the return object. Is this correct?
    That's not correct. You can access either dependencies injected directly to the aspect (e.g. ThreadLocal objects that are used by the target method) or current joinpoint data (not only method arguments and return value but called object itself).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •