PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/AZ-SVN/azureus2/src/org/gudy/azureus2/core3/util/Constants.java

https://bitbucket.org/crunchiness/vuze
Java | 527 lines | 334 code | 142 blank | 51 comment | 64 complexity | 60b8c4e4ec77427d25b44e3f77f60992 MD5 | raw file
  1. /*
  2. * Created on 16 juin 2003
  3. * Copyright (C) 2003, 2004, 2005, 2006 Aelitis, All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * AELITIS, SAS au capital de 46,603.30 euros
  18. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  19. *
  20. */
  21. package org.gudy.azureus2.core3.util;
  22. import java.io.InputStreamReader;
  23. import java.io.LineNumberReader;
  24. import java.nio.charset.Charset;
  25. import java.security.AccessControlException;
  26. import java.util.Locale;
  27. import java.util.StringTokenizer;
  28. import java.util.TimeZone;
  29. import java.util.regex.Pattern;
  30. /**
  31. *
  32. * @author Olivier
  33. *
  34. */
  35. public class
  36. Constants
  37. {
  38. public static final String EMPTY_STRING = "";
  39. public static final String SF_WEB_SITE = "http://plugins.vuze.com/";
  40. public static final String AELITIS_TORRENTS = "http://cf1.vuze.com/torrent/torrents/";
  41. public static final String AELITIS_FILES = "http://cf1.vuze.com/torrent/files/";
  42. public static final String AZUREUS_WIKI = "http://wiki.vuze.com/w/";
  43. public static final String VERSION_SERVER_V4 = "version.vuze.com";
  44. public static final String VERSION_SERVER_V6 = "version6.vuze.com";
  45. public static final String DHT_SEED_ADDRESS_V4 = "dht.vuze.com";
  46. public static final String DHT_SEED_ADDRESS_V6 = "dht6.vuze.com";
  47. public static final String NAT_TEST_SERVER = "nettest.vuze.com";
  48. public static final String NAT_TEST_SERVER_HTTP = "http://nettest.vuze.com/";
  49. public static final String SPEED_TEST_SERVER = "speedtest.vuze.com";
  50. public static final String PAIRING_URL = "http://pair.vuze.com/pairing";
  51. public static final String[] AZUREUS_DOMAINS = { "azureusplatform.com", "azureus.com", "aelitis.com", "vuze.com" };
  52. public static final String DEFAULT_ENCODING = "UTF8";
  53. public static final String BYTE_ENCODING = "ISO-8859-1";
  54. public static final Charset BYTE_CHARSET;
  55. public static final Charset DEFAULT_CHARSET;
  56. static{
  57. Charset bc = null;
  58. Charset dc = null;
  59. try{
  60. bc = Charset.forName( Constants.BYTE_ENCODING );
  61. dc = Charset.forName( Constants.DEFAULT_ENCODING );
  62. }catch( Throwable e ){
  63. e.printStackTrace();
  64. }
  65. BYTE_CHARSET = bc;
  66. DEFAULT_CHARSET = dc;
  67. }
  68. public static final Locale LOCALE_ENGLISH = new Locale("en", "");
  69. static{
  70. try{
  71. String timezone = System.getProperty( "azureus.timezone", null );
  72. if ( timezone != null ){
  73. TimeZone.setDefault( TimeZone.getTimeZone( timezone ));
  74. }
  75. }catch( Throwable e ){
  76. // can happen in applet
  77. if ( e instanceof AccessControlException ){
  78. }else{
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. public static final String INFINITY_STRING = "\u221E"; // "oo";pa
  84. public static final int CRAPPY_INFINITY_AS_INT = 365*24*3600; // seconds (365days)
  85. public static final long CRAPPY_INFINITE_AS_LONG = 10000*365*24*3600; // seconds (10k years)
  86. public static boolean DOWNLOAD_SOURCES_PRETEND_COMPLETE = false;
  87. // keep the CVS style constant coz version checkers depend on it!
  88. // e.g. 2.0.8.3
  89. // 2.0.8.3_CVS
  90. // 2.0.8.3_Bnn // incremental build
  91. public static String APP_NAME = "Vuze";
  92. public static final String AZUREUS_NAME = "Azureus";
  93. public static final String AZUREUS_VERSION = "4.8.0.2_CRUNCH";
  94. public static final String BUILD_VERSION = "@build.version@"; //Ant replace
  95. public static final String AZUREUS_SUBVER = "";
  96. public static final byte[] VERSION_ID = ("-" + "AZ" + "4801" + "-").getBytes(); //MUST be 8 chars long!
  97. private static final boolean FORCE_NON_CVS = System.getProperty( "az.force.noncvs", "0" ).equals( "1" );
  98. public static final boolean IS_CVS_VERSION = isCVSVersion( AZUREUS_VERSION ) && !FORCE_NON_CVS;
  99. public static final String OSName = System.getProperty("os.name");
  100. public static final boolean isOSX = OSName.toLowerCase().startsWith("mac os");
  101. public static final boolean isLinux = OSName.equalsIgnoreCase("Linux");
  102. public static final boolean isSolaris = OSName.equalsIgnoreCase("SunOS");
  103. public static final boolean isFreeBSD = OSName.equalsIgnoreCase("FreeBSD");
  104. public static final boolean isWindowsXP = OSName.equalsIgnoreCase("Windows XP");
  105. public static final boolean isWindows95 = OSName.equalsIgnoreCase("Windows 95");
  106. public static final boolean isWindows98 = OSName.equalsIgnoreCase("Windows 98");
  107. public static final boolean isWindows2000 = OSName.equalsIgnoreCase("Windows 2000");
  108. public static final boolean isWindowsME = OSName.equalsIgnoreCase("Windows ME");
  109. public static final boolean isWindows9598ME = isWindows95 || isWindows98 || isWindowsME;
  110. public static boolean isSafeMode = false;
  111. public static final boolean isWindows = OSName.toLowerCase().startsWith("windows");
  112. // If it isn't windows or osx, it's most likely an unix flavor
  113. public static final boolean isUnix = !isWindows && !isOSX;
  114. public static final boolean isWindowsVista;
  115. public static final boolean isWindowsVistaSP2OrHigher;
  116. public static final boolean isWindowsVistaOrHigher;
  117. public static final boolean isWindows7OrHigher;
  118. // Common Patterns
  119. public static final Pattern PAT_SPLIT_COMMAWORDS = Pattern.compile("\\s*,\\s*");
  120. public static final Pattern PAT_SPLIT_COMMA = Pattern.compile(",");
  121. public static final Pattern PAT_SPLIT_DOT = Pattern.compile("\\.");
  122. public static final Pattern PAT_SPLIT_SPACE = Pattern.compile(" ");
  123. public static final Pattern PAT_SPLIT_SLASH_N = Pattern.compile("\n");
  124. public static final boolean is64Bit;
  125. static{
  126. boolean _is64Bit;
  127. try{
  128. _is64Bit = System.getProperty( "os.arch" ).contains( "64" );
  129. }catch( Throwable e ){
  130. _is64Bit = false;
  131. }
  132. is64Bit = _is64Bit;
  133. if ( isWindows ){
  134. Float ver = null;
  135. try{
  136. ver = new Float( System.getProperty( "os.version" ));
  137. }catch (Throwable e){
  138. }
  139. boolean vista_sp2_or_higher = false;
  140. if ( ver == null ){
  141. isWindowsVista = false;
  142. isWindowsVistaOrHigher = false;
  143. isWindows7OrHigher = false;
  144. }else{
  145. float f_ver = ver.floatValue();
  146. isWindowsVista = f_ver == 6;
  147. isWindowsVistaOrHigher = f_ver >= 6;
  148. isWindows7OrHigher = f_ver >= 6.1f;
  149. if ( isWindowsVista ){
  150. LineNumberReader lnr = null;
  151. try{
  152. Process p =
  153. Runtime.getRuntime().exec(
  154. new String[]{
  155. "reg",
  156. "query",
  157. "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
  158. "/v",
  159. "CSDVersion" });
  160. lnr = new LineNumberReader( new InputStreamReader( p.getInputStream()));
  161. while( true ){
  162. String line = lnr.readLine();
  163. if ( line == null ){
  164. break;
  165. }
  166. if ( line.matches( ".*CSDVersion.*" )){
  167. vista_sp2_or_higher = line.matches( ".*Service Pack [2-9]" );
  168. break;
  169. }
  170. }
  171. }catch( Throwable e ){
  172. }finally{
  173. if ( lnr != null ){
  174. try{
  175. lnr.close();
  176. }catch( Throwable e ){
  177. }
  178. }
  179. }
  180. }
  181. }
  182. isWindowsVistaSP2OrHigher = vista_sp2_or_higher;
  183. }else{
  184. isWindowsVista = false;
  185. isWindowsVistaSP2OrHigher = false;
  186. isWindowsVistaOrHigher = false;
  187. isWindows7OrHigher = false;
  188. }
  189. }
  190. public static final boolean isOSX_10_5_OrHigher;
  191. public static final boolean isOSX_10_6_OrHigher;
  192. public static final boolean isOSX_10_7_OrHigher;
  193. public static final boolean isOSX_10_8_OrHigher;
  194. static{
  195. if ( isOSX ){
  196. int first_digit = 0;
  197. int second_digit = 0;
  198. try{
  199. String os_version = System.getProperty( "os.version" );
  200. String[] bits = os_version.split( "\\." );
  201. first_digit = Integer.parseInt( bits[0] );
  202. if ( bits.length > 1 ){
  203. second_digit = Integer.parseInt( bits[1] );
  204. }
  205. }catch( Throwable e ){
  206. }
  207. isOSX_10_5_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 5 );
  208. isOSX_10_6_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 6 );
  209. isOSX_10_7_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 7 );
  210. isOSX_10_8_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 8 );
  211. }else{
  212. isOSX_10_5_OrHigher = false;
  213. isOSX_10_6_OrHigher = false;
  214. isOSX_10_7_OrHigher = false;
  215. isOSX_10_8_OrHigher = false;
  216. }
  217. }
  218. public static final String JAVA_VERSION = System.getProperty("java.version");
  219. public static final String FILE_WILDCARD = isWindows?"*.*":"*";
  220. /**
  221. * Gets the current version, or if a CVS version, the one on which it is based
  222. * @return
  223. */
  224. public static String
  225. getBaseVersion()
  226. {
  227. return( getBaseVersion( AZUREUS_VERSION ));
  228. }
  229. public static String
  230. getBaseVersion(
  231. String version )
  232. {
  233. int p1 = version.indexOf("_"); // _CVS or _Bnn
  234. if ( p1 == -1 ){
  235. return( version );
  236. }
  237. return( version.substring(0,p1));
  238. }
  239. /**
  240. * is this a formal build or CVS/incremental
  241. * @return
  242. */
  243. public static boolean
  244. isCVSVersion()
  245. {
  246. return IS_CVS_VERSION;
  247. }
  248. public static boolean
  249. isCVSVersion(
  250. String version )
  251. {
  252. return( version.indexOf("_") != -1 );
  253. }
  254. /**
  255. * For CVS builds this returns the incremental build number. For people running their own
  256. * builds this returns -1
  257. * @return
  258. */
  259. public static int
  260. getIncrementalBuild()
  261. {
  262. return( getIncrementalBuild( AZUREUS_VERSION ));
  263. }
  264. public static int
  265. getIncrementalBuild(
  266. String version )
  267. {
  268. if ( !isCVSVersion(version)){
  269. return( 0 );
  270. }
  271. int p1 = version.indexOf( "_B" );
  272. if ( p1 == -1 ){
  273. return( -1 );
  274. }
  275. try{
  276. return( Integer.parseInt( version.substring(p1+2)));
  277. }catch( Throwable e ){
  278. System.out.println("can't parse version");
  279. return( -1 );
  280. }
  281. }
  282. public static boolean
  283. isCurrentVersionLT(
  284. String version )
  285. {
  286. return( compareVersions( AZUREUS_VERSION, version ) < 0 );
  287. }
  288. public static boolean
  289. isCurrentVersionGE(
  290. String version )
  291. {
  292. return( compareVersions( AZUREUS_VERSION, version ) >= 0 );
  293. }
  294. /**
  295. * compare two version strings of form n.n.n.n (e.g. 1.2.3.4)
  296. * @param version_1
  297. * @param version_2
  298. * @return -ve -> version_1 lower, 0 = same, +ve -> version_1 higher
  299. */
  300. public static int
  301. compareVersions(
  302. String version_1,
  303. String version_2 )
  304. {
  305. try{
  306. version_1 = version_1.replaceAll( "_CVS", "_B100" );
  307. version_2 = version_2.replaceAll( "_CVS", "_B100" );
  308. if ( version_1.startsWith("." )){
  309. version_1 = "0" + version_1;
  310. }
  311. if ( version_2.startsWith("." )){
  312. version_2 = "0" + version_2;
  313. }
  314. version_1 = version_1.replaceAll("[^0-9.]", ".");
  315. version_2 = version_2.replaceAll("[^0-9.]", ".");
  316. StringTokenizer tok1 = new StringTokenizer(version_1,".");
  317. StringTokenizer tok2 = new StringTokenizer(version_2,".");
  318. while( true ){
  319. if ( tok1.hasMoreTokens() && tok2.hasMoreTokens()){
  320. int i1 = Integer.parseInt(tok1.nextToken());
  321. int i2 = Integer.parseInt(tok2.nextToken());
  322. if ( i1 != i2 ){
  323. return( i1 - i2 );
  324. }
  325. }else if ( tok1.hasMoreTokens()){
  326. int i1 = Integer.parseInt(tok1.nextToken());
  327. if ( i1 != 0 ){
  328. return( 1 );
  329. }
  330. }else if ( tok2.hasMoreTokens()){
  331. int i2 = Integer.parseInt(tok2.nextToken());
  332. if ( i2 != 0 ){
  333. return( -1 );
  334. }
  335. }else{
  336. return( 0 );
  337. }
  338. }
  339. }catch( Throwable e ){
  340. Debug.printStackTrace(e);
  341. return( 0 );
  342. }
  343. }
  344. public static boolean
  345. isValidVersionFormat(
  346. String version )
  347. {
  348. if ( version == null || version.length() == 0 ){
  349. return( false );
  350. }
  351. for (int i=0;i<version.length();i++){
  352. char c = version.charAt(i);
  353. if ( !( Character.isDigit( c ) || c == '.' )){
  354. return( false) ;
  355. }
  356. }
  357. if ( version.startsWith( "." ) || version.endsWith( "." ) || version.indexOf( ".." ) != -1 ){
  358. return( false );
  359. }
  360. return( true );
  361. }
  362. public static boolean
  363. isAzureusDomain(
  364. String host )
  365. {
  366. host = host.toLowerCase();
  367. for (int i=0; i<AZUREUS_DOMAINS.length; i++) {
  368. String domain = (String) AZUREUS_DOMAINS[i];
  369. if ( domain.equals( host )){
  370. return( true );
  371. }
  372. if ( host.endsWith("." + domain)){
  373. return( true );
  374. }
  375. }
  376. return( false );
  377. }
  378. public static void main(String[] args) {
  379. System.out.println(compareVersions("3.0.0.1", "3.0.0.0"));
  380. System.out.println(compareVersions("3.0.0.0_B1", "3.0.0.0"));
  381. System.out.println(compareVersions("3.0.0.0", "3.0.0.0_B1"));
  382. System.out.println(compareVersions("3.0.0.0_B1", "3.0.0.0_B4"));
  383. System.out.println(compareVersions("3.0.0.0..B1", "3.0.0.0_B4"));
  384. }
  385. }