Skip to content

Commit c205c19

Browse files
committed
VRNSKY-18. Changes after review
1 parent b3feed8 commit c205c19

File tree

43 files changed

+284
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+284
-307
lines changed

chapter10/Annotations/src/main/resources/log4j.properties

-8
This file was deleted.

chapter10/Config/src/main/resources/log4j.properties

-8
This file was deleted.

chapter10/Mapping/src/main/java/controllers/Adverter.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import org.apache.commons.fileupload.FileUploadException;
88
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
99
import org.apache.commons.fileupload.servlet.ServletFileUpload;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1012
import repos.AdvertRepo;
1113
import repos.CarRepo;
14+
1215
import javax.servlet.ServletContext;
1316
import javax.servlet.ServletException;
1417
import javax.servlet.annotation.WebServlet;
@@ -21,23 +24,21 @@
2124
import java.util.List;
2225
import java.util.concurrent.ConcurrentHashMap;
2326
import java.util.concurrent.ConcurrentMap;
24-
import java.util.logging.Level;
25-
import java.util.logging.Logger;
2627

2728
/**
2829
* @author evrnsky(vrnsky at protonmail.ch)
2930
* @version 0.1
3031
* @since 30.03.2017
31-
*
32+
* <p>
3233
* This servlet add new advert to the system.
3334
*/
3435
@WebServlet("/newadvert")
35-
public class Adverter extends HttpServlet {
36+
public class Adverter extends HttpServlet {
3637

3738
/**
3839
* Instance of logger.
3940
*/
40-
private static final Logger LOG = Logger.getLogger(Adverter.class.getSimpleName());
41+
private static final Logger log = LoggerFactory.getLogger(Adverter.class.getSimpleName());
4142

4243
/**
4344
* Contains data which collected from the form.
@@ -46,10 +47,11 @@ public class Adverter extends HttpServlet {
4647

4748
/**
4849
* Forward request to the view.
49-
* @param req from client to server.
50+
*
51+
* @param req from client to server.
5052
* @param resp from server to client.
5153
* @throws ServletException if request for post could not be handled.
52-
* @throws IOException if io error detected.
54+
* @throws IOException if io error detected.
5355
*/
5456
@Override
5557
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@@ -58,10 +60,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
5860

5961
/**
6062
* Collect data from form and processing it, and at the end add new advert.
61-
* @param req from client to server.
63+
*
64+
* @param req from client to server.
6265
* @param resp from server to client.
6366
* @throws ServletException if request for POST could not be handled.
64-
* @throws IOException if an i/o error detected.
67+
* @throws IOException if an i/o error detected.
6568
*/
6669
@Override
6770
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@@ -77,20 +80,21 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
7780
fillMapFromParsedRequest((User) session.getAttribute("user"), items);
7881
FORM.clear();
7982
} catch (FileUploadException fue) {
80-
LOG.log(Level.SEVERE, fue.getMessage(), fue);
83+
log.error(fue.getMessage(), fue);
8184
}
8285

8386
}
8487
}
8588

