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