View Javadoc

1   package es.caib.signatura.impl;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.net.URL;
6   import java.net.URLEncoder;
7   import java.util.StringTokenizer;
8   
9   import es.caib.signatura.api.SignerFactory;
10  
11  
12  
13  /**
14   * 
15   */
16  public class SigUtil
17  {
18  
19  	// Codigo de los sistemas operativos.
20  	public static String		OS_UNAVAILABLE		= "No disponible";
21  
22  	public static String		OS_UNKNOWN			= "Desconocido";
23  
24  	public static String		OS_WINDOWS			= "windows";
25  
26  	public static String		OS_LINUX			= "Linux";
27  
28  	public static String		OS_MAC				= "Mac";
29  
30  	public static String		OS_REGEXP_WINDOWS	= "\\A(?i:win).*\\z";
31  
32  	// 	Comença per win, ignore case.
33  
34  	public static String		OS_REGEXP_LINUX		= "\\A(?i:linux).*\\z";
35  
36  	// 	Comença per linux, ignore case.
37  
38  	public static String		OS_REGEXP_MAC		= "\\A(?i:mac).*\\z";
39  
40  	// 	Comença per mac, ignore case.
41  
42  	// private final static String	INSTALL_URL			= "http://www.caib.es:8080/signaturacaib/install.jnlp";
43  	// private final static String	INSTALL_URL			= "http://www.caib.es:80/signaturacaib/install.jnlp";
44  
45  	/**
46  	 * Obtiene el código del sistema operativo actual en que corre
47  	 * la máquina virtual de Java.
48  	 * 
49  	 * <p>Devuelve OS_UNKNOWN si no es capz de determinar el sistema operativo
50  	 * y OS_UNAVAILABLE si ha tenido problemas (seguramente de seguridad) para
51  	 * obtener la información de la JVM.
52  	 * 
53  	 * @return Sistema operativo en que se ejecuta la JVM.
54  	 */
55  	public static String getCurrentOs()
56  	{
57  		// if( SigDebug.isActive() ) SigDebug.write("Detección del OS");
58  		String currentOsName = null;
59  		try {
60  			currentOsName = System.getProperty("os.name");
61  		} catch (Throwable t) {
62  			currentOsName = null;
63  			// if( SigDebug.isActive() ) SigDebug.write("    Error obteniendo el sistema operativo: " + t.toString());
64  			t.printStackTrace();
65  		}
66  
67  		if (currentOsName == null || currentOsName.length() == 0) {
68  			// if( SigDebug.isActive() ) SigDebug.write("    Incapaz de obtener el sistema operativo.");
69  			return OS_UNAVAILABLE;
70  		}
71  
72  		if (currentOsName != null) {
73  			// if( SigDebug.isActive() ) SigDebug.write("    Nombre del sistema operativo: " + currentOsName);
74  			if (currentOsName.matches(OS_REGEXP_WINDOWS)) {
75  				// if( SigDebug.isActive() ) SigDebug.write("  - Sistema operativo Windows.");
76  				return OS_WINDOWS;
77  			} else if (currentOsName.matches(OS_REGEXP_LINUX)) {
78  				// if( SigDebug.isActive() ) SigDebug.write("  - Sistema operativo Linux.");
79  				return OS_LINUX;
80  			} else if (currentOsName.matches(OS_REGEXP_MAC)) {
81  				// if( SigDebug.isActive() ) SigDebug.write("  - Sistema operativo Mac.");
82  				return OS_MAC;
83  			}
84  		}
85  		// if( SigDebug.isActive() ) SigDebug.write("  - Sistema operativo desconocido.");
86  		return OS_UNKNOWN;
87  	}
88  
89  	/**
90  	 * Obtiene el path de la JVM de java que executa el procés actual.
91  	 * 
92  	 * @return Path del JVM o null si no lo puede calcular.
93  	 */
94  	public static String getCurrentJVMPath()
95  	{
96  		try {
97  			return System.getProperty("java.home");
98  		} catch (Throwable t) {
99  			t.printStackTrace();
100 		}
101 
102 		return null;
103 	}
104 
105 	/**
106 	 * Obtiene la URL de instalación de la API.
107 	 * 
108 	 * @return URL de actualización.
109 	 */
110 	public static URL getUpgradeUrl()
111 	{
112 		try {
113 			SignerFactory sf = new SignerFactory();
114 			String updateSite = sf.getUpdateSite();
115 			if ( updateSite != null && !updateSite.equals("")) {
116 				String installUrl = updateSite + "/install.jnlp";
117 				// if( SigDebug.isActive() ) SigDebug.write("Obtenint URL d'actualitzacio ...");
118 				String surl = installUrl + "?os=" + URLEncoder.encode(getCurrentOs(), "UTF-8");
119 
120 				/** esto no funciona si el jar no va firmado **/
121 //				String jvm = getCurrentJVMPath();
122 //				if (jvm != null && jvm.length() > 0) {
123 //					surl += "&jvm=" + URLEncoder.encode(jvm, "UTF-8");
124 //				}
125 				URL url = new URL(surl);
126 				// if( SigDebug.isActive() ) SigDebug.write("   URL = " + url.toString()); 
127 				return url;
128 			}
129 		} catch (Exception e) {
130 			e.printStackTrace();
131 		}
132 		return null;
133 	}
134 	
135 
136 
137 
138 	
139 }