/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.jena.fuseki.server;

import io.micrometer.core.instrument.MeterRegistry;
import javax.servlet.ServletContext;

import org.apache.jena.atlas.lib.Registry;
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.fuseki.Fuseki;
import org.apache.jena.fuseki.FusekiException;
import org.apache.jena.fuseki.metrics.FusekiRequestsMetrics;

/**
 * Registry of (dataset name, {@link DataAccessPoint}).
 * In addition, registered {@link DataAccessPoint} are added to the metrics.
 */
public class DataAccessPointRegistry extends Registry<String, DataAccessPoint>
{
    private MeterRegistry meterRegistry;

    public DataAccessPointRegistry(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    public DataAccessPointRegistry(DataAccessPointRegistry other) {
        other.forEach((name, accessPoint)->register(name, accessPoint));
        this.meterRegistry = other.meterRegistry;
    }

    // Preferred way to register. Other method for legacy.
    public void register(DataAccessPoint accessPt) {
        register(accessPt.getName(), accessPt);
    }

    private void register(String name, DataAccessPoint accessPt) {
        if ( isRegistered(name) )
            throw new FusekiException("Already registered: "+name);
        super.put(name, accessPt);
        if (meterRegistry != null) {
            new FusekiRequestsMetrics( accessPt ).bindTo( meterRegistry );
        }
    }

    /** @deprecated Use {@link #register(DataAccessPoint)} */
    @Override
    @Deprecated
    public void put(String key, DataAccessPoint dap) {
        // Ignore presented name. Use canonical name in the DataAccessPoint. */
        register(dap);
    }

    @Override
    public DataAccessPoint get(String key) {
        return super.get(key);
    }

    // Debugging
    public void print() {
        print(null);
    }

    // Debugging
    public void print(String string) {
        System.out.flush();
        if ( string == null )
            string = "DataAccessPointRegistry";
        System.out.println("== "+string);
        this.forEach((k,ref)->{
            System.out.printf("  (key=%s, ref=%s)\n", k, ref.getName());
            ref.getDataService().getOperations().forEach((op)->{
                ref.getDataService().getEndpoints(op).forEach(ep->{
                    System.out.printf("    %-10s @ \"%s\"\n", op, ep.getName());
                });
            });
        });
    }

    // The server DataAccessPointRegistry is held in the ServletContext for the server.
    public static DataAccessPointRegistry get(ServletContext cxt) {
        DataAccessPointRegistry registry = (DataAccessPointRegistry)cxt.getAttribute(Fuseki.attrNameRegistry);
        if ( registry == null )
            Log.warn(DataAccessPointRegistry.class, "No data access point registry for ServletContext");
        return registry;
    }

    public static void set(ServletContext cxt, DataAccessPointRegistry registry) {
        cxt.setAttribute(Fuseki.attrNameRegistry, registry);
    }
}

@@ -847,16 +847,106 @@
 %0Aimport 
+java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport javax.servlet.ServletContext;%0A%0Aimport 
 io.micro
@@ -985,46 +985,8 @@
 try;
-%0Aimport javax.servlet.ServletContext;%0A
 %0Aimp
@@ -2304,42 +2304,283 @@
  /**
- @deprecated Use %7B@link #register(
+%0A     * Collection of the %7B@link DataAccessPoint DataAccessPoints%7D. This is a new list%0A     * generated from the registry contents and not still connected to the registry.%0A     * Registry changes will not interfere with iteration over the list.%0A     * %7B@link DataAccessPoint 
 Data
@@ -2594,158 +2594,711 @@
 oint
-)%7D */%0A    @Override%0A    @Deprecated%0A    public void put(String key, DataAccessPoint dap) %7B
+s%7D can not be registered twice under%0A     * differerent names (the same dataset can be via different%0A     * %7B@link DataAccessPoint DataAccessPoints%7D so the list has no duplicates.%0A     * There is no defined order to the list.%0A     */%0A    public List%3CDataAccessPoint%3E accessPoints() %7B%0A        List%3CDataAccessPoint%3E accessPoints = new ArrayList%3C%3E(size());%0A        // Make a copy for safety.%0A        forEach((_name, accessPoint) -%3E accessPoints.add(accessPoint));%0A        return accessPoints;
 %0A    
+%7D%0A%0A
     /
-/ Ignore presented name. Use canonical name in the
+**%0A     *  @deprecated Use %7B@link #register(DataAccessPoint)%7D.%0A     * This method ignores the accessPointName argument.%0A     */%0A    @Override%0A    @Deprecated%0A    public void put(String accessPointName,
  Dat
@@ -3313,12 +3313,15 @@
 oint
-. */
+ dap) %7B
 %0A   

Add DataAccessPointRegistry.accessPoints