You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/04/30 07:44:25 UTC

svn commit: r652271 - /incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java

Author: bdelacretaz
Date: Tue Apr 29 22:44:25 2008
New Revision: 652271

URL: http://svn.apache.org/viewvc?rev=652271&view=rev
Log:
SLING-397 - Generate javascript wrapper methods from specific interfaces automatically - add missing file

Added:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java   (with props)

Added: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java?rev=652271&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java (added)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java Tue Apr 29 22:44:25 2008
@@ -0,0 +1,80 @@
+/*
+ * 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.javascript.wrapper;
+
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.mozilla.javascript.NativeJavaObject;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+
+/** Base class for Scriptable objects, uses the NativeJavaObject wrapper to provide
+ *  default wrapping of methods and properties (SLING-397)
+ */
+public abstract class ScriptableBase extends ScriptableObject {
+    
+    private NativeJavaObject njo;
+    private final Set<String> jsMethods = getJsMethodNames();
+    
+    public static final String JSFUNC_PREFIX = "jsFunction_";
+    
+    protected Object getNative(String name, Scriptable start) {
+        final Object wrapped = getWrappedObject();
+        
+        if(wrapped == null) {
+            return Scriptable.NOT_FOUND;
+        }
+        
+        if(jsMethods.contains(name)) {
+            return Scriptable.NOT_FOUND;
+        }
+        
+        if(njo == null) {
+            synchronized (this) {
+                if(njo == null) {
+                    njo = new NativeJavaObject(start, wrapped, getStaticType());
+                }
+            }
+        }
+        
+        return njo.get(name, start);
+    }
+    
+    /** @return the Java object that we're wrapping, used to create a NativeJavaObject
+     *  instance for default wrapping.
+     */
+    protected abstract Object getWrappedObject();
+    
+    /** @return the static type to use for NativeJavaObject wrapping */
+    protected abstract Class<?> getStaticType();
+    
+    /** @return the Set of method names that clazz defines, i.e. all public methods
+     *  with names that start with jsFunction_ */
+    private Set<String> getJsMethodNames() {
+        final Set<String> result = new HashSet<String>();
+        
+        for(Method m : getClass().getMethods()) {
+            if(m.getName().startsWith(JSFUNC_PREFIX)) {
+                result.add(m.getName().substring(JSFUNC_PREFIX.length()));
+            }
+        }
+        
+        return result;
+    }
+}

Propchange: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Re: svn commit: r652271 - /incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java

Posted by Felix Meschberger <fm...@gmail.com>.
Hi Bertrand,

Am Mittwoch, den 30.04.2008, 05:44 +0000 schrieb bdelacretaz@apache.org:
> Added:
>     incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableBase.java   (with props)
> 
> +    /** @return the Java object that we're wrapping, used to create a NativeJavaObject
> +     *  instance for default wrapping.
> +     */
> +    protected abstract Object getWrappedObject();

Is there a reason do define this method in the ScriptableBase class.
Because there is the org.mozilla.javascript.Wrapper which does exactly
the same (and which is extended by SlingWrapper).

In fact, IIRC all wrappers we have in Sling implement the SlingWrapper
interface, so these two things might probably be merged (or at least
aligned) because they serve the same purpose.

Regards
Felix