Skip to content

Commit cd54cf5

Browse files
authored
Merge pull request iluwatar#563 from sunilmogadati/SonarQubeBlockerBugs
iluwatar#507 SonarQube blocker severity bugs
2 parents 073d06c + 60ebcc5 commit cd54cf5

File tree

7 files changed

+38
-35
lines changed

7 files changed

+38
-35
lines changed

model-view-presenter/src/main/java/com/iluwatar/model/view/presenter/FileLoader.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public class FileLoader {
4848
* Loads the data of the file specified.
4949
*/
5050
public String loadData() {
51-
try {
52-
BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)));
51+
try (BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)))) {
5352
StringBuilder sb = new StringBuilder();
5453
String line;
5554

@@ -58,7 +57,6 @@ public String loadData() {
5857
}
5958

6059
this.loaded = true;
61-
br.close();
6260

6361
return sb.toString();
6462
} catch (Exception e) {

queue-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@
3535
public class ServiceExecutor implements Runnable {
3636

3737
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
38-
38+
3939
private final MessageQueue msgQueue;
4040

4141
public ServiceExecutor(MessageQueue msgQueue) {
4242
this.msgQueue = msgQueue;
4343
}
44-
44+
4545
/**
4646
* The ServiceExecutor thread will retrieve each message and process it.
4747
*/
4848
public void run() {
4949
try {
50-
while (true) {
50+
while (!Thread.currentThread().isInterrupted()) {
5151
Message msg = msgQueue.retrieveMsg();
52-
52+
5353
if (null != msg) {
5454
LOGGER.info(msg.toString() + " is served.");
5555
} else {
5656
LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
5757
}
58-
58+
5959
Thread.sleep(1000);
6060
}
6161
} catch (InterruptedException ie) {

service-layer/src/main/java/com/iluwatar/servicelayer/common/DaoBaseImpl.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.hibernate.Criteria;
2929
import org.hibernate.Session;
30+
import org.hibernate.SessionFactory;
3031
import org.hibernate.Transaction;
3132
import org.hibernate.criterion.Restrictions;
3233

@@ -45,13 +46,17 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
4546
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
4647
.getGenericSuperclass()).getActualTypeArguments()[0];
4748

48-
protected Session getSession() {
49-
return HibernateUtil.getSessionFactory().openSession();
49+
/*
50+
* Making this getSessionFactory() instead of getSession() so that it is the responsibility
51+
* of the caller to open as well as close the session (prevents potential resource leak).
52+
*/
53+
protected SessionFactory getSessionFactory() {
54+
return HibernateUtil.getSessionFactory();
5055
}
5156

5257
@Override
5358
public E find(Long id) {
54-
Session session = getSession();
59+
Session session = getSessionFactory().openSession();
5560
Transaction tx = null;
5661
E result = null;
5762
try {
@@ -73,7 +78,7 @@ public E find(Long id) {
7378

7479
@Override
7580
public void persist(E entity) {
76-
Session session = getSession();
81+
Session session = getSessionFactory().openSession();
7782
Transaction tx = null;
7883
try {
7984
tx = session.beginTransaction();
@@ -91,7 +96,7 @@ public void persist(E entity) {
9196

9297
@Override
9398
public E merge(E entity) {
94-
Session session = getSession();
99+
Session session = getSessionFactory().openSession();
95100
Transaction tx = null;
96101
E result = null;
97102
try {
@@ -111,7 +116,7 @@ public E merge(E entity) {
111116

112117
@Override
113118
public void delete(E entity) {
114-
Session session = getSession();
119+
Session session = getSessionFactory().openSession();
115120
Transaction tx = null;
116121
try {
117122
tx = session.beginTransaction();
@@ -129,7 +134,7 @@ public void delete(E entity) {
129134

130135
@Override
131136
public List<E> findAll() {
132-
Session session = getSession();
137+
Session session = getSessionFactory().openSession();
133138
Transaction tx = null;
134139
List<E> result = null;
135140
try {

service-layer/src/main/java/com/iluwatar/servicelayer/spell/SpellDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
3838

3939
@Override
4040
public Spell findByName(String name) {
41-
Session session = getSession();
41+
Session session = getSessionFactory().openSession();
4242
Transaction tx = null;
4343
Spell result = null;
4444
try {

service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements Spellboo
3838

3939
@Override
4040
public Spellbook findByName(String name) {
41-
Session session = getSession();
41+
Session session = getSessionFactory().openSession();
4242
Transaction tx = null;
4343
Spellbook result = null;
4444
try {

service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class WizardDaoImpl extends DaoBaseImpl<Wizard> implements WizardDao {
3939

4040
@Override
4141
public Wizard findByName(String name) {
42-
Session session = getSession();
42+
Session session = getSessionFactory().openSession();
4343
Transaction tx = null;
4444
Wizard result = null;
4545
try {

tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ public static void writeV1(RainbowFish rainbowFish, String filename) throws IOEx
5252
map.put("age", String.format("%d", rainbowFish.getAge()));
5353
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
5454
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
55-
FileOutputStream fileOut = new FileOutputStream(filename);
56-
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
57-
objOut.writeObject(map);
58-
objOut.close();
59-
fileOut.close();
55+
try (FileOutputStream fileOut = new FileOutputStream(filename);
56+
ObjectOutputStream objOut = new ObjectOutputStream(fileOut)) {
57+
objOut.writeObject(map);
58+
}
6059
}
6160

6261
/**
@@ -71,23 +70,24 @@ public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IO
7170
map.put("angry", Boolean.toString(rainbowFish.getAngry()));
7271
map.put("hungry", Boolean.toString(rainbowFish.getHungry()));
7372
map.put("sleeping", Boolean.toString(rainbowFish.getSleeping()));
74-
FileOutputStream fileOut = new FileOutputStream(filename);
75-
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
76-
objOut.writeObject(map);
77-
objOut.close();
78-
fileOut.close();
73+
try (FileOutputStream fileOut = new FileOutputStream(filename);
74+
ObjectOutputStream objOut = new ObjectOutputStream(fileOut)) {
75+
objOut.writeObject(map);
76+
}
7977
}
8078

8179
/**
8280
* Read V1 RainbowFish from file
8381
*/
8482
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
85-
FileInputStream fileIn = new FileInputStream(filename);
86-
ObjectInputStream objIn = new ObjectInputStream(fileIn);
87-
Map<String, String> map = (Map<String, String>) objIn.readObject();
88-
objIn.close();
89-
fileIn.close();
90-
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer.parseInt(map
91-
.get("lengthMeters")), Integer.parseInt(map.get("weightTons")));
83+
Map<String, String> map = null;
84+
85+
try (FileInputStream fileIn = new FileInputStream(filename);
86+
ObjectInputStream objIn = new ObjectInputStream(fileIn)) {
87+
map = (Map<String, String>) objIn.readObject();
88+
}
89+
90+
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer.parseInt(map.get("lengthMeters")),
91+
Integer.parseInt(map.get("weightTons")));
9292
}
9393
}

0 commit comments

Comments
 (0)