langtools/src/jdk.jshell/share/classes/jdk/jshell/JDIEventHandler.java
changeset 33362 65ec6de1d6b4
child 34752 9c262a013456
equal deleted inserted replaced
33361:1c96344ecd49 33362:65ec6de1d6b4
       
     1 /*
       
     2  * Copyright (c) 1998, 2011, 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 
       
    26 package jdk.jshell;
       
    27 
       
    28 import com.sun.jdi.*;
       
    29 import com.sun.jdi.event.*;
       
    30 
       
    31 /**
       
    32  * Handler of Java Debug Interface events.
       
    33  * Adapted from jdb EventHandler; Handling of events not used by JShell stubbed out.
       
    34  */
       
    35 class JDIEventHandler implements Runnable {
       
    36 
       
    37     Thread thread;
       
    38     volatile boolean connected = true;
       
    39     boolean completed = false;
       
    40     String shutdownMessageKey;
       
    41     final JDIEnv env;
       
    42 
       
    43     JDIEventHandler(JDIEnv env) {
       
    44         this.env = env;
       
    45         this.thread = new Thread(this, "event-handler");
       
    46         this.thread.start();
       
    47     }
       
    48 
       
    49     synchronized void shutdown() {
       
    50         connected = false;  // force run() loop termination
       
    51         thread.interrupt();
       
    52         while (!completed) {
       
    53             try {wait();} catch (InterruptedException exc) {}
       
    54         }
       
    55     }
       
    56 
       
    57     @Override
       
    58     public void run() {
       
    59         EventQueue queue = env.vm().eventQueue();
       
    60         while (connected) {
       
    61             try {
       
    62                 EventSet eventSet = queue.remove();
       
    63                 boolean resumeStoppedApp = false;
       
    64                 EventIterator it = eventSet.eventIterator();
       
    65                 while (it.hasNext()) {
       
    66                     resumeStoppedApp |= handleEvent(it.nextEvent());
       
    67                 }
       
    68 
       
    69                 if (resumeStoppedApp) {
       
    70                     eventSet.resume();
       
    71                 }
       
    72             } catch (InterruptedException exc) {
       
    73                 // Do nothing. Any changes will be seen at top of loop.
       
    74             } catch (VMDisconnectedException discExc) {
       
    75                 handleDisconnectedException();
       
    76                 break;
       
    77             }
       
    78         }
       
    79         synchronized (this) {
       
    80             completed = true;
       
    81             notifyAll();
       
    82         }
       
    83     }
       
    84 
       
    85     private boolean handleEvent(Event event) {
       
    86         if (event instanceof ExceptionEvent) {
       
    87             exceptionEvent(event);
       
    88         } else if (event instanceof WatchpointEvent) {
       
    89             fieldWatchEvent(event);
       
    90         } else if (event instanceof MethodEntryEvent) {
       
    91             methodEntryEvent(event);
       
    92         } else if (event instanceof MethodExitEvent) {
       
    93             methodExitEvent(event);
       
    94         } else if (event instanceof ClassPrepareEvent) {
       
    95             classPrepareEvent(event);
       
    96         } else if (event instanceof ThreadStartEvent) {
       
    97             threadStartEvent(event);
       
    98         } else if (event instanceof ThreadDeathEvent) {
       
    99             threadDeathEvent(event);
       
   100         } else if (event instanceof VMStartEvent) {
       
   101             vmStartEvent(event);
       
   102             return true;
       
   103         } else {
       
   104             handleExitEvent(event);
       
   105         }
       
   106         return true;
       
   107     }
       
   108 
       
   109     private boolean vmDied = false;
       
   110 
       
   111     private void handleExitEvent(Event event) {
       
   112         if (event instanceof VMDeathEvent) {
       
   113             vmDied = true;
       
   114             shutdownMessageKey = "The application exited";
       
   115         } else if (event instanceof VMDisconnectEvent) {
       
   116             connected = false;
       
   117             if (!vmDied) {
       
   118                 shutdownMessageKey = "The application has been disconnected";
       
   119             }
       
   120         } else {
       
   121             throw new InternalError("Unexpected event type: " +
       
   122                     event.getClass());
       
   123         }
       
   124         env.shutdown();
       
   125     }
       
   126 
       
   127     synchronized void handleDisconnectedException() {
       
   128         /*
       
   129          * A VMDisconnectedException has happened while dealing with
       
   130          * another event. We need to flush the event queue, dealing only
       
   131          * with exit events (VMDeath, VMDisconnect) so that we terminate
       
   132          * correctly.
       
   133          */
       
   134         EventQueue queue = env.vm().eventQueue();
       
   135         while (connected) {
       
   136             try {
       
   137                 EventSet eventSet = queue.remove();
       
   138                 EventIterator iter = eventSet.eventIterator();
       
   139                 while (iter.hasNext()) {
       
   140                     handleExitEvent(iter.next());
       
   141                 }
       
   142             } catch (InterruptedException exc) {
       
   143                 // ignore
       
   144             } catch (InternalError exc) {
       
   145                 // ignore
       
   146             }
       
   147         }
       
   148     }
       
   149 
       
   150     private void vmStartEvent(Event event)  {
       
   151         VMStartEvent se = (VMStartEvent)event;
       
   152     }
       
   153 
       
   154     private void methodEntryEvent(Event event)  {
       
   155         MethodEntryEvent me = (MethodEntryEvent)event;
       
   156     }
       
   157 
       
   158     private void methodExitEvent(Event event)  {
       
   159         MethodExitEvent me = (MethodExitEvent)event;
       
   160     }
       
   161 
       
   162     private void fieldWatchEvent(Event event)  {
       
   163         WatchpointEvent fwe = (WatchpointEvent)event;
       
   164     }
       
   165 
       
   166     private void classPrepareEvent(Event event)  {
       
   167         ClassPrepareEvent cle = (ClassPrepareEvent)event;
       
   168     }
       
   169 
       
   170     private void exceptionEvent(Event event) {
       
   171         ExceptionEvent ee = (ExceptionEvent)event;
       
   172     }
       
   173 
       
   174     private void threadDeathEvent(Event event) {
       
   175         ThreadDeathEvent tee = (ThreadDeathEvent)event;
       
   176     }
       
   177 
       
   178     private void threadStartEvent(Event event) {
       
   179         ThreadStartEvent tse = (ThreadStartEvent)event;
       
   180     }
       
   181 }