SLING-12189 : Improve RuntimeExtension registry
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/ExtensionRegistryService.java b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/ExtensionRegistryService.java
index b5728c8..735e483 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/ExtensionRegistryService.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/ExtensionRegistryService.java
@@ -1,21 +1,21 @@
-/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~ Licensed to the Apache Software Foundation (ASF) under one
- ~ or more contributor license agreements.  See the NOTICE file
- ~ distributed with this work for additional information
- ~ regarding copyright ownership.  The ASF licenses this file
- ~ to you under the Apache License, Version 2.0 (the
- ~ "License"); you may not use this file except in compliance
- ~ with the License.  You may obtain a copy of the License at
- ~
- ~   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.
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   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.apache.sling.scripting.sightly.impl.engine;
 
 import java.util.Collections;
@@ -30,7 +30,6 @@
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.component.annotations.ReferencePolicy;
-import org.osgi.util.converter.Converters;
 
 /**
  * Aggregator for all runtime extensions.
@@ -52,9 +51,9 @@
             )
     @SuppressWarnings("unused")
     protected void bindExtensionService(ServiceReference<RuntimeExtension> serviceReference, RuntimeExtension runtimeExtension) {
-        RuntimeExtensionReference rer = new RuntimeExtensionReference(serviceReference, runtimeExtension);
+        final RuntimeExtensionReference rer = new RuntimeExtensionReference(serviceReference, runtimeExtension);
         synchronized (extensions) {
-            Set<RuntimeExtensionReference> namedExtensions = extensions.computeIfAbsent(rer.getName(), key -> new TreeSet<>());
+            final Set<RuntimeExtensionReference> namedExtensions = extensions.computeIfAbsent(rer.getName(), key -> new TreeSet<>());
             if (namedExtensions.add(rer)) {
                 mapping = getRuntimeExtensions();
             }
@@ -63,23 +62,13 @@
 
     @SuppressWarnings("unused")
     protected void unbindExtensionService(ServiceReference<RuntimeExtension> serviceReference) {
+        final RuntimeExtensionReference rer = new RuntimeExtensionReference(serviceReference, null);
         synchronized (extensions) {
-            String name = Converters.standardConverter().convert(serviceReference.getProperty(RuntimeExtension.NAME)).defaultValue("").to(String.class);
-            Set<RuntimeExtensionReference> namedExtensions = extensions.get(name);
-            boolean changed = false;
-            if (namedExtensions != null) {
-                for (RuntimeExtensionReference runtimeExtensionReference : namedExtensions) {
-                    if (serviceReference.equals(runtimeExtensionReference.getServiceReference())) {
-                        namedExtensions.remove(runtimeExtensionReference);
-                        if (namedExtensions.isEmpty()) {
-                            extensions.remove(name);
-                        }
-                        changed = true;
-                        break;
-                    }
+            final Set<RuntimeExtensionReference> namedExtensions = extensions.get(rer.getName());
+            if (namedExtensions != null && namedExtensions.remove(rer)) {
+                if (namedExtensions.isEmpty()) {
+                    extensions.remove(rer.getName());
                 }
-            }
-            if (changed) {
                 mapping = getRuntimeExtensions();
             }
         }
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/RuntimeExtensionReference.java b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/RuntimeExtensionReference.java
index 74413e6..46c46b0 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/RuntimeExtensionReference.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/RuntimeExtensionReference.java
@@ -1,21 +1,21 @@
-/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~ Licensed to the Apache Software Foundation (ASF) under one
- ~ or more contributor license agreements.  See the NOTICE file
- ~ distributed with this work for additional information
- ~ regarding copyright ownership.  The ASF licenses this file
- ~ to you under the Apache License, Version 2.0 (the
- ~ "License"); you may not use this file except in compliance
- ~ with the License.  You may obtain a copy of the License at
- ~
- ~   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.
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   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.apache.sling.scripting.sightly.impl.engine;
 
 import org.apache.sling.scripting.sightly.extension.RuntimeExtension;
@@ -50,7 +50,7 @@
         if (name.equals(other.name)) {
             return priority - other.priority;
         }
-        return -1;
+        return name.compareTo(other.name);
     }
 
     @Override
@@ -75,8 +75,7 @@
         return serviceReference;
     }
 
-    synchronized RuntimeExtension getService() {
+    RuntimeExtension getService() {
         return runtimeExtension;
     }
-
 }