001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.restfulEndpoints;
019
020import io.stallion.exceptions.UsageException;
021import io.stallion.monitoring.InternalEndpoints;
022import io.stallion.sitemaps.SiteMapEndpoints;
023import io.stallion.services.Log;
024
025import java.util.ArrayList;
026import java.util.List;
027
028
029public class EndpointsRegistry {
030    private ArrayList<RestEndpointBase> endpoints = new ArrayList<>();
031
032    private static EndpointsRegistry _instance;
033
034    public static EndpointsRegistry instance() {
035        if (_instance == null) {
036            throw new UsageException("You must call endpoint load before instance() is called.");
037        }
038        return _instance;
039    }
040
041    public static EndpointsRegistry load() {
042        _instance = new EndpointsRegistry();
043        registerDefaultEndpoints();
044        return _instance;
045    }
046
047    public static void registerDefaultEndpoints() {
048        _instance.addResource("", new InternalEndpoints());
049        //_instance.addResource("", new SiteMapEndpoints());
050        SiteMapEndpoints.registerEndpoints();
051    }
052
053    public static void shutdown() {
054        _instance = null;
055    }
056
057
058
059    public EndpointsRegistry addResource(String basePath, EndpointResource ...resources) {
060        ResourceToEndpoints converter = new ResourceToEndpoints(basePath);
061        for (EndpointResource resource: resources) {
062            Log.finer("Register resource {0}", resource.getClass().getName());
063            List<? extends RestEndpointBase> endPoints = converter.convert(resource);
064            addEndpoints(endPoints.toArray(new RestEndpointBase[endPoints.size()]));
065        }
066        return this;
067    }
068
069    public EndpointsRegistry addEndpoints(RestEndpointBase...newEndpoints) {
070        for(RestEndpointBase endpoint: newEndpoints) {
071            if (endpoints.contains(endpoint)) {
072                endpoints.remove(endpoint);
073            }
074            Log.fine("Adding endpoint {0} {1}", endpoint.getRoute(), endpoint.getClass());
075            endpoints.add(endpoint);
076        }
077        return this;
078    }
079
080
081    public List<RestEndpointBase> getEndpoints() {
082        return endpoints;
083    }
084}