001: package net.refractions.udig.catalog.internal.wfs;
002:
003: import java.util.MissingResourceException;
004: import java.util.ResourceBundle;
005: import java.util.logging.ConsoleHandler;
006: import java.util.logging.Handler;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import org.eclipse.core.runtime.IStatus;
011: import org.eclipse.core.runtime.Platform;
012: import org.eclipse.core.runtime.Status;
013: import org.eclipse.ui.plugin.AbstractUIPlugin;
014: import org.geotools.data.wfs.WFSDataStore;
015: import org.geotools.xml.gml.GMLSchema;
016: import org.osgi.framework.BundleContext;
017:
018: /**
019: * The main plugin class to be used in the desktop.
020: */
021: public class WfsPlugin extends AbstractUIPlugin {
022: //The shared instance.
023: private static WfsPlugin plugin;
024: //Resource bundle.
025: private ResourceBundle resourceBundle;
026: public static final String ID = "net.refractions.udig.catalog.internal.wfs"; //$NON-NLS-1$
027:
028: /**
029: * The constructor.
030: */
031: public WfsPlugin() {
032: super ();
033: plugin = this ;
034: }
035:
036: /**
037: * This method is called upon plug-in activation
038: */
039: public void start(BundleContext context) throws Exception {
040: super .start(context);
041: ClassLoader current = getClass().getClassLoader();
042: try {
043: Thread.currentThread().setContextClassLoader(
044: WFSDataStore.class.getClassLoader());
045: setLoggerLevel(
046: "org.geotools.data.wfs", "net.refractions.udig.catalog.wfs/debug/wfs"); //$NON-NLS-1$ //$NON-NLS-2$
047: setLoggerLevel(
048: "org.geotools.xml", "net.refractions.udig.catalog.wfs/debug/wfs"); //$NON-NLS-1$ //$NON-NLS-2$
049: setLoggerLevel(
050: "org.geotools.xml.sax", "net.refractions.udig.catalog.wfs/debug/wfs"); //$NON-NLS-1$ //$NON-NLS-2$
051: if (isDebugging("net.refractions.udig.catalog.wfs/debug/gml")) //$NON-NLS-1$
052: GMLSchema.setLogLevel(Level.FINE);
053: else
054: GMLSchema.setLogLevel(Level.SEVERE);
055: } finally {
056: Thread.currentThread().setContextClassLoader(current);
057: }
058: }
059:
060: private void setLoggerLevel(String loggerID, String traceID) {
061: Logger logger = Logger.getLogger(loggerID);
062: if (isDebugging(traceID))
063: logger.setLevel(Level.FINE);
064: else
065: logger.setLevel(Level.SEVERE);
066: ConsoleHandler consoleHandler = new ConsoleHandler();
067: consoleHandler.setLevel(Level.ALL);
068: logger.addHandler(consoleHandler);
069: }
070:
071: /**
072: * This method is called when the plug-in is stopped
073: */
074: public void stop(BundleContext context) throws Exception {
075: plugin = null;
076: resourceBundle = null;
077: super .stop(context);
078: }
079:
080: /**
081: * Returns the shared instance.
082: */
083: public static WfsPlugin getDefault() {
084: return plugin;
085: }
086:
087: /**
088: * Returns the string from the plugin's resource bundle,
089: * or 'key' if not found.
090: */
091: public static String getResourceString(String key) {
092: ResourceBundle bundle = WfsPlugin.getDefault()
093: .getResourceBundle();
094: try {
095: return (bundle != null) ? bundle.getString(key) : key;
096: } catch (MissingResourceException e) {
097: return key;
098: }
099: }
100:
101: /**
102: * Returns the plugin's resource bundle,
103: */
104: public ResourceBundle getResourceBundle() {
105: try {
106: if (resourceBundle == null)
107: resourceBundle = ResourceBundle
108: .getBundle("net.refractions.udig.catalog.internal.wfs.WfsPluginResources"); //$NON-NLS-1$
109: } catch (MissingResourceException x) {
110: resourceBundle = null;
111: }
112: return resourceBundle;
113: }
114:
115: /**
116: * Logs the Throwable in the plugin's log.
117: * <p>
118: * This will be a user visable ERROR iff:
119: * <ul>
120: * <li>t is an Exception we are assuming it is human readable or if a message is provided
121: * </ul>
122: * </p>
123: * @param message
124: * @param t
125: */
126: public static void log(String message, Throwable t) {
127: int status = t instanceof Exception || message != null ? IStatus.ERROR
128: : IStatus.WARNING;
129: getDefault().getLog().log(
130: new Status(status, ID, IStatus.OK, message, t));
131: }
132:
133: /**
134: * Messages that only engage if getDefault().isDebugging()
135: * <p>
136: * It is much prefered to do this:<pre><code>
137: * private static final String RENDERING = "net.refractions.udig.project/render/trace";
138: * if( ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase( RENDERING ) ){
139: * System.out.println( "your message here" );
140: * }
141: * </code></pre>
142: * </p>
143: * @param message
144: * @param e
145: */
146: public static void trace(String message, Throwable e) {
147: if (getDefault().isDebugging()) {
148: if (message != null)
149: System.out.println(message);
150: if (e != null)
151: e.printStackTrace();
152: }
153: }
154:
155: /**
156: * Performs the Platform.getDebugOption true check on the provided trace
157: * <p>
158: * Note: ProjectUIPlugin.getDefault().isDebugging() must also be on.
159: * <ul>
160: * <li>Trace.RENDER - trace rendering progress
161: * </ul>
162: * </p>
163: * @param trace currently only RENDER is defined
164: * @return true if -debug is on for this plugin
165: */
166: public static boolean isDebugging(final String trace) {
167: return getDefault().isDebugging()
168: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
169: }
170: }
|