From eccf17109bda47084d5b08f763892e8f39bcc29f Mon Sep 17 00:00:00 2001 From: "antihax@gmail.com" Date: Fri, 17 Mar 2017 10:54:01 -0500 Subject: [PATCH 1/2] Add guards around structures to prevent racing. --- .../java/io/swagger/sample/data/PetData.java | 87 +++++++++++-------- .../io/swagger/sample/data/StoreData.java | 40 +++++---- .../java/io/swagger/sample/data/UserData.java | 36 ++++---- 3 files changed, 92 insertions(+), 71 deletions(-) diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java index 61868df7..00ea1664 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java @@ -23,8 +23,9 @@ import java.util.*; public class PetData { - static List pets = new ArrayList(); - static List categories = new ArrayList(); + + static List pets = Collections.synchronizedList(new ArrayList()); + static List categories = Collections.synchronizedList(new ArrayList()); static { categories.add(createCategory(1, "Dogs")); @@ -68,11 +69,13 @@ public Pet getPetById(long petId) { public boolean deletePet(long petId) { if(pets.size() > 0) { - for (int i = pets.size() - 1; i >= 0; i--) { - Pet pet = pets.get(i); - if(pet.getId() == petId) { - pets.remove(i); - return true; + synchronized (pets) { + for (int i = pets.size() - 1; i >= 0; i--) { + Pet pet = pets.get(i); + if(pet.getId() == petId) { + pets.remove(i); + return true; + } } } } @@ -85,10 +88,12 @@ public List findPetByStatus(String status) { return result; } String[] statuses = status.split(","); - for (Pet pet : pets) { - for (String s : statuses) { - if (s.equals(pet.getStatus())) { - result.add(pet); + synchronized (pets) { + for (Pet pet : pets) { + for (String s : statuses) { + if (s.equals(pet.getStatus())) { + result.add(pet); + } } } } @@ -102,12 +107,14 @@ public List findPetByTags(String tags) { return result; } String[] tagList = tags.split(","); - for (Pet pet : pets) { - if (null != pet.getTags()) { - for (Tag tag : pet.getTags()) { - for (String tagListString : tagList) { - if (tagListString.equals(tag.getName())) - result.add(pet); + synchronized (pets) { + for (Pet pet : pets) { + if (null != pet.getTags()) { + for (Tag tag : pet.getTags()) { + for (String tagListString : tagList) { + if (tagListString.equals(tag.getName())) + result.add(pet); + } } } } @@ -116,37 +123,41 @@ public List findPetByTags(String tags) { } public Pet addPet(Pet pet) { - if(pet.getId() == 0) { - long maxId = 0; - for (int i = pets.size() - 1; i >= 0; i--) { - if(pets.get(i).getId() > maxId) { - maxId = pets.get(i).getId(); + synchronized (pets) { + if(pet.getId() == 0) { + long maxId = 0; + for (int i = pets.size() - 1; i >= 0; i--) { + if(pets.get(i).getId() > maxId) { + maxId = pets.get(i).getId(); + } } + pet.setId(maxId + 1); } - pet.setId(maxId + 1); - } - if (pets.size() > 0) { - for (int i = pets.size() - 1; i >= 0; i--) { - if (pets.get(i).getId() == pet.getId()) { - pets.remove(i); + if (pets.size() > 0) { + for (int i = pets.size() - 1; i >= 0; i--) { + if (pets.get(i).getId() == pet.getId()) { + pets.remove(i); + } } } + pets.add(pet); } - pets.add(pet); return pet; } public Map getInventoryByStatus() { Map output = new HashMap(); - for(Pet pet : pets) { - String status = pet.getStatus(); - if(status != null && !"".equals(status)) { - Integer count = output.get(status); - if(count == null) - count = new Integer(1); - else - count = count.intValue() + 1; - output.put(status, count); + synchronized (pets) { + for(Pet pet : pets) { + String status = pet.getStatus(); + if(status != null && !"".equals(status)) { + Integer count = output.get(status); + if(count == null) + count = new Integer(1); + else + count = count.intValue() + 1; + output.put(status, count); + } } } return output; diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java index e4415bcc..0d56f68a 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java @@ -18,12 +18,10 @@ import io.swagger.sample.model.Order; -import java.util.Date; -import java.util.List; -import java.util.ArrayList; +import java.util.*; public class StoreData { - static List orders = new ArrayList(); + static List orders = Collections.synchronizedList(new ArrayList()); static { orders.add(createOrder(1, 1, 2, new Date(), "placed")); @@ -39,32 +37,38 @@ public class StoreData { } public Order findOrderById(long orderId) { - for (Order order : orders) { - if (order.getId() == orderId) { - return order; + synchronized (orders) { + for (Order order : orders) { + if (order.getId() == orderId) { + return order; + } } } return null; } public Order placeOrder(Order order) { - if (orders.size() > 0) { - for (int i = orders.size() - 1; i >= 0; i--) { - if (orders.get(i).getId() == order.getId()) { - orders.remove(i); - } + synchronized (orders) { + if (orders.size() > 0) { + for (int i = orders.size() - 1; i >= 0; i--) { + if (orders.get(i).getId() == order.getId()) { + orders.remove(i); + } + } + orders.add(order); } + return order; } - orders.add(order); - return order; } public boolean deleteOrder(long orderId) { if (orders.size() > 0) { - for (int i = orders.size() - 1; i >= 0; i--) { - if (orders.get(i).getId() == orderId) { - orders.remove(i); - return true; + synchronized (orders) { + for (int i = orders.size() - 1; i >= 0; i--) { + if (orders.get(i).getId() == orderId) { + orders.remove(i); + return true; + } } } } diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java index d0bc2edf..81ba0293 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java @@ -18,11 +18,10 @@ import io.swagger.sample.model.User; -import java.util.List; -import java.util.ArrayList; +import java.util.*; public class UserData { - static List users = new ArrayList(); + static List users = Collections.synchronizedList( new ArrayList() ); static { users.add(createUser(1, "user1", "first name 1", "last name 1", @@ -51,9 +50,11 @@ public class UserData { } public User findUserByName(String username) { - for (User user : users) { - if (user.getUsername().equals(username)) { - return user; + synchronized (users) { + for (User user : users) { + if (user.getUsername().equals(username)) { + return user; + } } } return null; @@ -62,22 +63,27 @@ public User findUserByName(String username) { public void addUser(User user) { if(user.getUsername() == null) return; - if (users.size() > 0) { - for (int i = users.size() - 1; i >= 0; i--) { - if (users.get(i).getUsername().equals(user.getUsername())) { - users.remove(i); + + synchronized (users) { + if (users.size() > 0) { + for (int i = users.size() - 1; i >= 0; i--) { + if (users.get(i).getUsername().equals(user.getUsername())) { + users.remove(i); + } } } + users.add(user); } - users.add(user); } public boolean removeUser(String username) { if (users.size() > 0) { - for (int i = users.size() - 1; i >= 0; i--) { - if (users.get(i).getUsername().equals(username)) { - users.remove(i); - return true; + synchronized (users) { + for (int i = users.size() - 1; i >= 0; i--) { + if (users.get(i).getUsername().equals(username)) { + users.remove(i); + return true; + } } } } From 4510cb1deebbf92cec0491188adc7804a8bd2ffa Mon Sep 17 00:00:00 2001 From: "antihax@gmail.com" Date: Fri, 17 Mar 2017 13:00:48 -0500 Subject: [PATCH 2/2] rework --- .../src/main/java/io/swagger/sample/data/PetData.java | 10 ++++++---- .../main/java/io/swagger/sample/data/StoreData.java | 2 +- .../src/main/java/io/swagger/sample/data/UserData.java | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java index 00ea1664..82e2f41f 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/PetData.java @@ -24,8 +24,8 @@ public class PetData { - static List pets = Collections.synchronizedList(new ArrayList()); - static List categories = Collections.synchronizedList(new ArrayList()); + static List pets = new ArrayList(); + static List categories = new ArrayList(); static { categories.add(createCategory(1, "Dogs")); @@ -59,9 +59,11 @@ public class PetData { } public Pet getPetById(long petId) { + synchronized (pets) { for (Pet pet : pets) { - if (pet.getId() == petId) { - return pet; + if (pet.getId() == petId) { + return pet; + } } } return null; diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java index 0d56f68a..5160eef2 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/StoreData.java @@ -21,7 +21,7 @@ import java.util.*; public class StoreData { - static List orders = Collections.synchronizedList(new ArrayList()); + static List orders = new ArrayList(); static { orders.add(createOrder(1, 1, 2, new Date(), "placed")); diff --git a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java index 81ba0293..22fcd7d5 100644 --- a/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java +++ b/java/java-jersey-jaxrs/src/main/java/io/swagger/sample/data/UserData.java @@ -21,7 +21,7 @@ import java.util.*; public class UserData { - static List users = Collections.synchronizedList( new ArrayList() ); + static List users = new ArrayList(); static { users.add(createUser(1, "user1", "first name 1", "last name 1",