Announcement Announcement Module
Collapse
No announcement yet.
Using Around Advice for error handling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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...

    Comment


    • #3
      There is no way of making this possible???

      Comment


      • #4
        Not with aspects.

        Comment


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

          Comment


          • #6
            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?

            Comment


            • #7
              No. You cannot access the internal variables...

              Comment


              • #8
                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?

                Comment


                • #9
                  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).

                  Comment

                  Working...
                  X