Paradigmas de Linguagens de Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
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
Benefícios da AOP Melhora a modularidade de  crosscutting concerns Exemplos: distribuição, gerenciamento de dados, controle de concorrência, tratamento de exceções, logging, debugging, … Auxilia na  separation of concerns “ Programs that clearly express the design structure they implement are easier to maintain” Gregor Kiczales
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
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
Weaving Hello.java Monitor.aj ajc Hello.class Monitor.class
Exemplo: Logger OO Exemplo de Tirelo et al. – JAI2004
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()); }
Exemplo: Disque-saúde com RMI Exemplo de Sérgio Soares e Paulo Borba - UFPE
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; . . .  }
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
Problemas na implementação OO Tangled code  (código entrelaçado) lógicade distribuição misturado com lógicade negócio Spread code  (código espalhado) código de distribuição em várias classes distribuição é um  crosscutting concern Difícil de manter e reusar mudanças no protocolo de distribuição (RMI   CORBA    EJB ) são invasivas
Disque-saúde com AOP
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) {...} } }
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

E:\Plp 2009 2\Plp 9

  • 1.
    Paradigmas de Linguagensde Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
  • 2.
    AOP A ProgramaçãoOrientada 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
  • 3.
    Benefícios da AOPMelhora a modularidade de crosscutting concerns Exemplos: distribuição, gerenciamento de dados, controle de concorrência, tratamento de exceções, logging, debugging, … Auxilia na separation of concerns “ Programs that clearly express the design structure they implement are easier to maintain” Gregor Kiczales
  • 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
  • 6.
    Weaving Hello.java Monitor.ajajc Hello.class Monitor.class
  • 7.
    Exemplo: Logger OOExemplo de Tirelo et al. – JAI2004
  • 8.
    Exemplo: Logger AOPpublic 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()); }
  • 9.
    Exemplo: Disque-saúde comRMI Exemplo de Sérgio Soares e Paulo Borba - UFPE
  • 10.
    Implementação com RMIpublic 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 docó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
  • 12.
    Problemas na implementaçãoOO Tangled code (código entrelaçado) lógicade distribuição misturado com lógicade negócio Spread code (código espalhado) código de distribuição em várias classes distribuição é um crosscutting concern Difícil de manter e reusar mudanças no protocolo de distribuição (RMI  CORBA  EJB ) são invasivas
  • 13.
  • 14.
    Implementação em AspectJpublic 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