EXTSCRIPT-154: Code Rewrite/Refactoring, loading classes now works


git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1298440 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingApplicationFactory.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingApplicationFactory.java
new file mode 100644
index 0000000..485c1be
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingApplicationFactory.java
@@ -0,0 +1,78 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.factories;
+
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.Decorated;
+import rewrite.org.apache.myfaces.extensions.scripting.core.context.WeavingContext;
+import rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.implementations.ApplicationProxy;
+
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+
+/**
+ * Application factory which introduces
+ * scripting proxies for their artefacts
+ * <p/>
+ * We use a mix of AOP and helper constructs
+ * to reach the goal to be dynamic.
+ * For most artefacts we just need to
+ * check if the object is a Groovy object
+ * and then reload at their connection interfaces
+ * <p/>
+ * Some artefacts have a longer lifespan and/or are stateless
+ * for those we have to work with reloading AOP
+ *
+ * @author Werner Punz
+ */
+public class ScriptingApplicationFactory extends ApplicationFactory implements Decorated
+{
+
+    ApplicationFactory _delegate;
+
+
+    public ScriptingApplicationFactory(ApplicationFactory delegate) {
+        _delegate = delegate;
+
+    }
+
+    public Application getApplication() {
+        Application retVal = _delegate.getApplication();
+
+        if (WeavingContext.getInstance().isScriptingEnabled()  && !(retVal instanceof ApplicationProxy))
+            retVal = new ApplicationProxy(retVal);
+
+        return retVal;
+    }
+
+    public void setApplication(Application application) {
+        if (WeavingContext.getInstance().isScriptingEnabled() && !(application instanceof ApplicationProxy))
+            application = new ApplicationProxy(application);
+
+        _delegate.setApplication(application);
+    }
+
+    @Override
+    public ApplicationFactory getWrapped() {
+        return _delegate.getWrapped();
+    }
+
+    public Object getDelegate() {
+        return _delegate;
+    }
+}
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingFacesContextFactory.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingFacesContextFactory.java
new file mode 100644
index 0000000..4f966db
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingFacesContextFactory.java
@@ -0,0 +1,66 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.factories;
+
+
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.Decorated;
+import rewrite.org.apache.myfaces.extensions.scripting.core.context.WeavingContext;
+import rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.implementations.FacesContextProxy;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.lifecycle.Lifecycle;
+
+/**
+ * Faces context weaver which builds
+ * our reloading proxy around the current faces context
+ *
+ * @author Werner Punz
+ */
+public class ScriptingFacesContextFactory extends FacesContextFactory implements Decorated
+{
+
+    public FacesContextFactory _delegate;
+
+    public ScriptingFacesContextFactory(FacesContextFactory delegate) {
+        _delegate = delegate;
+    }
+
+    public void setDelegate(FacesContextFactory delegate) {
+        _delegate = delegate;
+    }
+
+    public FacesContext getFacesContext(Object o, Object o1, Object o2, Lifecycle lifecycle) throws FacesException {
+        FacesContext retVal = _delegate.getFacesContext(o, o1, o2, lifecycle);
+
+        if (WeavingContext.getInstance().isScriptingEnabled()  && !(retVal instanceof FacesContextProxy))
+            return new FacesContextProxy(retVal);
+        return retVal;
+    }
+
+    @Override
+    public FacesContextFactory getWrapped() {
+        return _delegate.getWrapped();
+    }
+
+    public Object getDelegate() {
+        return _delegate;
+    }
+}
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingLifecycleFactory.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingLifecycleFactory.java
new file mode 100644
index 0000000..4a4b90f
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingLifecycleFactory.java
@@ -0,0 +1,74 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.factories;
+
+import org.apache.myfaces.extensions.scripting.api.Decorated;
+import org.apache.myfaces.extensions.scripting.core.util.WeavingContext;
+import rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.implementations.LifefcycleProxy;
+
+import javax.faces.lifecycle.Lifecycle;
+import javax.faces.lifecycle.LifecycleFactory;
+import java.util.Iterator;
+
+/**
+ * Lifecyclefactory which introduces scripting proxies
+ * for their artefacts
+ *
+ * @author Werner Punz
+ */
+public class ScriptingLifecycleFactory extends LifecycleFactory implements Decorated {
+
+    LifecycleFactory _delegate;
+
+
+    public ScriptingLifecycleFactory(LifecycleFactory delegate) {
+        _delegate = delegate;
+    }
+
+    public void addLifecycle(String s, Lifecycle lifecycle) {
+        if (WeavingContext.isScriptingEnabled()  && !(lifecycle instanceof LifefcycleProxy))
+            lifecycle = new LifefcycleProxy(lifecycle);
+        _delegate.addLifecycle(s, lifecycle);
+    }
+
+    public Lifecycle getLifecycle(String s) {
+        Lifecycle retVal = _delegate.getLifecycle(s);
+        if (WeavingContext.isScriptingEnabled()  && !(retVal instanceof LifefcycleProxy))
+            retVal = new LifefcycleProxy(retVal);
+
+        return retVal;
+    }
+
+    public Iterator getLifecycleIds() {
+        return _delegate.getLifecycleIds();
+    }
+
+    public void setDelegate(LifecycleFactory delegate) {
+        this._delegate = delegate;
+    }
+
+    @Override
+    public LifecycleFactory getWrapped() {
+        return _delegate.getWrapped();
+    }
+
+    public Object getDelegate() {
+        return _delegate;
+    }
+}
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingRenderkitFactory.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingRenderkitFactory.java
new file mode 100644
index 0000000..b6dfb56
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/factories/ScriptingRenderkitFactory.java
@@ -0,0 +1,76 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.factories;
+
+
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.Decorated;
+import rewrite.org.apache.myfaces.extensions.scripting.core.context.WeavingContext;
+import rewrite.org.apache.myfaces.extensions.scripting.jsf.dynamicdecorators.implementations.RenderkitProxy;
+
+import javax.faces.context.FacesContext;
+import javax.faces.render.RenderKit;
+import javax.faces.render.RenderKitFactory;
+import java.util.Iterator;
+
+/**
+ * Scripting enabled renderkit factory
+ *
+ * @author Werner Punz
+ */
+public class ScriptingRenderkitFactory extends RenderKitFactory implements Decorated
+{
+
+
+    public ScriptingRenderkitFactory(RenderKitFactory delegate) {
+        _delegate = delegate;
+    }
+
+    public void addRenderKit(String s, RenderKit renderKit) {
+        if (WeavingContext.getInstance().isScriptingEnabled() && renderKit != null && !(renderKit instanceof RenderkitProxy))
+            renderKit = new RenderkitProxy(renderKit);
+
+        _delegate.addRenderKit(s, renderKit);
+    }
+
+    public RenderKit getRenderKit(FacesContext facesContext, String s) {
+        RenderKit retVal = _delegate.getRenderKit(facesContext, s);
+        if (WeavingContext.getInstance().isScriptingEnabled() && retVal != null && !(retVal instanceof RenderkitProxy))
+            retVal = new RenderkitProxy(retVal);
+        return retVal;
+    }
+
+    public Iterator getRenderKitIds() {
+        return _delegate.getRenderKitIds();
+    }
+
+    public void setDelegate(RenderKitFactory delegate) {
+        _delegate = delegate;
+    }
+
+    @Override
+    public RenderKitFactory getWrapped() {
+        return _delegate.getWrapped();
+    }
+
+    RenderKitFactory _delegate = null;
+
+    public Object getDelegate() {
+        return _delegate;
+    }
+}