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
15
16
17
18
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
65
66
67
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
109
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;
116 v2 = (i<splitAPI.length)?Integer.parseInt(splitAPI[i]):-1;
117 if (v1 > v2) {
118 return true;
119 }
120 if (v1 < v2) {
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 }