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