8689
/**
8790
* Process data collecting at the post method and adding new advert.
88-
* @param user instance of user class.
91+
*
92+
* @param user instance of user class.
8993
* @param items data from client part of app.
9094
*/
9195
private void fillMapFromParsedRequest(User user, List<FileItem> items) {
92-
for (FileItem item: items) {
93-
LOG.log(Level.INFO, String.format("Field:%s | Value:%s", item.getFieldName(), item.getString()));
96+
for (FileItem item : items) {
97+
log.info("Field:{} | Value:{}", item.getFieldName(), item.getString());
9498
if (item.isFormField()) {
9599
FORM.put(item.getFieldName(), item.getString());
96100
} else {

chapter10/Mapping/src/main/resources/log4j.properties

-8
This file was deleted.

chapter11/IoC/src/main/resources/log4j.properties

-8
This file was deleted.

chapter11/IoC/src/test/resources/log4j.properties

-8
This file was deleted.

chapter11/MVC/src/main/resources/log4j.properties

-8
This file was deleted.

chapter11/MVC/src/test/resources/log4j.properties

-8
This file was deleted.

chapter7/Monitor/src/main/java/file/TextSearcher.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package file;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.io.BufferedReader;
47
import java.io.File;
58
import java.io.FileReader;
69
import java.io.IOException;
710
import java.util.ArrayList;
811
import java.util.List;
912
import java.util.concurrent.atomic.AtomicBoolean;
10-
import java.util.logging.Logger;
1113

1214

1315
/**
@@ -24,7 +26,7 @@ public class TextSearcher extends Thread {
2426
/**
2527
* Logger for this class.
2628
*/
27-
private static final Logger LOG = Logger.getLogger(TextSearcher.class.getSimpleName());
29+
private static final Logger log = LoggerFactory.getLogger(TextSearcher.class.getSimpleName());
2830

2931
/**
3032
* Flag which signal about find text or not.
@@ -140,9 +142,9 @@ private boolean search(String disk) {
140142
*/
141143
private void processingFile(File file, String text) {
142144
this.founded = readFile(file.getAbsolutePath(), text);
143-
LOG.info(String.format("SEARCH AT: %s", file.getAbsolutePath()));
145+
log.info(String.format("SEARCH AT: %s", file.getAbsolutePath()));
144146
if (this.founded) {
145-
LOG.info(String.format("FOUND AT: %s", file.getAbsolutePath()));
147+
log.info(String.format("FOUND AT: %s", file.getAbsolutePath()));
146148
synchronized (this.resultFiles) {
147149
this.resultFiles.add(file.getAbsolutePath());
148150
}

chapter7/Monitor/src/main/resources/log4j.properties

-8
This file was deleted.

chapter7/Wait-notify-notifyAll/src/main/java/pool/ThreadPool.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package pool;
22

33

4-
import java.util.LinkedList;
5-
import java.util.logging.Logger;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
66

7+
import java.util.LinkedList;
78

89
/**
910
* @author evrnsky
@@ -17,7 +18,7 @@ public class ThreadPool {
1718
/**
1819
* Instance of logger.
1920
*/
20-
private static final Logger LOG = Logger.getLogger(ThreadPool.class.getSimpleName());
21+
private static final Logger log = LoggerFactory.getLogger(ThreadPool.class.getSimpleName());
2122

2223
/**
2324
* Boundary for count of thread which may accept this queue.
@@ -69,13 +70,14 @@ public void run() {
6970
synchronized (queue) {
7071
while (queue.isEmpty()) {
7172
try {
72-
LOG.info("Pool at this moment is empty and wait task.");
73+
log.info("Pool at this moment is empty and wait task");
7374
queue.wait();
7475
} catch (InterruptedException e) {
75-
e.printStackTrace();
76+
log.warn("Thread interrupted while waiting for tasks: {}", e.getMessage());
77+
Thread.currentThread().interrupt();
7678
}
7779
}
78-
LOG.info("Pool going execute head of the list async task.");
80+
log.info("Pool going execute head of the list async task.");
7981
r = queue.removeFirst();
8082
}
8183

chapter7/Wait-notify-notifyAll/src/main/java/queue/BlockingQueue.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package queue;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35

46
import java.util.ArrayList;
5-
import java.util.logging.Logger;
67

78
/**
89
* @author evrnsky
@@ -17,7 +18,7 @@ public class BlockingQueue<T> {
1718
/**
1819
* Instance of logger.
1920
*/
20-
private static final Logger LOG = Logger.getLogger(BlockingQueue.class.getSimpleName());
21+
private static final Logger log = LoggerFactory.getLogger(BlockingQueue.class.getSimpleName());
2122

2223
/**
2324
* Head of the queue.
@@ -60,11 +61,11 @@ public void add(T data) {
6061
if (this.tail == 0) {
6162
this.tail++;
6263
this.objects.add(data);
63-
LOG.info("Now all threads get signal about resource is free.");
64+
log.info("Now all threads get signal about resource is free.");
6465
lock.notifyAll();
6566
} else if (this.tail != this.objects.size()) {
6667
this.objects.add(data);
67-
LOG.info("Now all threads get signal about resource is free.");
68+
log.info("Now all threads get signal about resource is free.");
6869
this.lock.notifyAll();
6970
} else {
7071
throw new IllegalStateException("Queue is full.");
@@ -81,7 +82,7 @@ public T poll() {
8182
synchronized (lock) {
8283
while (this.isEmpty()) {
8384
try {
84-
LOG.info("Wait until producer put data to the queue.");
85+
log.info("Wait until producer put data to the queue.");
8586
lock.wait();
8687
} catch (InterruptedException e) {
8788
e.printStackTrace();

chapter7/Wait-notify-notifyAll/src/main/java/queue/ProducerCustomer.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package queue;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.util.concurrent.atomic.AtomicInteger;
4-
import java.util.logging.Logger;
57

68
/**
79
* @author evrnsky
@@ -14,7 +16,7 @@ public class ProducerCustomer {
1416
/**
1517
* Instance of logger, show usefully information about program execution.
1618
*/
17-
private static final Logger LOG = Logger.getLogger(ProducerCustomer.class.getSimpleName());
19+
private static final Logger log = LoggerFactory.getLogger(ProducerCustomer.class.getSimpleName());
1820

1921
/**
2022
* Instance of blocking queue which provide a thread safe access to the elements.
@@ -30,7 +32,7 @@ public class ProducerCustomer {
3032
* Producer method which put data to the end of the queue.
3133
*/
3234
public void produce() {
33-
LOG.info(String.format("%s %s", "Now push to the end of queue is", number.get()));
35+
log.info("{} {}", "Now push to the end of queue is", number.get());
3436
queue.add(String.format("%s", number.get()));
3537
number.incrementAndGet();
3638
}
@@ -39,29 +41,24 @@ public void produce() {
3941
* Consume method which takes data from the head of the queue.
4042
*/
4143
public void consume() {
42-
LOG.info(String.format("%s %s", "Received from producer: ", queue.poll()));
44+
String item = queue.poll();
45+
if (item != null) {
46+
log.info("Received from producer: {}", item);
47+
} else {
48+
log.warn("Received a null from queue");
49+
}
4350
}
4451

4552
/**
4653
* Entry point of app.
54+
*
4755
* @param args keys for app.
48-
* @throws InterruptedException if some problem with threads.
4956
*/
50-
public static void main(String[] args) throws InterruptedException {
57+
public static void main(String[] args) {
5158
ProducerCustomer template = new ProducerCustomer();
52-
Runnable producer = new Runnable() {
53-
@Override
54-
public void run() {
55-
template.produce();
56-
}
57-
};
59+
Runnable producer = template::produce;
5860

59-
Runnable consumer = new Runnable() {
60-
@Override
61-
public void run() {
62-
template.consume();
63-
}
64-
};
61+
Runnable consumer = template::consume;
6562

6663
Thread publisher = new Thread(producer);
6764
Thread subscriber = new Thread(consumer);

chapter7/Wait-notify-notifyAll/src/main/resources/log4j.properties

-8
This file was deleted.

0 commit comments

Comments
 (0)