Merge from trunk to TILES_2_1_X branch.

TILES-468
Changed the class of the causes.

TILES-469
Checking if a class of the porlet API is present. If not, this problem is trapped and the PortletTilesRequestContextFactory ignored.

git-svn-id: https://svn.apache.org/repos/asf/tiles/framework/branches/TILES_2_1_X@819649 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/tiles-api/src/main/java/org/apache/tiles/TilesException.java b/tiles-api/src/main/java/org/apache/tiles/TilesException.java
index 7a37d8a..24d3bf8 100644
--- a/tiles-api/src/main/java/org/apache/tiles/TilesException.java
+++ b/tiles-api/src/main/java/org/apache/tiles/TilesException.java
@@ -52,9 +52,9 @@
      * one, and its message will become the default message for
      * the TilesException.</p>
      *
-     * @param e The exception to be wrapped.
+     * @param e The cause to be wrapped.
      */
-    public TilesException(Exception e) {
+    public TilesException(Throwable e) {
         super(e);
     }
 
@@ -66,9 +66,9 @@
      * one, but the new exception will have its own message.</p>
      *
      * @param message The detail message.
-     * @param e       The exception to be wrapped.
+     * @param e       The cause to be wrapped.
      */
-    public TilesException(String message, Exception e) {
+    public TilesException(String message, Throwable e) {
         super(message, e);
     }
 }
diff --git a/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java b/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java
index 8a6278a..47ccaca 100644
--- a/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java
+++ b/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java
@@ -175,6 +175,11 @@
             if (log.isDebugEnabled()) {
                 log.debug("Cannot find class '" + className + "', ignoring problem", e);
             }
+        } catch (NotAvailableFeatureException e) {
+            if (log.isDebugEnabled()) {
+                log.debug("Not available feature exception during instantiation of class '"
+                        + className + "', ignoring problem", e);
+            }
         } catch (InstantiationException e) {
             throw new TilesContainerFactoryException(
                     "Cannot instantiate '" + className + "'", e);
diff --git a/tiles-core/src/main/java/org/apache/tiles/factory/NotAvailableFeatureException.java b/tiles-core/src/main/java/org/apache/tiles/factory/NotAvailableFeatureException.java
new file mode 100644
index 0000000..30ca030
--- /dev/null
+++ b/tiles-core/src/main/java/org/apache/tiles/factory/NotAvailableFeatureException.java
@@ -0,0 +1,73 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.factory;
+
+import org.apache.tiles.TilesException;
+
+/**
+ * Exception that indicates that a feature could not be used since it is not
+ * available.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.4
+ */
+public class NotAvailableFeatureException extends TilesException {
+
+    /**
+     * Constructor.
+     *
+     * @since 2.1.4
+     */
+    public NotAvailableFeatureException() {
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @since 2.1.4
+     */
+    public NotAvailableFeatureException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param e The cause to be wrapped.
+     * @since 2.1.4
+     */
+    public NotAvailableFeatureException(Throwable e) {
+        super(e);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @param e The cause to be wrapped.
+     * @since 2.1.4
+     */
+    public NotAvailableFeatureException(String message, Throwable e) {
+        super(message, e);
+    }
+}
diff --git a/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/NotAPortletEnvironmentException.java b/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/NotAPortletEnvironmentException.java
new file mode 100644
index 0000000..7463fc4
--- /dev/null
+++ b/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/NotAPortletEnvironmentException.java
@@ -0,0 +1,73 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.portlet.context;
+
+import org.apache.tiles.factory.NotAvailableFeatureException;
+
+/**
+ * Exception that indicates that a resource could not be used because it is not
+ * in a portlet environment.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.4
+ */
+public class NotAPortletEnvironmentException extends NotAvailableFeatureException {
+
+    /**
+     * Constructor.
+     *
+     * @since 2.1.4
+     */
+    public NotAPortletEnvironmentException() {
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @since 2.1.4
+     */
+    public NotAPortletEnvironmentException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param e The cause to be wrapped.
+     * @since 2.1.4
+     */
+    public NotAPortletEnvironmentException(Throwable e) {
+        super(e);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @param e The cause to be wrapped.
+     * @since 2.1.4
+     */
+    public NotAPortletEnvironmentException(String message, Throwable e) {
+        super(message, e);
+    }
+}
diff --git a/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java b/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java
index a17bed8..2e2d894 100644
--- a/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java
+++ b/tiles-portlet/src/main/java/org/apache/tiles/portlet/context/PortletTilesRequestContextFactory.java
@@ -21,14 +21,17 @@
 
 package org.apache.tiles.portlet.context;
 
-import org.apache.tiles.TilesApplicationContext;
-import org.apache.tiles.context.TilesRequestContext;
-import org.apache.tiles.context.TilesRequestContextFactory;
+import java.util.Map;
 
 import javax.portlet.PortletContext;
 import javax.portlet.PortletRequest;
 import javax.portlet.PortletResponse;
-import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.context.TilesRequestContextFactory;
 
 /**
  * Creates an instance of the appropriate {@link TilesRequestContext}
@@ -41,6 +44,11 @@
         TilesRequestContextFactory {
 
     /**
+     * The logging object.
+     */
+    private Log logger = LogFactory.getLog(getClass());
+
+    /**
      * The site of the request object array in case there is a request, a
      * response and a portlet context.
      *
@@ -48,6 +56,23 @@
      */
     private static final int REQUEST_OBJECTS_LENGTH = 3;
 
+    /**
+     * Constructor. To see if a portlet context is available, simply accesses a
+     * portlet class.
+     */
+    public PortletTilesRequestContextFactory() {
+        try {
+            if (logger.isDebugEnabled()) {
+                logger.debug("The portlet environment is available, "
+                        + "since the class " + PortletRequest.class
+                        + "is present");
+            }
+        } catch (NoClassDefFoundError e) {
+            throw new NotAPortletEnvironmentException(
+                    "Cannot access portlet classes", e);
+        }
+    }
+
     /** {@inheritDoc} */
     public void init(Map<String, String> configParameters) {
     }