You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by dd...@apache.org on 2006/11/01 15:36:33 UTC

svn commit: r469920 - in /struts/sandbox/trunk/tiles/tiles-core/src: main/java/org/apache/tiles/preparer/ test/java/org/apache/tiles/preparer/

Author: ddewolf
Date: Wed Nov  1 06:36:33 2006
New Revision: 469920

URL: http://svn.apache.org/viewvc?view=rev&rev=469920
Log:
Modeling PreparerFactory similarly to DefinitionsFactory.  Providing identical extention points.

Added:
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java   (with props)
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java   (with props)
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java   (with props)
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java   (with props)
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java   (with props)

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java?view=auto&rev=469920
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java Wed Nov  1 06:36:33 2006
@@ -0,0 +1,95 @@
+/*
+ * $Id$
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.tiles.preparer;
+
+import org.apache.tiles.ViewPreparer;
+import org.apache.tiles.TilesRequestContext;
+import org.apache.tiles.util.RequestUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Default implementation of the {@link PreparerFactory}.
+ * This factory provides no contextual configuration.  It
+ * simply instantiates the named preparer and returns it.
+ */
+public class BasicPreparerFactory implements PreparerFactory {
+
+    private static final Log LOG =
+            LogFactory.getLog(BasicPreparerFactory.class);
+
+    private Map<String, ViewPreparer> preparers;
+
+    public BasicPreparerFactory() {
+        this.preparers = new HashMap<String, ViewPreparer>();
+    }
+
+
+    /**
+     * Create a new instance of the named preparer.  This factory
+     * expects all names to be qualified class names.
+     *
+     * @param name the named preparer
+     * @param context
+     * @return
+     * @throws NoSuchPreparerException
+     */
+    public ViewPreparer getPreparer(String name, TilesRequestContext context)
+            throws NoSuchPreparerException {
+
+        if (!preparers.containsKey(name)) {
+            preparers.put(name, createPreparer(name));
+        }
+
+        return preparers.get(name);
+    }
+
+    private ViewPreparer createPreparer(String name) throws NoSuchPreparerException {
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Creating ViewPreparer '" + name + "' . . .");
+        }
+
+        try {
+            Class requestedClass = RequestUtils.applicationClass(name);
+            Object instance = requestedClass.newInstance();
+            LOG.debug("ViewPreparer created successfully");
+            return (ViewPreparer) instance;
+
+        } catch (java.lang.ClassNotFoundException ex) {
+            throw new NoSuchPreparerException(
+                "Error - Class not found :" + ex.getMessage(), ex);
+
+        } catch (java.lang.IllegalAccessException ex) {
+            throw new NoSuchPreparerException(
+                "Error - Illegal class access :" + ex.getMessage(), ex);
+
+        } catch (java.lang.ClassCastException ex) {
+            throw new NoSuchPreparerException(
+                "ViewPreparer of class '" + name
+                 + "' should implements 'ViewPreparer' or extends 'Action'");
+        } catch (InstantiationException e) {
+            throw new NoSuchPreparerException(
+                "Error - Unable to instantiate ViewPreparer '"
+                    + name + "'. Does it have a default constructor?", e);
+        }
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/BasicPreparerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java?view=auto&rev=469920
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java Wed Nov  1 06:36:33 2006
@@ -0,0 +1,35 @@
+/*
+ * $Id$
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.tiles.preparer;
+
+/**
+ * Thrown when the named preparer can not be found.
+ *
+ * @since 2.0
+ * @version $Id$
+ */
+public class NoSuchPreparerException extends PreparerException {
+
+    public NoSuchPreparerException(String message) {
+        super(message);
+    }
+
+    public NoSuchPreparerException(String message, Exception e) {
+        super(message, e);
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/NoSuchPreparerException.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java?view=auto&rev=469920
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java Wed Nov  1 06:36:33 2006
@@ -0,0 +1,31 @@
+/*
+ * $Id$
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.tiles.preparer;
+
+import org.apache.tiles.TilesException;
+
+public class PreparerException extends TilesException {
+
+    public PreparerException(String message) {
+        super(message);
+    }
+
+    public PreparerException(String message, Exception e) {
+        super(message, e);
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerException.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java?view=auto&rev=469920
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java Wed Nov  1 06:36:33 2006
@@ -0,0 +1,49 @@
+/*
+ * $Id$
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.tiles.preparer;
+
+import org.apache.tiles.ViewPreparer;
+import org.apache.tiles.TilesRequestContext;
+
+/**
+ * Factory interface used to create/retrieve instances of
+ * the {@link ViewPreparer} interface.
+ *
+ * This factory provides an extension point into the default
+ * tiles implementation. Implementors wishing to provide
+ * per request initialization of the ViewPreparer (for instance)
+ * may provide a custom prerparer.
+ *
+ * @since 2.0
+ * @verion $Id$
+ */
+public interface PreparerFactory {
+
+    /**
+     * Create the named {link ViewPreparer} for the specified context.
+     *
+     * @param name ViewPreparer name, commonly the qualified classname.
+     * @param context the context within which the preparer will be invoked.
+     * @return instance of the ViewPreparer
+     * @throws NoSuchPreparerException when the named preparer can not be found.
+     */
+    ViewPreparer getPreparer(String name, TilesRequestContext context)
+        throws NoSuchPreparerException;
+
+
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/preparer/PreparerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date

Added: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java?view=auto&rev=469920
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java Wed Nov  1 06:36:33 2006
@@ -0,0 +1,47 @@
+/*
+ * $Id$
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.tiles.preparer;
+
+import junit.framework.TestCase;
+import org.apache.tiles.ViewPreparerSupport;
+import org.apache.tiles.ViewPreparer;
+
+public class BasicPreparerFactoryTest extends TestCase {
+
+    private BasicPreparerFactory factory;
+
+    public void setUp() {
+        factory = new BasicPreparerFactory();
+    }
+
+    public void testGetPreparer() throws NoSuchPreparerException {
+        String name = ViewPreparerSupport.class.getName();
+        ViewPreparer p = factory.getPreparer(name, null);
+        assertNotNull(p);
+        assertTrue(p instanceof ViewPreparerSupport);
+
+        name = "org.doesnotexist.Class";
+        try {
+            p = factory.getPreparer(name, null);
+            assertNull(p);
+            fail("Exception should have been thrown.");
+        } catch(NoSuchPreparerException n) {
+            //Good!
+        }
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/preparer/BasicPreparerFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date