[MSO-8] Update the maven dependency
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / IdentityAuthenticationTypeAbstract.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.cloud;
21
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import org.openecomp.mso.cloud.authentication.AuthenticationWrapper;
26
27 public abstract class IdentityAuthenticationTypeAbstract {
28
29         // This map will prevent duplicates (as if it was an Enum).
30         // Without this, using an instance specific field for the class could allow
31         // different classes bound to the same entry name.
32         private static final Map<String, IdentityAuthenticationTypeAbstract> entries = new ConcurrentHashMap<>();
33
34         private String identityType;
35
36         private Class<? extends AuthenticationWrapper> wrapperClass;
37
38         protected IdentityAuthenticationTypeAbstract(String identityType, Class<? extends AuthenticationWrapper> wrapperClass) {
39                 try {
40                         this.identityType = identityType;
41                         this.wrapperClass = wrapperClass;
42                         entries.put(identityType, this);
43                         AuthenticationWrapper.register(this.toString(), wrapperClass);
44                 } catch (IllegalAccessException | InstantiationException e) {
45                         // Do not add the class if an exception occurs as we won't get the class anyway
46                 }
47         }
48
49         public static final IdentityAuthenticationTypeAbstract valueOf(String serverType) {
50                 return entries.get(serverType);
51         }
52
53         @Override
54         public final String toString() {
55                 return this.identityType;
56         }
57
58         public final String name() {
59                 return this.identityType;
60         }
61
62         public static final IdentityAuthenticationTypeAbstract[] values() {
63                 return (IdentityAuthenticationTypeAbstract[]) entries.values().stream().toArray(IdentityAuthenticationTypeAbstract[]::new);
64         }
65
66         public final Class<? extends AuthenticationWrapper> getWrapperClass() {
67                 return this.wrapperClass;
68         }
69
70         @Override
71         public final boolean equals(Object other) {
72                 return ((this.identityType != null) && (other != null) && (other instanceof IdentityAuthenticationTypeAbstract) && (this.identityType.equals(other.toString())));
73         }
74
75 }