Created
October 3, 2017 03:09
-
-
Save JavierGelatti/bd01d1a16f1453754503bb331b2f5fba to your computer and use it in GitHub Desktop.
Ejemplo de Variaciones Protegidas usando metaprogramación
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dds.proveedores; | |
import java.io.File; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
public class CargadorSistemasProveedores { | |
public static final String PREFIJO_CLASE = "com.dds.proveedores.adaptadores.Adaptador"; | |
public static final String CARPETA_JARS = "./lib/"; | |
public SistemaProveedor getSistemaProveedor(String codigoEmpresa) throws Exception { | |
String nombreClase = PREFIJO_CLASE + codigoEmpresa; | |
Class<SistemaProveedor> claseSistemaProveedor = getClase(nombreClase); | |
return claseSistemaProveedor.newInstance(); | |
} | |
private <T> Class<T> getClase(String nombreClase) throws ClassNotFoundException { | |
return (Class<T>) Class.forName(nombreClase); | |
} | |
public SistemaProveedor getSistemaProveedorDesdeJar(String codigoEmpresa) throws Exception { | |
File archivoJar = new File(CARPETA_JARS + codigoEmpresa + ".jar"); | |
String nombreClase = PREFIJO_CLASE + codigoEmpresa; | |
Class<SistemaProveedor> claseSistemaProveedor = getClase(nombreClase, archivoJar); | |
return claseSistemaProveedor.newInstance(); | |
} | |
private <T> Class<T> getClase(String nombreClase, File archivoJar) throws Exception { | |
return (Class<T>) Class.forName(nombreClase, true, cargadorDeClasesPara(archivoJar)); | |
} | |
private URLClassLoader cargadorDeClasesPara(File unArchivoJar) throws MalformedURLException { | |
return new URLClassLoader(urlDe(unArchivoJar), cargadorDeClasesActual()); | |
} | |
private ClassLoader cargadorDeClasesActual() { | |
return this.getClass().getClassLoader(); | |
} | |
private URL[] urlDe(File unArchivo) throws MalformedURLException { | |
return new URL[]{unArchivo.toURI().toURL()}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dds.proveedores; | |
public class Main { | |
private static CargadorSistemasProveedores cargadorSistemas = new CargadorSistemasProveedores(); | |
public static void main(String[] args) throws Exception { | |
OrdenDeCompra ordenDeCompra = new OrdenDeCompra(); | |
SistemaProveedor sistemaProveedor1 = cargadorSistemas.getSistemaProveedor("DinaSur"); | |
sistemaProveedor1.enviarOrdenDeCompra(ordenDeCompra); | |
SistemaProveedor sistemaProveedor2 = cargadorSistemas.getSistemaProveedorDesdeJar("XYZ"); | |
sistemaProveedor2.enviarOrdenDeCompra(ordenDeCompra); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dds.proveedores; | |
public class OrdenDeCompra { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dds.proveedores; | |
public interface SistemaProveedor { | |
void enviarOrdenDeCompra(OrdenDeCompra datos); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment