incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1520039 - in /sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl: BindingsValuesProviderCustomizer.java SlingScriptAdapterFactory.java
Date Wed, 04 Sep 2013 14:50:41 GMT
Author: bdelacretaz
Date: Wed Sep  4 14:50:41 2013
New Revision: 1520039

URL: http://svn.apache.org/r1520039
Log:
SLING-3038 - extract BindingsValuesProviderCustomizer to make it testable

Added:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
  (with props)
Modified:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java

Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java?rev=1520039&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
(added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
Wed Sep  4 14:50:41 2013
@@ -0,0 +1,127 @@
+/*
+ * 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.core.impl;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.scripting.api.BindingsValuesProvider;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+/** ServiceTrackerCustomizer used to track our BindingsValuesProviders */
+class BindingsValuesProviderCustomizer implements ServiceTrackerCustomizer {
+    
+    /** list of service property values which indicate 'any' script engine */
+    private static final List<String> ANY_ENGINE = Arrays.asList("*", "ANY");
+
+    private final BundleContext bundleContext;
+    
+    /**
+     * The BindingsValuesProvider impls which apply to all languages. Keys are serviceIds.
+     */
+    private final Map<Object, BindingsValuesProvider> genericBindingsValuesProviders;
+
+    /**
+     * The BindingsValuesProvider impls which apply to a specific language.
+     */
+    private final Map<String, Map<Object, BindingsValuesProvider>> langBindingsValuesProviders;
+    
+    BindingsValuesProviderCustomizer(BundleContext bc) {
+        bundleContext = bc;
+        genericBindingsValuesProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
+        langBindingsValuesProviders = new ConcurrentHashMap<String, Map<Object, BindingsValuesProvider>>();
+    }
+
+    @SuppressWarnings("unchecked")
+    public Object addingService(final ServiceReference ref) {
+        final String[] engineNames = PropertiesUtil
+                .toStringArray(ref.getProperty(ScriptEngine.NAME), new String[0]);
+        final Object serviceId = ref.getProperty(Constants.SERVICE_ID);
+        Object service = bundleContext.getService(ref);
+        if (service != null) {
+            if (service instanceof Map) {
+                service = new MapWrappingBindingsValuesProvider((Map<String, Object>)
service);
+            }
+            if (engineNames.length == 0) {
+                genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider) service);
+            } else if (engineNames.length == 1 && ANY_ENGINE.contains(engineNames[0].toUpperCase()))
{
+                genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider) service);
+            } else {
+                for (String engineName : engineNames) {
+                    Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(engineName);
+                    if (langProviders == null) {
+                        langProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
+                        langBindingsValuesProviders.put(engineName, langProviders);
+                    }
+
+                    langProviders.put(serviceId, (BindingsValuesProvider) service);
+                }
+            }
+        }
+        return service;
+    }
+
+    public void modifiedService(final ServiceReference ref, final Object service) {
+        removedService(ref, service);
+        addingService(ref);
+    }
+
+    public void removedService(final ServiceReference ref, final Object service) {
+        Object serviceId = ref.getProperty(Constants.SERVICE_ID);
+        if (genericBindingsValuesProviders.remove(serviceId) == null) {
+            for (Map<Object, BindingsValuesProvider> coll : langBindingsValuesProviders.values())
{
+                if (coll.remove(service) != null) {
+                    return;
+                }
+            }
+        }
+    }
+    
+    Map<Object, BindingsValuesProvider> getGenericBindingsValuesProviders() {
+        return genericBindingsValuesProviders;
+    }
+
+    Map<String, Map<Object, BindingsValuesProvider>> getLangBindingsValuesProviders()
{
+        return langBindingsValuesProviders;
+    }
+
+    private class MapWrappingBindingsValuesProvider implements BindingsValuesProvider {
+
+        private Map<String,Object> map;
+
+        MapWrappingBindingsValuesProvider(Map<String, Object> map) {
+            this.map = map;
+        }
+
+        public void addBindings(Bindings bindings) {
+            for (String key : map.keySet()) {
+                bindings.put(key, map.get(key));
+            }
+        }
+
+    }
+
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProviderCustomizer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java?rev=1520039&r1=1520038&r2=1520039&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
Wed Sep  4 14:50:41 2013
@@ -17,13 +17,10 @@
 package org.apache.sling.scripting.core.impl;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
-import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
 
@@ -39,13 +36,10 @@ import org.apache.sling.commons.osgi.Pro
 import org.apache.sling.scripting.api.BindingsValuesProvider;
 import org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -67,9 +61,6 @@ public class SlingScriptAdapterFactory i
 
     private final Logger log = LoggerFactory.getLogger(SlingScriptAdapterFactory.class);
 
-    /** list of service property values which indicate 'any' script engine */
-    private static final List<String> ANY_ENGINE = Arrays.asList("*", "ANY");
-
     private BundleContext bundleContext;
 
     /**
@@ -83,16 +74,6 @@ public class SlingScriptAdapterFactory i
     private ServiceTracker mapBindingsValuesProviderTracker;
 
     /**
-     * The BindingsValuesProvider impls which apply to all languages. Keys are serviceIds.
-     */
-    private Map<Object, BindingsValuesProvider> genericBindingsValuesProviders;
-
-    /**
-     * The BindingsValuesProvider impls which apply to a specific language.
-     */
-    private Map<String, Map<Object, BindingsValuesProvider>> langBindingsValuesProviders;
-
-    /**
      * The service cache for script execution.
      */
     private ServiceCache serviceCache;
@@ -102,6 +83,11 @@ public class SlingScriptAdapterFactory i
      */
     @Reference
     private SlingScriptEngineManager scriptEngineManager;
