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 public String updateSite=null;
27 private String configurationFile=null;
28 private String propertiesCache=null;
29
30 public SignerFactory() {
31
32 }
33
34 public SignerFactory(Map signerConfig) {
35 signerConfiguration = signerConfig;
36 }
37
38 public String getAPIVersion()
39 throws FileNotFoundException, IOException
40 {
41 InputStream inputStream = SignerFactory.class.getResourceAsStream("version.properties");
42 if (inputStream == null) {
43 throw new FileNotFoundException();
44 }
45 Properties tempProperties = new Properties();
46 tempProperties.load(inputStream);
47
48 inputStream.close();
49 return tempProperties.getProperty("Version");
50 }
51
52 public void configUpdateSite() throws FileNotFoundException, IOException {
53 InputStream inputStream = SignerFactory.class.getResourceAsStream("version.properties");
54 if (inputStream == null) {
55 throw new FileNotFoundException();
56 }
57 Properties tempProperties = new Properties();
58 tempProperties.load(inputStream);
59 inputStream.close();
60 updateSite = tempProperties.getProperty("updateSite");
61 configurationFile=tempProperties.getProperty("configurationFile");
62 propertiesCache=tempProperties.getProperty("propertiesCache");
63
64 }
65
66 public String getUpdateSite() throws FileNotFoundException, IOException {
67 configUpdateSite();
68 return updateSite;
69 }
70
71
72
73
74
75
76
77 public Signer getSigner()
78 throws UpgradeNeededException
79 {
80 try {
81 if (!started) {
82 started = true;
83 }
84 Signer s = null;
85 if (signerConfiguration != null) {
86 s = new CAIBSigner(signerConfiguration);
87 }
88 else {
89 configUpdateSite();
90 s = new CAIBSigner(updateSite,new URL(configurationFile),propertiesCache);
91 }
92 if (!isValidVersion(getAPIVersion(), s.getAPIVersion())) {
93 System.out.println("Necesita actualizarse a la versión " + getAPIVersion()
94 + " o superior. Actualmente tiene instalada la versión " + s.getAPIVersion());
95 throw new UpgradeNeededException("Necesita actualizarse a la versión " + getAPIVersion()
96 + " o superior. Actualmente tiene instalada la versión " + s.getAPIVersion(),
97 SigUtil.getUpgradeUrl());
98 }
99 return s;
100 } catch (Throwable e) {
101 throw new UpgradeNeededException("Sistema de firma no instalado, o requiere reiniciar navegador", SigUtil.getUpgradeUrl(), e);
102 }
103 }
104
105 private boolean isValidVersion( String apiVersion, String jreVersion )
106 {
107 if (jreVersion == null) {
108 return false;
109 }
110 if (apiVersion == null) {
111 return false;
112 }
113 String splitJRE[] = jreVersion.split("[.-]");
114 String splitAPI[] = apiVersion.split("[.-]");
115
116
117
118
119
120 for (int i = 0; i < Math.max(splitAPI.length,splitJRE.length) ; i++) {
121 int v1;
122 int v2;
123 try {
124 v1 = (i<splitJRE.length)?Integer.parseInt(splitJRE[i]):-1;
125 v2 = (i<splitAPI.length)?Integer.parseInt(splitAPI[i]):-1;
126 if (v1 > v2) {
127 return true;
128 }
129 if (v1 < v2) {
130 return false;
131 }
132 } catch (NumberFormatException e) {
133 if (i >= splitJRE.length) {
134 return false;
135 }
136
137 if (splitJRE[i].compareTo(splitAPI[i]) > 0)
138 return true;
139 if (splitJRE[i].compareTo(splitAPI[i]) < 0)
140 return false;
141 }
142 }
143 return true;
144 }
145 }