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