Skip to content

Commit 06376e6

Browse files
committed
Applied patches 6.6-6.11
1 parent 973d2e4 commit 06376e6

27 files changed

Lines changed: 434 additions & 53 deletions

File tree

config/messages/app.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
app.title=Calories management
2+
app.home=Home
3+
app.footer=Project <a href\="https\://github.com/JavaOPs/topjava" target\=_blank>Maven/ Spring/ Security/ JPA(Hibernate)/ Jackson/jQuery</a> application
4+
app.login=Login as
5+
6+
users.title=User list
7+
8+
meals.title=Meals list
9+
10+
common.select=Select

config/messages/app_ru.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
app.title=Подсчет калорий
2+
app.home=Главная
3+
app.footer=Приложение по проекту <a href\="https\://github.com/JavaOPs/topjava" target\=_blank>Maven/ Spring/ Security/ JPA(Hibernate)/ Jackson/jQuery</a>
4+
app.login=Зайти как
5+
6+
users.title=Список пользователей
7+
8+
meals.title=Список еды
9+
10+
common.select=Выбрать

pom.xml

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,70 @@
5959
<argLine>-Dfile.encoding=UTF-8</argLine>
6060
</configuration>
6161
</plugin>
62+
63+
<!-- http://stackoverflow.com/questions/4305935/is-it-possible-to-supply-tomcat6s-context-xml-file-via-the-maven-cargo-plugin#4417945 -->
64+
<plugin>
65+
<groupId>org.codehaus.cargo</groupId>
66+
<artifactId>cargo-maven2-plugin</artifactId>
67+
<version>1.5.0</version>
68+
<configuration>
69+
<container>
70+
<containerId>tomcat8x</containerId>
71+
<systemProperties>
72+
<file.encoding>UTF-8</file.encoding>
73+
<spring.profiles.active>tomcat,datajpa</spring.profiles.active>
74+
</systemProperties>
75+
<dependencies>
76+
<dependency>
77+
<groupId>org.postgresql</groupId>
78+
<artifactId>postgresql</artifactId>
79+
</dependency>
80+
</dependencies>
81+
</container>
82+
<configuration>
83+
<configfiles>
84+
<configfile>
85+
<file>src/main/resources/tomcat/context.xml</file>
86+
<todir>conf/Catalina/localhost/</todir>
87+
<tofile>context.xml.default</tofile>
88+
</configfile>
89+
</configfiles>
90+
</configuration>
91+
<deployables>
92+
<deployable>
93+
<groupId>ru.javawebinar</groupId>
94+
<artifactId>topjava</artifactId>
95+
<type>war</type>
96+
<properties>
97+
<context>${project.build.finalName}</context>
98+
</properties>
99+
</deployable>
100+
</deployables>
101+
</configuration>
102+
</plugin>
103+
104+
<!--
105+
<plugin>
106+
<groupId>org.apache.tomcat.maven</groupId>
107+
<artifactId>tomcat7-maven-plugin</artifactId>
108+
<version>2.2</version>
109+
<configuration>
110+
<contextFile>src/main/resources/tomcat/context.xml</contextFile>
111+
<systemProperties>
112+
<file.encoding>UTF-8</file.encoding>
113+
<spring.profiles.active>tomcat,datajpa</spring.profiles.active>
114+
</systemProperties>
115+
<protocol>org.apache.coyote.http11.Http11NioProtocol</protocol>
116+
</configuration>
117+
<dependencies>
118+
<dependency>
119+
<groupId>org.postgresql</groupId>
120+
<artifactId>postgresql</artifactId>
121+
<version>${postgresql.version}</version>
122+
</dependency>
123+
</dependencies>
124+
</plugin>
125+
-->
62126
</plugins>
63127
</build>
64128

@@ -103,6 +167,18 @@
103167
<version>${spring-data-jpa.version}</version>
104168
</dependency>
105169

