click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bck...@apache.org
Subject svn commit: r956671 - in /click/trunk/click: documentation/docs/ framework/src/org/apache/click/ framework/src/org/apache/click/control/ framework/src/org/apache/click/service/ framework/test/org/apache/click/
Date Mon, 21 Jun 2010 19:11:56 GMT
Author: bckfnn
Date: Mon Jun 21 19:11:55 2010
New Revision: 956671

URL: http://svn.apache.org/viewvc?rev=956671&view=rev
Log:
add plugable messages map service. CLK-655

Added:
    click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java
  (with props)
    click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java   (with
props)
Modified:
    click/trunk/click/documentation/docs/roadmap-changes.html
    click/trunk/click/framework/src/org/apache/click/Context.java
    click/trunk/click/framework/src/org/apache/click/Page.java
    click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java
    click/trunk/click/framework/src/org/apache/click/service/ConfigService.java
    click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java
    click/trunk/click/framework/test/org/apache/click/ConfigServiceTest.java

Modified: click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/click/trunk/click/documentation/docs/roadmap-changes.html?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ click/trunk/click/documentation/docs/roadmap-changes.html Mon Jun 21 19:11:55 2010
@@ -92,6 +92,11 @@ includes improved Ajax support and @Bind
         <b>New features and issues resolved:</b>
       </div>
       <ul style="padding: 0em; margin-left:0em;margin-bottom: 2em">
