001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto.jsonschema;
014
015import static org.apache.juneau.internal.StringUtils.*;
016import java.io.*;
017import java.net.*;
018import java.util.concurrent.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.json.*;
022
023/**
024 * @deprecated Use JsonSchemaMap.
025 */
026@Deprecated
027public abstract class SchemaMap extends ConcurrentHashMap<URI,Schema> {
028
029   private static final long serialVersionUID = 1L;
030
031   /**
032    * Return the {@link Schema} object at the specified URI.
033    *
034    * <p>
035    * If this schema object has not been loaded yet, calls {@link #load(URI)}.
036    *
037    * <p>
038    * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
039    * Strings must be valid URIs.
040    *
041    * <p>
042    * URIs defined by {@link UriResolver} can be used for values.
043    *
044    * @param uri The URI of the schema to retrieve.
045    * @return The Schema, or <jk>null</jk> if schema was not located and could not be loaded.
046    */
047   @Override /* Map */
048   public Schema get(Object uri) {
049      URI u = toURI(uri);
050      Schema s = super.get(u);
051      if (s != null)
052         return s;
053      synchronized(this) {
054         s = load(u);
055         if (s != null) {
056            // Note:  Can't use add(Schema...) since the ID property may not be set.
057            s.setSchemaMap(this);
058            put(u, s);
059         }
060         return s;
061      }
062   }
063
064   /**
065    * Convenience method for pre-populating this map with the specified schemas.
066    *
067    * <p>
068    * The schemas passed in through this method MUST have their ID properties set.
069    *
070    * @param schemas The set of schemas to add to this map.
071    * @return This object (for method chaining).
072    * @throws RuntimeException If one or more schema objects did not have their ID property set.
073    */
074   public SchemaMap add(Schema...schemas) {
075      for (Schema schema : schemas) {
076         if (schema.getId() == null)
077            throw new RuntimeException("Schema with no ID passed to SchemaMap.add(Schema...)");
078         put(schema.getId(), schema);
079         schema.setSchemaMap(this);
080      }
081      return this;
082   }
083
084   /**
085    * Subclasses must implement either this method or {@link #getReader(URI)} to load the schema with the specified URI.
086    *
087    * <p>
088    * It's up to the implementer to decide where these come from.
089    *
090    * <p>
091    * The default implementation calls {@link #getReader(URI)} and parses the schema document.
092    * If {@link #getReader(URI)} returns <jk>null</jk>, this method returns <jk>null</jk> indicating this is an
093    * unreachable document.
094    *
095    * @param uri The URI to load the schema from.
096    * @return The parsed schema.
097    */
098   public Schema load(URI uri) {
099      try (Reader r = getReader(uri)) {
100         if (r == null)
101            return null;
102         return JsonParser.DEFAULT.parse(r, Schema.class);
103      } catch (Exception e) {
104         throw new RuntimeException(e);
105      }
106   }
107
108   /**
109    * Subclasses must implement either this method or {@link #load(URI)} to load the schema with the specified URI.
110    *
111    * <p>
112    * It's up to the implementer to decide where these come from.
113    *
114    * <p>
115    * The default implementation returns <jk>null</jk>.
116    *
117    * @param uri The URI to connect to and retrieve the contents.
118    * @return The reader from reading the specified URI.
119    */
120   public Reader getReader(URI uri) {
121      return null;
122   }
123}