< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/validation/SoftReferenceGrammarPool.java

Print this page


   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 


 174                 Entry entry = new Entry(hash, index, desc, grammar, fGrammars[index], fReferenceQueue);
 175                 fGrammars[index] = entry;
 176                 fGrammarCount++;
 177             }
 178         }
 179     } // putGrammar(Grammar)
 180 
 181     /**
 182      * Returns the grammar associated to the specified grammar description.
 183      * Currently, the root element name is used as the key for DTD grammars
 184      * and the target namespace  is used as the key for Schema grammars.
 185      *
 186      * @param desc The Grammar Description.
 187      */
 188     public Grammar getGrammar(XMLGrammarDescription desc) {
 189         synchronized (fGrammars) {
 190             clean();
 191             int hash = hashCode(desc);
 192             int index = (hash & 0x7FFFFFFF) % fGrammars.length;
 193             for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
 194                 Grammar tempGrammar = (Grammar) entry.grammar.get();
 195                 /** If the soft reference has been cleared, remove this entry from the pool. */
 196                 if (tempGrammar == null) {
 197                     removeEntry(entry);
 198                 }
 199                 else if ((entry.hash == hash) && equals(entry.desc, desc)) {
 200                     return tempGrammar;
 201                 }
 202             }
 203             return null;
 204         }
 205     } // getGrammar(XMLGrammarDescription):Grammar
 206 
 207     /**
 208      * Removes the grammar associated to the specified grammar description from the
 209      * grammar pool and returns the removed grammar. Currently, the root element name
 210      * is used as the key for DTD grammars and the target namespace  is used
 211      * as the key for Schema grammars.
 212      *
 213      * @param desc The Grammar Description.
 214      * @return     The removed grammar.


 224                 }
 225             }
 226             return null;
 227         }
 228     } // removeGrammar(XMLGrammarDescription):Grammar
 229 
 230     /**
 231      * Returns true if the grammar pool contains a grammar associated
 232      * to the specified grammar description. Currently, the root element name
 233      * is used as the key for DTD grammars and the target namespace  is used
 234      * as the key for Schema grammars.
 235      *
 236      * @param desc The Grammar Description.
 237      */
 238     public boolean containsGrammar(XMLGrammarDescription desc) {
 239         synchronized (fGrammars) {
 240             clean();
 241             int hash = hashCode(desc);
 242             int index = (hash & 0x7FFFFFFF) % fGrammars.length;
 243             for (Entry entry = fGrammars[index]; entry != null ; entry = entry.next) {
 244                 Grammar tempGrammar = (Grammar) entry.grammar.get();
 245                 /** If the soft reference has been cleared, remove this entry from the pool. */
 246                 if (tempGrammar == null) {
 247                     removeEntry(entry);
 248                 }
 249                 else if ((entry.hash == hash) && equals(entry.desc, desc)) {
 250                     return true;
 251                 }
 252             }
 253             return false;
 254         }
 255     } // containsGrammar(XMLGrammarDescription):boolean
 256 
 257     /* <p> Sets this grammar pool to a "locked" state--i.e.,
 258      * no new grammars will be added until it is "unlocked".
 259      */
 260     public void lockPool() {
 261         fPoolIsLocked = true;
 262     } // lockPool()
 263 
 264     /* <p> Sets this grammar pool to an "unlocked" state--i.e.,


 346     }
 347 
 348     /**
 349      * Removes the given entry from the pool
 350      *
 351      * @param entry the entry to remove
 352      * @return The grammar attached to this entry
 353      */
 354     private Grammar removeEntry(Entry entry) {
 355         if (entry.prev != null) {
 356             entry.prev.next = entry.next;
 357         }
 358         else {
 359             fGrammars[entry.bucket] = entry.next;
 360         }
 361         if (entry.next != null) {
 362             entry.next.prev = entry.prev;
 363         }
 364         --fGrammarCount;
 365         entry.grammar.entry = null;
 366         return (Grammar) entry.grammar.get();
 367     }
 368 
 369     /**
 370      * Removes stale entries from the pool.
 371      */
 372     private void clean() {
 373         Reference<? extends Grammar> ref = fReferenceQueue.poll();
 374         while (ref != null) {
 375             Entry entry = ((SoftGrammarReference) ref).entry;
 376             if (entry != null) {
 377                 removeEntry(entry);
 378             }
 379             ref = fReferenceQueue.poll();
 380         }
 381     }
 382 
 383     /**
 384      * This class is a grammar pool entry. Each entry acts as a node
 385      * in a doubly linked list.
 386      */


   1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Nov 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 


 174                 Entry entry = new Entry(hash, index, desc, grammar, fGrammars[index], fReferenceQueue);
 175                 fGrammars[index] = entry;
 176                 fGrammarCount++;
 177             }
 178         }
 179     } // putGrammar(Grammar)
 180 
 181     /**
 182      * Returns the grammar associated to the specified grammar description.
 183      * Currently, the root element name is used as the key for DTD grammars
 184      * and the target namespace  is used as the key for Schema grammars.
 185      *
 186      * @param desc The Grammar Description.
 187      */
 188     public Grammar getGrammar(XMLGrammarDescription desc) {
 189         synchronized (fGrammars) {
 190             clean();
 191             int hash = hashCode(desc);
 192             int index = (hash & 0x7FFFFFFF) % fGrammars.length;
 193             for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
 194                 Grammar tempGrammar = entry.grammar.get();
 195                 /** If the soft reference has been cleared, remove this entry from the pool. */
 196                 if (tempGrammar == null) {
 197                     removeEntry(entry);
 198                 }
 199                 else if ((entry.hash == hash) && equals(entry.desc, desc)) {
 200                     return tempGrammar;
 201                 }
 202             }
 203             return null;
 204         }
 205     } // getGrammar(XMLGrammarDescription):Grammar
 206 
 207     /**
 208      * Removes the grammar associated to the specified grammar description from the
 209      * grammar pool and returns the removed grammar. Currently, the root element name
 210      * is used as the key for DTD grammars and the target namespace  is used
 211      * as the key for Schema grammars.
 212      *
 213      * @param desc The Grammar Description.
 214      * @return     The removed grammar.


 224                 }
 225             }
 226             return null;
 227         }
 228     } // removeGrammar(XMLGrammarDescription):Grammar
 229 
 230     /**
 231      * Returns true if the grammar pool contains a grammar associated
 232      * to the specified grammar description. Currently, the root element name
 233      * is used as the key for DTD grammars and the target namespace  is used
 234      * as the key for Schema grammars.
 235      *
 236      * @param desc The Grammar Description.
 237      */
 238     public boolean containsGrammar(XMLGrammarDescription desc) {
 239         synchronized (fGrammars) {
 240             clean();
 241             int hash = hashCode(desc);
 242             int index = (hash & 0x7FFFFFFF) % fGrammars.length;
 243             for (Entry entry = fGrammars[index]; entry != null ; entry = entry.next) {
 244                 Grammar tempGrammar = entry.grammar.get();
 245                 /** If the soft reference has been cleared, remove this entry from the pool. */
 246                 if (tempGrammar == null) {
 247                     removeEntry(entry);
 248                 }
 249                 else if ((entry.hash == hash) && equals(entry.desc, desc)) {
 250                     return true;
 251                 }
 252             }
 253             return false;
 254         }
 255     } // containsGrammar(XMLGrammarDescription):boolean
 256 
 257     /* <p> Sets this grammar pool to a "locked" state--i.e.,
 258      * no new grammars will be added until it is "unlocked".
 259      */
 260     public void lockPool() {
 261         fPoolIsLocked = true;
 262     } // lockPool()
 263 
 264     /* <p> Sets this grammar pool to an "unlocked" state--i.e.,


 346     }
 347 
 348     /**
 349      * Removes the given entry from the pool
 350      *
 351      * @param entry the entry to remove
 352      * @return The grammar attached to this entry
 353      */
 354     private Grammar removeEntry(Entry entry) {
 355         if (entry.prev != null) {
 356             entry.prev.next = entry.next;
 357         }
 358         else {
 359             fGrammars[entry.bucket] = entry.next;
 360         }
 361         if (entry.next != null) {
 362             entry.next.prev = entry.prev;
 363         }
 364         --fGrammarCount;
 365         entry.grammar.entry = null;
 366         return entry.grammar.get();
 367     }
 368 
 369     /**
 370      * Removes stale entries from the pool.
 371      */
 372     private void clean() {
 373         Reference<? extends Grammar> ref = fReferenceQueue.poll();
 374         while (ref != null) {
 375             Entry entry = ((SoftGrammarReference) ref).entry;
 376             if (entry != null) {
 377                 removeEntry(entry);
 378             }
 379             ref = fReferenceQueue.poll();
 380         }
 381     }
 382 
 383     /**
 384      * This class is a grammar pool entry. Each entry acts as a node
 385      * in a doubly linked list.
 386      */


< prev index next >