1 21 22 package org.apache.derby.iapi.services.info; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.util.Properties ; 27 28 29 112 public final class ProductVersionHolder implements java.security.PrivilegedAction 113 { 114 115 private static final int BAD_NUMBER = -1; 119 private static final String ALPHA = "alpha"; 120 private static final String BETA = "beta"; 121 122 private final static int MAINT_ENCODING = 1000000; 123 124 private String productVendorName; 125 private String productName; 126 private String productTechnologyName; 127 private int majorVersion = BAD_NUMBER; 128 private int minorVersion = BAD_NUMBER; 129 private int maintVersion = BAD_NUMBER; 130 private int drdaMaintVersion = BAD_NUMBER; 131 private String buildNumber = "????"; 132 private Boolean isBeta; 133 134 private ProductVersionHolder() { 135 } 136 137 143 private ProductVersionHolder(String productVendorName, 144 String productName, 145 String productTechnologyName, 146 int majorVersion, 147 int minorVersion, 148 int maintVersion, 149 int drdaMaintVersion, 150 String buildNumber, 151 Boolean isBeta) 152 { 153 if (productVendorName != null) 154 this.productVendorName = productVendorName.trim(); 155 if (productName != null) 156 this.productName = productName.trim(); 157 if (productTechnologyName != null) 158 this.productTechnologyName = productTechnologyName.trim(); 159 this.majorVersion = majorVersion; 160 this.minorVersion = minorVersion; 161 this.maintVersion = maintVersion; 162 this.drdaMaintVersion = drdaMaintVersion; 163 this.buildNumber = buildNumber; 164 this.isBeta = isBeta; 165 } 166 167 185 public static ProductVersionHolder 186 getProductVersionHolder( 187 String productVendorName, 188 String productName, 189 String productTechnologyName, 190 int majorVersion, 191 int minorVersion, 192 int maintVersion, 193 int drdaMaintVersion, 194 String buildNumber, 195 Boolean isBeta) 196 { 197 ProductVersionHolder pvh = 198 new ProductVersionHolder(productVendorName, 199 productName, 200 productTechnologyName, 201 majorVersion, 202 minorVersion, 203 maintVersion, 204 drdaMaintVersion, 205 buildNumber, 206 isBeta); 207 return pvh; 208 } 209 210 222 public static ProductVersionHolder 223 getProductVersionHolderFromMyEnv(String productGenus) 224 { 225 226 ProductVersionHolder tempPVH = new ProductVersionHolder(); 227 228 tempPVH.productGenus = productGenus; 229 Properties p = (Properties ) java.security.AccessController.doPrivileged(tempPVH); 230 231 if (p == null) 232 return null; 233 234 return getProductVersionHolder(p); 235 } 236 237 238 247 public static ProductVersionHolder 248 getProductVersionHolderFromMyEnv(InputStream propertiesStream) 249 { 250 251 if (propertiesStream == null) 252 return null; 253 254 Properties p = new Properties (); 255 try { 256 p.load(propertiesStream); 257 } 258 catch (IOException ioe) { 259 260 System.out.println("IOE " + ioe.getMessage()); 261 return null; 267 } finally { 268 try { 269 propertiesStream.close(); 270 } catch (IOException ioe2) { 271 } 272 } 273 274 return getProductVersionHolder(p); 275 } 276 277 287 public static ProductVersionHolder 288 getProductVersionHolder(Properties p) 289 { 290 String pvn = p.getProperty(PropertyNames.PRODUCT_VENDOR_NAME); 291 String pn = p.getProperty(PropertyNames.PRODUCT_EXTERNAL_NAME); 292 String ptn = p.getProperty(PropertyNames.PRODUCT_TECHNOLOGY_NAME); 293 int v1 = parseInt(p.getProperty(PropertyNames.PRODUCT_MAJOR_VERSION)); 294 int v2 = parseInt(p.getProperty(PropertyNames.PRODUCT_MINOR_VERSION)); 295 int v3 = parseInt(p.getProperty(PropertyNames.PRODUCT_MAINT_VERSION)); 296 int v4 = parseInt(p.getProperty(PropertyNames.PRODUCT_DRDA_MAINT_VERSION)); 297 String bn = p.getProperty(PropertyNames.PRODUCT_BUILD_NUMBER); 298 Boolean isBeta = 299 Boolean.valueOf(p.getProperty(PropertyNames.PRODUCT_BETA_VERSION)); 300 return getProductVersionHolder(pvn,pn,ptn,v1,v2,v3,v4,bn,isBeta); 301 } 302 303 304 307 public String getProductVendorName() 308 { 309 return productVendorName; 310 } 311 312 313 316 public String getProductName() 317 { 318 return productName; 319 } 320 public String getProductTechnologyName() 321 { 322 return productTechnologyName; 323 } 324 325 328 public int getMajorVersion() {return majorVersion;} 329 332 public int getMinorVersion() {return minorVersion;} 333 336 public int getMaintVersion() {return maintVersion;} 337 338 343 public int getDrdaMaintVersion() {return drdaMaintVersion; } 344 345 348 public int getFixPackVersion() { return maintVersion / MAINT_ENCODING; } 349 350 351 354 public boolean isBeta() {return isBeta.booleanValue();} 355 358 public boolean isAlpha() { 359 return (majorVersion >= 5) 360 && (minorVersion > 2) 361 && ((maintVersion / MAINT_ENCODING) == 0); 362 } 363 366 public String getBuildNumber() {return buildNumber;} 367 368 376 public int getBuildNumberAsInt(){ 377 if (buildNumber == null) 378 return -1; 379 boolean dubiousCode = false; 380 int offset = buildNumber.indexOf('M'); 381 if (offset == -1) 382 offset = buildNumber.indexOf(':'); 383 else 384 dubiousCode = true; 385 if (offset == -1) 386 offset = buildNumber.length(); 387 else 388 dubiousCode = true; 389 390 try { 391 int bnai = Integer.parseInt(buildNumber.substring(0, offset)); 392 if (dubiousCode) 393 bnai = -bnai; 394 return bnai; 395 } catch (NumberFormatException nfe) 396 { 397 return -1; 398 } 399 } 400 401 409 private static int parseInt(String s) 410 { 411 int result = BAD_NUMBER; 413 try 414 { 415 if (s!=null) 416 result = Integer.parseInt(s); 417 } 418 catch (NumberFormatException nfe) 419 {} 420 421 if (result < 0) result = BAD_NUMBER; 422 return result; 423 } 424 425 431 public String toString() 432 { 433 StringBuffer sb = new StringBuffer (); 434 sb.append(getProductVendorName()); 435 sb.append(" - "); 436 sb.append(getProductName()); 437 sb.append(" - "); 438 sb.append(getVersionBuildString(true)); 439 return sb.toString(); 440 } 441 442 445 public String getSimpleVersionString() { 446 447 return ProductVersionHolder.simpleVersionString(majorVersion, minorVersion, isBeta()); 448 } 449 450 453 public static String simpleVersionString(int major, int minor, boolean isBeta) { 454 455 StringBuffer sb = new StringBuffer (); 456 457 sb.append(major); 458 sb.append('.'); 459 sb.append(minor); 460 if (isBeta) { 461 sb.append(' '); 462 sb.append(BETA); 463 } 464 465 return sb.toString(); 466 } 467 public static String fullVersionString(int major, int minor, int maint, boolean isBeta, String build) { 468 StringBuffer sb = new StringBuffer (); 469 sb.append(major); 470 sb.append('.'); 471 sb.append(minor); 472 sb.append('.'); 473 474 String preRelease = null; 475 if (major == 5 && minor <= 2 && maint < MAINT_ENCODING) 476 { 477 sb.append(maint); 478 if (isBeta) 479 preRelease = BETA; 480 } 481 else 482 { 483 int fixPack = maint / MAINT_ENCODING; 484 int bugVersion = maint % MAINT_ENCODING; 485 sb.append(fixPack); 486 sb.append('.'); 487 sb.append(bugVersion); 488 489 if (fixPack == 0) 490 { 491 preRelease = ALPHA; 492 } 493 else if (isBeta) { 494 preRelease = BETA; 495 } 496 } 497 498 if (preRelease != null) 499 { 500 sb.append(' '); 501 sb.append(preRelease); 502 } 503 if (build != null) { 504 sb.append(" - ("); 505 506 sb.append(build); 507 sb.append(')'); 508 } 509 return sb.toString(); 510 } 511 516 public String getVersionBuildString(boolean withBuild) 517 { 518 return ProductVersionHolder.fullVersionString(majorVersion, minorVersion, maintVersion, isBeta(), 519 withBuild ? buildNumber : null); 520 } 521 522 525 private String productGenus; 526 public final Object run() { 527 528 return loadProperties(this.productGenus); 530 } 531 private Properties loadProperties(String productGenus) { 533 String resourceName = "/org/apache/derby/info/" + productGenus+".properties"; 534 535 InputStream is = getClass().getResourceAsStream(resourceName); 536 if (is==null) { 537 return null; 538 } 539 540 Properties p = new Properties (); 541 try { 542 p.load(is); 543 return p; 544 } 545 catch (IOException ioe) { 546 return null; 552 } 553 } 554 } 555 | Popular Tags |