1 43 44 package org.jfree.base.modules; 45 46 import java.io.BufferedReader ; 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.InputStreamReader ; 50 import java.util.ArrayList ; 51 52 import org.jfree.util.ObjectUtilities; 53 54 55 98 public abstract class AbstractModule extends DefaultModuleInfo implements Module 99 { 100 105 private static class ReaderHelper 106 { 107 108 private String buffer; 109 110 private final BufferedReader reader; 111 112 117 public ReaderHelper(final BufferedReader reader) 118 { 119 this.reader = reader; 120 } 121 122 129 public boolean hasNext() throws IOException 130 { 131 if (this.buffer == null) 132 { 133 this.buffer = readLine(); 134 } 135 return this.buffer != null; 136 } 137 138 143 public String next() 144 { 145 final String line = this.buffer; 146 this.buffer = null; 147 return line; 148 } 149 150 156 public void pushBack(final String line) 157 { 158 this.buffer = line; 159 } 160 161 167 protected String readLine() throws IOException 168 { 169 String line = this.reader.readLine(); 170 while (line != null && (line.length() == 0 || line.startsWith("#"))) 171 { 172 line = this.reader.readLine(); 174 } 175 return line; 176 } 177 178 183 public void close() throws IOException 184 { 185 this.reader.close(); 186 } 187 } 188 189 190 private ModuleInfo[] requiredModules; 191 192 private ModuleInfo[] optionalModules; 193 194 195 private String name; 196 197 private String description; 198 199 private String producer; 200 201 private String subsystem; 202 203 206 public AbstractModule() 207 { 208 setModuleClass(this.getClass().getName()); 209 } 210 211 217 protected void loadModuleInfo() throws ModuleInitializeException 218 { 219 final InputStream in = ObjectUtilities.getResourceRelativeAsStream 220 ("module.properties", getClass()); 221 if (in == null) 222 { 223 throw new ModuleInitializeException 224 ("File 'module.properties' not found in module package."); 225 } 226 loadModuleInfo(in); 227 } 228 229 237 protected void loadModuleInfo(final InputStream in) throws ModuleInitializeException 238 { 239 try 240 { 241 if (in == null) 242 { 243 throw new NullPointerException 244 ("Given InputStream is null."); 245 } 246 final ReaderHelper rh = new ReaderHelper(new BufferedReader 247 (new InputStreamReader (in, "ISO-8859-1"))); 248 249 final ArrayList optionalModules = new ArrayList (); 250 final ArrayList dependendModules = new ArrayList (); 251 252 while (rh.hasNext()) 253 { 254 final String lastLineRead = rh.next(); 255 if (lastLineRead.startsWith("module-info:")) 256 { 257 readModuleInfo(rh); 258 } 259 else if (lastLineRead.startsWith("depends:")) 260 { 261 dependendModules.add(readExternalModule(rh)); 262 } 263 else if (lastLineRead.startsWith("optional:")) 264 { 265 optionalModules.add(readExternalModule(rh)); 266 } 267 else 268 { 269 } 272 } 273 rh.close(); 274 275 this.optionalModules = (ModuleInfo[]) 276 optionalModules.toArray(new ModuleInfo[optionalModules.size()]); 277 278 this.requiredModules = (ModuleInfo[]) 279 dependendModules.toArray(new ModuleInfo[dependendModules.size()]); 280 } 281 catch (IOException ioe) 282 { 283 throw new ModuleInitializeException("Failed to load properties", ioe); 284 } 285 } 286 287 296 private String readValue(final ReaderHelper reader, String firstLine) throws IOException 297 { 298 final StringBuffer b = new StringBuffer (firstLine.trim()); 299 boolean newLine = true; 300 while (isNextLineValueLine(reader)) 301 { 302 firstLine = reader.next(); 303 final String trimedLine = firstLine.trim(); 304 if (trimedLine.length() == 0 && (newLine == false)) 305 { 306 b.append ("\n"); 307 newLine = true; 308 } 309 else 310 { 311 if (newLine == false) 312 { 313 b.append(" "); 314 } 315 b.append(parseValue(trimedLine)); 316 newLine = false; 317 } 318 } 319 return b.toString(); 320 } 321 322 329 private boolean isNextLineValueLine (final ReaderHelper reader) throws IOException 330 { 331 if (reader.hasNext() == false) 332 { 333 return false; 334 } 335 final String firstLine = reader.next(); 336 if (firstLine == null) 337 { 338 return false; 339 } 340 if (parseKey(firstLine) != null) 341 { 342 reader.pushBack(firstLine); 343 return false; 344 } 345 reader.pushBack(firstLine); 346 return true; 347 } 348 349 356 private void readModuleInfo(final ReaderHelper reader) throws IOException 357 { 358 while (reader.hasNext()) 359 { 360 final String lastLineRead = reader.next(); 361 362 if (Character.isWhitespace(lastLineRead.charAt(0)) == false) 363 { 364 reader.pushBack(lastLineRead); 366 return; 367 } 368 369 final String line = lastLineRead.trim(); 370 final String key = parseKey(line); 371 if (key != null) 372 { 373 final String b = readValue(reader, parseValue(line.trim())); 375 376 if (key.equals("name")) 377 { 378 setName(b); 379 } 380 else if (key.equals("producer")) 381 { 382 setProducer(b); 383 } 384 else if (key.equals("description")) 385 { 386 setDescription(b); 387 } 388 else if (key.equals("subsystem")) 389 { 390 setSubSystem(b); 391 } 392 else if (key.equals("version.major")) 393 { 394 setMajorVersion(b); 395 } 396 else if (key.equals("version.minor")) 397 { 398 setMinorVersion(b); 399 } 400 else if (key.equals("version.patchlevel")) 401 { 402 setPatchLevel(b); 403 } 404 } 405 } 406 } 407 408 415 private String parseKey(final String line) 416 { 417 final int idx = line.indexOf(':'); 418 if (idx == -1) 419 { 420 return null; 421 } 422 return line.substring(0, idx); 423 } 424 425 431 private String parseValue(final String line) 432 { 433 final int idx = line.indexOf(':'); 434 if (idx == -1) 435 { 436 return line; 437 } 438 if ((idx + 1) == line.length()) 439 { 440 return ""; 441 } 442 return line.substring(idx + 1); 443 } 444 445 453 private DefaultModuleInfo readExternalModule(final ReaderHelper reader) 454 throws IOException 455 { 456 final DefaultModuleInfo mi = new DefaultModuleInfo(); 457 458 while (reader.hasNext()) 459 { 460 final String lastLineRead = reader.next(); 461 462 if (Character.isWhitespace(lastLineRead.charAt(0)) == false) 463 { 464 reader.pushBack(lastLineRead); 466 return mi; 467 } 468 469 final String line = lastLineRead.trim(); 470 final String key = parseKey(line); 471 if (key != null) 472 { 473 final String b = readValue(reader, parseValue(line)); 474 if (key.equals("module")) 475 { 476 mi.setModuleClass(b); 477 } 478 else if (key.equals("version.major")) 479 { 480 mi.setMajorVersion(b); 481 } 482 else if (key.equals("version.minor")) 483 { 484 mi.setMinorVersion(b); 485 } 486 else if (key.equals("version.patchlevel")) 487 { 488 mi.setPatchLevel(b); 489 } 490 } 491 } 492 return mi; 493 } 494 495 502 public String getName() 503 { 504 return this.name; 505 } 506 507 512 protected void setName(final String name) 513 { 514 this.name = name; 515 } 516 517 523 public String getDescription() 524 { 525 return this.description; 526 } 527 528 533 protected void setDescription(final String description) 534 { 535 this.description = description; 536 } 537 538 545 public String getProducer() 546 { 547 return this.producer; 548 } 549 550 555 protected void setProducer(final String producer) 556 { 557 this.producer = producer; 558 } 559 560 567 public ModuleInfo[] getRequiredModules() 568 { 569 final ModuleInfo[] retval = new ModuleInfo[this.requiredModules.length]; 570 System.arraycopy(this.requiredModules, 0, retval, 0, this.requiredModules.length); 571 return retval; 572 } 573 574 581 public ModuleInfo[] getOptionalModules() 582 { 583 final ModuleInfo[] retval = new ModuleInfo[this.optionalModules.length]; 584 System.arraycopy(this.optionalModules, 0, retval, 0, this.optionalModules.length); 585 return retval; 586 } 587 588 593 protected void setRequiredModules(final ModuleInfo[] requiredModules) 594 { 595 this.requiredModules = new ModuleInfo[requiredModules.length]; 596 System.arraycopy(requiredModules, 0, this.requiredModules, 0, requiredModules.length); 597 } 598 599 604 public void setOptionalModules(final ModuleInfo[] optionalModules) 605 { 606 this.optionalModules = new ModuleInfo[optionalModules.length]; 607 System.arraycopy(optionalModules, 0, this.optionalModules, 0, optionalModules.length); 608 } 609 610 616 public String toString() 617 { 618 final StringBuffer buffer = new StringBuffer (); 619 buffer.append("Module : "); 620 buffer.append(getName()); 621 buffer.append("\n"); 622 buffer.append("ModuleClass : "); 623 buffer.append(getModuleClass()); 624 buffer.append("\n"); 625 buffer.append("Version: "); 626 buffer.append(getMajorVersion()); 627 buffer.append("."); 628 buffer.append(getMinorVersion()); 629 buffer.append("."); 630 buffer.append(getPatchLevel()); 631 buffer.append("\n"); 632 buffer.append("Producer: "); 633 buffer.append(getProducer()); 634 buffer.append("\n"); 635 buffer.append("Description: "); 636 buffer.append(getDescription()); 637 buffer.append("\n"); 638 return buffer.toString(); 639 } 640 641 648 protected static boolean isClassLoadable(final String name) 649 { 650 try 651 { 652 Thread.currentThread().getContextClassLoader().loadClass(name); 653 return true; 654 } 655 catch (Exception e) 656 { 657 return false; 658 } 659 } 660 661 667 public void configure(final SubSystem subSystem) 668 { 669 final InputStream in = ObjectUtilities.getResourceRelativeAsStream 670 ("configuration.properties", getClass()); 671 if (in == null) 672 { 673 return; 674 } 675 subSystem.getPackageManager().getPackageConfiguration().load(in); 676 } 677 678 685 protected void performExternalInitialize(final String classname) 686 throws ModuleInitializeException 687 { 688 try 689 { 690 final Class c = Thread.currentThread().getContextClassLoader().loadClass(classname); 691 final ModuleInitializer mi = (ModuleInitializer) c.newInstance(); 692 mi.performInit(); 693 } 694 catch (ModuleInitializeException mie) 695 { 696 throw mie; 697 } 698 catch (Exception e) 699 { 700 throw new ModuleInitializeException("Failed to load specified initializer class.", e); 701 } 702 } 703 704 710 public String getSubSystem() 711 { 712 if (this.subsystem == null) 713 { 714 return getName(); 715 } 716 return this.subsystem; 717 } 718 719 724 protected void setSubSystem (final String name) 725 { 726 this.subsystem = name; 727 } 728 } 729 | Popular Tags |