Commit | Line | Data |
---|---|---|
e110fed1 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.common.core; | |
14 | ||
4bcdb26a | 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString; |
0b1faf49 AM |
16 | |
17 | import java.util.Collections; | |
e110fed1 AM |
18 | import java.util.HashMap; |
19 | import java.util.Map; | |
20 | ||
21 | import org.eclipse.core.runtime.IStatus; | |
22 | import org.eclipse.core.runtime.Plugin; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.jdt.annotation.Nullable; | |
25 | import org.osgi.framework.BundleContext; | |
26 | ||
27 | /** | |
28 | * The activator class controls the plug-in life cycle | |
29 | * | |
30 | * @author Alexandre Montplaisir | |
31 | */ | |
32 | public abstract class TraceCompassActivator extends Plugin { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
38 | /** Map of all the registered activators, indexed by plugin ID */ | |
39 | private static final Map<String, TraceCompassActivator> ACTIVATORS = | |
0e4f957e | 40 | Collections.synchronizedMap(new HashMap<String, TraceCompassActivator>()); |
e110fed1 AM |
41 | |
42 | /** This instance's plug-in ID */ | |
43 | private final String fPluginId; | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Constructors | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | /** | |
50 | * The constructor | |
51 | * | |
52 | * @param pluginID | |
53 | * The ID of the plugin | |
54 | */ | |
55 | public TraceCompassActivator(String pluginID) { | |
56 | fPluginId = pluginID; | |
57 | } | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Accessors | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | /** | |
64 | * Return this plug-in's ID. | |
65 | * | |
66 | * @return The plug-in ID | |
67 | */ | |
68 | public String getPluginId() { | |
69 | return fPluginId; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Get a registered activator. Subclasses should implement their own public | |
74 | * getInstance() method, which returns the result of this. | |
75 | * | |
76 | * @param id | |
77 | * The activator's plugin ID | |
78 | * @return The corresponding activator | |
79 | */ | |
80 | protected static TraceCompassActivator getInstance(String id) { | |
81 | TraceCompassActivator ret = ACTIVATORS.get(id); | |
82 | if (ret == null) { | |
83 | /* The activator should be registered at this point! */ | |
84 | throw new IllegalStateException(); | |
85 | } | |
86 | return ret; | |
87 | } | |
88 | ||
89 | // ------------------------------------------------------------------------ | |
90 | // Abstract methods | |
91 | // ------------------------------------------------------------------------ | |
92 | ||
93 | /** | |
94 | * Additional actions to run at the plug-in startup | |
95 | */ | |
96 | protected abstract void startActions(); | |
97 | ||
98 | /** | |
99 | * Additional actions to run at the plug-in shtudown | |
100 | */ | |
101 | protected abstract void stopActions(); | |
102 | ||
103 | // ------------------------------------------------------------------------ | |
104 | // ore.eclipse.core.runtime.Plugin | |
105 | // ------------------------------------------------------------------------ | |
106 | ||
107 | @Override | |
108 | public final void start(@Nullable BundleContext context) throws Exception { | |
109 | super.start(context); | |
110 | String id = this.getPluginId(); | |
0b1faf49 AM |
111 | synchronized (ACTIVATORS) { |
112 | if (ACTIVATORS.containsKey(id)) { | |
113 | logError("Duplicate Activator ID : " + id); //$NON-NLS-1$ | |
114 | } | |
115 | ACTIVATORS.put(id, this); | |
e110fed1 | 116 | } |
e110fed1 AM |
117 | startActions(); |
118 | } | |
119 | ||
120 | @Override | |
121 | public final void stop(@Nullable BundleContext context) throws Exception { | |
122 | stopActions(); | |
123 | ACTIVATORS.remove(this.getPluginId()); | |
124 | super.stop(context); | |
125 | } | |
126 | ||
127 | // ------------------------------------------------------------------------ | |
128 | // Logging helpers | |
129 | // ------------------------------------------------------------------------ | |
130 | ||
131 | /** | |
132 | * Logs a message with severity INFO in the runtime log of the plug-in. | |
133 | * | |
134 | * @param message | |
135 | * A message to log | |
136 | */ | |
4bcdb26a AM |
137 | public void logInfo(@Nullable String message) { |
138 | getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message))); | |
e110fed1 AM |
139 | } |
140 | ||
141 | /** | |
142 | * Logs a message and exception with severity INFO in the runtime log of the | |
143 | * plug-in. | |
144 | * | |
145 | * @param message | |
146 | * A message to log | |
147 | * @param exception | |
148 | * A exception to log | |
149 | */ | |
4bcdb26a AM |
150 | public void logInfo(@Nullable String message, Throwable exception) { |
151 | getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message), exception)); | |
e110fed1 AM |
152 | } |
153 | ||
154 | /** | |
155 | * Logs a message and exception with severity WARNING in the runtime log of | |
156 | * the plug-in. | |
157 | * | |
158 | * @param message | |
159 | * A message to log | |
160 | */ | |
4bcdb26a AM |
161 | public void logWarning(@Nullable String message) { |
162 | getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message))); | |
e110fed1 AM |
163 | } |
164 | ||
165 | /** | |
166 | * Logs a message and exception with severity WARNING in the runtime log of | |
167 | * the plug-in. | |
168 | * | |
169 | * @param message | |
170 | * A message to log | |
171 | * @param exception | |
172 | * A exception to log | |
173 | */ | |
4bcdb26a AM |
174 | public void logWarning(@Nullable String message, Throwable exception) { |
175 | getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message), exception)); | |
e110fed1 AM |
176 | } |
177 | ||
178 | /** | |
179 | * Logs a message and exception with severity ERROR in the runtime log of | |
180 | * the plug-in. | |
181 | * | |
182 | * @param message | |
183 | * A message to log | |
184 | */ | |
4bcdb26a AM |
185 | public void logError(@Nullable String message) { |
186 | getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message))); | |
e110fed1 AM |
187 | } |
188 | ||
189 | /** | |
190 | * Logs a message and exception with severity ERROR in the runtime log of | |
191 | * the plug-in. | |
192 | * | |
193 | * @param message | |
194 | * A message to log | |
195 | * @param exception | |
196 | * A exception to log | |
197 | */ | |
4bcdb26a AM |
198 | public void logError(@Nullable String message, Throwable exception) { |
199 | getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message), exception)); | |
e110fed1 AM |
200 | } |
201 | ||
202 | } |