You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2009/09/03 15:59:03 UTC

svn commit: r810947 - in /myfaces/extensions/scripting/trunk: core/src/main/java/org/apache/myfaces/scripting/loaders/java/ core/src/main/java/org/apache/myfaces/scripting/servlet/ examples/src/main/webapp/ examples/src/main/webapp/WEB-INF/ examples/sr...

Author: werpu
Date: Thu Sep  3 13:59:03 2009
New Revision: 810947

URL: http://svn.apache.org/viewvc?rev=810947&view=rev
Log:
https://issues.apache.org/jira/browse/EXTSCRIPT-7
added possible jar search paths and other classpaths 
which will be picked up by the java compiler

We probably wont need it for the groovy classloader since it calls everything at runtime hence shifts the dependency resolution from compile to runtime




Modified:
    myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/loaders/java/JavaScriptingWeaver.java
    myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/servlet/CustomChainLoader.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogEntry.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogService.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/web.xml
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/javablog.xhtml

Modified: myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/loaders/java/JavaScriptingWeaver.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/loaders/java/JavaScriptingWeaver.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/loaders/java/JavaScriptingWeaver.java (original)
+++ myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/loaders/java/JavaScriptingWeaver.java Thu Sep  3 13:59:03 2009
@@ -32,6 +32,8 @@
 import java.io.FilenameFilter;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Iterator;
+import java.util.List;
+import java.util.LinkedList;
 
 /**
  * @author werpu
@@ -46,20 +48,72 @@
     DynamicClassIdentifier identifier = new DynamicClassIdentifier();
 
     /**
+     * this override is needed because we cannot sanely determine all jar
+     * paths we need for our compiler in the various web container configurations
+     */
+    static final String CUSTOM_JAR_PATHS = "org.apache.myfaces.scripting.java.JAR_PATHS";
+    /*comma separated list of additional classpaths*/
+    static final String CUSTOM_CLASS_PATHS = "org.apache.myfaces.scripting.java.CLASS_PATHS";
+
+    private static final String JAVA_FILE_ENDING = ".java";
+
+    /**
      * helper to allow initial compiler classpath scanning
      *
      * @param servletContext
      */
     public JavaScriptingWeaver(ServletContext servletContext) {
-        super(".java", ScriptingConst.ENGINE_TYPE_JAVA);
+        super(JAVA_FILE_ENDING, ScriptingConst.ENGINE_TYPE_JAVA);
         initClasspath(servletContext);
     }
 
     public JavaScriptingWeaver() {
-        super(".java", ScriptingConst.ENGINE_TYPE_JAVA);
+        super(JAVA_FILE_ENDING, ScriptingConst.ENGINE_TYPE_JAVA);
     }
 
 
+    /**
+     * recursive directory scan
+     *
+     * @param rootPath
+     * @return
+     */
+    private List<String> scanPath(String rootPath) {
+        File jarRoot = new File(rootPath);
+
+        List<String> retVal = new LinkedList<String>();
+        String[] dirs = jarRoot.list(new FilenameFilter() {
+            public boolean accept(File dir,
+                                  String name) {
+
+                String dirPath = dir.getAbsolutePath();
+                File checkFile = new File(dirPath + File.separator + name);
+                return checkFile.isDirectory() && !(name.equals(".") && !name.equals(".."));
+            }
+        });
+
+        for (String dir : dirs) {
+            retVal.addAll(scanPath(rootPath + File.separator + dir));
+        }
+
+        String[] foundNames = jarRoot.list(new FilenameFilter() {
+            public boolean accept(File dir,
+                                  String name) {
+
+                name = name.toLowerCase();
+                name = name.trim();
+                String dirPath = dir.getAbsolutePath();
+                File checkFile = new File(dirPath + File.separator + name);
+                return (!checkFile.isDirectory()) && name.endsWith(".jar") || name.endsWith(".zip");
+            }
+        });
+
+        for (String foundPath : foundNames) {
+            retVal.add(rootPath + File.separator + foundPath);
+        }
+        return retVal;
+    }
+
     private void initClasspath(ServletContext context) {
         String webInf = context.getRealPath(File.separator + "WEB-INF");
         StringBuilder classPath = new StringBuilder(255);
@@ -69,35 +123,70 @@
         classPath.append(File.separator);
         classPath.append("classes");
 
+        List<String> fileNames = new LinkedList<String>();
         if (jarRoot.exists()) {
             log.info("Scanning paths for possible java compiler classpaths");
-            //TODO make the scan recursive for directory trees
-            String[] fileNames = jarRoot.list(new FilenameFilter() {
-                public boolean accept(File dir,
-                                      String name) {
-                    name = name.toLowerCase();
-                    name = name.trim();
-                    return name.endsWith(".jar") || name.endsWith(".zip");
-                }
-            });
 
-            for (String name : fileNames) {
-                classPath.append(File.pathSeparator);
-                classPath.append(webInf);
-                classPath.append(File.separator);
-                classPath.append("lib");
-                classPath.append(File.separator);
-                classPath.append(name);
-            }
+            this.classPath = classPath.toString() + File.pathSeparatorChar + addExternalClassPaths(context) + File.pathSeparator + addStandardJarPaths(jarRoot) + addExternalJarPaths(context);
 
-            this.classPath = classPath.toString();
-            //TODO also go one level up to scan for the lib dir of the ear container
-            //TODO add additional jar scan paths via configuration
-            //for now this should do it
         } else {
             log.warn("web-inf/lib not found, you might have to adjust the jar scan paths manually");
         }
+    }
+
+    private String addStandardJarPaths(File jarRoot) {
+        List<String> fileNames = new LinkedList<String>();
+        StringBuilder retVal = new StringBuilder();
+        fileNames.addAll(scanPath(jarRoot.getAbsolutePath()));
+        int cnt = 0;
+        for (String classPath : fileNames) {
+            cnt++;
+            retVal.append(classPath);
+            if (cnt < fileNames.size()) {
+                retVal.append(File.pathSeparator);
+            }
+        }
+        return retVal.toString();
+    }
 
