1 /*
   2  * Copyright (c) 1999, 2012, 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 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.bdi;
  36 
  37 import com.sun.jdi.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42 class EventRequestSpecList {
  43 
  44     // all specs
  45     private List<EventRequestSpec> eventRequestSpecs = Collections.synchronizedList(
  46                                                   new ArrayList<EventRequestSpec>());
  47 
  48     final ExecutionManager runtime;
  49 
  50     EventRequestSpecList(ExecutionManager runtime) {
  51         this.runtime = runtime;
  52     }
  53 
  54     /**
  55      * Resolve all deferred eventRequests waiting for 'refType'.
  56      */
  57     void resolve(ReferenceType refType) {
  58         synchronized(eventRequestSpecs) {
  59             for (EventRequestSpec spec : eventRequestSpecs) {
  60                 spec.attemptResolve(refType);
  61              }
  62         }
  63     }
  64 
  65     void install(EventRequestSpec ers, VirtualMachine vm) {
  66         synchronized (eventRequestSpecs) {
  67             eventRequestSpecs.add(ers);
  68         }
  69         if (vm != null) {
  70             ers.attemptImmediateResolve(vm);
  71         }
  72     }
  73 
  74     BreakpointSpec
  75     createClassLineBreakpoint(String classPattern, int line) {
  76         ReferenceTypeSpec refSpec =
  77             new PatternReferenceTypeSpec(classPattern);
  78         return new LineBreakpointSpec(this, refSpec, line);
  79     }
  80 
  81     BreakpointSpec
  82     createSourceLineBreakpoint(String sourceName, int line) {
  83         ReferenceTypeSpec refSpec =
  84             new SourceNameReferenceTypeSpec(sourceName, line);
  85         return new LineBreakpointSpec(this, refSpec, line);
  86     }
  87 
  88     BreakpointSpec
  89     createMethodBreakpoint(String classPattern,
  90                            String methodId, List<String> methodArgs) {
  91         ReferenceTypeSpec refSpec =
  92             new PatternReferenceTypeSpec(classPattern);
  93         return new MethodBreakpointSpec(this, refSpec,
  94                                         methodId, methodArgs);
  95     }
  96 
  97     ExceptionSpec
  98     createExceptionIntercept(String classPattern,
  99                              boolean notifyCaught,
 100                              boolean notifyUncaught) {
 101         ReferenceTypeSpec refSpec =
 102             new PatternReferenceTypeSpec(classPattern);
 103         return new ExceptionSpec(this, refSpec,
 104                                  notifyCaught, notifyUncaught);
 105     }
 106 
 107     AccessWatchpointSpec
 108     createAccessWatchpoint(String classPattern, String fieldId) {
 109         ReferenceTypeSpec refSpec =
 110             new PatternReferenceTypeSpec(classPattern);
 111         return new AccessWatchpointSpec(this, refSpec, fieldId);
 112     }
 113 
 114     ModificationWatchpointSpec
 115     createModificationWatchpoint(String classPattern, String fieldId) {
 116         ReferenceTypeSpec refSpec =
 117             new PatternReferenceTypeSpec(classPattern);
 118         return new ModificationWatchpointSpec(this, refSpec, fieldId);
 119     }
 120 
 121     void delete(EventRequestSpec ers) {
 122         EventRequest request = ers.getEventRequest();
 123         synchronized (eventRequestSpecs) {
 124             eventRequestSpecs.remove(ers);
 125         }
 126         if (request != null) {
 127             request.virtualMachine().eventRequestManager()
 128                 .deleteEventRequest(request);
 129         }
 130         notifyDeleted(ers);
 131         //### notify delete - here?
 132     }
 133 
 134     List<EventRequestSpec> eventRequestSpecs() {
 135         // We need to make a copy to avoid synchronization problems
 136         synchronized (eventRequestSpecs) {
 137             return new ArrayList<EventRequestSpec>(eventRequestSpecs);
 138         }
 139     }
 140 
 141     // --------  notify routines --------------------
 142 
 143     private ArrayList<SpecListener> specListeners() {
 144         return runtime.specListeners.clone();
 145     }
 146 
 147     void notifySet(EventRequestSpec spec) {
 148         List<SpecListener> l = specListeners();
 149         SpecEvent evt = new SpecEvent(spec);
 150         for (int i = 0; i < l.size(); i++) {
 151             spec.notifySet(l.get(i), evt);
 152         }
 153     }
 154 
 155     void notifyDeferred(EventRequestSpec spec) {
 156         List<SpecListener> l = specListeners();
 157         SpecEvent evt = new SpecEvent(spec);
 158         for (int i = 0; i < l.size(); i++) {
 159             spec.notifyDeferred(l.get(i), evt);
 160         }
 161     }
 162 
 163     void notifyDeleted(EventRequestSpec spec) {
 164         List<SpecListener> l = specListeners();
 165         SpecEvent evt = new SpecEvent(spec);
 166         for (int i = 0; i < l.size(); i++) {
 167             spec.notifyDeleted(l.get(i), evt);
 168         }
 169     }
 170 
 171     void notifyResolved(EventRequestSpec spec) {
 172         List<SpecListener> l = specListeners();
 173         SpecEvent evt = new SpecEvent(spec);
 174         for (int i = 0; i < l.size(); i++) {
 175             spec.notifyResolved(l.get(i), evt);
 176         }
 177     }
 178 
 179     void notifyError(EventRequestSpec spec, Exception exc) {
 180         List<SpecListener> l = specListeners();
 181         SpecErrorEvent evt = new SpecErrorEvent(spec, exc);
 182         for (int i = 0; i < l.size(); i++) {
 183             spec.notifyError(l.get(i), evt);
 184         }
 185     }
 186 }