2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 package org
.lttng
.ust
.agent
.context
;
20 import java
.io
.IOException
;
21 import java
.util
.HashMap
;
23 import java
.util
.concurrent
.ConcurrentHashMap
;
26 * The singleton manager of {@link IContextInfoRetriever} objects.
28 * @author Alexandre Montplaisir
30 public final class ContextInfoManager
{
32 private static final String SHARED_LIBRARY_NAME
= "lttng-ust-context-jni";
34 private static ContextInfoManager instance
;
36 private final Map
<String
, IContextInfoRetriever
> contextInfoRetrievers
= new ConcurrentHashMap
<String
, IContextInfoRetriever
>();
37 private final Map
<String
, Long
> contextInforRetrieverRefs
= new HashMap
<String
, Long
>();
39 private final Object retrieverLock
= new Object();
41 /** Singleton class, constructor should not be accessed directly */
42 private ContextInfoManager() {
46 * Get the singleton instance.
49 * Usage of this class requires the "liblttng-ust-context-jni.so" native
50 * library to be present on the system and available (passing
51 * -Djava.library.path=path to the JVM may be needed).
54 * @return The singleton instance
56 * If the shared library cannot be found.
57 * @throws SecurityException
58 * We will forward any SecurityExcepion that may be thrown when
59 * trying to load the JNI library.
61 public static synchronized ContextInfoManager
getInstance() throws IOException
, SecurityException
{
62 if (instance
== null) {
64 System
.loadLibrary(SHARED_LIBRARY_NAME
);
65 } catch (UnsatisfiedLinkError e
) {
66 throw new IOException(e
);
68 instance
= new ContextInfoManager();
74 * Register a new context info retriever.
77 * Each context info retriever is registered with a given "retriever name",
78 * which specifies the namespace of the context elements. This name is
79 * specified separately from the retriever objects, which would allow
80 * register the same retriever under different namespaces for example.
84 * If the method returns false (indicating registration failure), then the
85 * retriever object will *not* be used for context information.
88 * @param retrieverName
89 * The name to register to the context retriever object with.
90 * @param contextInfoRetriever
91 * The context info retriever to register
92 * @return True if the retriever was successfully registered, false if there
93 * was an error, for example if a retriever is already registered
96 public boolean registerContextInfoRetriever(String retrieverName
, IContextInfoRetriever contextInfoRetriever
) {
97 synchronized (retrieverLock
) {
98 if (contextInfoRetrievers
.containsKey(retrieverName
)) {
100 * There is already a retriever registered with that name,
101 * refuse the new registration.
106 * Inform LTTng-UST of the new retriever. The names have to start
107 * with "$app." on the UST side!
109 long ref
= LttngContextApi
.registerProvider("$app." + retrieverName
);
114 contextInfoRetrievers
.put(retrieverName
, contextInfoRetriever
);
115 contextInforRetrieverRefs
.put(retrieverName
, Long
.valueOf(ref
));
122 * Unregister a previously added context info retriever.
124 * This method has no effect if the retriever was not already registered.
126 * @param retrieverName
127 * The context info retriever to unregister
128 * @return True if unregistration was successful, false if there was an
131 public boolean unregisterContextInfoRetriever(String retrieverName
) {
132 synchronized (retrieverLock
) {
133 if (!contextInfoRetrievers
.containsKey(retrieverName
)) {
135 * There was no retriever registered with that name.
139 contextInfoRetrievers
.remove(retrieverName
);
140 long ref
= contextInforRetrieverRefs
.remove(retrieverName
).longValue();
142 /* Unregister the retriever on the UST side too */
143 LttngContextApi
.unregisterProvider(ref
);
150 * Return the context info retriever object registered with the given name.
152 * @param retrieverName
153 * The retriever name to look for
154 * @return The corresponding retriever object, or <code>null</code> if there
157 public IContextInfoRetriever
getContextInfoRetriever(String retrieverName
) {
158 return contextInfoRetrievers
.get(retrieverName
);