Updating licenses in all files
[appc.git] / appc-adapters / appc-rest-adapter / appc-rest-adapter-bundle / src / main / java / org / openecomp / appc / adapter / rest / RestActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.rest;
24
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.rest.impl.RestAdapterImpl;
27 import org.openecomp.appc.configuration.Configuration;
28 import org.openecomp.appc.configuration.ConfigurationFactory;
29 import org.openecomp.appc.i18n.Msg;
30 import org.osgi.framework.BundleActivator;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.ServiceRegistration;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37
38 /**
39  * This activator is used to initialize and terminate the connection pool to one or more providers.
40  * <p>
41  * The CDP abstraction layer supports multiple types of providers, with each provider supporting multiple tenants. The
42  * "connection" to a specific tenant on a specific provider is represented by a "context" object. These context objects
43  * are authenticated to a specific tenant on the provider, but can be reused from one request to another. Contexts are
44  * slow to set up and are resource intensive, so they are cached. However, the contexts for a specific tenant on a
45  * specific provider must be cached separately.
46  * </p>
47  * <p>
48  * Activation of the bundle creates an empty cache which is organized first by provider type, then by tenant name, with
49  * the contents being an empty pool of contexts for that provider/tenant combination. The pool is created on first use,
50  * and retained for as long as the bundle is active.
51  * </p>
52  * <p>
53  * When the bundle is deactivated, the cache is torn down with all contexts being closed.
54  * </p>
55  */
56 public class RestActivator implements BundleActivator {
57
58     /**
59      * The bundle registration
60      */
61     private ServiceRegistration registration = null;
62
63     /**
64      * The reference to the actual implementation object that implements the services
65      */
66     private RestAdapter adapter;
67
68     /**
69      * The logger to be used
70      */
71     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestActivator.class);
72
73     /**
74      * The configuration object used to configure this bundle
75      */
76     private Configuration configuration;
77
78     /**
79      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
80      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
81      * <p>
82      * This method must complete and return to its caller in a timely manner.
83      * </p>
84      * 
85      * @param context
86      *            The execution context of the bundle being started.
87      * @throws java.lang.Exception
88      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
89      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
90      *             used by this bundle.
91      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
92      */
93     @Override
94     public void start(BundleContext context) throws Exception {
95         logger.info("Starting bundle " + getName());
96
97         configuration = ConfigurationFactory.getConfiguration();
98         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
99         logger.info(Msg.COMPONENT_INITIALIZING, appName, "rest adapter");
100         adapter = new RestAdapterImpl(configuration.getProperties());
101         if (registration == null) {
102             logger.info(Msg.REGISTERING_SERVICE, appName, adapter.getAdapterName(),
103                 RestAdapter.class.getSimpleName());
104             registration = context.registerService(RestAdapter.class, adapter, null);
105         }
106
107         logger.info(Msg.COMPONENT_INITIALIZED, appName, "REST adapter");
108     }
109
110     /**
111      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
112      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
113      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
114      * call any Framework objects.
115      * <p>
116      * This method must complete and return to its caller in a timely manner.
117      * </p>
118      * 
119      * @param context
120      *            The execution context of the bundle being stopped.
121      * @throws java.lang.Exception
122      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
123      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
124      *             services used by the bundle. *
125      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
126      */
127     @Override
128     public void stop(BundleContext context) throws Exception {
129         logger.info("Stopping bundle " + getName());
130
131         if (registration != null) {
132             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
133             logger.info(Msg.COMPONENT_TERMINATING, appName, "REST adapter");
134             logger.info(Msg.UNREGISTERING_SERVICE, appName, adapter.getAdapterName());
135             registration.unregister();
136             registration = null;
137             logger.info(Msg.COMPONENT_TERMINATED, appName, "REST adapter");
138         }
139     }
140
141     public String getName() {
142         return "APPC IaaS adapter";
143     }
144
145 }