More license header changes to appc-client files
[appc.git] / appc-client / client-kit / src / main / java / org / onap / appc / client / lcm / impl / business / AppcLifeCycleManagerServiceFactoryImpl.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.lcm.impl.business;
25
26 import java.util.HashMap;
27
28 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
29 import org.onap.appc.client.lcm.api.ApplicationContext;
30 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
31 import org.onap.appc.client.lcm.exceptions.AppcClientException;
32
33 import java.lang.reflect.Proxy;
34 import java.util.Properties;
35
36 public class AppcLifeCycleManagerServiceFactoryImpl implements AppcLifeCycleManagerServiceFactory {
37
38     class AppcLifeCycleManagerServiceFactoryImplData {
39
40         private LifeCycleManagerStateful _lifeCycleManagerStateful;
41         private LCMRequestProcessor _lcmRequestProcessor;
42
43         AppcLifeCycleManagerServiceFactoryImplData(LifeCycleManagerStateful lifeCycleManagerStateful,
44                                                    LCMRequestProcessor lcmRequestProcessor) {
45
46             _lifeCycleManagerStateful = lifeCycleManagerStateful;
47             _lcmRequestProcessor = lcmRequestProcessor;
48         }
49
50         LifeCycleManagerStateful getLifeCycleManagerStateful() {
51             return _lifeCycleManagerStateful;
52         }
53
54         LCMRequestProcessor getLCMRequestProcessor() {
55             return _lcmRequestProcessor;
56         }
57     }
58
59     private HashMap<String, AppcLifeCycleManagerServiceFactoryImplData> lcmMap = new HashMap<String, AppcLifeCycleManagerServiceFactoryImplData>();
60
61     @Override
62     public synchronized LifeCycleManagerStateful createLifeCycleManagerStateful(ApplicationContext context,
63                                                                                 Properties properties) throws AppcClientException {
64         String cType = properties.getProperty("controllerType");
65         if (cType == null || cType.length() == 0)
66         {
67             cType = "APPC";
68             properties.put("controllerType", cType);
69         }
70
71         AppcLifeCycleManagerServiceFactoryImplData lcmData = lcmMap.get(cType);
72         LifeCycleManagerStateful lifeCycleManagerStateful = null;
73         LCMRequestProcessor lcmRequestProcessor = null;
74
75         if (lcmData != null) {
76             lifeCycleManagerStateful = lcmData.getLifeCycleManagerStateful();
77             lcmRequestProcessor = lcmData.getLCMRequestProcessor();
78         }
79
80         if (lifeCycleManagerStateful == null) {
81             lcmRequestProcessor = new LCMRequestProcessor(context, properties);
82             lifeCycleManagerStateful = (LifeCycleManagerStateful) Proxy.newProxyInstance(
83                     LifeCycleManagerStateful.class.getClassLoader(), new Class<?>[] { LifeCycleManagerStateful.class },
84                     new RPCInvocator(lcmRequestProcessor));
85             lcmMap.put(cType,
86                     new AppcLifeCycleManagerServiceFactoryImplData(lifeCycleManagerStateful, lcmRequestProcessor));
87         } else {
88             throw new IllegalStateException("already instansiated LifeCycleManagerStateful instance");
89         }
90         return lifeCycleManagerStateful;
91     }
92
93     public LifeCycleManagerStateful createLifeCycleManagerStateful(ApplicationContext context,
94                                                                    Properties properties, String controllerType) throws AppcClientException {
95         if (controllerType != null && controllerType.length() != 0)
96             properties.put("controllerType", controllerType.toUpperCase());
97         return createLifeCycleManagerStateful(context, properties);
98     }
99
100     @Override
101     public void shutdownLifeCycleManager(boolean isForceShutdown) {
102
103         shutdownLifeCycleManager(isForceShutdown, "APPC");
104     }
105
106     @Override
107     public void shutdownLifeCycleManager(boolean isForceShutdown, String controllerType) {
108         if (controllerType == null || controllerType.length() == 0)
109             controllerType = "APPC";
110         else
111             controllerType = controllerType.toUpperCase();
112
113         AppcLifeCycleManagerServiceFactoryImplData lcmData = lcmMap.get(controllerType);
114         LCMRequestProcessor lcmRequestProcessor = null;
115
116         if (lcmData != null) {
117             lcmRequestProcessor = lcmData.getLCMRequestProcessor();
118         }
119
120         if (lcmRequestProcessor != null) {
121             lcmRequestProcessor.shutdown(isForceShutdown);
122         } else {
123             throw new IllegalStateException(
124                     "The life cycle manager library wasn't instantiated properly, therefore the shutdown event will not be handled");
125         }
126     }
127
128 }