Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ea3685e8133fcb70095398592cda8c3e3b4f5eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/***************************************************************************
 * Copyright (c) 2004, 2005, 2006 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.eclipse;


import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;

import org.apache.log4j.Logger;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import java.io.IOException;

import java.net.URL;


/**
 * The main plugin class to be used in the desktop.
 */
public abstract class AbstractPlugin extends Plugin implements ClassLoaderFactory
{
  /**
   * Logger for this class
   */
  private final Logger logger = Logger.getLogger(getClass());

  private String pluginId;

  private ResourceBundle resourceBundle;

  private BundleContext bundleContext;

  /**
   * The constructor.
   */
  protected AbstractPlugin()
  {
  }

  public String getPluginId()
  {
    return pluginId;
  }

  public ClassLoader getClassLoader()
  {
    return getClass().getClassLoader();
  }

  public Logger getLogger()
  {
    return logger;
  }

  /**
   * This method is called upon plug-in activation
   */
  public void start(BundleContext context) throws Exception
  {
    super.start(context);
    bundleContext = context;
    pluginId = context.getBundle().getSymbolicName();

    try
    {
      if (resourceBundle == null)
      {
        try
        {
          resourceBundle = ResourceBundle.getBundle(pluginId);
        }
        catch (MissingResourceException x)
        {
          resourceBundle = null;
        }
      }

      if (logger.isDebugEnabled()) logger.debug("ENTER STARTUP");
      doStart();
    }
    catch (Throwable t)
    {
      logger.error("Startup error", t);
    }
    finally
    {
      if (logger.isDebugEnabled()) logger.debug("EXIT STARTUP");
    }
  }

  /**
   * This method is called upon plug-in deactivation
   */
  public void stop(BundleContext context) throws Exception
  {
    try
    {
      if (logger.isDebugEnabled()) logger.debug("ENTER SHUTDOWN");
      doStop();
    }
    catch (Throwable t)
    {
      logger.error("Shutdown error", t);
    }
    finally
    {
      if (logger.isDebugEnabled()) logger.debug("EXIT SHUTDOWN");
    }

    super.stop(context);
    resourceBundle = null;
  }

  /**
   * 
   */
  protected void doStart() throws Exception
  {
  }

  /**
   * 
   */
  protected void doStop() throws Exception
  {
  }

  /**
   * Returns the string from the plugin's resource bundle,
   * or 'key' if not found.
   */
  public String getResourceString(String key)
  {
    ResourceBundle bundle = getResourceBundle();

    try
    {
      return (bundle != null) ? bundle.getString(key) : key;
    }
    catch (MissingResourceException e)
    {
      return key;
    }
  }

  /**
   * Returns the plugin's resource bundle,
   */
  public ResourceBundle getResourceBundle()
  {
    return resourceBundle;
  }

  /**
   * @return Returns the bundleContext.
   */
  public BundleContext getBundleContext()
  {
    return bundleContext;
  }

  public String getBundleLocation() throws IOException
  {
    Bundle bundle = bundleContext.getBundle();
    return getBundleLocation(bundle);
  }

  /**
   * Progress monitor helpers
   */
  public static void checkCanceled(IProgressMonitor monitor)
  {
    if (monitor.isCanceled()) cancelOperation();
  }

  public static void cancelOperation()
  {
    throw new OperationCanceledException();
  }

  public static IProgressMonitor monitorFor(IProgressMonitor monitor)
  {
    if (monitor == null) return new NullProgressMonitor();
    return monitor;
  }

  public static IProgressMonitor subMonitorFor(IProgressMonitor monitor, int ticks)
  {
    if (monitor == null) return new NullProgressMonitor();
    if (monitor instanceof NullProgressMonitor) return monitor;
    return new SubProgressMonitor(monitor, ticks);
  }

  public static IProgressMonitor subMonitorFor(IProgressMonitor monitor, int ticks, int style)
  {
    if (monitor == null) return new NullProgressMonitor();
    if (monitor instanceof NullProgressMonitor) return monitor;
    return new SubProgressMonitor(monitor, ticks, style);
  }

  public static String getBundleLocation(Bundle bundle) throws IOException
  {
    URL url = bundle.getEntry("/");
    return FileLocator.toFileURL(url).getFile();
  }

  public void debug(String message)
  {
    logger.debug(message);
    getLog().log(new Status(IStatus.INFO, pluginId, IStatus.OK, message, null));
  }

  public void warn(String message)
  {
    warn(message, null);
  }

  public void error(String message)
  {
    error(message, null);
  }

  public void warn(Throwable t)
  {
    warn(t.getMessage(), t);
  }

  public void error(Throwable t)
  {
    error(t.getMessage(), t);
  }

  public void warn(String message, Throwable t)
  {
    logger.warn(message, t);
    getLog().log(new Status(IStatus.WARNING, pluginId, IStatus.OK, message, t));
  }

  public void error(String message, Throwable t)
  {
    logger.error(message, t);
    getLog().log(new Status(IStatus.ERROR, pluginId, IStatus.OK, message, t));
  }
}

Back to the top