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.json;
014
015import java.io.IOException;
016
017import org.apache.juneau.*;
018import org.apache.juneau.collections.*;
019import org.apache.juneau.jsonschema.*;
020import org.apache.juneau.serializer.*;
021
022/**
023 * Session object that lives for the duration of a single use of {@link JsonSchemaSerializer}.
024 *
025 * <p>
026 * This class is NOT thread safe.
027 * It is typically discarded after one-time use although it can be reused within the same thread.
028 */
029public class JsonSchemaSerializerSession extends JsonSerializerSession {
030
031   private final JsonSchemaGeneratorSession genSession;
032   private final JsonSchemaSerializer ctx;
033
034   /**
035    * Create a new session using properties specified in the context.
036    *
037    * @param ctx
038    *    The context creating this session object.
039    *    The context contains all the configuration settings for this object.
040    * @param args
041    *    Runtime arguments.
042    *    These specify session-level information such as locale and URI context.
043    *    It also include session-level properties that override the properties defined on the bean and
044    *    serializer contexts.
045    */
046   protected JsonSchemaSerializerSession(JsonSchemaSerializer ctx, SerializerSessionArgs args) {
047      super(ctx, args);
048      genSession = ctx.getGenerator().createSession(args);
049      this.ctx = ctx;
050   }
051
052   @Override /* SerializerSession */
053   protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
054      try {
055         super.doSerialize(out, genSession.getSchema(o));
056      } catch (BeanRecursionException e) {
057         throw new SerializeException(e);
058      }
059   }
060
061   //-----------------------------------------------------------------------------------------------------------------
062   // Extended metadata
063   //-----------------------------------------------------------------------------------------------------------------
064
065   /**
066    * Returns the language-specific metadata on the specified class.
067    *
068    * @param cm The class to return the metadata on.
069    * @return The metadata.
070    */
071   protected JsonSchemaClassMeta getJsonSchemaClassMeta(ClassMeta<?> cm) {
072      return ctx.getJsonSchemaClassMeta(cm);
073   }
074
075   /**
076    * Returns the language-specific metadata on the specified bean property.
077    *
078    * @param bpm The bean property to return the metadata on.
079    * @return The metadata.
080    */
081   protected JsonSchemaBeanPropertyMeta getJsonSchemaBeanPropertyMeta(BeanPropertyMeta bpm) {
082      return ctx.getJsonSchemaBeanPropertyMeta(bpm);
083   }
084
085   //-----------------------------------------------------------------------------------------------------------------
086   // Other methods
087   //-----------------------------------------------------------------------------------------------------------------
088
089   @Override /* Session */
090   public OMap toMap() {
091      return super.toMap()
092         .a("JsonSchemaSerializerSession", new DefaultFilteringOMap()
093      );
094   }
095}