- public String addServiceEntry(String namespace, String serviceName, ObjectName on) {
-
- String moduleName = on.getKeyProperty("moduleFactoryName");
- String instanceName = on.getKeyProperty("instanceName");
-
- String refName = addServiceEntry(namespace, serviceName, moduleName, instanceName);
- logger.trace("Added service entry to tracker. Service name {}, ref name {}, module name {}, instance name {}",
- serviceName, refName, moduleName, instanceName);
- return refName;
- }
-
- @VisibleForTesting
- public String addServiceEntry(String namespace, String serviceName, String moduleName, String instanceName) {
- ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
- serviceInstance.setServiceName(serviceName);
-
- String refName = instanceToRef.get(serviceInstance);
-
- Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance.get(namespace);
- if (serviceNameToRefNameToInstance == null) {
- serviceNameToRefNameToInstance = Maps.newHashMap();
- namespaceToServiceNameToRefNameToInstance.put(namespace, serviceNameToRefNameToInstance);
- }
-
- Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
- if (refNameToInstance == null) {
- refNameToInstance = Maps.newHashMap();
- serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
- }
-
- if (refName != null) {
- if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
- refNameToInstance.put(refName, serviceInstance);
- }
- return refName;
- } else {
- refName = "ref_" + instanceName;
-
- final Set<String> refNamesAsSet = toSet(instanceToRef.values());
- if (refNamesAsSet.contains(refName)) {
- refName = findAvailableRefName(refName, refNamesAsSet);
- }
-
- instanceToRef.put(serviceInstance, refName);
- refNameToInstance.put(refName, serviceInstance);
-
- return refName;
- }
- }
-
- private Set<String> toSet(Collection<String> values) {
- Set<String> refNamesAsSet = Sets.newHashSet();
-
- for (String refName : values) {
- boolean resultAdd = refNamesAsSet.add(refName);
- Preconditions.checkState(resultAdd,
- "Error occurred building services element, reference name {} was present twice", refName);
- }
-
- return refNamesAsSet;