KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > apt > pluggable > core > Apt6Plugin


1 /*******************************************************************************
2  * Copyright (c) 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.apt.pluggable.core;
12
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.Plugin;
19 import org.eclipse.core.runtime.Status;
20 import org.osgi.framework.BundleContext;
21
22 /**
23  * The plug-in responsible for dispatch of Java 6 (JSR269 Pluggable Annotation
24  * Processing API) annotation processors in the IDE.
25  * This is named Apt6Plugin to distinguish it from AptPlugin, which is responsible
26  * for Java 5 (com.sun.mirror) processors.
27  */

28 public class Apt6Plugin extends Plugin {
29
30     private static final SimpleDateFormat JavaDoc TRACE_DATE_FORMAT = new SimpleDateFormat JavaDoc("HH:mm:ss.SSS"); //$NON-NLS-1$
31

32     public static final String JavaDoc PLUGIN_ID = "org.eclipse.jdt.apt.pluggable.core"; //$NON-NLS-1$
33

34     /**
35      * Status IDs for system log entries. Must be unique per plugin.
36      */

37     public static final int STATUS_EXCEPTION = 1;
38
39     // Tracing options
40
public static boolean DEBUG = false;
41     public final static String JavaDoc APT_DEBUG_OPTION = Apt6Plugin.PLUGIN_ID + "/debug"; //$NON-NLS-1$
42

43     private static Apt6Plugin thePlugin = null; // singleton object
44

45     public Apt6Plugin() {
46     }
47
48     @Override JavaDoc
49     public void start(BundleContext context) throws Exception JavaDoc {
50         super.start(context);
51         thePlugin = this;
52         initDebugTracing();
53     }
54     
55     private void initDebugTracing() {
56         String JavaDoc option = Platform.getDebugOption(APT_DEBUG_OPTION);
57         if (option != null) {
58             DEBUG = option.equalsIgnoreCase("true"); //$NON-NLS-1$
59
}
60     }
61     
62     public static Apt6Plugin getPlugin() {
63         return thePlugin;
64     }
65
66     /**
67      * Log a status message to the platform log. Use this for reporting exceptions.
68      * @param status
69      */

70     public static void log(IStatus status) {
71         thePlugin.getLog().log(status);
72     }
73     
74     /**
75      * Convenience wrapper around log(IStatus), to log an exception
76      * with severity of ERROR.
77      */

78     public static void log(Throwable JavaDoc e, String JavaDoc message) {
79         log(new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e));
80     }
81     
82     /**
83      * Convenience wrapper around log(IStatus), to log an exception
84      * with severity of WARNING.
85      */

86     public static void logWarning(Throwable JavaDoc e, String JavaDoc message) {
87         log(createWarningStatus(e, message));
88     }
89     
90     /**
91      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
92      * with severity of ERROR.
93      */

94     public static Status createStatus(Throwable JavaDoc e, String JavaDoc message) {
95         return new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e);
96     }
97     
98     /**
99      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
100      * with severity of WARNING.
101      */

102     public static Status createWarningStatus(Throwable JavaDoc e, String JavaDoc message) {
103         return new Status(IStatus.WARNING, PLUGIN_ID, STATUS_EXCEPTION, message, e);
104     }
105     
106     /**
107      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
108      * with severity of INFO.
109      */

110     public static Status createInfoStatus(Throwable JavaDoc e, String JavaDoc message) {
111         return new Status(IStatus.INFO, PLUGIN_ID, STATUS_EXCEPTION, message, e);
112     }
113     
114     public static void trace(final String JavaDoc msg) {
115         if (DEBUG) {
116             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117             sb.append('[');
118             // SimpleDateFormat is not thread-safe, according to javadoc
119
synchronized (TRACE_DATE_FORMAT) {
120                 sb.append(TRACE_DATE_FORMAT.format(new Date JavaDoc()));
121             }
122             sb.append('-');
123             // Some threads have qualified type names; too long.
124
String JavaDoc threadName = Thread.currentThread().getName();
125             int dot = threadName.lastIndexOf('.');
126             if (dot < 0) {
127                 sb.append(threadName);
128             } else {
129                 sb.append(threadName.substring(dot + 1));
130             }
131             sb.append(']');
132             sb.append(msg);
133             System.out.println(sb);
134         }
135     }
136
137 }
138
Popular Tags