Top Banner
NSY102 1 Proxy et plus s de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle version : 4 Septembre 2005
31

NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

Apr 04, 2015

Download

Documents

Zoé Beguin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1021

Proxy et plus

Notes de cours java : le langage : Introduction, approche impérative

Cnam Parisjean-michel Douin, douin au cnam point fr

Nouvelle version : 4 Septembre 2005

Page 2: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1022

Sommaire

• La famille Proxy– Les Parents [Gof95]

– Les descendants• VirtualProxy• RemoteProxy• DynamicProxy

Page 3: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1023

Bibliographie utilisée

• Design Patterns, catalogue de modèles de conception réutilisables

de Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides [Gof95]International thomson publishing France

Mark Grand

• http://www.ida.liu.se/~uweas/Lectures/DesignPatterns01/panas-pattern-hatching.ppt

• http://www.mindspring.com/~mgrand/pattern_synopses.htm

Page 4: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1024

Objectifs

• Proxy-Procuration– Fournit à un tiers objet un mandataire, pour contrôler l ’accès à cet objet

• Alias– Subrogé (surrogate)

• Motivation– contrôler– différer– optimiser– sécuriser– ...

Page 5: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1025

Diagramme UML

– L ’original

Subject

request()…

RealSubject

request()…

Proxy

request()…

realSubject

realSubject->request();

Page 6: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1026

Une instance possible du pattern proxy

• http://www.theserverside.com/tt/articles/content/JIApresentations/Kabutz.pdf

Page 7: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1027

le Service/Sujet

public interface Service{

/** Offrir un bouquet de fleurs

* @param bouquet le bouquet

* @return réussite ou échec ...

*/

public boolean offrir(Bouquet b);

}

Service

offrir(Bouquet b)…

Page 8: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1028

Une implémentation du Service

public class ServiceImpl implements Service{

public boolean offrir(Bouquet b){

System.out.println(" recevez ce bouquet : " + b);

return true;

}

}

ServiceImpl

offrir(Bouquet b)…

Page 9: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY1029

Offrir en « direct »

Service service = new ServiceImpl();

assert service.offrir(unBouquet) == true;

Client Console...offrir(unBouquet)

Page 10: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10210

Le mandataire

public class ServiceProxy implements Service{

private ServiceImpl service;

public ServiceProxy(){

this.service = new ServiceImpl();

}

public boolean offrir(Bouquet bouquet){

boolean résultat;

System.out.print(" par procuration : ");

résultat = service.offrir(bouquet);

return résultat;

} }

ServiceProxy

offrir(Bouquet b)…

service.offrir(b)

Page 11: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10211

Offrir par l ’intermédiaire de

Service service = new ServiceProxy();

service.offrir(unBouquet);

Client Console ...offrir(unBouquet) offrir(unBouquet)

ServiceProxy

Page 12: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10212

VirtualProxy

• Création d ’objets « lourds » à la demande

– Coût de la création d ’une instance– Création d ’un objet seulement si nécessaire

Page 13: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10213

Virtual (lazy) Proxy

public class VirtualProxy implements Service{

private Service service;

public boolean offrir(Bouquet bouquet){

boolean résultat;

System.out.print(" par procuration, virtualProxy (lazy) : ");

Service service = getServiceImpl();

résultat = service.offrir(bouquet);

return résultat;

}

Page 14: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10214

VirtualProxy & Introspection

private Service getServiceImpl(){

if(service==null){

try{

Class classe = Class.forName("ServiceImpl");

service = (Service) classe.newInstance();

}catch(Exception e){

e.printStackTrace();

}

}

return service;

}

Page 15: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10215

La « famille » avec BlueJ

Page 16: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10216

Sécurité … VirtualProxy bis

• URLClassLoader hérite de SecureClassLoader– Il peut servir à « vérifier » un code avant son exécution– souvent associé à une stratégie de sécurité

private Service getServiceImpl(){

if(service==null){

try{

ClassLoader classLoader = new URLClassLoader( new URL[]{new URL("http://jfod.cnam.fr/nsy102/proxy_pattern.jar")});

Class classe = Class.forName("ServiceImpl",true, classLoader); service = (Service) classe.newInstance();

}catch(Exception e){

e.printStackTrace();

}

}

return service;

}

Page 17: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10217

Résumé, bilan intermédiaire

• Procuration à un tiers– Proxy

• contrôle complet de l ’accès au sujet réel

– VirtualProxy• des contraintes de coûts en temps d ’exécution• liée à une stratégie de sécurité

– ? Connaissance à l ’exécution du sujet réel ?

– ? Procuration pour un appel distant en complète « transparence » ?

Page 18: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10218

un cousin plutôt dynamique et standard

• extrait de l ’API du J2SE Dynamic Proxy• http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/InvocationHandler.html• http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Proxy.html

– L ’interface à implémenter

interface java.lang.reflect.InvocationHandler;

– ne contient qu ’une seule méthode

Object invoke( Object proxy, Method m, Object[] args);

• proxy : le contexte de l ’appelé, XXXClassLoader• m : la méthode sélectionnée• args : les arguments de cette méthode

Page 19: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10219

DynamicEtProxy

public class DynamicEtProxy implements InvocationHandler{

private ServiceImpl service;

public DynamicEtProxy(){

this.service = new ServiceImpl();

}

public Object invoke(Object proxy, Method method, Object[] args)

throws Exception{

System.out.print(" par procuration dynamique : ");

return method.invoke(service, args);

}

}

Page 20: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10220

Dynamic Proxy

ClassLoader cl = Service.class.getClassLoader();

Service service = (Service) Proxy.newProxyInstance(cl, new Class[]{Service.class},

new DynamicEtProxy());

service.offrir(unBouquet);

Client Console ...offrir(unBouquet)

Proxy

invoke() invoke(service,args)

InvocationHandler

Page 21: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10221

Doit-on connaître le nom de la classe ?

• VirtualProxyBis, le retour

ClassLoader cl = new URLClassLoader( new URL[]{new URL("http://jfod.cnam.fr/csiml/proxy_pattern.jar")});

Class classe = Class.forName("DynamicEtProxy",true, cl);

InvocationHandler handler = (InvocationHandler)classe.newInstance();

Service service = (Service) Proxy.newProxyInstance(cl,

new Class[]{Service.class},

handler);

service.offrir(unBouquet);

}

• encore plus dynamique ? Plus générique ?

Page 22: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10222

DynamicProxy et le pattern décorateur

Page 23: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10223

Le petit dernier

• Une procuration à distance

Page 24: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10224

un petit dernier

• http://www.mindspring.com/~mgrand/pattern_synopses.htm

Page 25: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10225

Page 26: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10226

Java RMI utilise les DynamicProxy

• appel distant transparents• génération des « stubs »

Page 27: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10227

Pourquoi

Page 28: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10228

Exemple

Page 29: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10229

Page 30: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10230

Page 31: NSY102 1 Proxy et plus Notes de cours java : le langage : Introduction, approche impérative Cnam Paris jean-michel Douin, douin au cnam point fr Nouvelle.

NSY10231