+         <li class="change">
+            Added <a href="click-api/org/apache/click/service/MessagesMapService.html">MessagesMapService</a>
+            interface to support plugable messages map implementations
+            [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-655">CLK-655</a>].
+          </li>
           <li class="change">
             Fixed the JavaScript function <tt>Click.submit(form, validate)</tt>
which incorrectly
             stated that the first argument is the form name. The first argument

Modified: click/trunk/click/framework/src/org/apache/click/Context.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/Context.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/Context.java (original)
+++ click/trunk/click/framework/src/org/apache/click/Context.java Mon Jun 21 19:11:55 2010
@@ -35,6 +35,7 @@ import javax.servlet.http.HttpSession;
 
 import org.apache.click.service.FileUploadService;
 import org.apache.click.service.LogService;
+import org.apache.click.service.MessagesMapService;
 import org.apache.click.service.TemplateService;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.FlashAttribute;
@@ -625,6 +626,21 @@ public class Context {
     }
 
     /**
+     * Return a new messages map for the given baseClass (a page or control)
+     * and the given global resource bundle name. 
+     *
+     * @param baseClass the target class
+     * @param globalResource the global resource bundle name     
+     * @return a new messages map with the messages for the target.
+     */
+    public Map<String, String> createMessagesMap(Class<?> baseClass, String globalResource)
{
+        MessagesMapService messagesMapService = 
+            clickServlet.getConfigService().getMessagesMapService();
+        
+        return messagesMapService.createMessagesMap(baseClass, globalResource);
+    }
+
+    /**
      * Returns a map of <tt>FileItem arrays</tt> keyed on request parameter
      * name for "multipart" POST requests (file uploads). Thus each map entry
      * will consist of one or more <tt>FileItem</tt> objects.

Modified: click/trunk/click/framework/src/org/apache/click/Page.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/Page.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/Page.java (original)
+++ click/trunk/click/framework/src/org/apache/click/Page.java Mon Jun 21 19:11:55 2010
@@ -179,7 +179,7 @@ public class Page implements Serializabl
     protected boolean headersEdited;
 
     /** The map of localized page resource messages. **/
-    protected transient MessagesMap messages;
+    protected transient Map<String, String> messages;
 
     /**
      * The page model. For Velocity templates the model is used to populate the
@@ -836,7 +836,7 @@ public class Page implements Serializabl
     public Map<String, String> getMessages() {
         if (messages == null) {
             if (getContext() != null) {
-                messages = new MessagesMap(getClass(), PAGE_MESSAGES);
+                messages = getContext().createMessagesMap(getClass(), PAGE_MESSAGES);
 
             } else {
                 String msg = "Context not set cannot initialize messages";

Modified: click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java Mon Jun
21 19:11:55 2010
@@ -525,7 +525,7 @@ public abstract class AbstractControl im
      */
     public Map<String, String> getMessages() {
         if (messages == null) {
-            messages = new MessagesMap(getClass(), CONTROL_MESSAGES);
+            messages = getContext().createMessagesMap(getClass(), CONTROL_MESSAGES);
         }
         return messages;
     }

Modified: click/trunk/click/framework/src/org/apache/click/service/ConfigService.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/service/ConfigService.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/service/ConfigService.java (original)
+++ click/trunk/click/framework/src/org/apache/click/service/ConfigService.java Mon Jun 21
19:11:55 2010
@@ -176,6 +176,13 @@ public interface ConfigService {
     public TemplateService getTemplateService();
 
     /**
+     * Return the application messages map service.
+     *
+     * @return the application messages Map service
+     */
+    public MessagesMapService getMessagesMapService();
+
+    /**
      * Return the Click application mode value: &nbsp;
      * <tt>["production", "profile", "development", "debug", "trace"]</tt>.
      *

Added: click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java?rev=956671&view=auto
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java
(added)
+++ click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java
Mon Jun 21 19:11:55 2010
@@ -0,0 +1,64 @@
+/*
+ * 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.click.service;
+
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.click.util.MessagesMap;
+
+/**
+ * Provides a default MessagesMapService which returns MessagesMap implementations
+ * of the messages map. 
+ */
+public class DefaultMessagesMapService implements MessagesMapService {
+
+    /**
+     * @see MessagesMapService#onInit(ServletContext)
+     *
+     * @param servletContext the application servlet context
+     * @throws Exception if an error occurs initializing the LogService
+     */
+    public void onInit(ServletContext servletContext) throws Exception {
+    }
+
+    /**
+     * @see MessagesMapService#onDestroy()
+     */
+    public void onDestroy() {
+    }
+
+    /**
+     * Return a MessagesMap instance for the target baseClass and global 
+     * resource name.
+     * 
+     * @param baseClass the target class
+     * @param globalResource the global resource bundle name
+     * 
+     * @return a MessagesMap instance.
+     * 
+     * @see MessagesMapService#createMessagesMap(Class, String)
+     * @see MessagesMap#MessagesMap(Class, String)
+     */
+    public Map<String, String> createMessagesMap(Class<?> baseClass,
+            String globalResource) {
+        return new MessagesMap(baseClass, globalResource);
+    }
+}

Propchange: click/trunk/click/framework/src/org/apache/click/service/DefaultMessagesMapService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java?rev=956671&view=auto
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java (added)
+++ click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java Mon Jun
21 19:11:55 2010
@@ -0,0 +1,82 @@
+/*
+ * 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.click.service;
+
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Provides a messages map factory service for the Click runtime.
+ *
+ * <h3>Configuration</h3>
+ * The default {@link MessagesMapService} implementation is {@link DefaultMessagesMapService}.
+ * <p/>
+ * You can instruct Click to use a different implementation by adding
+ * the following element to your <tt>click.xml</tt> configuration file.
+ *
+ * <pre class="codeConfig">
+ * &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
+ * &lt;click-app charset="UTF-8"&gt;
+ *
+ *     &lt;pages package="org.apache.click.examples.page"/&gt;
+ *
+ *     &lt;<span class="red">messages-map-service</span> classname="<span
class="blue">com.mycorp.CustomMessagesMapService</span>"/&gt;
+ *
+ * &lt;/click-app&gt; </pre>
+ *
+ * The class <tt>com.mycorp.CustomMessagesMapService</tt> might be defined as
follows:
+ *
+ * <pre class="prettyprint">
+ * package com.mycorp;
+ *
+ * public class CustomMessagesMapService implements MessagesMapService {
+ *
+ *     public Map<String, String> createMessagesMap(Class&lt;?&gt; baseClass,
String globalResource) {
+ *         return new MessagesMap(baseClass, globalResource); 
+ *     }
+ * } </pre>
+ */
+public interface MessagesMapService {
+    
+    /**
+     * Initialize the MessagesMapService with the given application servlet context.
+     * <p/>
+     * This method is invoked after the MessagesMapService has been constructed.
+     *
+     * @param servletContext the application servlet context
+     * @throws Exception if an error occurs initializing the LogService
+     */
+    public void onInit(ServletContext servletContext) throws Exception;
+
+    /**
+     * Destroy the MessagesMapService.
+     */
+    public void onDestroy();
+    
+    /**
+     * Return a new messages map for the given baseClass (a page or control)
+     * and the given global resource bundle name. 
+
+     * @param baseClass the target class
+     * @param globalResource the global resource bundle name     
+     * @return a new messages map with the messages for the target.
+     */
+    public Map<String, String> createMessagesMap(Class<?> baseClass, String globalResource);
+}

Propchange: click/trunk/click/framework/src/org/apache/click/service/MessagesMapService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java (original)
+++ click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java Mon Jun
21 19:11:55 2010
@@ -197,6 +197,9 @@ public class XmlConfigService implements
     /** The application TemplateService. */
     private TemplateService templateService;
 
+    /** The application TemplateService. */
+    private MessagesMapService messagesMapService;
+
     // --------------------------------------------------------- Public Methods
 
     /**
@@ -213,6 +216,7 @@ public class XmlConfigService implements
 
         // Set default logService early to log errors when services fail.
         logService = new ConsoleLogService();
+        messagesMapService = new DefaultMessagesMapService();
 
         InputStream inputStream = ClickUtils.getClickConfig(servletContext);
 
@@ -257,6 +261,9 @@ public class XmlConfigService implements
             // Load the Resource service
             loadResourceService(rootElm);
 
+            // Load the Messages Map service
+            loadMessagesMapService(rootElm);
+
             // Load the PageInterceptors
             loadPageInterceptors(rootElm);
 
@@ -278,6 +285,9 @@ public class XmlConfigService implements
         if (getResourceService() != null) {
             getResourceService().onDestroy();
         }
+        if (getMessagesMapService() != null) {
+            getMessagesMapService().onDestroy();
+        }
         if (getLogService() != null) {
             getLogService().onDestroy();
         }
@@ -341,6 +351,15 @@ public class XmlConfigService implements
     }
 
     /**
+     * @see ConfigService#getMessagesMapService()
+     *
+     * @return the messages map service
+     */
+    public MessagesMapService getMessagesMapService() {
+        return messagesMapService;
+    }
+    
+    /**
      * @see ConfigService#createFormat()
      *
      * @return a new format object
@@ -1659,6 +1678,39 @@ public class XmlConfigService implements
         templateService.onInit(servletContext);
     }
 
+    private void loadMessagesMapService(Element rootElm) throws Exception {
+        Element messagesMapServiceElm = ClickUtils.getChild(rootElm, "messages-map-service");
+
+        if (messagesMapServiceElm != null) {
+            Class messagesMapServiceClass = DefaultMessagesMapService.class;
+
+            String classname = messagesMapServiceElm.getAttribute("classname");
+
+            if (StringUtils.isNotBlank(classname)) {
+                messagesMapServiceClass = ClickUtils.classForName(classname);
+            }
+
+            messagesMapService = (MessagesMapService) messagesMapServiceClass.newInstance();
+
+            Map propertyMap = loadPropertyMap(messagesMapServiceElm);
+
+            for (Iterator i = propertyMap.keySet().iterator(); i.hasNext();) {
+                String name = i.next().toString();
+                String value = propertyMap.get(name).toString();
+
+                Ognl.setValue(name, messagesMapService, value);
+            }
+        }
+
+        if (getLogService().isDebugEnabled()) {
+            String msg = "initializing MessagesMapService: "
+                + messagesMapService.getClass().getName();
+            getLogService().debug(msg);
+        }
+
+        messagesMapService.onInit(servletContext);
+    }
+    
     private static Map loadPropertyMap(Element parentElm) {
         Map propertyMap = new HashMap();
 

Modified: click/trunk/click/framework/test/org/apache/click/ConfigServiceTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/ConfigServiceTest.java?rev=956671&r1=956670&r2=956671&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/ConfigServiceTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/ConfigServiceTest.java Mon Jun 21 19:11:55
2010
@@ -41,10 +41,13 @@ import org.apache.click.service.ClickRes
 import org.apache.click.service.CommonsFileUploadService;
 import org.apache.click.service.ConfigService;
 import org.apache.click.service.ConsoleLogService;
+import org.apache.click.service.DefaultMessagesMapService;
+import org.apache.click.service.MessagesMapService;
 import org.apache.click.service.VelocityTemplateService;
 import org.apache.click.service.ConfigService.AutoBinding;
 import org.apache.click.util.ErrorPage;
 import org.apache.click.util.Format;
+import org.apache.click.util.MessagesMap;
 
 /**
  * Tests for the XmlConfigService class.
@@ -506,6 +509,42 @@ public class ConfigServiceTest extends T
         deleteDir(tmpdir);
     }
 
+    public void testMessagesMapService() throws Exception {
+        File tmpdir = makeTmpDir();
+
+        PrintStream pstr = makeXmlStream(tmpdir, "WEB-INF/click.xml");
+        pstr.println("<click-app>");
+        pstr.println(" <pages/>");
+        pstr.println(" <messages-map-service classname='org.apache.click.ConfigServiceTest$MyMessagesMapService'/>");
+        pstr.println("</click-app>");
+        pstr.close();
+
+        MockContainer container = new MockContainer(tmpdir.getAbsolutePath());
+        container.start();
+
+        ConfigService config = container.getClickServlet().getConfigService();
+
+        assertTrue(config.getMessagesMapService() instanceof MyMessagesMapService);
+
+        container.stop();
+        deleteDir(tmpdir);
+    }
+    
+    static public class MyMessagesMapService implements MessagesMapService {
+
+        public void onInit(ServletContext servletContext) throws Exception {
+        }
+
+        public void onDestroy() {
+        }
+        
+        public Map<String, String> createMessagesMap(Class<?> baseClass,
+                String globalResource) {
+            return new MessagesMap(baseClass, globalResource);
+        }
+    }
+    
+    
     public void testControls() throws Exception {
         File tmpdir = makeTmpDir();
 



Mime
View raw message