+    private String addExternalClassPaths(ServletContext context) {
+        String classPaths = context.getInitParameter(CUSTOM_CLASS_PATHS);
+        if (classPaths != null && !classPaths.trim().equals("")) {
+            String[] classPathArr = classPaths.split(",");
+            StringBuilder retVal = new StringBuilder();
+            int cnt = 0;
+            for (String classPath : classPathArr) {
+                cnt++;
+                retVal.append(classPath);
+                if (cnt < classPathArr.length) {
+                    retVal.append(File.pathSeparator);
+                }
+            }
+            return retVal.toString();
+        }
+        return "";
+    }
+
+
+    private String addExternalJarPaths(ServletContext context) {
+        List<String> fileNames = new LinkedList<String>();
+        String jarPaths = context.getInitParameter(CUSTOM_JAR_PATHS);
+        StringBuilder retVal = new StringBuilder();
+        if (jarPaths != null && !jarPaths.trim().equals("")) {
+            String[] jarPathsArr = jarPaths.split(",");
+            for (String jarPath : jarPathsArr) {
+                fileNames.addAll(scanPath(jarPath));
+            }
+        }
+        int cnt = 0;
+        for (String classPath : fileNames) {
+            cnt++;
+            retVal.append(classPath);
+            if (cnt < fileNames.size()) {
+                retVal.append(File.pathSeparator);
+            }
+        }
+        return retVal.toString();
     }
 
     /**
@@ -119,9 +208,6 @@
     }
 
 
-  
-
-
     /**
      * loads a class from a given sourceroot and filename
      *

Modified: myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/servlet/CustomChainLoader.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/servlet/CustomChainLoader.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/servlet/CustomChainLoader.java (original)
+++ myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/servlet/CustomChainLoader.java Thu Sep  3 13:59:03 2009
@@ -45,6 +45,7 @@
     static String CUSTOM_LOADER_PATHS = "org.apache.myfaces.scripting.groovy.LOADER_PATHS";
     static String CUSTOM_JAVA_LOADER_PATHS = "org.apache.myfaces.scripting.java.LOADER_PATHS";
 
+
     String classRoot = "";
     String scriptingRoot = "";
     ScriptingWeaver scriptingWeaver = null;

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java Thu Sep  3 13:59:03 2009
@@ -1,3 +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.
+ */
 package org.apache.myfaces.javaloader.blog;
 
 import org.apache.commons.logging.Log;
