Changes after review. Added README
This commit is contained in:
@ -23,13 +23,9 @@
|
||||
|
||||
package com.iluwatar.mute;
|
||||
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.sql.Connection;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* Mute pattern is utilized when we need to suppress an exception due to an API flaw or in
|
||||
@ -75,31 +71,33 @@ public class App {
|
||||
}
|
||||
|
||||
private static void useOfLoggedMute() throws SQLException {
|
||||
Connection connection = null;
|
||||
Resource resource = null;
|
||||
try {
|
||||
connection = openConnection();
|
||||
readStuff(connection);
|
||||
resource = acquireResource();
|
||||
utilizeResource(resource);
|
||||
} finally {
|
||||
closeConnection(connection);
|
||||
closeResource(resource);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* All we can do while failed close of connection is to log it.
|
||||
* All we can do while failed close of a resource is to log it.
|
||||
*/
|
||||
private static void closeConnection(Connection connection) {
|
||||
Mute.loggedMute(() -> connection.close());
|
||||
private static void closeResource(Resource resource) {
|
||||
Mute.loggedMute(() -> resource.close());
|
||||
}
|
||||
|
||||
private static void readStuff(Connection connection) throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
System.out.println("Read data from statement");
|
||||
}
|
||||
private static void utilizeResource(Resource resource) throws SQLException {
|
||||
System.out.println("Utilizing acquired resource: " + resource);
|
||||
}
|
||||
|
||||
private static Connection openConnection() throws SQLException {
|
||||
Connection mockedConnection = mock(Connection.class);
|
||||
doThrow(SQLException.class).when(mockedConnection).close();
|
||||
return mockedConnection;
|
||||
private static Resource acquireResource() throws SQLException {
|
||||
return new Resource() {
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
throw new IOException("Error in closing resource: " + this);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
35
mute-idiom/src/main/java/com/iluwatar/mute/Resource.java
Normal file
35
mute-idiom/src/main/java/com/iluwatar/mute/Resource.java
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.mute;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
/**
|
||||
* Represents any resource that the application might acquire and that must be closed
|
||||
* after it is utilized. Example of such resources can be a database connection, open
|
||||
* files, sockets.
|
||||
*/
|
||||
public interface Resource extends Closeable {
|
||||
|
||||
}
|
Reference in New Issue
Block a user