Skip to content

Commit

Permalink
Changes after review. Added README
Browse files Browse the repository at this point in the history
  • Loading branch information
npathai committed Mar 26, 2016
1 parent e543ee0 commit 4d820b1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 25 deletions.
Binary file modified mute-idiom/etc/mute-idiom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 31 additions & 5 deletions mute-idiom/etc/mute-idiom.ucls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
realizations="true" associations="true" dependencies="true" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.mute.App" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="519" y="122"/>
<position height="-1" width="-1" x="168" y="234"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="false" static="true"/>
Expand All @@ -12,7 +12,7 @@
</class>
<class id="2" language="java" name="com.iluwatar.mute.Mute" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/Mute.java" binary="false" corner="BOTTOM_RIGHT">
<position height="115" width="203" x="291" y="267"/>
<position height="115" width="203" x="70" y="375"/>
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
Expand All @@ -21,21 +21,47 @@
</class>
<interface id="3" language="java" name="com.iluwatar.mute.CheckedRunnable" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/CheckedRunnable.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="723" y="322"/>
<position height="-1" width="-1" x="407" y="432"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<dependency id="4">
<interface id="4" language="java" name="com.iluwatar.mute.Resource" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/Resource.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="383" y="235"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<interface id="5" language="java" name="java.io.Closeable" project="dao"
file="/opt/Softwares/Eclipses/MARS/eclipse/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="384" y="109"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<dependency id="6">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="4"/>
</dependency>
<dependency id="7">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="2"/>
</dependency>
<dependency id="5">
<dependency id="8">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="3"/>
</dependency>
<generalization id="9">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="5"/>
</generalization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
Expand Down
38 changes: 18 additions & 20 deletions mute-idiom/src/main/java/com/iluwatar/mute/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,31 +71,33 @@ private static void useOfMute() {
}

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 changes: 35 additions & 0 deletions mute-idiom/src/main/java/com/iluwatar/mute/Resource.java
Original file line number Diff line number Diff line change
@@ -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 {

}

0 comments on commit 4d820b1

Please sign in to comment.