1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.List ; 35 import java.util.Properties ; 36 37 import net.sf.jasperreports.engine.JRException; 38 import net.sf.jasperreports.engine.JRPropertiesMap; 39 import net.sf.jasperreports.engine.JRRuntimeException; 40 41 56 public class JRProperties 57 { 58 61 protected static final String DEFAULT_PROPERTIES_FILE = "jasperreports.properties"; 62 63 66 public static final String PROPERTY_PREFIX = "net.sf.jasperreports."; 67 68 71 public static final String PROPERTIES_FILE = PROPERTY_PREFIX + "properties"; 72 73 78 public static final String COMPILER_CLASS = PROPERTY_PREFIX + "compiler.class"; 79 80 85 public static final String COMPILER_XML_VALIDATION = PROPERTY_PREFIX + "compiler.xml.validation"; 86 87 92 public static final String COMPILER_KEEP_JAVA_FILE = PROPERTY_PREFIX + "compiler.keep.java.file"; 93 94 99 public static final String COMPILER_TEMP_DIR = PROPERTY_PREFIX + "compiler.temp.dir"; 100 101 106 public static final String COMPILER_CLASSPATH = PROPERTY_PREFIX + "compiler.classpath"; 107 108 113 public static final String EXPORT_XML_VALIDATION = PROPERTY_PREFIX + "export.xml.validation"; 114 115 118 public static final String PDF_FONT_FILES_PREFIX = PROPERTY_PREFIX + "export.pdf.font."; 119 120 123 public static final String PDF_FONT_DIRS_PREFIX = PROPERTY_PREFIX + "export.pdf.fontdir."; 124 125 131 public static final String QUERY_EXECUTER_FACTORY_PREFIX = PROPERTY_PREFIX + "query.executer.factory."; 132 133 134 138 public static final String SUBREPORT_RUNNER_FACTORY = PROPERTY_PREFIX + "subreport.runner.factory"; 139 140 147 public static final String PDF_FORCE_LINEBREAK_POLICY = PROPERTY_PREFIX + "export.pdf.force.linebreak.policy"; 148 149 150 protected static Properties props; 151 152 protected static Properties savedProps; 153 154 static 155 { 156 initProperties(); 157 } 158 159 162 protected static void initProperties() 163 { 164 try 165 { 166 Properties defaults = getDefaults(); 167 String propFile = getSystemProperty(PROPERTIES_FILE); 168 if (propFile == null) 169 { 170 props = loadProperties(DEFAULT_PROPERTIES_FILE, defaults); 171 if (props == null) 172 { 173 props = new Properties (defaults); 174 } 175 } 176 else 177 { 178 props = loadProperties(propFile, defaults); 179 if (props == null) 180 { 181 throw new JRRuntimeException("Could not load properties file \"" + propFile + "\""); 182 } 183 } 184 185 loadSystemProperties(); 186 } 187 catch (JRException e) 188 { 189 throw new JRRuntimeException("Error loading the properties", e); 190 } 191 } 192 193 protected static void loadSystemProperties() 194 { 195 loadSystemProperty("jasper.reports.compiler.class", COMPILER_CLASS); 196 loadSystemProperty("jasper.reports.compile.xml.validation", COMPILER_XML_VALIDATION); 197 loadSystemProperty("jasper.reports.export.xml.validation", EXPORT_XML_VALIDATION); 198 loadSystemProperty("jasper.reports.compile.keep.java.file", COMPILER_KEEP_JAVA_FILE); 199 loadSystemProperty("jasper.reports.compile.temp", COMPILER_TEMP_DIR); 200 loadSystemProperty("jasper.reports.compile.class.path", COMPILER_CLASSPATH); 201 } 202 203 208 protected static Properties getDefaults() throws JRException 209 { 210 Properties defaults = new Properties (); 211 212 InputStream is = JRProperties.class.getResourceAsStream("/default.jasperreports.properties"); 213 214 if (is == null) 215 { 216 throw new JRException("Default properties file not found."); 217 } 218 219 try 220 { 221 defaults.load(is); 222 } 223 catch (IOException e) 224 { 225 throw new JRException("Failed to load default properties.", e); 226 } 227 finally 228 { 229 try 230 { 231 is.close(); 232 } 233 catch (IOException e) 234 { 235 } 236 } 237 238 String userDir = getSystemProperty("user.dir"); 239 if (userDir != null) 240 { 241 defaults.setProperty(COMPILER_TEMP_DIR, userDir); 242 } 243 244 String classPath = getSystemProperty("java.class.path"); 245 if (classPath != null) 246 { 247 defaults.setProperty(COMPILER_CLASSPATH, classPath); 248 } 249 250 return defaults; 251 } 252 253 256 protected static String getSystemProperty(String propertyName) 257 { 258 try 259 { 260 return System.getProperty(propertyName); 261 } 262 catch (SecurityException e) 263 { 264 return null; 269 } 270 } 271 272 protected static void loadSystemProperty(String sysKey, String propKey) 273 { 274 String val = getSystemProperty(sysKey); 275 if (val != null) 276 { 277 props.setProperty(propKey, val); 278 } 279 } 280 281 289 public static Properties loadProperties (String name, Properties defaults) throws JRException 290 { 291 Properties properties = null; 292 293 InputStream is = null; 294 295 try 296 { 297 is = JRLoader.getLocationInputStream(name); 298 } 299 catch (SecurityException e) 300 { 301 } 306 307 if (is != null) 308 { 309 properties = new Properties (defaults); 310 try 311 { 312 properties.load(is); 313 } 314 catch (IOException e) 315 { 316 throw new JRException("Failed to load properties file \"" + name + "\"", e); 317 } 318 finally 319 { 320 try 321 { 322 is.close(); 323 } 324 catch (IOException e) 325 { 326 } 327 } 328 } 329 330 return properties; 331 } 332 333 339 public static String getProperty (String key) 340 { 341 return props.getProperty(key); 342 } 343 344 350 public static boolean getBooleanProperty (String key) 351 { 352 return asBoolean(props.getProperty(key)); 353 } 354 355 361 public static int getIntegerProperty (String key) 362 { 363 return asInteger(props.getProperty(key)); 364 } 365 366 372 public static boolean asBoolean(String value) 373 { 374 return Boolean.valueOf(value).booleanValue(); 375 } 376 377 383 public static int asInteger(String value) 384 { 385 return Integer.parseInt(value); 386 } 387 388 394 public static void setProperty (String key, String value) 395 { 396 props.setProperty(key, value); 397 } 398 399 405 public static void setProperty (String key, boolean value) 406 { 407 props.setProperty(key, String.valueOf(value)); 408 } 409 410 415 public static void backupProperties () 416 { 417 savedProps = (Properties ) props.clone(); 418 } 419 420 425 public static void restoreProperties () 426 { 427 if (savedProps != null) 428 { 429 try 430 { 431 props.clear(); 432 props.putAll(savedProps); 433 } 434 finally 435 { 436 savedProps = null; 437 } 438 } 439 } 440 441 446 public static class PropertySuffix 447 { 448 protected final String key; 449 protected final String suffix; 450 protected final String value; 451 452 public PropertySuffix (String key, String suffix, String value) 453 { 454 this.key = key; 455 this.suffix = suffix; 456 this.value = value; 457 } 458 459 public String getKey() 460 { 461 return key; 462 } 463 464 public String getSuffix () 465 { 466 return suffix; 467 } 468 469 public String getValue () 470 { 471 return value; 472 } 473 } 474 475 481 public static List getProperties (String prefix) 482 { 483 int prefixLength = prefix.length(); 484 List values = new ArrayList (); 485 for (Enumeration names = props.propertyNames(); names.hasMoreElements();) 486 { 487 String name = (String ) names.nextElement(); 488 if (name.startsWith(prefix)) 489 { 490 String suffix = name.substring(prefixLength); 491 String value = props.getProperty(name); 492 values.add(new PropertySuffix(name, suffix, value)); 493 } 494 } 495 return values; 496 } 497 498 506 public static String getProperty (JRPropertiesMap propertiesMap, String key) 507 { 508 String value = null; 509 if (propertiesMap != null) 510 { 511 value = propertiesMap.getProperty(key); 512 } 513 514 if (value == null) 515 { 516 value = props.getProperty(key); 517 } 518 519 return value; 520 } 521 522 531 public static boolean getBooleanProperty (JRPropertiesMap propertiesMap, String key, boolean defaultValue) 532 { 533 String value = getProperty(propertiesMap, key); 534 535 return value == null ? defaultValue : asBoolean(value); 536 } 537 538 547 public static int getIntegerProperty (JRPropertiesMap propertiesMap, String key, int defaultValue) 548 { 549 String value = getProperty(propertiesMap, key); 550 551 return value == null ? defaultValue : asInteger(value); 552 } 553 554 560 public static long asLong(String value) 561 { 562 return Long.parseLong(value); 563 } 564 565 571 public static long getLongProperty (String key) 572 { 573 return asLong(props.getProperty(key)); 574 } 575 576 585 public static long getLongProperty (JRPropertiesMap propertiesMap, String key, int defaultValue) 586 { 587 String value = getProperty(propertiesMap, key); 588 589 return value == null ? defaultValue : asLong(value); 590 } 591 } 592 | Popular Tags |