Change the header to SO
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / IdentityAuthenticationTypeAbstract.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.cloud;\r
21 \r
22 import java.util.Map;\r
23 import java.util.concurrent.ConcurrentHashMap;\r
24 \r
25 import org.openecomp.mso.cloud.authentication.AuthenticationWrapper;\r
26 \r
27 public abstract class IdentityAuthenticationTypeAbstract {\r
28 \r
29         // This map will prevent duplicates (as if it was an Enum).\r
30         // Without this, using an instance specific field for the class could allow\r
31         // different classes bound to the same entry name.\r
32         private static final Map<String, IdentityAuthenticationTypeAbstract> entries = new ConcurrentHashMap<>();\r
33 \r
34         private String identityType;\r
35 \r
36         private Class<? extends AuthenticationWrapper> wrapperClass;\r
37 \r
38         protected IdentityAuthenticationTypeAbstract(String identityType, Class<? extends AuthenticationWrapper> wrapperClass) {\r
39                 try {\r
40                         this.identityType = identityType;\r
41                         this.wrapperClass = wrapperClass;\r
42                         entries.put(identityType, this);\r
43                         AuthenticationWrapper.register(this.toString(), wrapperClass);\r
44                 } catch (IllegalAccessException | InstantiationException e) {\r
45                         // Do not add the class if an exception occurs as we won't get the class anyway\r
46                 }\r
47         }\r
48 \r
49         public static final IdentityAuthenticationTypeAbstract valueOf(String serverType) {\r
50                 return entries.get(serverType);\r
51         }\r
52 \r
53         @Override\r
54         public final String toString() {\r
55                 return this.identityType;\r
56         }\r
57 \r
58         public final String name() {\r
59                 return this.identityType;\r
60         }\r
61 \r
62         public static final IdentityAuthenticationTypeAbstract[] values() {\r
63                 return (IdentityAuthenticationTypeAbstract[]) entries.values().stream().toArray(IdentityAuthenticationTypeAbstract[]::new);\r
64         }\r
65 \r
66         public final Class<? extends AuthenticationWrapper> getWrapperClass() {\r
67                 return this.wrapperClass;\r
68         }\r
69 \r
70         @Override\r
71         public final boolean equals(Object other) {\r
72                 return ((this.identityType != null) && (other != null) && (other instanceof IdentityAuthenticationTypeAbstract) && (this.identityType.equals(other.toString())));\r
73         }\r
74 \r
75 }\r