/*
 * Copyright 2015 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package org.kie.internal.runtime.manager;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;

import org.kie.api.runtime.manager.RuntimeManager;

public class RuntimeManagerRegistry {
	
	private static RuntimeManagerRegistry INSTANCE = new RuntimeManagerRegistry();

	protected volatile ConcurrentHashMap<String, RuntimeManager> registeredManager = new ConcurrentHashMap<String, RuntimeManager>();
		
	private RuntimeManagerRegistry() {
		
	}
	
	public static RuntimeManagerRegistry get() {
		return INSTANCE;
	}
	
	public synchronized void register(RuntimeManager manager) {
		if (registeredManager.containsKey(manager.getIdentifier())) {
			throw new IllegalStateException("RuntimeManager is already registered with id " + manager.getIdentifier());
		}
		this.registeredManager.put(manager.getIdentifier(), manager);
	}
	
	public synchronized void remove(RuntimeManager manager) {

		this.registeredManager.remove(manager.getIdentifier());
	}
	
	public synchronized void remove(String identifier) {

		this.registeredManager.remove(identifier);
	}
	
	public RuntimeManager getManager(String id) {
		return this.registeredManager.get(id);
	}
	
	public boolean isRegistered(String id) {
		return this.registeredManager.containsKey(id);
	}
	
	public Collection<String> getRegisteredIdentifiers() {
		// Using "registeredManager.keySet()" directly would result in issues when compiling with JDK 8+. The "keySet()"
		// method returns type ConcurrentHashMap$KeySetView which is only available in Java 8+. That means
		// the bytecode would contain reference to that type, which does not exist in Java 6 and Java 7 and thus
		// clients would get NoSuchMethodError at runtime. The "keys()" method is fully backwards compatible, but it
		// requires wrapping inside additional collection as "unmodifiableCollection()" does not accept Enumerations.
	    return Collections.unmodifiableCollection(Collections.list(this.registeredManager.keys()));
	}
	
}

@@ -785,19 +785,21 @@
 istry %7B%0A
-%09%0A%09
+%0A    
 private 
@@ -870,17 +870,20 @@
 try();%0A%0A
-%09
+    
 protecte
@@ -1004,20 +1004,21 @@
 ger%3E();%0A
-%09%09%0A%09
+%0A    
 private 
@@ -1048,17 +1048,20 @@
 ) %7B%0A
-%09%09%0A%09%7D%0A%09%0A%09
+%0A    %7D%0A%0A    
 publ
@@ -1097,26 +1097,32 @@
 try get() %7B%0A
-%09%09
+        
 return INSTA
@@ -1126,22 +1126,27 @@
 STANCE;%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public s
@@ -1197,18 +1197,24 @@
 ager) %7B%0A
-%09%09
+        
 if (regi
@@ -1271,11 +1271,20 @@
 ) %7B%0A
-%09%09%09
+            
 thro
@@ -1391,14 +1391,26 @@
 ));%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 this
@@ -1463,30 +1463,35 @@
 , manager);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public synch
@@ -1533,26 +1533,32 @@
 manager) %7B%0A%0A
-%09%09
+        
 this.registe
@@ -1601,22 +1601,27 @@
 ier());%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public s
@@ -1666,18 +1666,24 @@
 ier) %7B%0A%0A
-%09%09
+        
 this.reg
@@ -1717,22 +1717,27 @@
 ifier);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public R
@@ -1766,34 +1766,40 @@
 er(String id) %7B%0A
-%09%09
+        
 return this.regi
@@ -1817,30 +1817,35 @@
 er.get(id);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public boole
@@ -1873,18 +1873,24 @@
 g id) %7B%0A
-%09%09
+        
 return t
@@ -1932,14 +1932,19 @@
 d);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 publ
@@ -1994,18 +1994,24 @@
 ers() %7B%0A
-%09%09
+        
 // Using
@@ -2115,18 +2115,24 @@
 ySet()%22%0A
-%09%09
+        
 // metho
@@ -2222,18 +2222,24 @@
 t means%0A
-%09%09
+        
 // the b
@@ -2335,18 +2335,24 @@
 nd thus%0A
-%09%09
+        
 // clien
@@ -2456,10 +2456,16 @@
  it%0A
-%09%09
+        
 // r
@@ -2570,17 +2570,20 @@
 ations.%0A
-%09
+    
     retu
@@ -2674,11 +2674,13 @@
 ));%0A
-%09
+    
 %7D%0A
-%09
 %0A%7D%0A

Fix formatting in RuntimeManagerRegistry