1 52 53 package com.go.teatools; 54 55 import java.lang.reflect.Method ; 56 import java.util.Date ; 57 58 66 public class PackageDescriptor { 67 68 71 public static PackageDescriptor forName(String packageName) { 72 return forName(packageName, null); 73 } 74 75 79 public static PackageDescriptor forName(String packageName, 80 ClassLoader classLoader) { 81 82 PackageDescriptor pd = new PackageDescriptor(packageName); 83 if (classLoader == null) { 84 classLoader = ClassLoader.getSystemClassLoader(); 85 } 86 87 if (packageName == null || packageName.trim().length() == 0) { 88 return pd; 89 } 90 91 String packageInfoClassName = packageName; 92 93 if (!packageInfoClassName.endsWith(".")) { 94 packageInfoClassName = packageInfoClassName + "."; 95 } 96 97 packageInfoClassName = packageInfoClassName + "PackageInfo"; 98 99 100 Class packageInfoClass = null; 101 102 try { 103 if (classLoader != null) { 104 packageInfoClass = 105 classLoader.loadClass(packageInfoClassName); 106 } 107 else { 108 packageInfoClass = Class.forName(packageInfoClassName); 109 } 110 } 111 catch (Throwable t) { 112 } 113 114 if (packageInfoClass != null) { 115 try { 116 initFromPackageInfo(pd, packageInfoClass); 117 } 118 catch (Throwable t) { 119 } 120 } 121 else { 122 123 Package pkg = null; 124 125 if (classLoader != null) { 126 PackageLoader packageLoader = 128 new PackageLoader(classLoader); 129 130 pkg = packageLoader.getPackage(packageName); 131 } 132 133 if (pkg == null) { 134 pkg = Package.getPackage(packageName); 135 } 136 137 if (pkg != null) { 138 initFromPackage(pd, pkg); 139 } 140 } 141 142 return pd; 143 } 144 145 private static void initFromPackageInfo(PackageDescriptor pd, 147 Class packageInfoClass) 148 throws Exception { 149 150 151 pd.setExists(true); 152 153 Class [] ca = new Class [0]; 154 Object [] oa = new Object [0]; 155 156 pd.setSpecificationTitle( 157 (String ) (packageInfoClass.getMethod("getSpecificationTitle", 158 ca)).invoke(null, oa)); 159 160 pd.setSpecificationVersion( 161 (String ) (packageInfoClass.getMethod("getSpecificationVersion", 162 ca)).invoke(null, oa)); 163 164 pd.setSpecificationVendor( 165 (String ) (packageInfoClass.getMethod("getSpecificationVendor", 166 ca)).invoke(null, oa)); 167 168 pd.setImplementationTitle( 169 (String ) (packageInfoClass.getMethod("getImplementationTitle", 170 ca)).invoke(null, oa)); 171 172 pd.setImplementationVersion( 173 (String ) (packageInfoClass.getMethod("getImplementationVersion", 174 ca)).invoke(null, oa)); 175 176 pd.setImplementationVendor( 177 (String ) (packageInfoClass.getMethod("getImplementationVendor", 178 ca)).invoke(null, oa)); 179 180 pd.setBaseDirectory( 181 (String ) (packageInfoClass.getMethod("getBaseDirectory", 182 ca)).invoke(null, oa)); 183 184 pd.setRepository( 185 (String ) (packageInfoClass.getMethod("getRepository", 186 ca)).invoke(null, oa)); 187 188 pd.setUsername( 189 (String ) (packageInfoClass.getMethod("getUsername", 190 ca)).invoke(null, oa)); 191 192 pd.setBuildMachine( 193 (String ) (packageInfoClass.getMethod("getBuildMachine", 194 ca)).invoke(null, oa)); 195 196 pd.setGroup( 197 (String ) (packageInfoClass.getMethod("getGroup", 198 ca)).invoke(null, oa)); 199 200 pd.setProject( 201 (String ) (packageInfoClass.getMethod("getProject", 202 ca)).invoke(null, oa)); 203 204 pd.setBuildLocation( 205 (String ) (packageInfoClass.getMethod("getBuildLocation", 206 ca)).invoke(null, oa)); 207 208 pd.setProduct( 209 (String ) (packageInfoClass.getMethod("getProduct", 210 ca)).invoke(null, oa)); 211 212 pd.setProductVersion( 213 (String ) (packageInfoClass.getMethod("getProductVersion", 214 ca)).invoke(null, oa)); 215 216 pd.setBuildNumber( 217 (String ) (packageInfoClass.getMethod("getBuildNumber", 218 ca)).invoke(null, oa)); 219 220 pd.setBuildDate( 221 (Date ) (packageInfoClass.getMethod("getBuildDate", 222 ca)).invoke(null, oa)); 223 224 225 } 226 227 private static void initFromPackage(PackageDescriptor pd, 229 Package pkg) { 230 231 pd.setExists(true); 232 233 String specificationTitle = pkg.getSpecificationTitle(); 234 String specificationVersion = pkg.getSpecificationVersion(); 235 String specificationVendor = pkg.getSpecificationVendor(); 236 237 String implementationTitle = pkg.getImplementationTitle(); 238 String implementationVersion = pkg.getImplementationVersion(); 239 String implementationVendor = pkg.getImplementationVendor(); 240 241 if (implementationTitle == null) { 242 implementationTitle = specificationTitle; 243 } 244 245 if (implementationVersion == null) { 246 implementationVersion = specificationVersion; 247 } 248 249 if (implementationVendor == null) { 250 implementationVendor = specificationVendor; 251 } 252 253 254 pd.setSpecificationTitle(specificationTitle); 255 pd.setSpecificationVersion(specificationVersion); 256 pd.setSpecificationVendor(specificationVendor); 257 258 pd.setImplementationTitle(implementationTitle); 259 pd.setImplementationVersion(implementationVersion); 260 pd.setImplementationVendor(implementationVendor); 261 262 pd.setProduct(implementationTitle); 263 pd.setProductVersion(implementationVersion); 264 } 265 266 267 268 protected String mPackageName; 269 protected boolean mExists; 270 271 protected String mSpecificationTitle; 272 protected String mSpecificationVersion; 273 protected String mSpecificationVendor; 274 protected String mImplementationTitle; 275 protected String mImplementationVersion; 276 protected String mImplementationVendor; 277 protected String mBaseDirectory; 278 protected String mRepository; 279 protected String mUsername; 280 protected String mBuildMachine; 281 protected String mGroup; 282 protected String mProject; 283 protected String mBuildLocation; 284 protected String mProduct; 285 protected String mProductVersion; 286 protected String mBuildNumber; 287 protected Date mBuildDate; 288 289 290 293 public PackageDescriptor(String packageName) { 294 setPackageName(packageName); 295 } 296 297 public void setPackageName(String packageName) { 298 mPackageName = packageName; 299 } 300 301 public String getPackageName() { 302 return mPackageName; 303 } 304 305 309 public boolean getExists() { 310 return mExists; 311 } 312 313 public void setSpecificationTitle(String s) { 314 mSpecificationTitle = s; 315 } 316 317 public String getSpecificationTitle() { 318 return mSpecificationTitle; 319 } 320 321 public void setSpecificationVersion(String s) { 322 mSpecificationVersion = s; 323 } 324 325 public String getSpecificationVersion() { 326 return mSpecificationVersion; 327 } 328 329 public void setSpecificationVendor(String s) { 330 mSpecificationVendor = s; 331 } 332 333 public String getSpecificationVendor() { 334 return mSpecificationVendor; 335 } 336 337 public void setImplementationTitle(String s) { 338 mImplementationTitle = s; 339 } 340 341 public String getImplementationTitle() { 342 return mImplementationTitle; 343 } 344 345 public void setImplementationVersion(String s) { 346 mImplementationVersion = s; 347 } 348 349 public String getImplementationVersion() { 350 return mImplementationVersion; 351 } 352 353 public void setImplementationVendor(String s) { 354 mImplementationVendor = s; 355 } 356 357 public String getImplementationVendor() { 358 return mImplementationVendor; 359 } 360 361 public void setBaseDirectory(String s) { 362 mBaseDirectory = s; 363 } 364 365 public String getBaseDirectory() { 366 return mBaseDirectory; 367 } 368 369 public void setRepository(String s) { 370 mRepository = s; 371 } 372 373 public String getRepository() { 374 return mRepository; 375 } 376 377 public void setUsername(String s) { 378 mUsername = s; 379 } 380 381 public String getUsername() { 382 return mUsername; 383 } 384 385 public void setBuildMachine(String s) { 386 mBuildMachine = s; 387 } 388 389 public String getBuildMachine() { 390 return mBuildMachine; 391 } 392 393 public void setGroup(String s) { 394 mGroup = s; 395 } 396 397 public String getGroup() { 398 return mGroup; 399 } 400 401 public void setProject(String s) { 402 mProject = s; 403 } 404 405 public String getProject() { 406 return mProject; 407 } 408 409 public void setBuildLocation(String s) { 410 mBuildLocation = s; 411 } 412 413 public String getBuildLocation() { 414 return mBuildLocation; 415 } 416 417 public void setProduct(String s) { 418 mProduct = s; 419 } 420 421 public String getProduct() { 422 return mProduct; 423 } 424 425 public void setProductVersion(String s) { 426 mProductVersion = s; 427 } 428 429 public String getProductVersion() { 430 return mProductVersion; 431 } 432 433 public void setBuildNumber(String s) { 434 mBuildNumber = s; 435 } 436 437 public String getBuildNumber() { 438 return mBuildNumber; 439 } 440 441 public void setBuildDate(Date d) { 442 mBuildDate = d; 443 } 444 445 public Date getBuildDate() { 446 return mBuildDate; 447 } 448 449 private void setExists(boolean exists) { 450 mExists = exists; 451 } 452 453 private static class PackageLoader extends ClassLoader { 454 455 PackageLoader(ClassLoader parent) { 456 super(parent); 457 } 458 459 public Package getPackage(String name) { 460 return super.getPackage(name); 461 } 462 } 463 464 } 465 | Popular Tags |