1 /*
   2  * Copyright (c) 2010-2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.runtime;
  26 
  27 import java.io.IOException;
  28 import java.io.Serializable;
  29 import java.util.Arrays;
  30 import java.util.Map;
  31 
  32 /**
  33  * Interface for persistent caching of compiled scripts.
  34  */
  35 public interface CodeCache {
  36 
  37     /**
  38      * Return a compiled script from the cache, or null if it isn't found.
  39      *
  40      * @param source the source
  41      * @return the compiled script or null
  42      * @throws IOException
  43      * @throws ClassNotFoundException
  44      */
  45     CachedScript getScript(Source source) throws IOException, ClassNotFoundException;
  46 
  47     /**
  48      * Store a compiled script in the cache.
  49      *
  50      * @param source the source
  51      * @param mainClassName the main class name
  52      * @param classBytes a map of class bytes
  53      * @param constants the constants array
  54      * @throws IOException
  55      */
  56     void putScript(Source source, String mainClassName, Map<String, byte[]> classBytes, Object[] constants) throws IOException;
  57 
  58     /**
  59      * Class representing a compiled script.
  60      */
  61     public static class CachedScript implements Serializable {
  62 
  63         /** Main class name. */
  64         private final String mainClassName;
  65 
  66         /** Map of class names to class bytes. */
  67         private final Map<String, byte[]> classBytes;
  68 
  69         /** Constants array. */
  70         private final Object[] constants;
  71 
  72         /** The source */
  73         private transient Source source;
  74 
  75         private static final long serialVersionUID = 2346867899194682619L;
  76 
  77         /**
  78          * Constructor.
  79          *
  80          * @param mainClassName main class name
  81          * @param classBytes map of class names to class bytes
  82          * @param constants constants array
  83          */
  84         CachedScript(final Source source, final String mainClassName, final Map<String, byte[]> classBytes, final Object[] constants) {
  85             this.source = source;
  86             this.mainClassName = mainClassName;
  87             this.classBytes = classBytes;
  88             this.constants = constants;
  89         }
  90 
  91         /**
  92          * Returns the main class name.
  93          * @return the main class name
  94          */
  95         public String getMainClassName() {
  96             return mainClassName;
  97         }
  98 
  99         /**
 100          * Returns a map of class names to class bytes.
 101          * @return map of class bytes
 102          */
 103         public Map<String, byte[]> getClassBytes() {
 104             return classBytes;
 105         }
 106 
 107         /**
 108          * Returns the constants array.
 109          * @return constants array
 110          */
 111         public Object[] getConstants() {
 112             return constants;
 113         }
 114 
 115         /**
 116          * Returns the source of this cached script.
 117          * @return the source
 118          */
 119         public Source getSource() {
 120             return source;
 121         }
 122 
 123         /**
 124          * Sets the source of this cached script.
 125          * @param source the source
 126          */
 127         protected void setSource(final Source source) {
 128             this.source = source;
 129         }
 130 
 131         @Override
 132         public int hashCode() {
 133             int hash = mainClassName.hashCode();
 134             hash = 31 * hash + classBytes.hashCode();
 135             hash = 31 * hash + Arrays.hashCode(constants);
 136             return hash;
 137         }
 138 
 139         @Override
 140         public boolean equals(Object obj) {
 141             if (obj == this) {
 142                 return true;
 143             }
 144             if (!(obj instanceof CachedScript)) {
 145                 return false;
 146             }
 147 
 148             final CachedScript cs = (CachedScript) obj;
 149             return mainClassName.equals(cs.mainClassName)
 150                     && classBytes.equals(cs.classBytes)
 151                     && Arrays.equals(constants, cs.constants);
 152         }
 153     }
 154 }