Commit | Line | Data |
---|---|---|
a2fb04cd AM |
1 | /* |
2 | * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
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 | ||
10 | package org.lttng.scope.tmf2.views.core; | |
11 | ||
12 | import static java.util.Objects.requireNonNull; | |
13 | import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString; | |
14 | ||
15 | import java.util.Collections; | |
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | ||
19 | import org.eclipse.core.runtime.IStatus; | |
20 | import org.eclipse.core.runtime.Plugin; | |
21 | import org.eclipse.core.runtime.Status; | |
22 | import org.eclipse.jdt.annotation.Nullable; | |
23 | import org.osgi.framework.BundleContext; | |
24 | ||
25 | /** | |
26 | * The activator class controls the plug-in life cycle | |
27 | * | |
28 | * @author Alexandre Montplaisir | |
29 | */ | |
30 | public abstract class ScopeCoreActivator extends Plugin { | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
36 | /** Map of all the registered activators, indexed by activator classes */ | |
37 | private static final Map<Class<? extends ScopeCoreActivator>, ScopeCoreActivator> CORE_ACTIVATORS = | |
38 | Collections.synchronizedMap(new HashMap<Class<? extends ScopeCoreActivator>, ScopeCoreActivator>()); | |
39 | ||
40 | /** This instance's plug-in ID */ | |
41 | private final String fPluginId; | |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Constructors | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
47 | /** | |
48 | * Constructor | |
49 | */ | |
50 | public ScopeCoreActivator() { | |
51 | fPluginId = requireNonNull(getBundle().getSymbolicName()); | |
52 | } | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Accessors | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | /** | |
59 | * Return this plug-in's ID. | |
60 | * | |
61 | * @return The plug-in ID | |
62 | */ | |
63 | public String getPluginId() { | |
64 | return fPluginId; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Get a registered activator. Subclasses should implement their own public | |
69 | * getInstance() method, which returns the result of this. | |
70 | * | |
71 | * @param activatorClass | |
72 | * The activator's runtime class | |
73 | * @return The corresponding activator | |
74 | */ | |
75 | protected static <T extends ScopeCoreActivator> T getInstance(Class<T> activatorClass) { | |
76 | ScopeCoreActivator activator = CORE_ACTIVATORS.get(activatorClass); | |
77 | if (activator == null) { | |
78 | /* The activator should be registered at this point! */ | |
79 | throw new IllegalStateException(); | |
80 | } | |
81 | /* | |
82 | * We inserted the corresponding class into the map ourselves, cast | |
83 | * should always be safe. | |
84 | */ | |
85 | @SuppressWarnings("unchecked") | |
86 | T ret = (T) activator; | |
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 | Class<? extends ScopeCoreActivator> activatorClass = this.getClass(); | |
112 | synchronized (CORE_ACTIVATORS) { | |
113 | if (CORE_ACTIVATORS.containsKey(activatorClass)) { | |
114 | logError("Duplicate Activator : " + activatorClass.getCanonicalName()); //$NON-NLS-1$ | |
115 | } | |
116 | CORE_ACTIVATORS.put(activatorClass, this); | |
117 | } | |
118 | startActions(); | |
119 | } | |
120 | ||
121 | @Override | |
122 | public final void stop(@Nullable BundleContext context) throws Exception { | |
123 | stopActions(); | |
124 | CORE_ACTIVATORS.remove(this.getClass()); | |
125 | super.stop(context); | |
126 | } | |
127 | ||
128 | // ------------------------------------------------------------------------ | |
129 | // Logging helpers | |
130 | // ------------------------------------------------------------------------ | |
131 | ||
132 | /** | |
133 | * Log a message with severity INFO. | |
134 | * | |
135 | * @param message | |
136 | * The message to log | |
137 | * @param exception | |
138 | * Optional exception to attach to the message | |
139 | */ | |
140 | public void logInfo(@Nullable String message, Throwable... exception) { | |
141 | if (exception.length < 1) { | |
142 | getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message))); | |
143 | } else { | |
144 | getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message), exception[0])); | |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | /** | |
150 | * Log a message with severity WARNING. | |
151 | * | |
152 | * @param message | |
153 | * The message to log | |
154 | * @param exception | |
155 | * Optional exception to attach to the message | |
156 | */ | |
157 | public void logWarning(@Nullable String message, Throwable... exception) { | |
158 | if (exception.length < 1) { | |
159 | getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message))); | |
160 | } else { | |
161 | getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message), exception[0])); | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
166 | * Log a message with severity ERROR. | |
167 | * | |
168 | * @param message | |
169 | * The message to log | |
170 | * @param exception | |
171 | * Optional exception to attach to the message | |
172 | */ | |
173 | public void logError(@Nullable String message, Throwable... exception) { | |
174 | if (exception.length < 1) { | |
175 | getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message))); | |
176 | } else { | |
177 | getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message), exception[0])); | |
178 | } | |
179 | } | |
180 | ||
181 | } |