001: package net.refractions.udig.core.internal;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.net.URLConnection;
008: import java.net.URLStreamHandler;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Platform;
014: import org.eclipse.core.runtime.Plugin;
015: import org.eclipse.core.runtime.Status;
016: import org.osgi.framework.BundleContext;
017: import org.picocontainer.MutablePicoContainer;
018: import org.picocontainer.defaults.DefaultPicoContainer;
019:
020: /**
021: * PlugIn for net.refractions.udig.core, used by utility classes to access workbench log.
022: *
023: * @author jones
024: * @since 0.3
025: */
026: public class CorePlugin extends Plugin {
027:
028: /** Plugin <code>ID</code> field */
029: public static final String ID = "net.refractions.udig.core"; //$NON-NLS-1$
030: private static CorePlugin plugin;
031:
032: /**
033: * A url stream handler that delegates to the default one but if it doesn't work then it returns null as the stream.
034: */
035: public final static URLStreamHandler RELAXED_HANDLER = new URLStreamHandler() {
036:
037: @Override
038: protected URLConnection openConnection(URL u)
039: throws IOException {
040: try {
041: URL url = new URL(u.toString());
042: return url.openConnection();
043: } catch (MalformedURLException e) {
044: return null;
045: }
046: }
047: };
048:
049: /**
050: * creates a plugin instance
051: */
052: public CorePlugin() {
053: super ();
054: plugin = this ;
055: }
056:
057: /**
058: * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
059: */
060: public void start(BundleContext context) throws Exception {
061: super .start(context);
062:
063: }
064:
065: /**
066: * Returns the system created plugin object
067: *
068: * @return the plugin object
069: */
070: public static CorePlugin getDefault() {
071: return plugin;
072: }
073:
074: private static volatile MutablePicoContainer blackboard = null;
075:
076: /**
077: * This is intended to return the top level Pico Container to use for blackBoarding.
078: * <p>
079: * For most applications, a sub container is required. I recommend the following code be
080: * inserted into your main plugin class. <code>
081: * private static MutablePicoContainer myContainer = null;
082: *
083: * /**
084: * * Gets the container for my plugin.
085: * *
086: * * Make it 'public' if you want to share ... protected otherwise.
087: * * /
088: * public static MutablePicoContainer getMyContainer(){
089: * if(myContainer == null){
090: * // XXXPlugin is the name of the Plugin class
091: * synchronized(XXXPlugin.class){
092: * // check so see that you were not queued for double creation
093: * if(myContainer == null){
094: * // This line does it ... careful to only call it once!
095: * myContainer = CorePlugin.getBlackBoard().makeChildContainer();
096: * }
097: * }
098: * }
099: * return myContainer;
100: * }
101: * </code>
102: * </p>
103: * <p>
104: * NOTE:<br>
105: * Please check to ensure the child you want is not already created (important for two plugins
106: * sharing one container).
107: * </p>
108: *
109: * @return
110: */
111: public static MutablePicoContainer getBlackBoard() {
112: if (blackboard == null) {
113: synchronized (CorePlugin.class) {
114: if (blackboard == null) {
115: blackboard = new DefaultPicoContainer();
116: }
117: }
118: }
119: return blackboard;
120: }
121:
122: /**
123: * Takes a string, and splits it on '\n' and calls stringsToURLs(String[])
124: */
125: public static List<URL> stringsToURLs(String string) {
126: String[] strings = string.split("\n"); //$NON-NLS-1$
127:
128: return stringsToURLs(strings);
129: }
130:
131: /**
132: * Converts each element of an array from a String to a URL. If the String is not a valid URL,
133: * it attempts to load it as a File and then convert it. If that fails, it ignores it. It will
134: * not insert a null into the returning List, so the size of the List may be smaller than the
135: * size of <code>strings</code>
136: *
137: * @param strings an array of strings, each to be converted to a URL
138: * @return a List of URLs, in the same order as the array
139: */
140: public static List<URL> stringsToURLs(String[] strings) {
141: List<URL> urls = new ArrayList<URL>();
142:
143: for (String string : strings) {
144: try {
145: urls.add(new URL(string));
146: } catch (MalformedURLException e) {
147: // not a URL, maybe it is a file
148: try {
149: urls.add(new File(string).toURL());
150: } catch (MalformedURLException e1) {
151: // Not a URL, not a File. nothing to do now.
152: }
153: }
154: }
155: return urls;
156: }
157:
158: /**
159: * Writes an info log in the plugin's log.
160: * <p>
161: * This should be used for user level messages.
162: * </p>
163: */
164: public static void log(String message2, Throwable e) {
165: String message = message2;
166: if (message == null)
167: message = ""; //$NON-NLS-1$
168: getDefault().getLog().log(
169: new Status(IStatus.INFO, ID, 0, message, e));
170: }
171:
172: /**
173: * Messages that only engage if getDefault().isDebugging()
174: * <p>
175: * It is much prefered to do this:
176: *
177: * <pre><code>
178: * private static final String RENDERING = "net.refractions.udig.project/render/trace";
179: * if (ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase(RENDERING)) {
180: * System.out.println("your message here");
181: * }
182: *
183: */
184: public static void trace(String message, Throwable e) {
185: if (getDefault().isDebugging()) {
186: if (message != null)
187: System.out.println(message);
188: if (e != null)
189: e.printStackTrace();
190: }
191: }
192:
193: /**
194: * Performs the Platform.getDebugOption true check on the provided trace
195: * <p>
196: * Note: ProjectUIPlugin.getDefault().isDebugging() must also be on.
197: * <ul>
198: * <li>Trace.RENDER - trace rendering progress
199: * </ul>
200: * </p>
201: *
202: * @param trace currently only RENDER is defined
203: */
204: public static boolean isDebugging(final String trace) {
205: return getDefault().isDebugging()
206: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
207: }
208:
209: public static boolean isDeveloping() {
210: return System.getProperty("UDIG_DEVELOPING") != null; //$NON-NLS-1$
211: }
212: }
|