File tree Expand file tree Collapse file tree
core-java/src/main/java/com/baeldung/designpatterns/service/locator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * Created by Gebruiker on 4/20/2018.
8+ */
9+ public class Cache {
10+ private List <MessagingService > services ;
11+
12+ public Cache (){
13+ services = new ArrayList <MessagingService >();
14+ }
15+
16+ public MessagingService getService (String serviceName ){
17+
18+ for (MessagingService service : services ) {
19+ if (service .getServiceName ().equalsIgnoreCase (serviceName )){
20+ System .out .println ("Returning cached " + serviceName + " object" );
21+ return service ;
22+ }
23+ }
24+ return null ;
25+ }
26+
27+ public void addService (MessagingService newService ){
28+ boolean exists = false ;
29+
30+ for (MessagingService service : services ) {
31+ if (service .getServiceName ().equalsIgnoreCase (newService .getServiceName ())){
32+ exists = true ;
33+ }
34+ }
35+ if (!exists ){
36+ services .add (newService );
37+ }
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ /**
4+ * Created by Gebruiker on 4/20/2018.
5+ */
6+ public class EmailService implements MessagingService {
7+
8+ public String getMessageBody () {
9+ return "email message" ;
10+ }
11+
12+ public String getServiceName () {
13+ return "EmailService" ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ /**
4+ * Created by Gebruiker on 4/20/2018.
5+ */
6+ public class InitialContext {
7+
8+ public Object lookup (String jndiName ) {
9+
10+ if (jndiName .equalsIgnoreCase ("EmailService" )) {
11+ return new EmailService ();
12+ } else if (jndiName .equalsIgnoreCase ("SMSService" )) {
13+ return new SMSService ();
14+ }
15+ return null ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ /**
4+ * Created by Gebruiker on 4/20/2018.
5+ */
6+ public class Main {
7+
8+ public static void main (String [] args ) {
9+ MessagingService service = ServiceLocator .getService ("EmailService" );
10+ service .getMessageBody ();
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ public interface MessagingService {
4+
5+ String getMessageBody ();
6+
7+ String getServiceName ();
8+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ /**
4+ * Created by Gebruiker on 4/20/2018.
5+ */
6+ public class SMSService implements MessagingService {
7+
8+ public String getMessageBody () {
9+ return "sms message" ;
10+ }
11+
12+ public String getServiceName () {
13+ return "SMSService" ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .service .locator ;
2+
3+ /**
4+ * Created by Gebruiker on 4/20/2018.
5+ */
6+ public class ServiceLocator {
7+ private static Cache cache ;
8+
9+ static {
10+ cache = new Cache ();
11+ }
12+
13+ public static MessagingService getService (String jndiName ){
14+
15+ MessagingService service = cache .getService (jndiName );
16+
17+ if (service != null ){
18+ return service ;
19+ }
20+
21+ InitialContext context = new InitialContext ();
22+ MessagingService service1 = (MessagingService )context .lookup (jndiName );
23+ cache .addService (service1 );
24+ return service1 ;
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments