Ici comme pour les services et l’internationalisation vous pourrez réaliser les exercices de différentes manières:

  • en architecture modulaire ou non (commencer par l’option "non modulaire")

  • avec un déploiement sur fichier ou en jar.

Exercices

Ressource de configuration

Reprendre les codes qui permettent d'établir la taxation des véhicules. Faire en sorte qu’une ressource taxes.properties permette d'établir le taux de taxe particulier à chaque type de véhicule.

Pour avoir une bonne organisation mettre ce fichier dans un pseudo sous-package config.

Attention : il serait souhaitable que tous les taux de taxes soient calculés au load time.

Propositions de solutions:

Un dernier point à regarder: comment tester le déploiement correct d’un ensemble de codes?

Reprendre l’exercice dans un architecture modulaire: essayer avec un ClassLoader puis en écrivant un service de recherche de ressource.

Exemples

Ressource de configuration

package com.garaje.deploy2;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Properties;

import com.garaje.commons.Empatement;
import com.garaje.commons.Vehicule;

/**
 * ici ce code fait extends Vehicule UNIQUEMENT parceque nous ne pouvons pas présenter
 * plusieurs version de la classe Vehicule.
 * <P>
 * Pour simplifier les autres codes nous n'avons pas repris l'ensemble des
 * hiérarchies
 */
public abstract class Vehicule2 extends Vehicule {

   static final Properties PROPS_TAXES = new Properties();
   static {
      try {
      InputStream is = Vehicule2.class.getResourceAsStream("config/taxes.properties");
      PROPS_TAXES.load(is) ;
      } catch(Exception exc) {
         throw new AssertionError("deploiement config/taxes.properties" +exc) ;
      }
   }

   /**
    * doit être appelée au load time
    * @param clef
    * @return
    * @throws AssertionError si clef non configurée correctement
    */
   protected static BigDecimal tauxTVAPour(String clef) {
      try {
      String val = PROPS_TAXES.getProperty(clef);
      return new BigDecimal(val) ;
      } catch (Exception exc) {
         throw new AssertionError("config/taxes.properties valeur: "
               + clef
               + "inexacte. "+exc) ;
      }
   }

   public Vehicule2(String ref, String marque, Empatement empatement,
         BigDecimal prixHT) {
      super(ref, marque, empatement, prixHT);
   }

   public Vehicule2(String ref, String marque, Empatement empatement,
         String stringPrix) {
      super(ref, marque, empatement, stringPrix);
   }

   /**
    * doit renvoyer une valeur calculée au load-time par consultation
    * de <TT>tauxTVAPour</TT>
    */
   @Override
   protected abstract BigDecimal getTauxTVA() ;

}
package com.garaje.deploy2;

import java.math.BigDecimal;

import com.garaje.commons.Empatement;

public class VoitureElectrique extends Vehicule2 {

   private static final BigDecimal TAUX_TVA = tauxTVAPour("voitureElectrique") ;

   public VoitureElectrique(String ref, String marque,
         BigDecimal prixHT, int longueur, int largeur) {
      super(ref, marque, new Empatement(longueur, largeur), prixHT);
   }

   public VoitureElectrique(String ref, String marque,
         String stringPrix,int longueur, int largeur ) {
      super(ref, marque, new Empatement(longueur, largeur), stringPrix);
   }

   @Override
   protected BigDecimal getTauxTVA() {
      return TAUX_TVA;
   }
   public String toString() {
      return super.toString() +
      "; prix: " + this.getPrixTTC() ;
   }

}
package com.garaje.deploy2;

import java.math.BigDecimal;

import com.garaje.commons.Empatement;

public class MotoElectrique extends Vehicule2 {

   private static final BigDecimal TAUX_TVA = tauxTVAPour("motoElectrique") ;

   public MotoElectrique(String ref, String marque,
         BigDecimal prixHT,int longueur, int largeur) {
      super(ref, marque, new Empatement(longueur, largeur), prixHT);
   }

   public MotoElectrique(String ref, String marque,
         String stringPrix, int longueur, int largeur) {
      super(ref, marque, new Empatement(longueur, largeur), stringPrix);
   }

   @Override
   protected BigDecimal getTauxTVA() {
      return TAUX_TVA;
   }

   public String toString() {
      return super.toString() +
      "; prix: " + this.getPrixTTC() ;
   }

}
# calculs taxe
voitureElectrique : 1.196
motoElectrique : 1.33
package com.garaje.simulacres;

import com.garaje.commons.Vehicule;

import com.garaje.deploy2.MotoElectrique;
import com.garaje.deploy2.VoitureElectrique;

public class PseudoCatalogue2bis extends PseudoCatalogue2 {

   private static Vehicule[] tabVehic = {
      new VoitureElectrique("X66", "jamais contente", "1000", 425, 220),
      new MotoElectrique("Z99", "Zipadoo", "1000", 160, 120),

   } ;
   public Vehicule[] getVehicules() {
      return tabVehic ;
   }

}
package com.garaje.deploy2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class TestConfig {
   public static void main(String[] args) {
      String fileName = args[0] ;
      try {
         InputStream is = new FileInputStream(fileName);
         BufferedReader bufr = new BufferedReader(new InputStreamReader(is)) ;
         String nomClasse = null;

         while(null!=(nomClasse = bufr.readLine())){
            nomClasse = nomClasse.trim() ;
            if("".equals(nomClasse))continue ;
            if(nomClasse.startsWith("#")) continue ;
            try {
               Class.forName(nomClasse) ;
               System.out.println("classe ok :" + nomClasse);
            }catch (Throwable th) {
               // à ne pas faire:mettre un logger
               System.err.println("ERREUR DEPLOIEMENT: " + th) ;
            }

         }

      } catch (IOException ex) {
               // à ne pas faire:mettre un logger
         System.err.println( "lecture fichier des classes à  tester"+ ex);
      }

   }

}