Fix: Correctly compute Java agent list loggers response size
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / context / ContextInfoManager.java
CommitLineData
27dbdc00
AM
1/*
2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18package org.lttng.ust.agent.context;
19
8ab5c06b
AM
20import java.io.IOException;
21import java.util.HashMap;
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
27dbdc00
AM
24
25/**
26 * The singleton manager of {@link IContextInfoRetriever} objects.
27 *
28 * @author Alexandre Montplaisir
29 */
30public final class ContextInfoManager {
31
8ab5c06b 32 private static final String SHARED_LIBRARY_NAME = "lttng-ust-context-jni";
27dbdc00 33
8ab5c06b
AM
34 private static ContextInfoManager instance;
35
36 private final Map<String, IContextInfoRetriever> contextInfoRetrievers = new ConcurrentHashMap<String, IContextInfoRetriever>();
37 private final Map<String, Long> contextInforRetrieverRefs = new HashMap<String, Long>();
38
39 private final Object retrieverLock = new Object();
27dbdc00
AM
40
41 /** Singleton class, constructor should not be accessed directly */
42 private ContextInfoManager() {
43 }
44
45 /**
46 * Get the singleton instance.
47 *
8ab5c06b
AM
48 * <p>
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).
52 * </p>
53 *
27dbdc00 54 * @return The singleton instance
8ab5c06b
AM
55 * @throws IOException
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.
27dbdc00 60 */
8ab5c06b
AM
61 public static synchronized ContextInfoManager getInstance() throws IOException, SecurityException {
62 if (instance == null) {
63 try {
64 System.loadLibrary(SHARED_LIBRARY_NAME);
65 } catch (UnsatisfiedLinkError e) {
66 throw new IOException(e);
67 }
68 instance = new ContextInfoManager();
69 }
70 return instance;
27dbdc00
AM
71 }
72
73 /**
74 * Register a new context info retriever.
75 *
8ab5c06b
AM
76 * <p>
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.
81 * </p>
82 *
83 * <p>
84 * If the method returns false (indicating registration failure), then the
85 * retriever object will *not* be used for context information.
86 * </p>
27dbdc00 87 *
8ab5c06b
AM
88 * @param retrieverName
89 * The name to register to the context retriever object with.
90 * @param contextInfoRetriever
27dbdc00 91 * The context info retriever to register
8ab5c06b
AM
92 * @return True if the retriever was successfully registered, false if there
93 * was an error, for example if a retriever is already registered
94 * with that name.
27dbdc00 95 */
8ab5c06b
AM
96 public boolean registerContextInfoRetriever(String retrieverName, IContextInfoRetriever contextInfoRetriever) {
97 synchronized (retrieverLock) {
98 if (contextInfoRetrievers.containsKey(retrieverName)) {
99 /*
100 * There is already a retriever registered with that name,
101 * refuse the new registration.
102 */
103 return false;
104 }
105 /*
106 * Inform LTTng-UST of the new retriever. The names have to start
107 * with "$app." on the UST side!
108 */
109 long ref = LttngContextApi.registerProvider("$app." + retrieverName);
110 if (ref == 0) {
111 return false;
112 }
113
114 contextInfoRetrievers.put(retrieverName, contextInfoRetriever);
115 contextInforRetrieverRefs.put(retrieverName, Long.valueOf(ref));
116
117 return true;
118 }
27dbdc00
AM
119 }
120
121 /**
122 * Unregister a previously added context info retriever.
123 *
124 * This method has no effect if the retriever was not already registered.
125 *
8ab5c06b 126 * @param retrieverName
27dbdc00 127 * The context info retriever to unregister
8ab5c06b
AM
128 * @return True if unregistration was successful, false if there was an
129 * error
27dbdc00 130 */
8ab5c06b
AM
131 public boolean unregisterContextInfoRetriever(String retrieverName) {
132 synchronized (retrieverLock) {
133 if (!contextInfoRetrievers.containsKey(retrieverName)) {
134 /*
135 * There was no retriever registered with that name.
136 */
137 return false;
138 }
139 contextInfoRetrievers.remove(retrieverName);
140 long ref = contextInforRetrieverRefs.remove(retrieverName).longValue();
141
142 /* Unregister the retriever on the UST side too */
143 LttngContextApi.unregisterProvider(ref);
144
145 return true;
146 }
27dbdc00
AM
147 }
148
149 /**
8ab5c06b 150 * Return the context info retriever object registered with the given name.
27dbdc00 151 *
8ab5c06b
AM
152 * @param retrieverName
153 * The retriever name to look for
154 * @return The corresponding retriever object, or <code>null</code> if there
155 * was none
27dbdc00 156 */
8ab5c06b
AM
157 public IContextInfoRetriever getContextInfoRetriever(String retrieverName) {
158 return contextInfoRetrievers.get(retrieverName);
27dbdc00
AM
159 }
160}
This page took 0.029737 seconds and 4 git commands to generate.