Registry.java [src/csip] Revision: 71821307bfe742c00c6dc582c171224a9ac59935  Date: Fri Apr 21 11:46:19 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) -> getServiceName(o1).compareTo(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;
    }


    String getServicePath(Class<?> c) {
        Path p = (Path) c.getAnnotation(Path.class);
        return (p == null) ? "" : p.value();
    }


    String getServiceName(Class<?> c) {
        Name p = (Name) c.getAnnotation(Name.class);
        return (p == null) ? "" : p.value();
    }


    String getServiceDescription(Class<?> c) {
        Description p = (Description) c.getAnnotation(Description.class);
        return (p == null) ? "" : p.value();
    }

}