Registry.java [src/csip] Revision: 3af6c48ce47c53e489a005b55aebf48251e622b9  Date: Tue Apr 25 21:50:59 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;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;

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

    List<Class<?>> s;


    Registry() {
    }


    synchronized public void register(Set<Class<?>> service) {
        if (s != null) { // onne 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;
    }

}