sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r932420 - in /sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl: InternalScriptHelper.java ServiceCache.java
Date Fri, 09 Apr 2010 14:02:31 GMT
Author: cziegeler
Date: Fri Apr  9 14:02:31 2010
New Revision: 932420

URL: http://svn.apache.org/viewvc?rev=932420&view=rev
Log:
SLING-1478 : Improve SlingScriptHelper service handling

Added:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
  (with props)
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
  (with props)

Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java?rev=932420&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
(added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
Fri Apr  9 14:02:31 2010
@@ -0,0 +1,56 @@
+/*
+ * 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 org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.scripting.core.ScriptHelper;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Internal script helper
+ */
+public class InternalScriptHelper extends ScriptHelper {
+
+    private final ServiceCache serviceCache;
+
+    public InternalScriptHelper(final BundleContext ctx,
+            final SlingScript script,
+            final SlingHttpServletRequest request,
+            final SlingHttpServletResponse response,
+            final ServiceCache cache) {
+        super(ctx, script, request, response);
+        this.serviceCache = cache;
+    }
+
+    public InternalScriptHelper(final BundleContext ctx,
+            final SlingScript script,
+            final ServiceCache cache) {
+        super(ctx, script);
+        this.serviceCache = cache;
+    }
+
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#getService(java.lang.Class)
+     */
+    public <ServiceType> ServiceType getService(Class<ServiceType> type) {
+        return this.serviceCache.getService(type);
+    }
+}

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

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/InternalScriptHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java?rev=932420&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
(added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
Fri Apr  9 14:02:31 2010
@@ -0,0 +1,121 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+public class ServiceCache implements ServiceListener {
+
+    private final BundleContext bundleContext;
+
+    private static final Reference NULL_REFERENCE = new Reference();
+
+    private final ConcurrentHashMap<String, Reference> cache = new ConcurrentHashMap<String,
Reference>();
+
+    /**
+     * The list of references - we don't need to synchronize this as we are
+     * running in one single request.
+     */
+    protected final List<ServiceReference> references = new ArrayList<ServiceReference>();
+
+    public ServiceCache(final BundleContext ctx) {
+        this.bundleContext = ctx;
+        this.bundleContext.addServiceListener(this);
+    }
+
+    public void dispose() {
+        this.bundleContext.removeServiceListener(this);
+        for(final Reference ref : cache.values()) {
+            if ( ref != NULL_REFERENCE ) {
+                this.bundleContext.ungetService(ref.reference);
+            }
+        }
+    }
+
+    /**
+     * Return a service for the given service class.
+     * @param <ServiceType> The service class / interface
+     * @param type The requested service
+     * @return The service or <code>null</code>
+     */
+    @SuppressWarnings("unchecked")
+    public <ServiceType> ServiceType getService(Class<ServiceType> type) {
+        final String key = type.getName();
+        Reference reference = this.cache.get(key);
+        if (reference == null ) {
+            // if the service is not in the cache, we have to go into a synchronized
+            // block to avoid concurrent gets of the same service
+            synchronized (this) {
+                reference = this.cache.get(key);
+                if ( reference == null ) {
+                    // try to get service from bundle context
+                    final ServiceReference ref = this.bundleContext.getServiceReference(key);
+                    if (ref != null) {
+                        final Object service = this.bundleContext.getService(ref);
+                        if ( service != null ) {
+                            reference = new Reference();
+                            reference.service = service;
+                            reference.reference = ref;
+                        }
+                    }
+                    if ( reference == null ) {
+                        reference = NULL_REFERENCE;
+                    }
+                    this.cache.put(key, reference);
+                }
+            }
+
+        }
+        if ( reference == NULL_REFERENCE ) {
+            return null;
+        }
+        return (ServiceType) reference.service;
+    }
+
+    /**
+     * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
+     */
+    public void serviceChanged(ServiceEvent event) {
+        final String[] objectClasses = (String[])event.getServiceReference().getProperty(Constants.OBJECTCLASS);
+        if ( objectClasses != null) {
+            for(final String key : objectClasses) {
+                Reference ref = null;
+                synchronized ( this ) {
+                    ref = this.cache.remove(key);
+                }
+                if ( ref != null && ref != NULL_REFERENCE ) {
+                    this.bundleContext.ungetService(ref.reference);
+                }
+            }
+        }
+    }
+
+    protected static final class Reference {
+        public ServiceReference reference;
+        public Object           service;
+    }
+}

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

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message