Contenu connexe Similaire à Paradigmas de linguagens de programacao - aula#9 (20) Plus de Ismar Silveira (15) Paradigmas de linguagens de programacao - aula#91. Paradigmas de Linguagens de Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br 2. AOP A Programação Orientada por Aspectos (AOP), proposta por Gregor Kiczales em 1997 tem por objetivo modularizar decisões de projeto que não podem ser adequadamente definidas por meio da POO. Na AOP, requisitos de sistemas são modelados por meio de classes , que implementam os objetos do mundo real, e aspectos , que implementam requisitos transversais do sistema 4. Exemplo: Hello, world (que novidade!) class Hello{ public static void main(String args[ ]) { System.out.println("Início da Monitoração"); System.out.println("Hello World!"); System.out.println("Final da monitoração"); } } Tangled code 5. Exemplo: Hello, world (novidade!) aspect Monitor{ pointcut impressao(): execution ( public static void Hello.main(String [])); before (): impressao () { System.out.println("Inicio da impressao"); } after (): impressao () { System.out.println("Final da impressao"); } } class Hello{ public static void main(String args[ ]){ System.out.println("Hello World!"); } } Hello.java Monitor.aj 8. Exemplo: Logger AOP public aspect LoggingAspect { pointcut publicMethods(): execution (public * * (..)); pointcut logObjectCalls() : execution ( * Logger. * (..)); pointcut loggableCalls() : publicMethods() && ! logObjectCalls(); before (): loggableCalls() { Logger.logEntry( thisJoinPoint.getSignature().toString()); } after () returning: loggableCalls() { Logger.logNormalExit( thisJoinPoint.getSignature().toString()); } 10. Implementação com RMI public class Complaint implements java.io.Serializable { private String description; private Person complainer; ... public Complaint(String description, Person complainer, ...) { ... } public String getDescription() { return this.description; } public Person getComplainer() { return this.complainer; } public void setDescription(String desc) { this.description = desc; } public void setComplainer(Person complainer) { this.complainer = complainer; } ... } public class HealthWatcherFacade implements IFacade { public void update(Complaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException { ... } public static void main(String[] args) { try { HealthWatcherFacade facade = HealthWatcherFacade.getInstance(); System.out.println("Creating RMI server..."); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind("/HealthWatcher"); System.out.println("Server created and ready."); } catch (RemoteException rmiEx) {... } catch (MalformedURLException rmiEx) { ...} catch(Exception ex) {... } } } public class ServletUpdateComplaintData extends HttpServlet { private IFacade facade; public void init(ServletConfig config) throws ServletException { try { facade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher"); } catch (java.rmi.RemoteException rmiEx) {...} catch (java.rmi.NotBoundException rmiEx) {...} catch (java.net.MalformedURLException rmiEx) {...} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... facade.update(complaint); ... } ... } public class Person implements java.io.Serializable { private String nome; ... public Person(String nome, …) { this.nome = nome; … } public String getNome() { return nome; } … } public interface IFacade extends java.rmi.Remote { public void updateComplaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException, RemoteException; . . . } 11. Visão geral do código OO public class Complaint implements java.io.Serializable { private String description; private Person complainer; ... public Complaint(String description, Person complainer, ...) { ... } public String getDescription() { return this.description; } public Person getComplainer() { return this.complainer; } public void setDescription(String desc) { this.description = desc; } public void setComplainer(Person complainer) { this.complainer = complainer; } ... } public interface IFacade extends java.rmi.Remote { public void updateComplaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException, RemoteException; . . . } public class HealthWatcherFacade implements IFacade { public void update(Complaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException { ... } public static void main(String[] args) { try { HealthWatcherFacade facade = HealthWatcherFacade.getInstance(); System.out.println("Creating RMI server..."); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind("/HealthWatcher"); System.out.println("Server created and ready."); } catch (RemoteException rmiEx) {... } catch (MalformedURLException rmiEx) { ...} catch(Exception ex) {... } } } public class Person implements java.io.Serializable { private String nome; ... public Person(String nome, …) { this.nome = nome; … } public String getNome() { return nome; } … } public class ServletUpdateComplaintData extends HttpServlet { private IFacade facade; public void init(ServletConfig config) throws ServletException { try { facade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher"); } catch (java.rmi.RemoteException rmiEx) {...} catch (java.rmi.NotBoundException rmiEx) {...} catch (java.net.MalformedURLException rmiEx) {...} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... facade.update(complaint); ... } ... } Código RMI 14. Implementação em AspectJ public class Complaint { private String description; private Person complainer; ... public Complaint(String description, Person complainer, ...) { ... } public String getDescription() { return this.description; } public Person getComplainer() { return this.complainer; } public void setDescription(String desc) { this.description = desc; } public void setComplainer(Person complainer) { this.complainer = complainer; } } public class ServletUpdateComplaintData extends HttpServlet { private HealthWatcherFacade facade; public void init(ServletConfig config) throws ServletException { try { facade = HealthWatcherFacade.getInstance(); } catch (Exception ex) {...} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... } ... } public class Person { private String nome; ... public Person(String nome, ...) { this.matricula = matricula; ... } public String getNome() { return nome; } ... } public class HealthWatcherFacade { public void update(Complaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException { ... } } public interface IFacade extends java.rmi.Remote { public void updateComplaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException, RemoteException; . . . } aspect DistributionAspect { declare parents: HealthWatcherFacade implements IFacade; declare parents: Complaint || Person implements java.io.Serializable; public static void HealthWatcherFacade.main(String[] args) { try { HealthWatcherFacade facade = HealthWatcherFacade.getInstance(); System.out.println("Creating RMI server..."); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind("/HealthWatcher"); System.out.println("Server created and ready."); } catch (RemoteException rmiEx) {...} catch (MalformedURLException rmiEx) {...} catch(Exception ex) {...} } //segue... private IFacade remoteFacade; pointcut facadeMethodsExecution(): within(HttpServlet+) && execution(* HealthWatcherFacade.*(..)) && this(HealthWatcherFacade); before(): facadeMethodsExecution() { prepareFacade();} private synchronized void prepareFacade() { if (healthWatcher == null) { try { remoteFacade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher"); } catch (java.rmi.RemoteException rmiEx) {...} catch (java.rmi.NotBoundException rmiEx) {...} catch (java.net.MalformedURLException rmiEx) {...} } void around(Complaint complaint) throws TransactionException, RepositoryException ObjectNotFoundException,ObjectNotValidException: facadeRemoteExecutions() && args(complaint) && call(void update(Complaint)) { try { remoteFacade.update(complaint); } catch (RemoteException rmiEx) {...} } } 15. Disque-Saúde em AOP : Visão geral public class Complaint { private String description; private Person complainer; ... public Complaint(String description, Person complainer, ...) { ... } public String getDescription() { return this.description; } public Person getComplainer() { return this.complainer; } public void setDescription(String desc) { this.description = desc; } public void setComplainer(Person complainer) { this.complainer = complainer; } } public interface IFacade extends java.rmi.Remote { public void updateComplaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException, RemoteException; . . . } public class HealthWatcherFacade { public void update(Complaint complaint) throws TransactionException, RepositoryException, ObjectNotFoundException, ObjectNotValidException { ... } } public class Person { private String nome; ... public Person(String nome, ...) { this.matricula = matricula; ... } public String getNome() { return nome; } ... } public class ServletUpdateComplaintData extends HttpServlet { private HealthWatcherFacade facade; public void init(ServletConfig config) throws ServletException { try { facade = HealthWatcherFacade.getInstance(); } catch (Exception ex) {...} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... } ... } aspect DistributionAspect { declare parents: HealthWatcherFacade implements IFacade; declare parents: Complaint || Person implements java.io.Serializable; public static void HealthWatcherFacade.main(String[] args) { try { HealthWatcherFacade facade = HealthWatcherFacade.getInstance(); System.out.println("Creating RMI server..."); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind("/HealthWatcher"); System.out.println("Server created and ready."); } catch (RemoteException rmiEx) {...} catch (MalformedURLException rmiEx) {...} catch(Exception ex) {...} } private IFacade remoteFacade; pointcut facadeMethodsExecution(): within(HttpServlet+) && execution(* HealthWatcherFacade.*(..)) && this(HealthWatcherFacade); before(): facadeMethodsExecution() { prepareFacade();} private synchronized void prepareFacade() { if (healthWatcher == null) { try { remoteFacade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher"); } catch (java.rmi.RemoteException rmiEx) {...} catch (java.rmi.NotBoundException rmiEx) {...} catch (java.net.MalformedURLException rmiEx) {...} } void around(Complaint complaint) throws TransactionException, RepositoryExceptio n ObjectNotFoundException,ObjectNotValidException: facadeRemoteExecutions() & & args(complaint) && call(void update(Complaint)) { try { remoteFacade.update(complaint); } catch (RemoteException rmiEx) {...} } } Sistema local Aspectos de Distribuição para RMI