Merge "Reorder modifiers"
[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  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.openecomp.mso.cloud;\r
22 \r
23 import java.util.Map;\r
24 import java.util.concurrent.ConcurrentHashMap;\r
25 \r
26 import org.openecomp.mso.cloud.authentication.AuthenticationWrapper;\r
27 import org.openecomp.mso.logger.MsoLogger;\r
28 \r
29 public abstract class IdentityAuthenticationTypeAbstract {\r
30 \r
31         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);\r
32         // This map will prevent duplicates (as if it was an Enum).\r
33         // Without this, using an instance specific field for the class could allow\r
34         // different classes bound to the same entry name.\r
35         private static final Map<String, IdentityAuthenticationTypeAbstract> entries = new ConcurrentHashMap<>();\r
36 \r
37         private String identityType;\r
38 \r
39         private Class<? extends AuthenticationWrapper> wrapperClass;\r
40 \r
41         protected IdentityAuthenticationTypeAbstract(String identityType, Class<? extends AuthenticationWrapper> wrapperClass) {\r
42                 try {\r
43                         this.identityType = identityType;\r
44                         this.wrapperClass = wrapperClass;\r
45                         entries.put(identityType, this);\r
46                         AuthenticationWrapper.register(this.toString(), wrapperClass);\r
47                 } catch (IllegalAccessException | InstantiationException e) {\r
48                         LOGGER.debug("Exception in Identity Authentication",e);\r
49                 }\r
50         }\r
51 \r
52         public static final IdentityAuthenticationTypeAbstract valueOf(String serverType) {\r
53                 return entries.get(serverType);\r
54         }\r
55 \r
56         @Override\r
57         public final String toString() {\r
58                 return this.identityType;\r
59         }\r
60 \r
61         public final String name() {\r
62                 return this.identityType;\r
63         }\r
64 \r
65         public static final IdentityAuthenticationTypeAbstract[] values() {\r
66                 return (IdentityAuthenticationTypeAbstract[]) entries.values().stream().toArray(IdentityAuthenticationTypeAbstract[]::new);\r
67         }\r
68 \r
69         public final Class<? extends AuthenticationWrapper> getWrapperClass() {\r
70                 return this.wrapperClass;\r
71         }\r
72 \r
73         @Override\r
74         public final boolean equals(Object other) {\r
75                 return (this.identityType != null) && (other != null) && (other instanceof IdentityAuthenticationTypeAbstract) && (this.identityType.equals(other.toString()));\r
76         }\r
77 \r
78         @Override\r
79         public int hashCode() {\r
80                 final int prime = 31;\r
81                 int result = 1;\r
82                 result = prime * result + ((identityType == null) ? 0 : identityType.hashCode());\r
83                 return result;\r
84         }\r
85 \r
86 }\r