diff --git a/demoCoursAL/.classpath b/demoCoursAL/.classpath new file mode 100644 index 0000000..fd14ee7 --- /dev/null +++ b/demoCoursAL/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/demoCoursAL/.gitignore b/demoCoursAL/.gitignore new file mode 100644 index 0000000..4a95481 --- /dev/null +++ b/demoCoursAL/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/build/ diff --git a/demoCoursAL/.project b/demoCoursAL/.project new file mode 100644 index 0000000..2b2c38e --- /dev/null +++ b/demoCoursAL/.project @@ -0,0 +1,17 @@ + + + demoCoursAL + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/demoCoursAL/.settings/org.eclipse.jdt.core.prefs b/demoCoursAL/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/demoCoursAL/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/demoCoursAL/build.xml b/demoCoursAL/build.xml new file mode 100755 index 0000000..ecb4913 --- /dev/null +++ b/demoCoursAL/build.xml @@ -0,0 +1,196 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/demoCoursAL/jboss-ejb-client.properties b/demoCoursAL/jboss-ejb-client.properties new file mode 100644 index 0000000..6128a44 --- /dev/null +++ b/demoCoursAL/jboss-ejb-client.properties @@ -0,0 +1,9 @@ +endpoint.name=client-endpoint +remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false + +remote.connections=default + +remote.connection.default.host=localhost +remote.connection.default.port = 8080 +remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false + diff --git a/demoCoursAL/jndi.properties b/demoCoursAL/jndi.properties new file mode 100755 index 0000000..ee5a96b --- /dev/null +++ b/demoCoursAL/jndi.properties @@ -0,0 +1 @@ +java.naming.factory.url.pkgs=org.jboss.ejb.client.naming diff --git a/demoCoursAL/src/Reflection.java b/demoCoursAL/src/Reflection.java new file mode 100644 index 0000000..7c18387 --- /dev/null +++ b/demoCoursAL/src/Reflection.java @@ -0,0 +1,52 @@ +import java.lang.reflect.* ; +import java.beans.Beans ; +import java.util.ArrayList ; + +public class Reflection { + + @SuppressWarnings("rawtypes") + public static void main(String args[]) { + ArrayList propBool=new ArrayList() ; + Class[] paramType= new Class[] { Boolean.TYPE }; + try { + Object obj = Beans.instantiate(null,args[0]) ; + Class laClasse = obj.getClass() ; + for (Method m : laClasse.getMethods()) { + if (m.getName().startsWith("is") && + m.getParameterTypes().length==0 && + m.getReturnType().toString().equals("boolean")) { + System.out.println("getter method found : "+m.getName()) ; + String setName="set"+m.getName().substring(2) ; + try { + Method methodeSet=laClasse.getMethod(setName,paramType) ; + propBool.add(methodeSet) ; + String propertyName = setName.substring(0,1).toLowerCase() + setName.substring(1); + System.out.println("boolean property found : "+propertyName) ; + } catch(NoSuchMethodException e1) { + System.out.println("No corresponding setter method found : "+setName) ; + } + } + } + Object param[] =new Boolean[1] ; + param[0]=new Boolean(true) ; + for (Method m : propBool) { + System.out.println("invoke method "+m.getName()+" with true parameter") ; + m.invoke(obj,param) ; + } + } catch (ArrayIndexOutOfBoundsException e) { + System.err.println("Erreur : java Reflection class name") ; + } catch (SecurityException e) { + System.err.println("exception raised...") ; + } catch(IllegalAccessException e) { + System.err.println("Access...") ; + } catch(InvocationTargetException e) { + System.err.println("Argument...") ; + } catch(IllegalArgumentException e) { + System.err.println("Argument...") ; + } catch(java.io.IOException e) { + System.err.println("IO...") ; + } catch(ClassNotFoundException e) { + System.err.println("class...") ; + } + } +} diff --git a/demoCoursAL/src/application.xml b/demoCoursAL/src/application.xml new file mode 100755 index 0000000..7ea0e58 --- /dev/null +++ b/demoCoursAL/src/application.xml @@ -0,0 +1,15 @@ + + + + Appli Banque + + + appliBanqueSessions.jar + + + appliBanqueEntites.jar + + + diff --git a/demoCoursAL/src/client/Main.java b/demoCoursAL/src/client/Main.java new file mode 100644 index 0000000..5551909 --- /dev/null +++ b/demoCoursAL/src/client/Main.java @@ -0,0 +1,58 @@ +package client ; + + + +import javax.naming.InitialContext; +import javax.naming.NamingException ; + +import ejb.sessions.* ; +import ejb.entites.* ; + +public class Main { + + public static void main(String[] args) { + try { + InitialContext ctx = new InitialContext(); + System.out.println("Accès au service distant") ; + Object obj = ctx.lookup("ejb:appliBanque/appliBanqueSessions//ServiceBanqueBean!ejb.sessions.ServiceBanque"); + + ServiceBanque service = (ServiceBanque) obj ; + service.addCompte(1, "Olivier", 2000.0); + service.addCompte(2, "Paul", 300.0) ; + service.addAction("nintendo",46.5) ; + service.addAction("sega", 12.0) ; + System.out.println("achat de 10 actions sega") ; + service.acheteActions(1, "sega", 10); + System.out.println("achat de 3 actions nintendo") ; + service.acheteActions(1, "nintendo", 3); + System.out.println("Actions de Olivier:") ; + for (LigneAction la : service.getActionsAchetees(1)) + System.out.println(la.getNombre()+" action(s) "+la.getAction().getNom() + +" au taux de "+la.getAction().getTaux()) ; + System.out.println("vente de 2 actions sega") ; + service.vendActions(1, "sega", 2); + System.out.println("Actions de Olivier:") ; + for (LigneAction la : service.getActionsAchetees(1)) + System.out.println(la.getNombre()+" action(s) "+la.getAction().getNom() + +" au taux de "+la.getAction().getTaux()) ; + System.out.println("vente de 8 actions sega") ; + service.vendActions(1, "sega", 8); + System.out.println("Actions de Olivier:") ; + for (LigneAction la : service.getActionsAchetees(1)) + System.out.println(la.getNombre()+" action(s) "+la.getAction().getNom() + +" au taux de "+la.getAction().getTaux()) ; + } catch(CompteInconnuException e) { + System.err.println("Compte inconnu") ; + } catch (NamingException e1) { + System.err.println("erreur accès service") ; + } catch (ActionInconnueException e) { + System.err.println("Action inconnue") ; + } catch (ApprovisionnementException e) { + System.err.println("pb approvisionnement") ; + } catch (CompteDejaExistantException e) { + System.err.println("Compte existe") ; + } catch (ActionDejaExistanteException e) { + System.err.println("Action existe") ; + } + } +} \ No newline at end of file diff --git a/demoCoursAL/src/ejb/entites/Action.java b/demoCoursAL/src/ejb/entites/Action.java new file mode 100644 index 0000000..1a6ba3f --- /dev/null +++ b/demoCoursAL/src/ejb/entites/Action.java @@ -0,0 +1,26 @@ +package ejb.entites; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Action implements java.io.Serializable { + private static final long serialVersionUID = 1L; + + public Action() {} + + @Id private String nom ; + private double taux ; + + public String getNom() { return nom; } + public void setNom(String nom) { + this.nom = nom; + } + + public double getTaux() { return taux; } + public void setTaux(double taux) { + this.taux = taux; + } + + +} diff --git a/demoCoursAL/src/ejb/entites/Compte.java b/demoCoursAL/src/ejb/entites/Compte.java new file mode 100644 index 0000000..abd19e3 --- /dev/null +++ b/demoCoursAL/src/ejb/entites/Compte.java @@ -0,0 +1,62 @@ +package ejb.entites; + +import java.io.Serializable ; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class Compte implements Serializable { + + @Id private int numero ; + private double solde ; + private String titulaire ; + @OneToMany(mappedBy="proprietaire", fetch=FetchType.EAGER) + private Set lignesactions ; + + + + public int getNumero() { return numero; } + + public void setNumero(int numero) { + this.numero = numero; + } + + public double getSolde() { + return solde; + } + + public void setSolde(double solde) { + this.solde = solde; + } + + public String getTitulaire() { + return titulaire; + } + + public void setTitulaire(String titulaire) { + this.titulaire = titulaire; + } + + /** + * + */ + private static final long serialVersionUID = 1L; + + public Compte() {} + + public Set getLignesactions() { + return lignesactions; + } + + public void setLignesactions(Set lignesactions) { + this.lignesactions = lignesactions; + } + + + + +} diff --git a/demoCoursAL/src/ejb/entites/LigneAction.java b/demoCoursAL/src/ejb/entites/LigneAction.java new file mode 100644 index 0000000..a53eea0 --- /dev/null +++ b/demoCoursAL/src/ejb/entites/LigneAction.java @@ -0,0 +1,45 @@ +package ejb.entites; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + + +@Table (name="t_ligne_action") @Entity +public class LigneAction implements java.io.Serializable { + private static final long serialVersionUID = 1L; + + public LigneAction() {} + + @Id @GeneratedValue private int id ; + private int nombre ; + @ManyToOne private Compte proprietaire ; + @ManyToOne(fetch=FetchType.EAGER) private Action action ; + + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + + public int getNombre() { return nombre; } + public void setNombre(int nombre) { + this.nombre = nombre; + } + + public Compte getProprietaire() { + return proprietaire; + } + public void setProprietaire(Compte proprietaire) { + this.proprietaire = proprietaire; + } + + public Action getAction() { + return action; + } + public void setAction(Action action) { + this.action = action; + } + +} diff --git a/demoCoursAL/src/ejb/entites/persistence.xml b/demoCoursAL/src/ejb/entites/persistence.xml new file mode 100644 index 0000000..70cb705 --- /dev/null +++ b/demoCoursAL/src/ejb/entites/persistence.xml @@ -0,0 +1,12 @@ + + + + java:/PostgresDS + + + + + + + diff --git a/demoCoursAL/src/ejb/sessions/ActionDejaExistanteException.java b/demoCoursAL/src/ejb/sessions/ActionDejaExistanteException.java new file mode 100644 index 0000000..d683a6d --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/ActionDejaExistanteException.java @@ -0,0 +1,10 @@ +package ejb.sessions; + +public class ActionDejaExistanteException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/demoCoursAL/src/ejb/sessions/ActionInconnueException.java b/demoCoursAL/src/ejb/sessions/ActionInconnueException.java new file mode 100644 index 0000000..6a0518e --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/ActionInconnueException.java @@ -0,0 +1,10 @@ +package ejb.sessions; + +public class ActionInconnueException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/demoCoursAL/src/ejb/sessions/ApprovisionnementException.java b/demoCoursAL/src/ejb/sessions/ApprovisionnementException.java new file mode 100644 index 0000000..c4729f0 --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/ApprovisionnementException.java @@ -0,0 +1,10 @@ +package ejb.sessions; + +public class ApprovisionnementException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/demoCoursAL/src/ejb/sessions/CompteDejaExistantException.java b/demoCoursAL/src/ejb/sessions/CompteDejaExistantException.java new file mode 100644 index 0000000..6b6c7c5 --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/CompteDejaExistantException.java @@ -0,0 +1,10 @@ +package ejb.sessions; + +public class CompteDejaExistantException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/demoCoursAL/src/ejb/sessions/CompteInconnuException.java b/demoCoursAL/src/ejb/sessions/CompteInconnuException.java new file mode 100644 index 0000000..9041d28 --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/CompteInconnuException.java @@ -0,0 +1,10 @@ +package ejb.sessions; + +public class CompteInconnuException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/demoCoursAL/src/ejb/sessions/ServiceBanque.java b/demoCoursAL/src/ejb/sessions/ServiceBanque.java new file mode 100644 index 0000000..4840c25 --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/ServiceBanque.java @@ -0,0 +1,23 @@ +package ejb.sessions; + +import ejb.entites.LigneAction ; + +@javax.ejb.Remote +public interface ServiceBanque { + public void addCompte(int numeroCompte, String nomTitulaire, double soldeDepart) + throws CompteDejaExistantException ; + public void addAction(String nomAction, double taux) + throws ActionDejaExistanteException ; + public void crediterCompte(int numeroCompte, double montant) + throws CompteInconnuException ; + public void debiterCompte(int numeroCompte, double montant) + throws CompteInconnuException ; + public void virementVers(int numCompteDebit, int numCompteCredit, double montant) + throws CompteInconnuException,ApprovisionnementException ; + public void acheteActions(int numeroCompte, String nomAction, int nb) + throws CompteInconnuException, ActionInconnueException, ApprovisionnementException ; + public void vendActions(int numeroCompte, String nomAction, int nb) + throws CompteInconnuException, ActionInconnueException, ApprovisionnementException ; + public java.util.Set getActionsAchetees(int numeroCompte) + throws CompteInconnuException ; +} diff --git a/demoCoursAL/src/ejb/sessions/ServiceBanqueBean.java b/demoCoursAL/src/ejb/sessions/ServiceBanqueBean.java new file mode 100644 index 0000000..3f0c21d --- /dev/null +++ b/demoCoursAL/src/ejb/sessions/ServiceBanqueBean.java @@ -0,0 +1,128 @@ +package ejb.sessions; + +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import ejb.entites.Action; +import ejb.entites.Compte; +import ejb.entites.LigneAction; + +@javax.ejb.Stateless +public class ServiceBanqueBean implements ServiceBanque { + @PersistenceContext(unitName="appliBanque") protected EntityManager em ; + @Override + public void addCompte(int numeroCompte, String nomTitulaire, double soldeDepart) + throws CompteDejaExistantException { + try { + this.getCompte(numeroCompte) ; + throw new CompteDejaExistantException() ; + } catch(CompteInconnuException e) { + Compte c=new Compte() ; + c.setNumero(numeroCompte); c.setSolde(soldeDepart); + c.setTitulaire(nomTitulaire); + em.persist(c); + } + } + + @Override + public void addAction(String nomAction, double taux) + throws ActionDejaExistanteException { + try { + this.getAction(nomAction) ; + throw new ActionDejaExistanteException() ; + } catch(ActionInconnueException e) { + Action a=new Action() ; + a.setNom(nomAction); a.setTaux(taux); + em.persist(a); + } + + } + + private Action getAction(String nomAction) throws ActionInconnueException { + Action a= (Action) em.find(Action.class, nomAction) ; + if (a==null) throw new ActionInconnueException() ; + return a ; + } + + @Override + public void crediterCompte(int numeroCompte, double montant) + throws CompteInconnuException { + Compte c = this.getCompte(numeroCompte) ; + c.setSolde(c.getSolde()+montant); + } + + @Override + public void debiterCompte(int numeroCompte, double montant) + throws CompteInconnuException { + Compte c = this.getCompte(numeroCompte) ; + c.setSolde(c.getSolde()-montant); + + } + + @Override + public void virementVers(int numCompteDebit, int numCompteCredit, + double montant) throws CompteInconnuException, + ApprovisionnementException { + + + } + + @Override + public void acheteActions(int numeroCompte, String nomAction, int nb) + throws CompteInconnuException, ActionInconnueException, + ApprovisionnementException { + Compte c = this.getCompte(numeroCompte) ; + Action a = this.getAction(nomAction) ; + if (c.getSolde()< nb*a.getTaux()) + throw new ApprovisionnementException() ; + else + c.setSolde(c.getSolde()-nb*a.getTaux()) ; + boolean trouve=false ; + for (LigneAction la : c.getLignesactions()) + if (la.getAction().getNom().equals(nomAction)) { + trouve=true ; la.setNombre(la.getNombre()+nb); + } + if (!trouve) { + LigneAction la = new LigneAction() ; + la.setAction(a); la.setProprietaire(c) ; la.setNombre(nb); + em.persist(la); + } + } + + @Override + public void vendActions(int numeroCompte, String nomAction, int nb) + throws CompteInconnuException, ActionInconnueException, + ApprovisionnementException { + Compte c = this.getCompte(numeroCompte) ; + LigneAction la=null ; + for (LigneAction vla : c.getLignesactions()) + if (vla.getAction().getNom().equals(nomAction)) { + la=vla ; + } + if (la != null) { + if (nb > la.getNombre()) + throw new ApprovisionnementException() ; + else { + c.setSolde(c.getSolde()+nb*la.getAction().getTaux()); + la.setNombre(la.getNombre()-nb) ; + if (la.getNombre()==0) em.remove(la); + } + } + } + + @Override + public Set getActionsAchetees(int numeroCompte) + throws CompteInconnuException { + Compte c = this.getCompte(numeroCompte) ; + return c.getLignesactions() ; + } + + private Compte getCompte(int numeroCompte) throws CompteInconnuException { + Compte c= (Compte) em.find(Compte.class, numeroCompte) ; + if (c==null) throw new CompteInconnuException() ; + return c; + } + +} -- libgit2 0.21.2