170+
<dependency>
171+
<groupId>org.springframework</groupId>
172+
<artifactId>spring-webmvc</artifactId>
173+
<version>${spring.version}</version>
174+
<exclusions>
175+
<exclusion>
176+
<groupId>commons-logging</groupId>
177+
<artifactId>commons-logging</artifactId>
178+
</exclusion>
179+
</exclusions>
180+
</dependency>
181+
106182
<dependency>
107183
<groupId>org.hibernate</groupId>
108184
<artifactId>hibernate-core</artifactId>
@@ -119,6 +195,11 @@
119195
<artifactId>hibernate-validator</artifactId>
120196
<version>${hibernate-validator.version}</version>
121197
</dependency>
198+
<dependency>
199+
<groupId>org.hibernate</groupId>
200+
<artifactId>hibernate-ehcache</artifactId>
201+
<version>${hibernate.version}</version>
202+
</dependency>
122203

123204
<dependency>
124205
<groupId>net.sf.ehcache</groupId>
@@ -130,13 +211,14 @@
130211
<groupId>javax.el</groupId>
131212
<artifactId>javax.el-api</artifactId>
132213
<version>2.2.5</version>
214+
<scope>provided</scope>
133215
</dependency>
134216

135217
<!--Web-->
136218
<dependency>
137-
<groupId>javax.servlet</groupId>
138-
<artifactId>javax.servlet-api</artifactId>
139-
<version>3.1.0</version>
219+
<groupId>org.apache.tomcat</groupId>
220+
<artifactId>tomcat-servlet-api</artifactId>
221+
<version>${tomcat.version}</version>
140222
<scope>provided</scope>
141223
</dependency>
142224

src/main/java/ru/javawebinar/topjava/model/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.javawebinar.topjava.model;
22