+    
+    /**
+     * The customizer for BindingsValuesProvider service trackers
+     */
+    private BindingsValuesProviderCustomizer bindingsValuesProviderCustomizer;
 
     // ---------- AdapterFactory -----------------------------------------------
 
@@ -174,21 +160,17 @@ public class SlingScriptAdapterFactory i
     // ---------- SCR integration ----------------------------------------------
 
     protected void activate(ComponentContext context) {
-        this.bundleContext = context.getBundleContext();
+        bundleContext = context.getBundleContext();
+        bindingsValuesProviderCustomizer = new BindingsValuesProviderCustomizer(bundleContext);
 
-        this.genericBindingsValuesProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
-        this.langBindingsValuesProviders = new ConcurrentHashMap<String, Map<Object,
BindingsValuesProvider>>();
-
-        ServiceTrackerCustomizer customizer = new BindingsValuesProviderCustomizer();
-
-        this.bindingsValuesProviderTracker = new ServiceTracker(this.bundleContext, BindingsValuesProvider.class.getName(),
customizer);
+        this.bindingsValuesProviderTracker = new ServiceTracker(this.bundleContext, BindingsValuesProvider.class.getName(),
bindingsValuesProviderCustomizer);
         this.bindingsValuesProviderTracker.open();
 
         try {
             Filter filter = this.bundleContext.createFilter(String.format("(&(objectclass=%s)(javax.script.name=*))",
                     Map.class.getName()));
 
-            this.mapBindingsValuesProviderTracker = new ServiceTracker(this.bundleContext,
filter, customizer);
+            this.mapBindingsValuesProviderTracker = new ServiceTracker(this.bundleContext,
filter, bindingsValuesProviderCustomizer);
             this.mapBindingsValuesProviderTracker.open();
         } catch (InvalidSyntaxException e) {
             log.warn("Unable to create ServiceTracker for Map-based script bindiings", e);
@@ -213,7 +195,7 @@ public class SlingScriptAdapterFactory i
 
     private Collection<BindingsValuesProvider> getBindingsValuesProviders(ScriptEngineFactory
scriptEngineFactory) {
         final List<BindingsValuesProvider> results = new ArrayList<BindingsValuesProvider>();
-        results.addAll(genericBindingsValuesProviders.values());
+        results.addAll(bindingsValuesProviderCustomizer.getGenericBindingsValuesProviders().values());
 
         // we load the compatible language ones first so that the most specific
         // overrides these
@@ -221,7 +203,7 @@ public class SlingScriptAdapterFactory i
         if (factoryProps != null) {
             String[] compatibleLangs = PropertiesUtil.toStringArray(factoryProps.get("compatible.javax.script.name"),
new String[0]);
             for (final String name : compatibleLangs) {
-                final Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(name);
+                final Map<Object, BindingsValuesProvider> langProviders = bindingsValuesProviderCustomizer.getLangBindingsValuesProviders().get(name);
                 if (langProviders != null) {
                     results.addAll(langProviders.values());
                 }
@@ -229,7 +211,7 @@ public class SlingScriptAdapterFactory i
         }
 
         for (final String name : scriptEngineFactory.getNames()) {
-            final Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(name);
+            final Map<Object, BindingsValuesProvider> langProviders = bindingsValuesProviderCustomizer.getLangBindingsValuesProviders().get(name);
             if (langProviders != null) {
                 results.addAll(langProviders.values());
             }
@@ -237,70 +219,4 @@ public class SlingScriptAdapterFactory i
 
         return results;
     }
-
-    private class BindingsValuesProviderCustomizer implements ServiceTrackerCustomizer {
-
-        @SuppressWarnings("unchecked")
-        public Object addingService(final ServiceReference ref) {
-            final String[] engineNames = PropertiesUtil
-                    .toStringArray(ref.getProperty(ScriptEngine.NAME), new String[0]);
-            final Object serviceId = ref.getProperty(Constants.SERVICE_ID);
-            Object service = bundleContext.getService(ref);
-            if (service != null) {
-                if (service instanceof Map) {
-                    service = new MapWrappingBindingsValuesProvider((Map<String, Object>)
service);
-                }
-                if (engineNames.length == 0) {
-                    genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider)
service);
-                } else if (engineNames.length == 1 && ANY_ENGINE.contains(engineNames[0].toUpperCase()))
{
-                    genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider)
service);
-                } else {
-                    for (String engineName : engineNames) {
-                        Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(engineName);
-                        if (langProviders == null) {
-                            langProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
-                            langBindingsValuesProviders.put(engineName, langProviders);
-                        }
-
-                        langProviders.put(serviceId, (BindingsValuesProvider) service);
-                    }
-                }
-            }
-            return service;
-        }
-
-        public void modifiedService(final ServiceReference ref, final Object service) {
-            removedService(ref, service);
-            addingService(ref);
-        }
-
-        public void removedService(final ServiceReference ref, final Object service) {
-            Object serviceId = ref.getProperty(Constants.SERVICE_ID);
-            if (genericBindingsValuesProviders.remove(serviceId) == null) {
-                for (Map<Object, BindingsValuesProvider> coll : langBindingsValuesProviders.values())
{
-                    if (coll.remove(service) != null) {
-                        return;
-                    }
-                }
-            }
-        }
-
-    }
-
-    private class MapWrappingBindingsValuesProvider implements BindingsValuesProvider {
-
-        private Map<String,Object> map;
-
-        MapWrappingBindingsValuesProvider(Map<String, Object> map) {
-            this.map = map;
-        }
-
-        public void addBindings(Bindings bindings) {
-            for (String key : map.keySet()) {
-                bindings.put(key, map.get(key));
-            }
-        }
-
-    }
-
 }



Mime
View raw message