Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] RE: Aspects fail if program interacts with user

Hi All,

I have a program which consists of a number of aspects.

The main class can be run automatically, or it can present the user with an
interactive screen - depending on whether a parameter is passed on the
command line.

When the program is run with the parameter passed to it, it runs and the
aspects work fine.  Without the parameter the program creates a screen in
which the user sets parameters for the program. Once these parameters are
set the control of the program is passed back to the exact same point in the
program.  The problem is that now the aspects *do not* work.  It is as if
the run-time treats the program like there is a compiler error and refuses
to run the weaved aspects on the code.

Any help would be appreciated.

public class Controller {
	public 		DataCopier		dataCopy;
	public 		RptGenerator	rptGen;
    private static	UI			view;
    		static	Params 		params;

	public Controller(){
	    dataCopy = new DataCopier();
	}

	public void runReports(Params params){  ** ADVISED HERE **
	    if (params.isCopyData()) {
	        if(!dataCopy.copyFiles()) {		** ADVISED HERE **
	            return;
	        }
	    }

	    rptGen = new RptGenerator(params);
	    if (params.isCopyData()) {
	        rptGen.cleanData();		** ADVISED HERE **
	    }
	    rptGen.printReports();
	}
	
	public static void main(String[] s) {
	    Controller ctrl;
	    
	    if (s.length == 0) {
	// Interactive - DOES NOT RUN ADVICES
	      ctrl = new Controller();
	      view = new UI(ctrl);
            view.setVisible(true);
            view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
The UI class does the equivalent of params.loadParams();
And then calls ctrl.runReports(params)
*/
        } else if (s[0].regionMatches(true, 0, "auto", 0, 4)) {
	// Automatic - Advices run OK
            ctrl = new Controller();
            params = new Params();
    	    params.loadParams();

    	    ctrl.runReports(params);
        }
	}
}

UI Class :
public class UI extends JFrame {

    public static void main(String[] args) {
        UI view = new UI();
        
        view.setVisible(true);
        view.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
	/**
	 * This is the default constructor
	 */
	public UI() {
		super();
		initialize();
	}
	
	public UI(Controller rptCtrl) {
	    this();
	    this.rptCtrl = rptCtrl;
	}
	
	...
	code to set up GUI components
	...
		runButton.addActionListener(new
java.awt.event.ActionListener() { 
			public void
actionPerformed(java.awt.event.ActionEvent e) {    
				UI.this.hide();
				Params params = new Params();
				int currents =
((Integer)currentSpin.getValue()).intValue();
				int oustandings =
((Integer)outstandingSpin.getValue()).intValue();
				
				
			    	params.setNumCurrentPrints(currents);
				params.setNumOutstandingPrints(oustandings);
			    
			    	rptCtrl.runReports(params);
			}
		});
	}
}

Aspect :
public aspect MonitorAspect {
    private long startTime;
    private long endTime;
    private long diffTime;
    private	boolean	running = false;
    final ReportMonitor rptMon = new ReportMonitor();
    
    pointcut runReports() :
        execution(* Controller.runReports(..));
    
    pointcut copyFiles() :
        call(boolean DataCopier.copyFiles());
    
    pointcut cleanData() :
        call(void RptGenerator.cleanData());

    before() : runReports() {
        startTime = System.currentTimeMillis();
        
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        if(!running) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ReportMonitor.showReportProgress(rptMon);
                }
            });
        }
        running = true;
    }
    
    after() returning : runReports() {
        endTime = System.currentTimeMillis();
        diffTime = endTime - startTime;
        long hrDiv = 60*60*1000;
        long hours = diffTime/hrDiv;
        long minDiff = diffTime%hrDiv;
        
        long minDiv = 60*1000;
        long minutes = minDiff/minDiv;
        long secDiff = minDiff%minDiv;
        
        float seconds = (float)secDiff/(1000.0f);
        
        rptMon.appendOutputMessage("\nProgram ran for :");
        rptMon.appendOutputMessage(hours + " hours\t");
        rptMon.appendOutputMessage(minutes + " minutes\t");
        rptMon.appendOutputMessage(seconds + " seconds");
    }
    
    after() throwing(Exception e) : runReports() {
        endTime = System.currentTimeMillis();
        diffTime = endTime - startTime;
        long hrDiv = 60*60*1000;
        long hours = diffTime/hrDiv;
        long minDiff = diffTime%hrDiv;
        
        long minDiv = 60*1000;
        long minutes = minDiff/minDiv;
        long secDiff = minDiff%minDiv;
        
        float seconds = (float)secDiff/(1000.0f);
        
        rptMon.appendOutputMessage("EXCEPTION After :");
        rptMon.appendOutputMessage(hours + " hours\t");
        rptMon.appendOutputMessage(minutes + " minutes\t");
        rptMon.appendOutputMessage(seconds + " seconds");
        rptMon.appendOutputMessage(e.getMessage());
    }

    before() : copyFiles() {
        rptMon.appendOutputMessage("Copying files");
    }
    
    after() returning(boolean copyOK) : copyFiles() {
        if(copyOK) {
            rptMon.appendOutputMessage("files copied and validated");
        } else {
 
rptMon.appendOutputMessage("******************************************");
            rptMon.appendOutputMessage("* ERROR : Could not copy files");
            rptMon.appendOutputMessage("*");
            rptMon.appendOutputMessage("*  Click Restart to try again");
 
rptMon.appendOutputMessage("******************************************");
        }
    }
    
    before() : cleanData() {
        rptMon.appendOutputMessage("Cleaning Data");
    }
    
    after() : cleanData() {
        rptMon.appendOutputMessage("Data has been cleaned");
    }
}


* ** *** ** * ** *** ** * ** *** ** * 
This email and any files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they 
are addressed. 
Any views or opinions presented are solely those of the author, and do not necessarily 
represent those of ESB. 
If you have received this email in error please notify the sender. 
 
Although ESB scans e-mail and attachments for viruses, it does not guarantee 
that either are virus-free and accepts no liability for any damage sustained 
as a result of viruses. 
 
* ** *** ** * ** *** ** * ** *** ** *



Back to the top