Registry.java [src/csip] Revision: 7810ef2bc4f0724731d07152254a68dc90ea0e75  Date: Tue Apr 25 21:55:55 MDT 2017
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2017, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Services classes registry.
 *
 * @author od
 */
public class Registry {

    List<Class<?>> s;


    Registry() {
    }


    synchronized public void register(Set<Class<?>> service) {
        if (s != null) { // one time registration only
            return;
        }
        s = new ArrayList<>();
        service.forEach((Class<?> c) -> {
            if (c.getCanonicalName().startsWith("m.") // model service
                    || c.getCanonicalName().startsWith("d.")) { // data service
                // data service
                Config.LOG.info("Register service " + c);
                s.add(c);
                Utils.callStaticMethodIfExist(c, "onContextInit");
            }
        });
        Collections.sort(s, (Class<?> o1, Class<?> o2)
                -> Utils.getServiceName(o1).compareTo(Utils.getServiceName(o2)));
        Config.LOG.info(">>>>>>>> Registered " + s.size() + " CSIP services.");
    }


    synchronized void unregister() {
        if (s == null) {
            return;
        }
        s.forEach((Class<?> c) -> {
            Config.LOG.info("Unregister service " + c);
            Utils.callStaticMethodIfExist(c, "onContextDestroy");
        });
        s.clear();
        s = null;
    }


    List<Class<?>> getServices() {
        return s;
    }

}