1 /*
   2  * Copyright (c) 2016, 2018, 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 
  26 package jdk.jfr.internal.handlers;
  27 
  28 import java.io.IOException;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 
  32 import jdk.jfr.EventType;
  33 import jdk.jfr.internal.EventControl;
  34 import jdk.jfr.internal.JVM;
  35 import jdk.jfr.internal.PlatformEventType;
  36 import jdk.jfr.internal.PrivateAccess;
  37 import jdk.jfr.internal.StringPool;
  38 
  39 // Users should not be subclass for security reasons.
  40 public abstract class EventHandler {
  41     // Accessed by generated sub class
  42     protected final PlatformEventType platformEventType;
  43 
  44     private final EventType eventType;
  45     private final EventControl eventControl;
  46 
  47     // Accessed by generated sub class
  48     protected EventHandler(boolean registered, EventType eventType, EventControl eventControl) {
  49         if (System.getSecurityManager() != null) {
  50             // Do not allow user subclasses when security is enforced.
  51             if (EventHandler.class.getClassLoader() != this.getClass().getClassLoader()) {
  52                 throw new SecurityException("Illegal subclass");
  53             }
  54         }
  55         this.eventType = eventType;
  56         this.platformEventType = PrivateAccess.getInstance().getPlatformEventType(eventType);
  57         this.eventControl = eventControl;
  58         platformEventType.setRegistered(registered);
  59     }
  60 
  61     final protected StringPool createStringFieldWriter() {
  62         return new StringPool();
  63     }
  64 
  65     // Accessed by generated code in event class
  66     public final boolean shouldCommit(long duration) {
  67         return isEnabled() && duration >= platformEventType.getThresholdTicks();
  68     }
  69 
  70     // Accessed by generated code in event class
  71     // Accessed by generated sub class
  72     public final boolean isEnabled() {
  73         return platformEventType.isCommitable();
  74     }
  75 
  76     public final EventType getEventType() {
  77         return eventType;
  78     }
  79 
  80     public final PlatformEventType getPlatformEventType() {
  81         return platformEventType;
  82     }
  83 
  84     public final EventControl getEventControl() {
  85         return eventControl;
  86     }
  87 
  88     public static long timestamp() {
  89         return JVM.counterTime();
  90     }
  91 
  92     public static long duration(long startTime) {
  93         if (startTime == 0) {
  94             // User forgot to invoke begin, or instrumentation was
  95             // added after the user invoked begin.
  96             // Returning 0 will make it an instant event
  97             return 0;
  98         }
  99         return timestamp() - startTime;
 100     }
 101 
 102     // Prevent a malicious user from instantiating a generated event handlers.
 103     @Override
 104     public final Object clone() throws java.lang.CloneNotSupportedException {
 105         throw new CloneNotSupportedException();
 106     }
 107 
 108     private final void writeObject(ObjectOutputStream out) throws IOException {
 109         throw new IOException("Object cannot be serialized");
 110     }
 111 
 112     private final void readObject(ObjectInputStream in) throws IOException {
 113         throw new IOException("Class cannot be deserialized");
 114     }
 115 
 116     public boolean isRegistered() {
 117         return platformEventType.isRegistered();
 118     }
 119 
 120     public boolean setRegistered(boolean registered) {
 121        return platformEventType.setRegistered(registered);
 122     }
 123 }