@@ -15,28 +33,33 @@
 @ScriptingClass
 public class Blog {
 
-    String title = "Hello to the myfaces dynamic blogging";
-    String title1 = "You can alter the code for this small blogging application on the fly, " +
-                    "you even can add new classes on the fly and Grooy will pick it up";
+    String title        =   "Hello to the myfaces dynamic blogging";
+    String title1       =   "You can alter the code for this small blogging application on the fly, " +
+                            "you even can add new classes on the fly and Java will pick it up";
+
+    String title3 = "bla";
+    String title4 = "bla2";
 
 
-    String firstName = "";
-    String lastName = "";
-    String topic = "";
+    String firstName    = "";
+    String lastName     = "";
+    String topic        = "";
 
-    String content = "";
+    String content      = "";
 
+    
 
     private Log getLog() {
         return LogFactory.getLog(this.getClass());
     }
 
 
-    public String addEntry() {
+    public String addEntry2 () {
         getLog().info("adding entry");
 
         Object service = JSFUtil.resolveVariable("javaBlogService");
 
+        
         if (service == null) {
             getLog().error("service not found");
         } else {
@@ -118,4 +141,20 @@
     public void setContent(String content) {
         this.content = content;
     }
+
+    public String getTitle3() {
+        return "aaaaa";
+    }
+
+    public void setTitle3(String title3) {
+        this.title3 = title3;
+    }
+
+    public String getTitle4() {
+        return title4;
+    }
+
+    public void setTitle4(String title4) {
+        this.title4 = title4;
+    }
 }

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogEntry.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogEntry.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogEntry.java (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogEntry.java Thu Sep  3 13:59:03 2009
@@ -1,3 +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.
+ */
 package org.apache.myfaces.javaloader.blog;
 
 /**

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogService.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogService.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogService.java (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/BlogService.java Thu Sep  3 13:59:03 2009
@@ -1,3 +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.
+ */
 package org.apache.myfaces.javaloader.blog;
 
 import org.apache.myfaces.scripting.loaders.java.ScriptingClass;
@@ -13,6 +31,7 @@
 @ScriptingClass
 public class BlogService {
 
+
     /**
      * note we cannot cast on dynamically referenced
      * and recompiled objects which are shared between beans

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java Thu Sep  3 13:59:03 2009
@@ -1,3 +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.
+ */
 package org.apache.myfaces.javaloader.blog;
 
 import org.apache.commons.logging.Log;
@@ -34,8 +52,20 @@
         Object elContext = executeFunction(facesContext, "getELContext");
         Object elResolver = executeFunction(elContext, "getELResolver");
 
-        log.info("ElResolver Instance:" + elResolver.toString());
         try {
+
+            /*
+             if you want to enable this then use
+             org.apache.myfaces.scripting.java.JAR_PATHS
+             pointing towards the lingering jars
+             The compiler cannot pick up the implicit containers classpaths
+
+             return FacesContext.getCurrentInstance().getELContext().getELResolver().getValue(FacesContext.getCurrentInstance().getELContext(), null, beanName);
+
+
+            */
+            //we use the introspection calls here to achieve our goal that way
+            //we can shift the dependency resolution from compile time to runtime
             return executeFunction(elResolver, "getValue", cast(ClassUtils.classForName("javax.el.ELContext"), elContext), nullCast(Object.class), cast(Object.class, beanName));
         } catch (ClassNotFoundException e) {
             throw new RuntimeException(e);
@@ -43,7 +73,4 @@
 
     }
 
-
-   
-
 }

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/web.xml?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/web.xml Thu Sep  3 13:59:03 2009
@@ -45,6 +45,7 @@
         <param-value>org.apache.myfaces.scripting.servlet.StartupServletContextPluginChainLoader</param-value>
     </context-param>
 
+
     <!--
     <context-param>
         <description>Additional comma separated loader paths to allow direct editing on the sources directory instead
@@ -56,6 +57,11 @@
     </context-param>
 
     <context-param>
+        <param-name>org.apache.myfaces.scripting.java.JAR_PATHS</param-name>
+        <param-value>/Users/werpu2/development/apache-tomcat-6.0.16/lib</param-value>
+    </context-param>
+
+    <context-param>
         <description>Additional comma separated loader paths to allow direct editing on the sources directory instead
             of the deployment dir
         </description>
@@ -63,7 +69,7 @@
         <param-value>/Users/werpu2/development/workspace/extensions-scripting/examples/src/main/webapp/WEB-INF/java
         </param-value>
     </context-param>
-    -->
+     -->
 
     <context-param>
         <description>State saving method: "client" or "server" (= default)

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/javablog.xhtml
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/javablog.xhtml?rev=810947&r1=810946&r2=810947&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/javablog.xhtml (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/javablog.xhtml Thu Sep  3 13:59:03 2009
@@ -10,6 +10,7 @@
 <ui:composition template = "/template.xhtml">
 
     <ui:define name = "body">
+        xxxx
         <h:form id = "form">
             <h:panelGrid id = "grid" columns = "1">
                 <h:outputText id = "title1" styleClass = "title" value = "#{javaBlogView.title}" />
@@ -26,7 +27,12 @@
                 <h:outputText value = "Content" /><h:inputText value = "#{javaBlogView.content}" />
 
 
-                <h:commandLink action = "#{javaBlogView.addEntry}" value = "Add Blog" />
+                <h:outputText value="#{javaBlogView.title3}" />
+
+                <h:outputText value="#{javaBlogView.title4}" />
+                
+
+                <h:commandLink action = "#{javaBlogView.addEntry2}" value = "Add Blog" />
             </h:panelGrid>
             <h:panelGrid columns = "1">
                 <h:outputText value = "Blog Entries" />