3+
import org.hibernate.annotations.Cache;
4+
import org.hibernate.annotations.CacheConcurrencyStrategy;
35
import org.hibernate.validator.constraints.Email;
46
import org.hibernate.validator.constraints.Length;
57
import org.hibernate.validator.constraints.NotEmpty;
@@ -21,6 +23,7 @@
2123
@NamedQuery(name = User.BY_EMAIL, query = "SELECT u FROM User u LEFT JOIN FETCH u.roles WHERE u.email=?1"),
2224
@NamedQuery(name = User.ALL_SORTED, query = "SELECT u FROM User u LEFT JOIN FETCH u.roles ORDER BY u.name, u.email"),
2325
})
26+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
2427
@Entity
2528
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "email", name = "users_unique_email_idx")})
2629
public class User extends NamedEntity {
@@ -49,6 +52,7 @@ public class User extends NamedEntity {
4952
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
5053
@Column(name = "role")
5154
@ElementCollection(fetch = FetchType.LAZY)
55+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
5256
private Set<Role> roles;
5357

5458
@Column(name = "calories_per_day", columnDefinition = "default 2000")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.javawebinar.topjava.repository;
2+
3+
import org.hibernate.Session;
4+
import org.hibernate.SessionFactory;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.PersistenceContext;
8+
9+
public class JpaUtil {
10+
11+
@PersistenceContext
12+
private EntityManager em;
13+
14+
public void clear2ndLevelHibernateCache() {
15+
Session s = (Session) em.getDelegate();
16+
SessionFactory sf = s.getSessionFactory();
17+
// sf.evict(User.class);
18+
// sf.getCache().evictEntity(User.class, BaseEntity.START_SEQ);
19+
// sf.getCache().evictEntityRegion(User.class);
20+
sf.getCache().evictQueryRegions();
21+
sf.getCache().evictDefaultQueryRegion();
22+
sf.getCache().evictCollectionRegions();
23+
sf.getCache().evictEntityRegions();
24+
}
25+
}

src/main/java/ru/javawebinar/topjava/service/MealService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public interface MealService {
1616
Meal get(int id, int userId) throws NotFoundException;
1717

18-
void delete(int id, int userId) throws NotFoundException;
18+
void delete(int id, int userId) throws NotFoundException;
1919

2020
default Collection<Meal> getBetweenDates(LocalDate startDate, LocalDate endDate, int userId) {
2121
return getBetweenDateTimes(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX), userId);

src/main/java/ru/javawebinar/topjava/web/MealServlet.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5-
import org.springframework.context.support.ClassPathXmlApplicationContext;
6-
import ru.javawebinar.topjava.Profiles;
5+
import org.springframework.web.context.WebApplicationContext;
6+
import org.springframework.web.context.support.WebApplicationContextUtils;
77
import ru.javawebinar.topjava.model.Meal;
88
import ru.javawebinar.topjava.util.TimeUtil;
99
import ru.javawebinar.topjava.web.meal.MealRestController;
@@ -27,26 +27,15 @@
2727
public class MealServlet extends HttpServlet {
2828
private static final Logger LOG = LoggerFactory.getLogger(MealServlet.class);
2929

30-
private ClassPathXmlApplicationContext springContext;
3130
private MealRestController mealController;
3231

3332
@Override
3433
public void init(ServletConfig config) throws ServletException {
3534
super.init(config);
36-
springContext = new ClassPathXmlApplicationContext(new String[]{"spring/spring-app.xml", "spring/spring-db.xml"}, false);
37-
springContext.getEnvironment().setActiveProfiles(Profiles.ACTIVE_DB, Profiles.DB_IMPLEMENTATION);
38-
// ClassPathXmlApplicationContext.setConfigLocations("spring/spring-app.xml", "spring/spring-db.xml");
39-
// GenericXmlApplicationContext.load("spring/spring-app.xml", "spring/mock.xml");
40-
springContext.refresh();
35+
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
4136
mealController = springContext.getBean(MealRestController.class);
4237
}
4338

44-
@Override
45-
public void destroy() {
46-
springContext.close();
47-
super.destroy();
48-
}
49-
5039
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
5140
request.setCharacterEncoding("UTF-8");
5241
String action = request.getParameter("action");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ru.javawebinar.topjava.web;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import ru.javawebinar.topjava.AuthorizedUser;
9+
import ru.javawebinar.topjava.service.UserService;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
* User: gkislin
15+
* Date: 22.08.2014
16+
*/
17+
@Controller
18+
public class RootController {
19+
@Autowired
20+
private UserService service;
21+
22+
@RequestMapping(value = "/", method = RequestMethod.GET)
23+
public String root() {
24+
return "index";
25+
}
26+
27+
@RequestMapping(value = "/users", method = RequestMethod.GET)
28+
public String users(Model model) {
29+
model.addAttribute("users", service.getAll());
30+
return "users";
31+
}
32+
33+
@RequestMapping(value = "/users", method = RequestMethod.POST)
34+
public String setUser(HttpServletRequest request) {
35+
int userId = Integer.valueOf(request.getParameter("userId"));
36+
AuthorizedUser.setId(userId);
37+
return "redirect:meals";
38+
}
39+
}

src/main/java/ru/javawebinar/topjava/web/UserServlet.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package ru.javawebinar.topjava.web;
22

33
import org.slf4j.Logger;
4+
import org.springframework.web.context.WebApplicationContext;
5+
import org.springframework.web.context.support.WebApplicationContextUtils;
46
import ru.javawebinar.topjava.AuthorizedUser;
7+
import ru.javawebinar.topjava.web.user.AdminRestController;
58

9+
import javax.servlet.ServletConfig;
610
import javax.servlet.ServletException;
711
import javax.servlet.http.HttpServlet;
812
import javax.servlet.http.HttpServletRequest;
@@ -18,14 +22,24 @@
1822
public class UserServlet extends HttpServlet {
1923
private static final Logger LOG = getLogger(UserServlet.class);
2024

25+
private AdminRestController adminController;
26+
27+
@Override
28+
public void init(ServletConfig config) throws ServletException {
29+
super.init(config);
30+
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
31+
adminController = springContext.getBean(AdminRestController.class);
32+
}
33+
2134
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2235
int userId = Integer.valueOf(request.getParameter("userId"));
2336
AuthorizedUser.setId(userId);
2437
response.sendRedirect("meals");
2538
}
2639

2740
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28-
LOG.debug("forward to users");
41+
LOG.debug("getAll");
42+
request.setAttribute("users", adminController.getAll());
2943
request.getRequestDispatcher("/users.jsp").forward(request, response);
3044
}
3145
}

src/main/resources/cache/ehcache.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@
2525
diskExpiryThreadIntervalSeconds="1"
2626
memoryStoreEvictionPolicy="LRU"/>
2727

28+
<cache name="ru.javawebinar.topjava.model.User"
29+
maxElementsInMemory="25000"/>
30+
2831
</ehcache>

0 commit comments

Comments
 (0)