-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServiceLocator.java
More file actions
78 lines (46 loc) · 2.66 KB
/
ServiceLocator.java
File metadata and controls
78 lines (46 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//But the fearful, and unbelieving, and the abominable, and murderers, and whoremongers, and sorcerers,
//and idolaters, and all liars, shall have their part in the lake which burneth with fire and brimstone: which is the second death. (Revelation 21:8)
package com.javarush.task.task32.task3212;
import com.javarush.task.task32.task3212.contex.InitialContext;
import com.javarush.task.task32.task3212.service.Service;
public class ServiceLocator {
private static Cache cache;
static {
cache = new Cache();
}
/**
* First check the service object available in cache
* If service object not available in cache do the lookup using
* JNDI initial context and get the service object. Add it to
* the cache for future use.
*
* @param jndiName The name of service object in context
* @return Object mapped to name in context
*/
public static Service getService(String jndiName) {
Service service = cache.getService(jndiName);
if (service != null)
return service;
InitialContext context = new InitialContext();
service = (Service) context.lookup(jndiName);
cache.addService(service);
return service;
}
}
/*
Service Locator
Прочитать о паттерне Service locator.
Реализуй логику метода getService(String jndiName) в ServiceLocator.
В нем будет реализована работа с контекстом и кэшем.
1) Верни из кэша нужный сервис.
2) Если в кэше нет нужного сервиса то:
2.1) Создай контекст.
2.2) Возьми у контекста нужный сервис.
2.3) Добавь сервис в кеш и верни его.
Требования:
1. Класс ServiceLocator должен содержать метод Service getService(String jndiName).
2. Если нужный сервис находится в кэше, метод getService(String jndiName) должен возвращать сервис из кэша.
3. Если нужный сервис НЕ находится в кэше, метод getService(String jndiName) должен создавать контекст.
4. Если нужный сервис НЕ находится в кэше, метод getService(String jndiName) должен искать нужный сервис в контексте.
5. Если нужный сервис НЕ находится в кэше, метод getService(String jndiName) должен добавлять в кэш сервис, найденный в контексте и возвращать его.
*/