jdk/src/jdk.runtime/share/classes/sun/tracing/PrintStreamProviderFactory.java
changeset 29037 8a11fed0d1a0
parent 28961 868dc757eab6
parent 29036 63d5aec20b48
child 29038 c2058b635c17
equal deleted inserted replaced
28961:868dc757eab6 29037:8a11fed0d1a0
     1 /*
       
     2  * Copyright (c) 2008, 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 sun.tracing;
       
    27 
       
    28 import java.lang.reflect.Method;
       
    29 import java.io.PrintStream;
       
    30 import java.util.HashMap;
       
    31 
       
    32 import com.sun.tracing.ProviderFactory;
       
    33 import com.sun.tracing.Provider;
       
    34 import com.sun.tracing.ProviderName;
       
    35 import com.sun.tracing.Probe;
       
    36 import com.sun.tracing.ProbeName;
       
    37 
       
    38 /**
       
    39  * Factory class to create tracing Providers.
       
    40  *
       
    41  * This factory will create tracing instances that print to a PrintStream
       
    42  * when activated.
       
    43  *
       
    44  * @since 1.7
       
    45  */
       
    46 public class PrintStreamProviderFactory extends ProviderFactory {
       
    47 
       
    48     private PrintStream stream;
       
    49 
       
    50     public PrintStreamProviderFactory(PrintStream stream) {
       
    51         this.stream = stream;
       
    52     }
       
    53 
       
    54     public <T extends Provider> T createProvider(Class<T> cls) {
       
    55         PrintStreamProvider provider = new PrintStreamProvider(cls, stream);
       
    56         provider.init();
       
    57         return provider.newProxyInstance();
       
    58     }
       
    59 }
       
    60 
       
    61 class PrintStreamProvider extends ProviderSkeleton {
       
    62 
       
    63     private PrintStream stream;
       
    64     private String providerName;
       
    65 
       
    66     protected ProbeSkeleton createProbe(Method m) {
       
    67         String probeName = getAnnotationString(m, ProbeName.class, m.getName());
       
    68         return new PrintStreamProbe(this, probeName, m.getParameterTypes());
       
    69     }
       
    70 
       
    71     PrintStreamProvider(Class<? extends Provider> type, PrintStream stream) {
       
    72         super(type);
       
    73         this.stream = stream;
       
    74         this.providerName = getProviderName();
       
    75     }
       
    76 
       
    77     PrintStream getStream() {
       
    78         return stream;
       
    79     }
       
    80 
       
    81     String getName() {
       
    82         return providerName;
       
    83     }
       
    84 }
       
    85 
       
    86 class PrintStreamProbe extends ProbeSkeleton {
       
    87 
       
    88     private PrintStreamProvider provider;
       
    89     private String name;
       
    90 
       
    91     PrintStreamProbe(PrintStreamProvider p, String name, Class<?>[] params) {
       
    92         super(params);
       
    93         this.provider = p;
       
    94         this.name = name;
       
    95     }
       
    96 
       
    97     public boolean isEnabled() {
       
    98         return true;
       
    99     }
       
   100 
       
   101     public void uncheckedTrigger(Object[] args) {
       
   102         StringBuilder sb = new StringBuilder();
       
   103         sb.append(provider.getName());
       
   104         sb.append(".");
       
   105         sb.append(name);
       
   106         sb.append("(");
       
   107         boolean first = true;
       
   108         for (Object o : args) {
       
   109             if (first == false) {
       
   110                 sb.append(",");
       
   111             } else {
       
   112                 first = false;
       
   113             }
       
   114             sb.append(o.toString());
       
   115         }
       
   116         sb.append(")");
       
   117         provider.getStream().println(sb.toString());
       
   118     }
       
   119 }
       
   120