View Javadoc

1   package es.caib.signatura.api;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.net.URL;
7   import java.util.Map;
8   import java.util.Properties;
9   
10  import es.caib.signatura.impl.CAIBSigner;
11  import es.caib.signatura.impl.SigUtil;
12  
13  /**
14   * Clase factory que obtiene la clase que implementa la interfaz <code>Signature</code> en función
15   * del nombre de la entidad certificadora
16   * @author Jesús Reyes (3dígits)
17   * @version 1.0
18   * @see Signature
19   */
20  
21  public class SignerFactory
22  {
23  
24  	static boolean	started	= false;
25  	private static Map signerConfiguration = null;
26  	
27  	public SignerFactory() {}
28  	
29  	public SignerFactory(Map signerConfig) {
30  		signerConfiguration = signerConfig;
31  	}
32  	
33  	public String getAPIVersion()
34  		throws FileNotFoundException, IOException
35  	{
36  		InputStream inputStream = SignerFactory.class.getResourceAsStream("version.properties");
37  		if (inputStream == null) {
38  			throw new FileNotFoundException();
39  		}
40  		Properties tempProperties = new Properties();
41  		tempProperties.load(inputStream);
42  
43  		inputStream.close();
44  		return tempProperties.getProperty("Version");
45  	}
46  	
47  	public URL getUpdateSite() throws FileNotFoundException, IOException {
48  		InputStream inputStream = SignerFactory.class.getResourceAsStream("version.properties");
49  		if (inputStream == null) {
50  			throw new FileNotFoundException();
51  		}
52  		Properties tempProperties = new Properties();
53  		tempProperties.load(inputStream);
54  		inputStream.close();
55  		String updateSite = tempProperties.getProperty("updateSite");
56  		if (updateSite != null && !updateSite.equals(""))
57  			return new URL(updateSite);
58  		else {
59  			return null;
60  		}
61  	}
62  
63  	/**
64  	 * Obtiene un objeto de la clase que implementa la interfaz <code>Signature</code> según
65  	 * el nombre de la entidad certificadora
66  	 * @return objeto que implementa <code>Signature</code> para la entidad certificadora <code>caName</code>
67  	 * @throws UpgradeNeededException 
68  	 */
69  	public Signer getSigner()
70  		throws UpgradeNeededException
71  	{
72  		try {
73  			if (!started) {
74  				started = true;
75  			}
76  			Signer s = null;
77  			if (signerConfiguration != null) {
78  				s = new CAIBSigner(signerConfiguration);
79  			}
80  			else {
81  				s = new CAIBSigner();
82  			}
83  			if (!isValidVersion(getAPIVersion(), s.getAPIVersion())) {
84  				System.out.println("Necesita actualizarse a la versión " + getAPIVersion()
85  					+ " o superior. Actualmente tiene instalada la versión " + s.getAPIVersion());
86  				throw new UpgradeNeededException("Necesita actualizarse a la versión " + getAPIVersion()
87  					+ " o superior. Actualmente tiene instalada la versión " + s.getAPIVersion(),
88  					SigUtil.getUpgradeUrl());
89  			}
90  			return s;
91  		} catch (Throwable e) {
92  			throw new UpgradeNeededException("Sistema de firma no instalado", SigUtil.getUpgradeUrl(), e);
93  		}
94  	}
95  
96  	private boolean isValidVersion( String apiVersion, String jreVersion )
97  	{
98  		if (jreVersion == null) {
99  			return false;
100 		}
101 		if (apiVersion == null) {
102 			return false;
103 		}
104 		String splitJRE[] = jreVersion.split("[.-]");
105 		String splitAPI[] = apiVersion.split("[.-]");
106 		
107 		/**
108 		 * modifiquem les condicions per a saber si és versió vàlida.
109 		 * Ara no tenir sub-versió es considera com a versió -1.
110 		 */
111 		for (int i = 0; i < Math.max(splitAPI.length,splitJRE.length) ; i++) {
112 			int v1;
113 			int v2;
114 			try {
115 				v1 = (i<splitJRE.length)?Integer.parseInt(splitJRE[i]):-1; //si no s'ha definit aquest nivell de versió, el considerem -1
116 				v2 = (i<splitAPI.length)?Integer.parseInt(splitAPI[i]):-1; //si no s'ha definit aquest nivell de versió, el considerem -1
117 				if (v1 > v2) { // Versión superior
118 					return true;
119 				}
120 				if (v1 < v2) { // Versión inferior
121 					return false;
122 				}
123 			} catch (NumberFormatException e) {
124 				if (i >= splitJRE.length) {
125 					return false;
126 				}
127 						
128 				if (splitJRE[i].compareTo(splitAPI[i]) > 0)
129 					return true;
130 				if (splitJRE[i].compareTo(splitAPI[i]) < 0)
131 					return false;
132 			}
133 		}
134 		return true;
135 	}
136 }