69907ba86b5d89dbc469ecb06b064ea5dbcb7e88
[appc.git] / appc-client / client-lib / src / main / java / org / onap / appc / client / impl / protocol / ProtocolFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.client.impl.protocol;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 public class ProtocolFactory {
30
31     private static ProtocolFactory instance;
32     private Map<ProtocolType,Protocol> protocols;
33
34     /**
35      * Singleton factory
36      */
37     private ProtocolFactory(){
38
39         protocols = new HashMap<ProtocolType, Protocol>();
40     }
41
42     /**
43      * get factory instance
44      * @return factory instance
45      */
46     public static synchronized ProtocolFactory getInstance(){
47
48         if (instance == null) {
49             instance = new ProtocolFactory();
50         }
51         return instance;
52     }
53
54     /**
55      * returns instantiated protocol object
56      * @param type of protocol object
57      * @return protocol object
58      */
59     public Protocol getProtocolObject(ProtocolType type) throws ProtocolException {
60
61         Protocol protocol = protocols.get(type);
62         synchronized (this) {
63             if (protocol == null) {
64                 switch (type) {
65                     case SYNC:
66                         throw new ProtocolException("Protocol SYNC is not implemented");
67                     case ASYNC:
68                         protocol = new AsyncProtocolImpl();
69                         protocols.put(type, protocol);
70                         break;
71                     default:
72                         throw new ProtocolException("Protocol type not found");
73                 }
74             }
75         }
76         return protocol;
77     }
78 }