All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.killbill.billing.plugin.deposit.DepositActivator Maven / Gradle / Ivy

/*
 * Copyright 2021-2021 Equinix, Inc
 * Copyright 2014-2021 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.plugin.deposit;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;

import org.killbill.billing.control.plugin.api.PaymentControlPluginApi;
import org.killbill.billing.osgi.api.Healthcheck;
import org.killbill.billing.osgi.api.OSGIPluginProperties;
import org.killbill.billing.osgi.libs.killbill.KillbillActivatorBase;
import org.killbill.billing.payment.plugin.api.PaymentPluginApi;
import org.killbill.billing.plugin.api.notification.PluginConfigurationEventHandler;
import org.killbill.billing.plugin.core.config.PluginEnvironmentConfig;
import org.killbill.billing.plugin.core.resources.jooby.PluginApp;
import org.killbill.billing.plugin.core.resources.jooby.PluginAppBuilder;
import org.killbill.billing.plugin.deposit.dao.DepositDao;
import org.osgi.framework.BundleContext;

public class DepositActivator extends KillbillActivatorBase {

    public static final String PLUGIN_NAME = "killbill-deposit";

    private DepositConfigurationHandler depositConfigurationHandler;

    @Override
    public void start(final BundleContext context) throws Exception {
        super.start(context);

        final String region = PluginEnvironmentConfig.getRegion(configProperties.getProperties());

        depositConfigurationHandler = new DepositConfigurationHandler(region, PLUGIN_NAME, killbillAPI);
        depositConfigurationHandler.setDefaultConfigurable(new DepositConfiguration());

        final PaymentControlPluginApi paymentControlPluginApi = new DepositPaymentControlPluginApi(depositConfigurationHandler,
                                                                                                   killbillAPI,
                                                                                                   configProperties,
                                                                                                   clock.getClock());
        registerPaymentControlPluginApi(context, paymentControlPluginApi);

        final DepositDao depositDao = new DepositDao(dataSource.getDataSource());
        final PaymentPluginApi paymentPluginApi = new DepositPaymentPluginApi(killbillAPI, configProperties, clock.getClock(), depositDao);
        registerPaymentPluginApi(context, paymentPluginApi);

        final Healthcheck healthcheck = new DepositHealthcheck();
        registerHealthcheck(context, healthcheck);

        final PluginApp pluginApp = new PluginAppBuilder(PLUGIN_NAME,
                                                         killbillAPI,
                                                         dataSource,
                                                         super.clock,
                                                         configProperties).withRouteClass(DepositServlet.class)
                                                                          .withRouteClass(DepositHealthcheckServlet.class)
                                                                          .withService(healthcheck)
                                                                          .build();
        final HttpServlet httpServlet = PluginApp.createServlet(pluginApp);
        registerServlet(context, httpServlet);

        registerHandlers();
    }

    private void registerHandlers() {
        final PluginConfigurationEventHandler configHandler = new PluginConfigurationEventHandler(depositConfigurationHandler);
        dispatcher.registerEventHandlers(configHandler);
    }

    private void registerServlet(final BundleContext context, final Servlet servlet) {
        final Hashtable props = new Hashtable();
        props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
        registrar.registerService(context, Servlet.class, servlet, props);
    }

    private void registerPaymentControlPluginApi(final BundleContext context, final PaymentControlPluginApi api) {
        final Dictionary props = new Hashtable();
        props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
        registrar.registerService(context, PaymentControlPluginApi.class, api, props);
    }

    private void registerPaymentPluginApi(final BundleContext context, final PaymentPluginApi api) {
        final Hashtable props = new Hashtable();
        props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
        registrar.registerService(context, PaymentPluginApi.class, api, props);
    }

    private void registerHealthcheck(final BundleContext context, final Healthcheck healthcheck) {
        final Hashtable props = new Hashtable();
        props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
        registrar.registerService(context, Healthcheck.class, healthcheck, props);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy