97f4875d45174d2b42c4b68dabec2b841ec1c1de
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.service.support;
21
22 import java.util.*;
23
24 import javax.naming.*;
25 import javax.naming.directory.*;
26 import javax.naming.ldap.*;
27
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.springframework.stereotype.Service;
30
31 /**
32  * This class implements the J2EE service locator pattern. It provides lookup
33  * facilities for various services. Currenttly LDAP (pre-v3) is supported
34  */
35 @Service("serviceLocator")
36 public class ServiceLocatorImpl implements ServiceLocator {
37
38   //private static ServiceLocator locator;   // The singleton instance
39
40   private Context            context;       // JNDI context (not currently in use)
41   private Context            rootContext;   // Java env root context (not currently in use)
42   private DirContext         dirContext;    // LDAP DIR context
43   private InitialLdapContext ldapContext;   // LDAP context LDAPv3-style (not currently in use)
44
45
46   // cannot directly instantiate
47   public ServiceLocatorImpl() {}
48
49   /*public static ServiceLocator getLocator() {
50     if (locator == null)
51         locator = new ServiceLocator();
52     return locator;
53   }
54
55   public Object clone() throws CloneNotSupportedException {
56     throw new CloneNotSupportedException();
57   }*/
58
59
60   // Get an LDAP directory context
61   public DirContext getDirContext(String initialContextFactory, String providerUrl, String securityPrincipal) {
62
63     if (dirContext == null) {
64
65       Properties properties = new Properties();
66       properties.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
67       properties.put(Context.PROVIDER_URL, providerUrl);
68       properties.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
69
70       try {
71         dirContext = new InitialDirContext(properties);
72       }
73       catch (NamingException ne) {
74         logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while creating an Initial Directory Context: " + ne.getMessage());
75         logger.error(EELFLoggerDelegate.errorLogger, "Explanation: " + ne.getExplanation());
76       }
77     }
78
79     return dirContext;
80   }
81
82   // Get an LDAP directory context - LDAPv3-style
83   /*public InitialLdapContext getLdapContext() { //throws NamingException {
84         if (ldapContext == null) {
85           Properties properties = new Properties();
86           // @todo - need to parameterize context factoy class and url
87           properties.put(Context.INITIAL_CONTEXT_FACTORY, AttLdap.DIR_INITIAL_CONTEXT_FACTORY);
88           properties.put(Context.PROVIDER_URL, AttLdap.DIR_PROVIDER_URL);
89           properties.put(Context.SECURITY_PRINCIPAL, AttLdap.DIR_SECURITY_PRINCIPAL);
90           Control[] ctrl = null;
91           try {
92                         ldapContext = new InitialLdapContext(properties, ctrl);
93           }
94           catch (NamingException ne) {
95                 // MJ FIX log exception?
96           }
97         }
98         return ldapContext;
99   }*/
100
101
102   /** Logger for this class and subclasses */
103   EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ServiceLocatorImpl.class);
104
105 }