...
Code Block | ||
---|---|---|
| ||
public final class Client { public void doSomething(File file) { final Lock lock = new ReentrantLock(); InputStream in = null; try { lock.lock(); in = new FileInputStream(file); // Perform operations on the open file lock.unlock(); } catch (FileNotFoundException x) { // Handle exception } finally { if (in != null) { try { in.close(); } catch (IOException x) { // Handle exception } } } } } |
Note that the lock is still held, even when the doSomething()
method returns.
Compliant Solution (finally
Block)
This compliant solution encapsulates operations that could throw an exception in a try
block immediately after acquiring the lock. The lock is acquired just before the try
block, which guarantees that it is held when the finally
block executes. Invoking Lock.unlock()
in the finally
block ensures that the lock is released regardless of whether an exception occurs or not.
Code Block | ||
---|---|---|
| ||
public final class Client { public void doSomething(File file) { final Lock lock = new ReentrantLock(); InputStream in = null; lock.lock(); try { in = new FileInputStream(file); // Perform operations on the open file } catch (FileNotFoundException fnf) { // Forward to handler } finally { lock.unlock(); if (in != null) { try { in.close(); } catch (IOException e) { // Forward to handler } } } } } |
...
Code Block | ||
---|---|---|
| ||
public interface LockAction { void doSomethingWithFile(InputStream in); } public final class ReentrantLockAction { public static void doSomething(File file, LockAction action) { Lock lock = new ReentrantLock(); InputStream in = null; lock.lock(); try { in = new FileInputStream(file); action.doSomethingWithFile(in); } catch (FileNotFoundException fnf) { // Forward to handler } finally { lock.unlock(); if (in != null) { try { in.close(); } catch (IOException e) { // Forward to handler } } } } } public final class Client { public void doSomething(File file) { ReentrantLockAction.doSomething(file, new LockAction() { public void doSomethingWithFile(InputStream in) { // Perform operations on the open file } }); } } |
Noncompliant Code Example (Unchecked Exception)
This noncompliant code example uses a ReentrantLock
to protect a java.util.Date
instance – recall that java.util.Date
is thread-unsafe by design. The doSomethingSafely()
method must catch Throwable
to comply with rule ERR01-J. Do not allow exceptions to expose sensitive information.
Code Block | ||
---|---|---|
| ||
final class DateHandler { private final Date date = new Date(); final Lock lock = new ReentrantLock(); public void doSomethingSafely(String str) { try { doSomething(str); } catch(Throwable t) { // Forward to handler } } public void doSomething(String str) { lock.lock(); String dateString = date.toString(); if (str.equals(dateString)) { // ... } // ... lock.unlock(); } } |
A runtime exception can occur because the doSomething()
method fails to check whether str
is a null reference, preventing the lock from being released.
...
This compliant solution encapsulates all operations that can throw an exception in a try
block and releases the lock in the associated finally
block. Consequently, the lock is released even in the event of a runtime exception.
Code Block | ||
---|---|---|
| ||
final class DateHandler { private final Date date = new Date(); final Lock lock = new ReentrantLock(); public void doSomethingSafely(String str) { try { doSomething(str); } catch(Throwable t) { // Forward to handler } } public void doSomething(String str) { lock.lock(); try { String dateString = date.toString(); if (str != null && str.equals(dateString)) { // ... } // ... } finally { lock.unlock(); } } } |
Consequently, the lock is released even in the event of a runtime exception. The doSomething()
method also avoids throwing a NullPointerException
by ensuring that the string does not contain a null reference.
...
The GERONIMO-2234 issue report describes a vulnerability in the Geronimo application server. If the user single-clicks the keystone portlet, the user will lock the default keystore without warning. This causes a crash and stack trace to be produced. HoweverFuthermore, the server cannot be restarted because the lock is never cleared.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="be1d9d5e2c62c5a3-a53eb106-4eb94f19-947d93a9-0ce8e64e81a3dd370e747f2c"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> |
...