1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.jaxp.validation;
  23 
  24 import java.lang.ref.WeakReference;
  25 
  26 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  27 
  28 /**
  29  * <p>An implementation of Schema for W3C XML Schemas
  30  * that keeps a weak reference to its grammar pool. If
  31  * no validators currently have a reference to the
  32  * grammar pool, the garbage collector is free to reclaim
  33  * its memory.</p>
  34  *
  35  * @author Michael Glavassevich, IBM
  36  */
  37 final class WeakReferenceXMLSchema extends AbstractXMLSchema {
  38 
  39     /** Weak reference to grammar pool. */
  40     private WeakReference<XMLGrammarPool> fGrammarPool = new WeakReference<>(null);
  41 
  42     public WeakReferenceXMLSchema() {}
  43 
  44     /*
  45      * XSGrammarPoolContainer methods
  46      */
  47 
  48     public synchronized XMLGrammarPool getGrammarPool() {
  49         XMLGrammarPool grammarPool = fGrammarPool.get();
  50         // If there's no grammar pool then either we haven't created one
  51         // yet or the garbage collector has already cleaned out the previous one.
  52         if (grammarPool == null) {
  53             grammarPool = new SoftReferenceGrammarPool();
  54             fGrammarPool = new WeakReference<>(grammarPool);
  55         }
  56         return grammarPool;
  57     }
  58 
  59     public boolean isFullyComposed() {
  60         return false;
  61     }
  62 
  63 } // WeakReferenceXMLSchema