243 } catch (Exception ex2) { 244 // doing a setEncoding with null should always work. 245 // assert false; 246 } 247 } 248 } 249 250 251 /** 252 * Construct a default <tt>FileHandler</tt>. This will be configured 253 * entirely from <tt>LogManager</tt> properties (or their default values). 254 * 255 * @exception IOException if there are IO problems opening the files. 256 * @exception SecurityException if a security manager exists and if 257 * the caller does not have <tt>LoggingPermission("control"))</tt>. 258 * @exception NullPointerException if pattern property is an empty String. 259 */ 260 public FileHandler() throws IOException, SecurityException { 261 checkPermission(); 262 configure(); 263 openFiles(); 264 } 265 266 /** 267 * Initialize a <tt>FileHandler</tt> to write to the given filename. 268 * <p> 269 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 270 * properties (or their default values) except that the given pattern 271 * argument is used as the filename pattern, the file limit is 272 * set to no limit, and the file count is set to one. 273 * <p> 274 * There is no limit on the amount of data that may be written, 275 * so use this with care. 276 * 277 * @param pattern the name of the output file 278 * @exception IOException if there are IO problems opening the files. 279 * @exception SecurityException if a security manager exists and if 280 * the caller does not have <tt>LoggingPermission("control")</tt>. 281 * @exception IllegalArgumentException if pattern is an empty string 282 */ 407 if (parent == null) { 408 parent = path.toAbsolutePath().getParent(); 409 } 410 return parent != null && Files.isWritable(parent); 411 } 412 413 /** 414 * Open the set of output files, based on the configured 415 * instance variables. 416 */ 417 private void openFiles() throws IOException { 418 LogManager manager = LogManager.getLogManager(); 419 manager.checkPermission(); 420 if (count < 1) { 421 throw new IllegalArgumentException("file count = " + count); 422 } 423 if (limit < 0) { 424 limit = 0; 425 } 426 427 // We register our own ErrorManager during initialization 428 // so we can record exceptions. 429 InitializationErrorManager em = new InitializationErrorManager(); 430 setErrorManager(em); 431 432 // Create a lock file. This grants us exclusive access 433 // to our set of output files, as long as we are alive. 434 int unique = -1; 435 for (;;) { 436 unique++; 437 if (unique > MAX_LOCKS) { 438 throw new IOException("Couldn't get lock for " + pattern); 439 } 440 // Generate a lock file name from the "unique" int. 441 lockFileName = generate(pattern, 0, unique).toString() + ".lck"; 442 // Now try to lock that filename. 443 // Because some systems (e.g., Solaris) can only do file locks 444 // between processes (and not within a process), we first check 445 // if we ourself already have the file locked. 446 synchronized(locks) { | 243 } catch (Exception ex2) { 244 // doing a setEncoding with null should always work. 245 // assert false; 246 } 247 } 248 } 249 250 251 /** 252 * Construct a default <tt>FileHandler</tt>. This will be configured 253 * entirely from <tt>LogManager</tt> properties (or their default values). 254 * 255 * @exception IOException if there are IO problems opening the files. 256 * @exception SecurityException if a security manager exists and if 257 * the caller does not have <tt>LoggingPermission("control"))</tt>. 258 * @exception NullPointerException if pattern property is an empty String. 259 */ 260 public FileHandler() throws IOException, SecurityException { 261 checkPermission(); 262 configure(); 263 // pattern will have been set by configure. check that it's not 264 // empty. 265 if (pattern.isEmpty()) { 266 throw new NullPointerException(); 267 } 268 openFiles(); 269 } 270 271 /** 272 * Initialize a <tt>FileHandler</tt> to write to the given filename. 273 * <p> 274 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 275 * properties (or their default values) except that the given pattern 276 * argument is used as the filename pattern, the file limit is 277 * set to no limit, and the file count is set to one. 278 * <p> 279 * There is no limit on the amount of data that may be written, 280 * so use this with care. 281 * 282 * @param pattern the name of the output file 283 * @exception IOException if there are IO problems opening the files. 284 * @exception SecurityException if a security manager exists and if 285 * the caller does not have <tt>LoggingPermission("control")</tt>. 286 * @exception IllegalArgumentException if pattern is an empty string 287 */ 412 if (parent == null) { 413 parent = path.toAbsolutePath().getParent(); 414 } 415 return parent != null && Files.isWritable(parent); 416 } 417 418 /** 419 * Open the set of output files, based on the configured 420 * instance variables. 421 */ 422 private void openFiles() throws IOException { 423 LogManager manager = LogManager.getLogManager(); 424 manager.checkPermission(); 425 if (count < 1) { 426 throw new IllegalArgumentException("file count = " + count); 427 } 428 if (limit < 0) { 429 limit = 0; 430 } 431 432 // All constructors check that pattern is neither null nor empty. 433 assert pattern != null : "pattern should not be null"; 434 assert !pattern.isEmpty() : "pattern should not be empty"; 435 436 // We register our own ErrorManager during initialization 437 // so we can record exceptions. 438 InitializationErrorManager em = new InitializationErrorManager(); 439 setErrorManager(em); 440 441 // Create a lock file. This grants us exclusive access 442 // to our set of output files, as long as we are alive. 443 int unique = -1; 444 for (;;) { 445 unique++; 446 if (unique > MAX_LOCKS) { 447 throw new IOException("Couldn't get lock for " + pattern); 448 } 449 // Generate a lock file name from the "unique" int. 450 lockFileName = generate(pattern, 0, unique).toString() + ".lck"; 451 // Now try to lock that filename. 452 // Because some systems (e.g., Solaris) can only do file locks 453 // between processes (and not within a process), we first check 454 // if we ourself already have the file locked. 455 synchronized(locks) { |