< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CodeStore.java

Print this page




  37 import java.security.AccessControlException;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedActionException;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.util.Iterator;
  42 import java.util.Map;
  43 import java.util.ServiceLoader;
  44 import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
  45 import jdk.nashorn.internal.codegen.types.Type;
  46 import jdk.nashorn.internal.runtime.logging.DebugLogger;
  47 import jdk.nashorn.internal.runtime.logging.Loggable;
  48 import jdk.nashorn.internal.runtime.logging.Logger;
  49 import jdk.nashorn.internal.runtime.options.Options;
  50 
  51 /**
  52  * A code cache for persistent caching of compiled scripts.
  53  */
  54 @Logger(name="codestore")
  55 public abstract class CodeStore implements Loggable {
  56 
  57     /**
  58      * Permission needed to provide a CodeStore instance via ServiceLoader.
  59      */
  60     public final static String NASHORN_PROVIDE_CODE_STORE = "nashorn.provideCodeStore";
  61 
  62     private DebugLogger log;
  63 
  64     /**
  65      * Constructor
  66      */
  67     protected CodeStore() {
  68     }
  69 
  70     @Override
  71     public DebugLogger initLogger(final Context context) {
  72         log = context.getLogger(getClass());
  73         return log;
  74     }
  75 
  76     @Override
  77     public DebugLogger getLogger() {
  78         return log;
  79     }
  80 
  81     /**
  82      * Returns a new code store instance.
  83      *
  84      * @param context the current context
  85      * @return The instance, or null if code store could not be created
  86      */
  87     public static CodeStore newCodeStore(final Context context) {
  88         final Class<CodeStore> baseClass = CodeStore.class;
  89         try {
  90             // security check first
  91             final SecurityManager sm = System.getSecurityManager();
  92             if (sm != null) {
  93                 sm.checkPermission(new RuntimePermission(NASHORN_PROVIDE_CODE_STORE));
  94             }
  95             final ServiceLoader<CodeStore> services = ServiceLoader.load(baseClass);
  96             final Iterator<CodeStore> iterator = services.iterator();
  97             if (iterator.hasNext()) {
  98                 final CodeStore store = iterator.next();
  99                 store.initLogger(context).info("using code store provider ", store.getClass().getCanonicalName());
 100                 return store;
 101             }
 102         } catch (final AccessControlException e) {
 103             context.getLogger(CodeStore.class).warning("failed to load code store provider ", e);
 104         }
 105         try {
 106             final CodeStore store = new DirectoryCodeStore(context);
 107             store.initLogger(context);
 108             return store;
 109         } catch (final IOException e) {
 110             context.getLogger(CodeStore.class).warning("failed to create cache directory ", e);
 111             return null;
 112         }
 113     }
 114 
 115 
 116     /**
 117      * Store a compiled script in the cache.
 118      *
 119      * @param functionKey   the function key
 120      * @param source        the source
 121      * @param mainClassName the main class name
 122      * @param classBytes    a map of class bytes
 123      * @param initializers  the function initializers
 124      * @param constants     the constants array




  37 import java.security.AccessControlException;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedActionException;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.util.Iterator;
  42 import java.util.Map;
  43 import java.util.ServiceLoader;
  44 import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
  45 import jdk.nashorn.internal.codegen.types.Type;
  46 import jdk.nashorn.internal.runtime.logging.DebugLogger;
  47 import jdk.nashorn.internal.runtime.logging.Loggable;
  48 import jdk.nashorn.internal.runtime.logging.Logger;
  49 import jdk.nashorn.internal.runtime.options.Options;
  50 
  51 /**
  52  * A code cache for persistent caching of compiled scripts.
  53  */
  54 @Logger(name="codestore")
  55 public abstract class CodeStore implements Loggable {
  56 





  57     private DebugLogger log;
  58 
  59     /**
  60      * Constructor
  61      */
  62     protected CodeStore() {
  63     }
  64 
  65     @Override
  66     public DebugLogger initLogger(final Context context) {
  67         log = context.getLogger(getClass());
  68         return log;
  69     }
  70 
  71     @Override
  72     public DebugLogger getLogger() {
  73         return log;
  74     }
  75 
  76     /**
  77      * Returns a new code store instance.
  78      *
  79      * @param context the current context
  80      * @return The instance, or null if code store could not be created
  81      */
  82     public static CodeStore newCodeStore(final Context context) {

















  83         try {
  84             final CodeStore store = new DirectoryCodeStore(context);
  85             store.initLogger(context);
  86             return store;
  87         } catch (final IOException e) {
  88             context.getLogger(CodeStore.class).warning("failed to create cache directory ", e);
  89             return null;
  90         }
  91     }
  92 
  93 
  94     /**
  95      * Store a compiled script in the cache.
  96      *
  97      * @param functionKey   the function key
  98      * @param source        the source
  99      * @param mainClassName the main class name
 100      * @param classBytes    a map of class bytes
 101      * @param initializers  the function initializers
 102      * @param constants     the constants array


< prev index next >