What's New Here?


Spring MVC and JQuery for Ajax Form Validation

In this tutorial, we will see how to validate a form on server side using Ajax with Spring MVC and JQuery. Spring MVC provides a very convenient process for adoption of Ajax with annotation driven configuration. We will use this annotation driven configuration to send Ajax response in form of JSON data. The response will contain state of the form validation and error messages in any error exist in form data.Tools Used:Spring MVC 3.0.3 JQuery 1.4.2 Jackson 1.5.3In the example, we will add users with name and education to a list using Ajax and Spring MVC. The user data will be send to Spring MVC controller with the help of JQuery and the controller will return the full list of users added till time if there is no validation error in form data or the controller will return validation errors if they present in form data. So, we will learn two important things in this tutorial:How to validate form data using Ajax in Spring MVC with the help of JQuery? and how send back list of objects to Ajax call as response ?Domain Class for User Following is our User domain class that will hold the form data:package com.raistudies.domain;public class User {private String name = null; private String education = null;public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEducation() { return education; } public void setEducation(String education) { this.education = education; } }There are two fields in our User domain object name and education. Ajax response domain class for sending JSON response Following is the domain object that will we use to send response: package com.raistudies.domain;public class JsonResponse { private String status = null; private Object result = null; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Object getResult() { return result; } public void setResult(Object result) { this.result = result; }}It contain two properties, “status” and “result”. “status” field is of type String and will contain “FAIL” or “SUCCESS”. “result” will contain the other data that are to be send back to the browser. UserController.java package com.raistudies.controllers;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.validation.ValidationUtils; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;import com.raistudies.domain.JsonResponse; import com.raistudies.domain.User;@Controller public class UserController { private List<User> userList = new ArrayList<User>();@RequestMapping(value="/AddUser.htm",method=RequestMethod.GET) public String showForm(){ return "AddUser"; }@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST) public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result ){ JsonResponse res = new JsonResponse(); ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty."); ValidationUtils.rejectIfEmpty(result, "education", "Educatioan not be empty"); if(!result.hasErrors()){ userList.add(user); res.setStatus("SUCCESS"); res.setResult(userList); }else{ res.setStatus("FAIL"); res.setResult(result.getAllErrors()); }return res; }}showForm() is used to shoe the Add user form to browser. method addUser() is will handle the ajax request and validate the User object and if there is no validation error in form data it will set the userList object to result property of JsonResponse class with status as “SUCCESS“. But, if there are errors in form data it will extract list of errors from BindingResult object using getAllErrors() method and set to result property of JsonResponse class with status as “FAIL“. AddUser.jsp Following is the jsp page that calls the UserController using Ajax with the help of JQuery:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Add Users using ajax</title> <script src="<%=request.getContextPath() %>/js/jquery.js"></script> <script type="text/javascript"> var contexPath = "<%=request.getContextPath() %>"; </script> <script src="<%=request.getContextPath() %>/js/user.js"></script> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/style/app.css"> </head> <body> <h1>Add Users using Ajax ........</h1> <table> <tr><td colspan="2"><div id="error" class="error"></div></td></tr> <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr> <tr><td>Education : </td><td> <input type="text" id="education"><br/></td></tr> <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr> <tr><td colspan="2"><div id="info" class="success"></div></td></tr> </table> </body> </html>The jsp page includes a js file user.js that has been used for holding the definition of the JavaScript method doAjaxPost() which produces the ajax class and also use the response of the Ajax call to dynamically update the page data. user.js Following is the code for ajax class and interpreting the response received from Spring MVC controller: function doAjaxPost() { // get the form values var name = $('#name').val(); var education = $('#education').val();$.ajax({ type: "POST", url: contexPath + "/AddUser.htm", data: "name=" + name + "&education=" + education, success: function(response){ // we have the response if(response.status == "SUCCESS"){ userInfo = "<ol>"; for(i =0 ; i < response.result.length ; i++){ userInfo += "<br><li><b>Name</b> : " + response.result[i].name + ";<b> Education</b> : " + response.result[i].education; } userInfo += "</ol>"; $('#info').html("User has been added to the list successfully. " + userInfo); $('#name').val(''); $('#education').val(''); $('#error').hide('slow'); $('#info').show('slow'); }else{ errorInfo = ""; for(i =0 ; i < response.result.length ; i++){ errorInfo += "<br>" + (i + 1) +". " + response.result[i].code; } $('#error').html("Please correct following errors: " + errorInfo); $('#info').hide('slow'); $('#error').show('slow'); } }, error: function(e){ alert('Error: ' + e); } }); }$.ajax() method of JQuery has been used for making an Ajax call here which sends the form data name and education field values to Spring Controller. On success of the Ajax call, it first checks the status field of the response. Note that, the response object here is the JSON representation of the JsonResponse java object. If the status field of the response is “SUCCESS” then it iterates through the user list using the notation response.result[i] , note that the list object of java is converted to json array by Jackson library. If the status is “FAIL” the the result object will contain the validation errors which we can access using the notation response.result[i].code , here the code will return error message added in Spring controller. While running the example in tomcat 6 server, it will open following form :Ajax Validation FormJust click on the “Add Users” button without entering any value, it will show errors for the field as shown in the bellow image:Ajax Validation Show Error in PageNow enter any name and education and click on the “Add Users” button. It will add the user detail to list and also show the info of the whole user list on the page :Ajax Validation with Spring MVC and JQuery Success PageYou can also try the example by downloading the example from bellow links: Source: Download Source + Lib :Download Reference: Ajax Form Validation using Spring MVC and JQuery from our JCG partner Rahul Mondal at the Rai Studies blog....

Spring and Quartz: Multi-Job Scheduling Service

Job Scheduling is so important requirement for the applications. Especially in large-scale projects, working with a lot of jobs can be a problem. Spring and Quartz have brought significant benefits for the solution of this problem. This article shows how to schedule multi jobs easily by using Spring and Quartz. Used Technologies : JDK 1.6.0_21 Spring 3.1.1 Quartz 1.8.5 Maven 3.0.2 STEP 1 : CREATE MAVEN PROJECT A maven project is created as below. (It can be created by using Maven or IDE Plug-in)STEP 2 : LIBRARIES Spring dependencies are added to Maven’ s pom.xml. <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency>STEP 3 : CREATE TASK CLASSES A FirstTask Class is created. package com.otv.task; import org.apache.log4j.Logger; /** * First Task * * @author onlinetechvision.com * @since 24 Feb 2012 * @version 1.0.0 * */ public class FirstTask { private static Logger log = Logger.getLogger(FirstTask.class); /** * Execute this task * */ public void execute() { log.debug("FirstTask runs successfully..."); } }A SecondTask Class is created. package com.otv.task; import org.apache.log4j.Logger; /** * Second Task * * @author onlinetechvision.com * @since 24 Feb 2012 * @version 1.0.0 * */ public class SecondTask { private static Logger log = Logger.getLogger(SecondTask.class); /** * Execute this task * */ public void execute() { log.debug("SecondTask runs successfully..."); } }STEP 4 : CREATE ISchedulerService INTERFACE ISchedulerService Interface is created. package com.otv.service; /** * Scheduler Service Interface * * @author onlinetechvision.com * @since 24 Feb 2012 * @version 1.0.0 * */ public interface ISchedulerService { /** * Execute First Task * * @param * @throws * @return */ public void executeFirstTask(); /** * Execute Second Task * * @param * @throws * @return */ public void executeSecondTask(); }STEP 5 : CREATE SchedulerService CLASS SchedulerService Class is created by implementing ISchedulerService Interface. This service schedules tasks. package com.otv.service; import com.otv.task.FirstTask; import com.otv.task.SecondTask; /** * Scheduler Service Implementation * * @author onlinetechvision.com * @since 24 Feb 2012 * @version 1.0.0 * */ public class SchedulerService implements ISchedulerService { private FirstTask firstTask; private SecondTask secondTask; /** * Execute First Task * */ public void executeFirstTask() { getFirstTask().execute(); } /** * Execute Second Task * */ public void executeSecondTask() { getSecondTask().execute(); } /** * Get First Task * * @return FirstTask */ public FirstTask getFirstTask() { return firstTask; } /** * Set First Task * * @param firstTask First Task */ public void setFirstTask(FirstTask firstTask) { this.firstTask = firstTask; } /** * Get Second Task * * @return SecondTask */ public SecondTask getSecondTask() { return secondTask; } /** * Set Second Task * * @param secondTask Second Task */ public void setSecondTask(SecondTask secondTask) { this.secondTask = secondTask; } }STEP 6 : CREATE Application CLASS Application Class is created. This class runs the application. package com.otv.starter; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Starter Class * * @author onlinetechvision.com * @since 24 Feb 2012 * @version 1.0.0 * */ public class Application { /** * Main method of the Application * */ public static void main(String[] args) { new ClassPathXmlApplicationContext("applicationContext.xml"); } }STEP 7 : DEFINE Job Detail CONFIGURATIONS Job Details can be defined via two ways in Spring. By using MethodInvokingJobDetailFactoryBean or by extending QuartzJobBean. In this example, MethodInvokingJobDetailFactoryBean method has been used. targetObject and targetMethod properties are given to MethodInvokingJobDetailFactoryBean. <!-- Job Details--> <bean id="FirstTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="SchedulerService" /> <property name="targetMethod" value="executeFirstTask" /> </bean> <bean id="SecondTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="SchedulerService" /> <property name="targetMethod" value="executeSecondTask" /> </bean>STEP 8 : DEFINE Trigger CONFIGURATIONS Triggers can also be defined via two ways in Spring. By defining SimpleTriggerBean or CronTriggerBean . When SimpleTriggerBean is used, jobDetail, repeatInterval and startDelay properties are defined. When CronTriggerBean is used, jobDetail and cronExpression properties are defined. In this example, repeat interval of first task has been set 5 secs and repeat interval of second task has been set 12 secs . <!-- Simple Trigger --> <bean id="FirstSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">     <property name="jobDetail" ref="FirstTaskJobDetail" />     <property name="repeatInterval" value="5000" />     <property name="startDelay" value="1000" /> </bean>   <!-- <bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">         <property name="jobDetail" ref="SecondTaskJobDetail" />         <property name="repeatInterval" value="12000" />         <property name="startDelay" value="1000" />     </bean> -->      <!-- Cron Trigger --> <bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">     <property name="jobDetail" ref="SecondTaskJobDetail" />     <property name="cronExpression" value="0/12 * * * * ?" /> </bean>STEP 9 : DEFINE SchedulerFactoryBean CONFIGURATION Finally, Job Details and Triggers are configured by creating SchedulerFactoryBean. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="FirstTaskJobDetail" /> <ref bean="SecondTaskJobDetail" /> </list> </property> <property name="triggers"> <list> <ref bean="FirstSimpleTrigger" /> <ref bean="SecondSimpleTrigger" /> </list> </property> </bean>STEP 10 : CREATE applicationContext.xml All applicationContext.xml content is shown as below. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Beans Declaration --> <bean id="FirstTask" class="com.otv.task.FirstTask"></bean> <bean id="SecondTask" class="com.otv.task.SecondTask"></bean> <bean id="SchedulerService" class="com.otv.service.SchedulerService"> <property name="firstTask" ref="FirstTask" /> <property name="secondTask" ref="SecondTask" /> </bean> <!-- Job Details--> <bean id="FirstTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="SchedulerService" /> <property name="targetMethod" value="executeFirstTask" /> </bean> <bean id="SecondTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="SchedulerService" /> <property name="targetMethod" value="executeSecondTask" /> </bean> <!-- Simple Trigger --> <bean id="FirstSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="FirstTaskJobDetail" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean> <!-- <bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="SecondTaskJobDetail" /> <property name="repeatInterval" value="12000" /> <property name="startDelay" value="1000" /> </bean> --> <!-- Cron Trigger --> <bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="SecondTaskJobDetail" /> <property name="cronExpression" value="0/12 * * * * ?" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="FirstTaskJobDetail" /> <ref bean="SecondTaskJobDetail" /> </list> </property> <property name="triggers"> <list> <ref bean="FirstSimpleTrigger" /> <ref bean="SecondSimpleTrigger" /> </list> </property> </bean> </beans>STEP 11 : RUN PROJECT After Application Class is started, below output logs will be shown : 25.02.2012 00:17:18 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:23 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:24 DEBUG (SecondTask.java:22) - SecondTask runs successfully... 25.02.2012 00:17:28 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:33 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:36 DEBUG (SecondTask.java:22) - SecondTask runs successfully... 25.02.2012 00:17:38 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:43 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:48 DEBUG (SecondTask.java:22) - SecondTask runs successfully... 25.02.2012 00:17:48 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:53 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:17:58 DEBUG (FirstTask.java:23) - FirstTask runs successfully... 25.02.2012 00:18:00 DEBUG (SecondTask.java:22) - SecondTask runs successfully...STEP 12 : DOWNLOAD OTV_SpringQuartz_MultiJobScheduling Reference: Multi-Job Scheduling Service by using Spring and Quartz from our JCG partner Eren Avsarogullari at the Online Technology Vision blog....

Customized Internationalization (i18n) in Java

Internationalization (i18n) is very important in our software projects. It brings mainly these benefits:Externalizing UI strings into external files other than code files and so easy-to-manage UI content. Supporting multiple languages.In this post, a brief practical example of i18n will be given for Eclipse and Java projects, including customizing i18n mechanism to have more maintainable and encapsulated approach.First, we must have some classes which includes string values shown on user interface:An example UI classThen we must have an instance of an i18n utility class. This is generally one of the two in Java:java.util.ResourceBundle (doesn’t need spring dependency)ResourceBundle initializationorg.springframework.context.support.ResourceBundleMessageSource (has multiple word externalization capability (which will be told later)).ResourceBundleMessageSource initializationWe’ll use ResourceBundleMessageSource instance in this tutorial because of its extended capabilities.Then right click on the class and choose Source –> Externalize Strings. A window as below will be shown. Enter keys for strings into the right column. Keys will be started with class name as default. Keys must be unique on the system, so a predefined pattern should be applied (e.g. <class_name>.<type_id>.<description>)Eclipse string externalization windowClick Next–> Finish and your strings will be changed as follows. And also Messages class and externalizer properties file will be created automatically (auto-comments on the right are markers for eclipse which means “externalized”):Class with externalized stringsAuto-created i18n utility and property classesNow externalization is complete. But we want i18n, and we must support multiple languages. For this, define another properties file with location post-tag (e.g. “EN”, “FR”, “TR”, …), copy keys and fill values with new language and set locale of resource bundle in a proper place/action of your application (e.g. on settings window or login page):Multiple property files for each languageMessages_tr_TR.properties file contentSetting new locale to the resource bundleAs the last step, we want to encapsulate i18n utility class and also want to use a more  capable i18n utility (e.g. ResourceBundleMessageSource). For this, define a class as below:Customized and encapsulated message source (i18n utility) classFinally, change “Messages.getString” statements into your new instance:Class with externalized strings (with customized i18n utility class)You can also externalize parameterized strings using your class. Its usage will be as follows:Getting a parameterized string from i18n utilityDefining a parameterized string in property fileReference: Customized Internationalization (i18n) in Java – Step by Step from our JCG partner Cagdas Basaraner at the CodeBuild blog....

Client-side server monitoring with Jolokia and JMX

The choice of Java monitoring tools is tremendous (random selection and order powered by Google): javamelodypsi-probeJVisualVMjconsoleJAMonJava JMX Nagios Plugin N/A Besides, there are various dedicated tools e.g. for ActiveMQ, JBoss, Quartz scheduler, Tomcat/tcServer… So which one should you use as an ultimate monitoring dashboard? Well, none of them provide out-of-the-box features you might require. In some applications you have to constantly monitor the contents and size of a given JMS queue. Others are known to have memory or CPU problems. I have also seen software where system administrators had to constantly run some SQL query and check the results or even parse the logs to make sure some essential background process is running. Possibilities are endless, because it really depends on the software and its use-cases. To make matters worse, your customer doesn’t care about GC activity, number of open JDBC connections and whether this nasty batch process is not hanging. It should just work. In this post we will try to develop easy, cheap, but yet powerful management console. It will be built around the idea of a single binary result – it works or not. If this single health indicator is green, no need to go deeper. But! If it turned red, we can easily drill-down. It is possible because instead of showing hundreds of unrelated metrics we will group them in a tree-like structure. The health status of each node in a tree is as bad as the worst child. This way if anything bad happens with our application, it will bubble-up. We are not forcing system administrator to constantly monitor several metrics. We decide what is important and if even tiniest piece of our software is malfunctioning, it will pop-up. Compare this to a continuous integration server that does not have green/red builds and e-mail notifications. Instead you have to go to the server every other build and manually check whether the code is compiling and all tests were green. The logs and results are there, but why parse them and aggregate manually? This is what we are trying to avoid in our home-grown monitoring solution. As a foundation I have chosen (not for the first time) Jolokia JMX to HTTP bridge. JVM already provides the monitoring infrastructure so why reinvent it? Also thanks to Jolokia the whole dashboard can be implemented in JavaScript on the client side. This has several advantages: server footprint is minimal, also it allows us to rapidly tune metrics by adding them or changing alert thresholds. We’ll start by downloading various JMX metrics onto the client (browser). I have developed some small application for demonstration purposes employing as many technologies as possible – Tomcat, Spring, Hibernate, ActiveMQ, Quartz, etc. I am not using the built-in JavaScript client library for Jolokia as I found it a bit cumbersome. But as you can see it is just a matter of a single AJAX call to fetch great deal of metrics. function request() { var mbeans = [ "java.lang:type=Memory", "java.lang:type=MemoryPool,name=Code Cache", "java.lang:type=MemoryPool,name=PS Eden Space", "java.lang:type=MemoryPool,name=PS Old Gen", "java.lang:type=MemoryPool,name=PS Perm Gen", "java.lang:type=MemoryPool,name=PS Survivor Space", "java.lang:type=OperatingSystem", "java.lang:type=Runtime", "java.lang:type=Threading", 'Catalina:name="http-bio-8080",type=ThreadPool', 'Catalina:type=GlobalRequestProcessor,name="http-bio-8080"', 'Catalina:type=Manager,context=/jmx-dashboard,host=localhost', 'org.hibernate:type=Statistics,application=jmx-dashboard', "net.sf.ehcache:type=CacheStatistics,CacheManager=jmx-dashboard,name=org.hibernate.cache.StandardQueryCache", "net.sf.ehcache:type=CacheStatistics,CacheManager=jmx-dashboard,name=org.hibernate.cache.UpdateTimestampsCache", "quartz:type=QuartzScheduler,name=schedulerFactory,instance=NON_CLUSTERED", 'org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=requests', "com.blogspot.nurkiewicz.spring:name=dataSource,type=ManagedBasicDataSource" ]; return _.map(mbeans, function(mbean) { return { type:'read', mbean: mbean } }); } $.ajax({ url: 'jmx?ignoreErrors=true', type: "POST", dataType: "json", data: JSON.stringify(request()), contentType: "application/json", success: function(response) { displayRawData(response); } });Just to give you an overview what kind of information is accessible on the client side, we will first dump everything and display it on jQuery UI accordion: function displayRawData(fullResponse) { _(fullResponse).each(function (response) { var content = $('<pre/>').append(JSON.stringify(response.value, null, '\t')); var header = $('<h3/>').append($("<a/>", {href:'#'}).append(response.request.mbean)); $('#rawDataPanel'). append(header). append($('<div/>').append(content)); }); $('#rawDataPanel').accordion({autoHeight: false, collapsible: true}); }Remember that this is just for reference and debug purposes, we are not aiming to display endless list of JMX attributes.As you can see it is actually possible to implement complete jconsole port inside a browser with Jolokia and JavaScript… maybe next time (anyone care to help?). Back to our project, let’s pick few essential metrics and display them in a list:The list itself looks very promising. Instead of displaying charts or values I have assigned an icon to each metric (more on that later). But I don’t want to go through the whole list all the time. Why can’t I just have a single indicator that aggregates several metrics? Since we are already using jsTree, the transition is relatively simple:On the first screenshot you see a healthy system. There is really no need to drill down since Overall metric is green. However the situation is worse on the second screenshot. System load is alarmingly high, also the Swap space needs attention, but is less important. As you can see the former metrics bubbles up all the way to the overall, top metric. This way we can easily discover what is working incorrectly in our system. You might be wondering how did we achieved this pretty tree while at the beginning we only had raw JMX data? No magic here, see how am I constructing the tree: function buildTreeModel(jmx) { return new CompositeNode('Overall', [ new CompositeNode('Servlet container', [ new Node( 'Active HTTP sessions', jmx['Catalina:context=/jmx-dashboard,host=localhost,type=Manager'].activeSessions, Node.threshold(200, 300, 500) ), new Node( 'HTTP sessions create rate', jmx['Catalina:context=/jmx-dashboard,host=localhost,type=Manager'].sessionCreateRate, Node.threshold(5, 10, 50) ), new Node( 'Rejected HTTP sessions', jmx['Catalina:context=/jmx-dashboard,host=localhost,type=Manager'].rejectedSessions, Node.threshold(1, 5, 10) ), new Node( 'Busy worker threads count', jmx['Catalina:name="http-bio-8080",type=ThreadPool'].currentThreadsBusy, Node.relativeThreshold(0.85, 0.9, 0.95, jmx['Catalina:name="http-bio-8080",type=ThreadPool'].maxThreads) ) ]), //... new CompositeNode('External systems', [ new CompositeNode('Persistence', [ new Node( 'Active database connections', jmx['com.blogspot.nurkiewicz.spring:name=dataSource,type=ManagedBasicDataSource'].NumActive, Node.relativeThreshold(0.75, 0.85, 0.95, jmx['com.blogspot.nurkiewicz.spring:name=dataSource,type=ManagedBasicDataSource'].MaxActive) ) ]), new CompositeNode('JMS messaging broker', [ new Node( 'Waiting in "requests" queue', jmx['org.apache.activemq:BrokerName=localhost,Destination=requests,Type=Queue'].QueueSize, Node.threshold(2, 5, 10) ), new Node( 'Number of consumers', jmx['org.apache.activemq:BrokerName=localhost,Destination=requests,Type=Queue'].ConsumerCount, Node.threshold(0.2, 0.1, 0) ) ]) ]) ]); }The tree model is quite simple. Root node can have a list of child nodes. Every child node can be either a leaf representing a single evaluated JMX metric or a composite node representing set of grandchildren. Each grandchild can in turns be a leaf or yet another composite node. Yes, it is a simple example of Composite design pattern! However it is not obvious where Strategy pattern was used. Look closer, each leaf node object has three properties: label (what you see on the screen), value (single JMX metric) and an odd function Node.threshold(200, 300, 500)… What is it? It is actually a higher order function (function returning a function) used later to interpret JMX metric. Remember, the raw value is meaningless, it has to be interpreted and translated into good-looking icon indicator. Here is how this implementation works: Node.threshold = function(attention, warning, fatal) { if(attention > warning && warning > fatal) { return function(value) { if(value > attention) { return 1.0; } if(value > warning) { return 0.5; } if(value > fatal) { return 0.0; } else { return -1.0; } } } if(attention < warning && warning < fatal) { return function(value) { if(value < attention) { return 1.0; } if(value < warning) { return 0.5; } if(value < fatal) { return 0.0; } else { return -1.0; } } } throw new Error("All thresholds should either be increasing or decreasing: " + attention + ", " + warning + ", " + fatal); }Now it becomes clear. The function receives level thresholds and returns a function that translates them to number in -1:1 range. I could have returned icons directly but I wanted to abstract tree model from GUI representation. If you now go back to Node.threshold(200, 300, 500) example of Active HTTP sessions metric it is finally obvious: if the number of active HTTP sessions exceed 200, show attention icon instead of OK. If it exceeds 300, warning appears. Above 500 fatal icon will appear. This function is a strategy that understands the input and handles it somehow. Of course these values/functions are only examples, but this is where real hard work manifests – for each JMX metric you have to define a set of sane thresholds. Is 500 HTTP sessions a disaster or only a high load we can deal with? Is 90% CPU load problematic or maybe if it is really low we should start worrying? Once you fine-tune these levels it should no longer be required to monitor everything at the same time. Just look at the top level single metric. If it is green, have a break. If it is not, drill-down in few seconds to find what the real problem is. Simple and effective. And did I mention it does not require any changes on the server side (except adding Jolokia and mapping it to some URL)? Obviously this is just a small proof-of-concept, not a complete monitoring solution. However if you are interested in trying it out and improving, the whole source code is available - as always on my GitHub account. Reference: Client-side server monitoring with Jolokia and JMX from our JCG partner Tomasz Nurkiewicz at the Java and neighbourhood blog....

Maven Archetype Creation Tips

I recently needed to create some Maven Archetypes for the Turmeric SOA project. A Maven Archetype for those that don’t know is a way to generate a project based off some pre-canned project templates. In the case of the current Turmeric SOA archetype, it will create a multi-module maven project, that contains the Interface, and Service projects, along with a basic WSDL, and the POMs configured appropriately. This can be run either at the Command Line or through the New Maven Project wizard of m2eclipse. Other IDE’s such as NetBeans, IntelliJ, and others can use it as well, through their support of Maven Archetypes. The hardest thing with getting started with Maven Archetypes is the relatively poor documentation that exists. Beyond the most basic use (archetype:create-from-project), which creates an archetype given an existing maven project or parent-project, there isn’t much to go on. So here are some tips. Directory/Filename Substitution There are cases during template generation where you want to output different module names and sometimes different filenames. To do this rename the portion of the directory or file you want to have substitued with a variable name. For example, if you specify __rootArtifactId__ as part of a Directory name or Filename, what ever is entered as the artifactId during the mvn archetype:generate will be substituted. You can do the same with custom properties. Example. Built in properties There are some built in properties that can be used during substitution. These are:groupId artifactId rootArtifactId version package – a base java package name, that is placed in src/main/java during project creation.You can use these in your POM and other file templates as well. Archetype-metadata.xml The archetype-metadata.xml stored in the archetype-resources/META-INF/maven folder after the project is created, is where you need to tweak what you want generated and how. For modules, you need to tweak attributes:id – This is the name of the module that will be generated. dir – the template dir name – the artifact Id that will be put in the pom file.For the id and name attributes you can use the standard maven properities specification: ${someproperty}. For the dir, you need to use the special __someproperty__ notation. In most cases this will be __rootArtifactId__. An example from the Turmeric SOA archetype. Required Properties Required Properties is how you prompt for additional information that can be substituted in your templates. Any properties you specify will be prompted for during generation. You can pass them in from the command line as well using the -D option. Required properties are referenced in your templates as properties and are substituted during generation. Be careful of specifying default values, default values are not prompted for during generation. They can still be overridden using the -D option. Archetype Catalogs It is important to have an archetype catalog. If you are using Nexus 1.9.x it will automatically search your repositories and generate an archetype catalog for each repository. It scans for all archetypes and as soon as an archetype is deployed the catalog entry is updated. If you don’t have an archetype catalog already, maven can create one for you from your local repository. Just run mvn archetype:crawl. The output will be put in your .m2 repository. Archetypes are important as they allow others to know about your custom archetype, and provide a way for them to be shared and discovered. Hopefull with these few tips, you can spare yourself some of the headaches that I endured. My next task is to help the Minerva project have a few Archetypes created, so that eclipse projects can get started with Tycho a bit easier. Do you have other tips, or items to watch out for? If so, please feel free to add a comment. Reference: Maven Archetype Creation Tips from our JCG partner David Carver at the Intellectual Cramps blog....

Best Of The Week – 2012 – W08

Hello guys, Time for the “Best Of The Week” links for the week that just passed. Here are some links that drew Java Code Geeks attention: * Java.next: In this presentation, the argument that Java is still the best programming language for the JVM (if simplified idioms are used along with proper tooling) is posed. Also see The Most Powerful JVM Language Available and Hate Java? You’re fighting the wrong battle. * The ultimate guide to passwords: Nice article on passwords and security. It discusses password hashing, rainbow attacks, salted hashes, offline cracking, secure passwords (those complex enough to make cracking it uneconomical) and password vaults. Also check out Introduction to Strong Cryptography for a developer’s perspective on the matter. * Installation: Where Software Remains in the Dark Ages: In this article the author criticizes some bad practices related to software installation, such as irreversible changes in the system, extensive use of environment variables, forced reboots and others. * Tracking User Behavior with Google Analytics SDK for Android: This tutorial show how to use the Google Analytics SDK for Android in order to track Android application events like screen loads and button clicks in order to determine what your application’s users are doing. Also check out Android Game Postmortem – ArkDroid Development and “Android Full Application Tutorial” series. * Should All Web Traffic Be Encrypted?: Jeff Atwood discusses web traffic encryption and argues that over the medium to long term, adopting encrypted web connections as a standard for logged-in users is the healthiest direction for the future of the web. Also see Hints for writing secure code. * The 7 habits of highly effective developers: Nice article on the habits that effective developers should adopt, such as adopting proactivity, thinking win-win, beginning with the end in mind and others. * Hidden evils of Java’s String.split() and replace(): This article explores the performance of String’s methods split and replace. Regular expressions should be considered instead of those. Also check out String performance and Exact String Matching. * Smell my Code. Code smell ? What’s code smell?: Short, but to the point, article on code smell, i.e any symptom in the source code of a program that possibly indicates a deeper problem. Common coding smells include duplicated code, large class, feature envy and others. * Tailoring Spring for Custom Usage: This presentation uncovers some of the hooks available in the Spring framework: life cycles, scopes, beans, resources, XML marshallers, REST, transactions, caching, Spring Integration adapters, and others. * Figure out why is JAVA eating CPU?: Short guide on how to identify which Java thread is consuming the system’s CPU. It uses Java’s debugging tools (e.g. jstack) along with Linux’s built-in tools (e.g. ps, grep). * Getting out of the way – Monitoring: In this article, the author suggests that we should be working to make the monitoring happen automatically without Ops involvement. This means that Ops selects monitoring systems that allow for discovery of new metrics & automatic collection of those metrics without additional incremental work each time. Also see Devops has made Release and Deployment Cool. That’s all for this week. Stay tuned for more, here at Java Code Geeks. Cheers, Ilias Tsagklis...

Java 7: WatchService

Of all the new features in Java 7, one of the more interesting is the WatchService, adding the capability to watch a directory for changes. The WatchService maps directly to the native file event notification mechanism, if available. If a native event notification mechanism is not available, then the default implementation will use polling. As a result, the responsiveness, ordering of events and details available are implementation specific. (NOTE: There is a companion post on using the Guava EventBus to process WatchService events) Watching A Directory The Path interface implements the register method that takes a WatchService object and varargs of type WatchEvent.Kind as arguments. There are 4 events to watch for:ENTRY_CREATE ENTRY_DELETE ENTRY_MODIFY OVERFLOWWhile the first 3 types are self explanatory, OVERFLOW indicates that events may been lost or discarded. A WatchService is created by calling FileSystem.newWatchService(). Watching a directory is accomplished by registering a Path object with the WatchService: import static java.nio.file.StandardWatchEventKinds.*; Path path = Paths.get("/home"); WatchService watchService = FileSystems.getDefault().newWatchService(); WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);As we can see from the example, the register method returns a WatchKey object. The WatchKey is a token that represents the registration of the Path with the WatchService. The WatchKey As a result of the registration process, the WatchKey is in a ‘ready’ state and is considered valid. A WatchKey remains valid until one of the following occurs:WatchKey.cancel() is called. The directory being watched is no longer available. The WatchService object is closed.Checking For Changes When a change is detected, the WatchKey state is set to ‘signaled’ and it is placed in a queue for processing. Getting WatchKeys off the queue involves calling WatchService.poll() or WatchService.take(). Here is a basic example: private boolean notDone = true; while(notDone){ try{ WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS); List<WatchEvent.Kind<?>> events = watchKey.pollEvents(); for(WatchEvent event : events){ ...process the events } if(!watchKey.reset()){ ...handle situation no longer valid } }catch(InterruptedException e){ Thread.currentThread().interrupt(); }On line 5 we are calling the pollEvents method to retrieve all the events for this WatchKey object. On line 9 you’ll notice a call to the reset method. The reset method sets the WatchKey state back to ‘ready’ and returns a boolean indicating if the WatchKey is still valid. If there are any pending events, then the WatchKey will be immediately re-queued, otherwise it will remain in the ready state until new events are detected. Calling reset on a WatchKey that has been cancelled or is in the ready state will have no effect. If a WatchKey is canceled while it is queued, it will reamin in the queue until retrieved. Cancellation could also happen automatically if the directory was deleted or is no longer available. Processing Events Now that we have detected an event, how do we determine:On which directory did the event happen? (assuming more than one directory registered) What was the actual event? (assuming listening for more than one event) What was the target of the event, i.e what Path object was created,deleted or updated?Jumping in to line 6 in the previous example, we will parse the needed information from a WatchKey and a WatchEvent: //WatchKey watchable returns the calling Path object of Path.register Path watchedPath = (Path) watchKey.watchable(); //returns the event type StandardWatchEventKinds eventKind = event.kind(); //returns the context of the event Path target = (Path)event.context();On line 6 we see the WatchEvent.context method being invoked. The context method will return a Path object if the event was a creation, delete or update and will be relative to the watched directory. It’s important to know that when a event is received there is no guarantee that the program(s) performing the operation have completed, so some level of coordination may be required. Conclusion The WatchService is a very interesting feature of the new java.nio.file package in Java 7. That being said, there are two things that about the WatchService to keep in mind:The WatchService does not pick up events for sub-directories of a watched directory. We still need to poll the WatchService for events, rather than receive asynchronous notification.To address the above issues, there is a follow up post using the Guava EventBus to process the WatchService events. Thanks for your time and see you in the next post.Resourcesjava.nio.file package that contains the WatchService, WatchKey and WatchEvent objects discussed here. A unit test demonstrating the WatchServiceReference: What’s New in Java 7: WatchService from our JCG partner Bill Bejeck at the Random Thoughts On Coding blog....

Is Java’s String Class a God Object?

In October I wrote a blog entitled Top Trumps in God Objects where I talked about the discovery of an object I’d found with 167 disparate methods that linked this object with all other parts of the application and, as you’d expect, followed the general criteria for a God or Monster Object. It was recently pointed out to me that method count alone isn’t an indication that an object is a God Object, which is quite true. The comment also went on to site the String class as an object that isn’t a God Object, but has a large number of methods. This got me thinking: is the String class a well designed class in terms of the general rules of Object Oriented Design? Does it, in fact, break the single responsibility principle or the laws of demeter and is it tightly coupled to all other parts of an application? Is it a God Object? The easy way to answer this is to look at a String in terms of the Single Responsibility Principle, and the best way to do that is to count the number of method calls it has and figure out what they do. If you do that you’ll find that Java’s String class has 66 method calls in it’s repertoire and you may conclude that a number this high could be the first sign of a God Object. Next, if you look at these 66 method calls you can group them into several different areas of responsibility. For example there are: Conversion MethodsgetBytes(...) getChars(...) valueOf(...) toUpperCase(...) toLowerCase(...) toCharArray(...)Methods that split Stringsubstring(...) split(...) subSequence(...)Equivalency Methodsequals(...) matches(...) compareTo(...)Search MethodsindexOf(..) lastIndexOf(...) charAt(...) contains(...)Combining Methodsappend(...) concat(..) + +=…and more besides. Using the lists above, you could now write a CRC card for a String that goes something like this: “A String is responsible for combining itself with other Strings to create new Strings AND converting itself into other objects or arrays of objects AND splitting itself into different pieces AND comparing itself to other String objects AND performing searches on itself” Sticking to the letter of the law then the lists above show that String’s method calls break down into at least five areas of responsibility, which means that, by the book, the Single Responsibility Principle has been violated. Taking this to its logical conclusion, then you could refactor a String to look something like this….However, and this is the question, why isn’t Java’s String class composed of some data and a bunch of classes that operate on it? The answer is both a matter of opinion and multifaceted. Firstly, history… Java as grown over time as so has the String class; methods have been added piece-meal as the Java version number ticks ever upwards. Secondly, pragmatism. A String object in it’s current design may break the Single Responsibility Rule, but it doesn’t matter, it’s easier to leave it as it is and get on with writing the programs that count. Lastly, and I think that this is important, we all generally know what a String is. Its name and meaning is in common usage. If we had to define what a String’s responsible for using a CRC card, then we’d write something like: “a String is responsible for being s String”. A String may have 66 methods, but I believe that by general consensus it’s not a God Object. However, software is just a matter of opinions, so I’ll leave you answer the other questions I posed at the beginning of this blog and to make your own mind up… Reference: Is Java’s String Class a God Object? from our JCG partner Roger Hughes at the Captain Debug’s Blog ....

Ajax with Spring MVC 3 using Annotations and JQuery

Its always been fun for me to work with Ajax! Is not it ? I will make it easy for you to use Ajax with Spring MVC 3 and JQuery. This post will illustrate you how to use Ajax in real life practices of industrial coding. As usual, we will take an practical example of Ajax in Spring MVC 3 framework and will implement it and I will make the implementation easy by make you understand the topic. Let us see what is our example’s requirement and how Spring MVC 3 Ajax facility will fulfill it : In our example, we will make a list of students with name and highest education level, to send the list to the placement office so that the students can get chance. We will make the “Add Student Form” available to the student online so that they can submit their name online and get registered. As a lot of students will use the system, so the performance of the system may very much low. To increase to performance of the web application we will use Ajax with Spring MVC 3 Framework and JQuery. Following steps we have to go through to implement our example :First of all, we will create a domain class (User.java) that will hold the value of student information. After that we will create our controller class (UserListController.java) to handle HTTP request. Our controller will handle three types of requests. First, to show the “Add Student Form”, second to handle Ajax request came from “Add Student Form” and add the students to a list, third to show the student information as a list. Then, we will create jsp page (AddUser.jsp) to show “Add Student Form” that will use JQuery to send Ajax request to the Spring MVC Controller. The jsp will also confirm to the user that Student has been added to the list. Then, we will create a jsp (ShowUsers.jsp) that will list all users in the list.User.java User.java has two properties name and education to store the student information. Following is the code of User.java : package com.raistudies.domain;public class User {private String name = null; private String education = null; // Getter and Setter are omitted for making the code short }UserListController.java Controllers has three method to handle three request urls. “showForm” method handle the request for showing the form to the user. Bellow code shows the UserListController.java : package com.raistudies.controllers;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;import com.raistudies.domain.User;@Controller public class UserListController { private List<User> userList = new ArrayList<User>();@RequestMapping(value="/AddUser.htm",method=RequestMethod.GET) public String showForm(){ return "AddUser"; }@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST) public @ResponseBody String addUser(@ModelAttribute(value="user") User user, BindingResult result ){ String returnText; if(!result.hasErrors()){ userList.add(user); returnText = "User has been added to the list. Total number of users are " + userList.size(); }else{ returnText = "Sorry, an error has occur. User has not been added to list."; } return returnText; }@RequestMapping(value="/ShowUsers.htm") public String showUsers(ModelMap model){ model.addAttribute("Users", userList); return "ShowUsers"; } }“addUsers” is same as the controller method that handle form expect that it also contain annotation @ResponseBody, which tells Spring MVC that the String returned by the method is the response to the request, it does not have to find view for this string. So the retuning String will be send back to the browser as response and hence the Ajax request will work. “showUsers” method is used to show the list of the students to the user. AddUser.jsp AddUser.jsp contain a simple form to collect information about the student and uses JQerey JavaScript framework to generate Ajax request to the server. Following is the code in AddUser.jsp : <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Add Users using ajax</title> <script src="/AjaxWithSpringMVC2Annotations/js/jquery.js"></script> <script type="text/javascript"> function doAjaxPost() { // get the form values var name = $('#name').val(); var education = $('#education').val();$.ajax({ type: "POST", url: "/AjaxWithSpringMVC2Annotations/AddUser.htm", data: "name=" + name + "&education=" + education, success: function(response){ // we have the response $('#info').html(response); $('#name').val(''); $('#education').val(''); }, error: function(e){ alert('Error: ' + e); } }); } </script> </head> <body> <h1>Add Users using Ajax ........</h1> <table> <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr> <tr><td>Education : </td><td> <input type="text" id="education"><br/></td></tr> <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr> <tr><td colspan="2"><div id="info" style="color: green;"></div></td></tr> </table> <a href="/AjaxWithSpringMVC2Annotations/ShowUsers.htm">Show All Users</a> </body> </html>You may be little bit confused if you are not aware of JQuery. Here is the explanation of the JQuery code :var name = $(‘#name’).val(); : – here the $ is JQuery selector that uses to select any node in HTML whose identifier is passed as argument. If the identifier is a prefix with # that means it is a id of the HTML node. Here, $(‘#name’).val() contains the value of the HTML node whose is “name’. The text box in which user will enter her/his name is with is as name. so java script variable name will contain the name of the user. $.ajax() :- It is the method in $ variable of JQuery to call Ajax. It has five arguments here. First of all “type” which indicated the request type of Ajax. It can be POST or GET. Then, “url” which indicates the url to be hit of Ajax submission. “data” will contain the raw data to be sent to the server. “success” will contain the function code that has to be call if the request get success and server sends an response to the browser. “error” will contain the function code that has to be call if the request get any error. $(‘#info’).html(response); :- will set the response of the server in to the div. In this way “Hello” + name will be shown in the div whose id is “info“.ShowUsers.jsp Following are the code in ShowUsers.jsp to print all student information from a ArrayList to jsp page : <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Users Added using Ajax</title> </head> <body style="color: green;"> The following are the users added in the list :<br> <ul> <c:forEach items="${Users}" var="user"> <li>Name : <c:out value="${user.name}" />; Education : <c:out value="${user.education}"/> </c:forEach> </ul> </body> </html>Here, we have used JSTL core taglib to iterate through the ArrayList and show every value in browser.<c:forEach items=”${Users}” var=”user”> : tag is used for iterate through the ArrayList. Property “items” is used to define the bean on which the List object has been stored, so items=”${Users}” says that the users list is present in “Users” bean. “var” attribute says the name of the variable in which each user will be stored. <c:out value=”${user.name}” /> : As, a single user will be stored in variable name “user” so to print the name property in User object we use ${user.name}.app-config.xml Our Spring MVC configuration file should be able to handle annotation driven controllers. The configuration are as follows : <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- Scans the classpath of this application for @Components to deploy as beans --> <context:component-scan base-package="com.raistudies" /><!-- Configures the @Controller programming model --> <mvc:annotation-driven /><!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>Deploy the war file to tomcat 6 and hit the url in browser, you will get following page displayed :Ajax Form Using Spring MVC 3Fill student information :Ajax Filled Form Using Spring MVC 3After that click on “Add Users” button, you will get message that user has been added to the list :Ajax Form Submission Confirmation Spring MVC 3To show all student added to the list click on the button “Show All Users”, you will get following page :Show All Users Spring MVC 3That is all from Ajax using Spring MVC 3 and JQuery. You can download source from bellow link. Source : Download Reference: Ajax with Spring MVC 3 using Annotations and JQuery from our JCG partner Rahul Mondal at the Rai Studies blog....

Student Questions about Scala, Part 1

Preface I’m currently teaching a course on Applied Text Analysis and am using Scala as the programming language taught and used in the course. Rather than creating more tutorials, I figured I’d take a page from Brian Dunning’s playbook on his Skeptoid podcast (highly recommended) when he takes student questions. So, I had the students in the course submit questions about Scala that they had, based on the readings and assignments thus far. This post covers over half of them — the rest will be covered in a follow up post. I start with some of the more basic questions, and the questions and/or answers progressively get into more intermediate level topics. Suggestions and comments to improve any of the answers are very welcome! Basic Questions Q. Concerning addressing parts of variables: To address individual parts of lists, the numbering of the items is (List 0,1,2 etc.) That is, the first element is called “0?. It seems to be the same for Arrays and Maps, but not for Tuples- to get the first element of a Tuple, I need to use Tuple._1. Why is that? A. It’s just a matter of convention — tuples have used a 1-based index in other languages like Haskell, and it seems that Scala has adopted the same convention/tradition. See: http://stackoverflow.com/questions/6241464/why-are-the-indexes-of-scala-tuples-1-based Q. It seems that Scala doesn’t recognize the “b” boundary character as a regular expression. Is there something similar in Scala? A. Scala does recognize boundary characters. For example, the following REPL session declares a regex that finds “the” with boundaries, and successfully retrieves the three tokens of “the” in the example sentence. scala> val TheRE = """\bthe\b""".r TheRE: scala.util.matching.Regex = \bthe\bscala> val sentence = "She think the man is a stick-in-the-mud, but the man disagrees." sentence: java.lang.String = She think the man is a stick-in-the-mud, but the man disagrees.scala> TheRE.findAllIn(sentence).toList res1: List[String] = List(the, the, the)Q. Why doesn’t the method “split” work on args? Example: val arg = args.split(” “). Args are strings right, so split should work? A. The args variable is an Array, so split doesn’t work on them. Arrays are, in effect, already split. Q. What is the major difference between foo.mapValues(x=>x.length) and foo.map(x=>x.length). Some places one works and one does not. A. The map function works on all sequence types, including Seqs and Maps (note that Maps can be seen as sequences of Tuple2s). The mapValues function, however, only works on Maps. It is essentially a convenience function. As an example, let’s start with a simple Map from Ints to Ints. scala> val foo = List((1,2),(3,4)).toMap foo: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)Now consider the task of adding 2 to each value in the Map. This can be done with the map function as follows. scala> foo.map { case(key,value) => (key,value+2) } res5: scala.collection.immutable.Map[Int,Int] = Map(1 -> 4, 3 -> 6)So, the map function iterates over key/value pairs. We need to match both of them, and then output the key and the changed value to create the new Map. The mapValues function makes this quite a bit easier. scala> foo.mapValues(2+) res6: scala.collection.immutable.Map[Int,Int] = Map(1 -> 4, 3 -> 6)Returning to the question about computing the length using mapValues or map — then it is just a question of which values you are transforming, as in the following examples. scala> val sentence = "here is a sentence with some words".split(" ").toList sentence: List = List(here, is, a, sentence, with, some, words)scala> sentence.map(_.length) res7: List[Int] = List(4, 2, 1, 8, 4, 4, 5)scala> val firstCharTokens = sentence.groupBy(x=>x(0)) firstCharTokens: scala.collection.immutable.Map[Char,List] = Map(s -> List(sentence, some), a -> List(a), i -> List(is), h -> List(here), w -> List(with, words))scala> firstCharTokens.mapValues(_.length) res9: scala.collection.immutable.Map[Char,Int] = Map(s -> 2, a -> 1, i -> 1, h -> 1, w -> 2)Q. Is there any function that splits a list into two lists with the elements in the alternating positions of the original list? For example, MainList =(1,2,3,4,5,6) List1 = (1,3,5) List2 = (2,4,6) A. Given the exact main list you provided, one can use the partition function and use the modulo operation to see whether the value is divisible evenly by 2 or not. scala> val mainList = List(1,2,3,4,5,6) mainList: List[Int] = List(1, 2, 3, 4, 5, 6)scala> mainList.partition(_ % 2 == 0) res0: (List[Int], List[Int]) = (List(2, 4, 6),List(1, 3, 5))So, partition returns a pair of Lists. The first has all the elements that match the condition and the second has all the ones that do not. Of course, this wouldn’t work in general for Lists that have Strings, or that don’t have Ints in order, etc. However, the indices of a List are always well-behaved in this way, so we just need to do a bit more work by zipping each element with its index and then partitioning based on indices. scala> val unordered = List("b","2","a","4","z","8") unordered: List = List(b, 2, a, 4, z, 8)scala> unordered.zipWithIndex res1: List[(java.lang.String, Int)] = List((b,0), (2,1), (a,2), (4,3), (z,4), (8,5))scala> val (evens, odds) = unordered.zipWithIndex.partition(_._2 % 2 == 0) evens: List[(java.lang.String, Int)] = List((b,0), (a,2), (z,4)) odds: List[(java.lang.String, Int)] = List((2,1), (4,3), (8,5))scala> evens.map(_._1) res2: List = List(b, a, z)scala> odds.map(_._1) res3: List = List(2, 4, 8)Based on this, you could of course write a function that does this for any arbitrary list. Q. How to convert a List to a Vector and vice-versa? A. Use toIndexSeq and toList. scala> val foo = List(1,2,3,4) foo: List[Int] = List(1, 2, 3, 4)scala> val bar = foo.toIndexedSeq bar: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)scala> val baz = bar.toList baz: List[Int] = List(1, 2, 3, 4)scala> foo == baz res0: Boolean = trueQ. The advantage of a vector over a list is the constant time look-up. What is the advantage of using a list over a vector? A. A List is slightly faster for operations at the head (front) of the sequence, so if all you are doing is doing a traversal (accessing each element in order, e.g. when mapping), then Lists are perfectly adequate and may be more efficient. They also have some nice pattern matching behavior for case statements. However, common wisdom seems to be that you should default to using Vectors. See Daniel Spiewak’s nice answer on Stackoverflow: http://stackoverflow.com/questions/6928327/when-should-i-choose-vector-in-scala Q. With splitting strings, holmes.split(“\\s”) – \n and \t just requires a single ‘\’ to recognize its special functionality but why two ‘\’s are required for white space character? A. That’s because \n and \t actually mean something in a String. scala> println("Here is a line with a tab\tor\ttwo, followed by\na new line.") Here is a line with a tab or two, followed by a new line.scala> println("This will break\s.") <console>:1: error: invalid escape character println("This will break\s.")So, you are supplying a String argument to split, and it uses that to construct a regular expression. Given that \s is not a string character, but is a regex metacharacter, you need to escape it. You can of course use split(“””\s”””), though that isn’t exactly better in this case. Q. I have long been programming in C++ and Java. Therefore, I put semicolon at the end of the line unconsciously. It seems that the standard coding style of Scala doesn’t recommend to use semicolons. However, I saw that there are some cases that require semicolons as you showed last class. Is there any specific reason why semicolon loses its role in Scala? A. The main reason is to improve readability since the semicolon is rarely needed when writing standard code in editors (as opposed to one liners in the REPL). However, when you want to do something in a single line, like handling multiple cases, you need the semicolons. scala> val foo = List("a",1,"b",2) foo: List[Any] = List(a, 1, b, 2)scala> foo.map { case(x: String) => x; case(x: Int) => x.toString } res5: List[String] = List(a, 1, b, 2)But, in general, it’s best to just split these cases over multiple lines in any actual code. Q. Is there no way to use _ in map like methods for collections that consist of pairs? For example, List((1,1),(2,2)).map(e => e._1 + e._2) works, but List((1,1),(2,2)).map(_._1 + _._2) does not work. A. The scope in which the _ remains unanambigious runs out past its first invocation, so you only get to use it once. It is better anyway to use a case statement that makes it clear what the members of the pairs are. scala> List((1,1),(2,2)).map { case(num1, num2) => num1+num2 } res6: List[Int] = List(2, 4)Q. I am unsure about the exact meaning of and the difference between “=>” and “->”. They both seem to mean something like “apply X to Y” and I see that each is used in a particular context, but what is the logic behind that? A. The use of -> simply constructs a Tuple2, as is pretty clear in the following snippet. scala> val foo = (1,2) foo: (Int, Int) = (1,2)scala> val bar = 1->2 bar: (Int, Int) = (1,2)scala> foo == bar res11: Boolean = truePrimarily, it is syntactic sugar that provides an intuitive symbol for creating elements of a a Map. Compare the following two ways of declaring the same Map. scala> Map(("a",1),("b",2)) res9: scala.collection.immutable.Map = Map(a -> 1, b -> 2)scala> Map("a"->1,"b"->2) res10: scala.collection.immutable.Map = Map(a -> 1, b -> 2)The second seems more readable to me. The use of => indicates that you are defining a function. The basic form is ARGUMENTS => RESULT. scala> val addOne = (x: Int) => x+1 addOne: Int => Int = <function1>scala> addOne(2) res7: Int = 3scala> val addTwoNumbers = (num1: Int, num2: Int) => num1+num2 addTwoNumbers: (Int, Int) => Int = <function2>scala> addTwoNumbers(3,5) res8: Int = 8Normally, you use it in defining anonymous functions as arguments to functions like map, filter, and such. Q. Is there a more convenient way of expressing vowels as [AEIOUaeiou] and consonants as [BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz] in RegExes? A. You can use Strings when defining regexes, so you can have a variable for vowels and one for consonants. scala> val vowel = "[AEIOUaeiou]" vowel: java.lang.String = [AEIOUaeiou]scala> val consonant = "[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]" consonant: java.lang.String = [BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]scala> val MyRE = ("("+vowel+")("+consonant+")("+vowel+")").r MyRE: scala.util.matching.Regex = ([AEIOUaeiou])([BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz])([AEIOUaeiou])scala> val MyRE(x,y,z) = "aJE" x: String = a y: String = J z: String = EQ. The “\b” in RegExes marks a boundary, right? So, it also captures the “-”. But if I have a single string “sdnfeorgn”, it does NOT capture the boundaries of that, is that correct? And if so, why doesn’t it? A. Because there are no boundaries in that string! Intermediate questions Q. The flatMap function takes lists of lists and merges them to single list. But in the example: scala> (1 to 10).toList.map(x=>squareOddNumber(x)) res16: List[Option[Int]] = List(Some(1), None, Some(9), None, Some(25), None, Some(49), None, Some(81), None)scala> (1 to 10).toList.flatMap(x=>squareOddNumber(x)) res17: List[Int] = List(1, 9, 25, 49, 81)Here it is not list of list but just a list. In this case it expects the list to be Option list. I tried running the code with function returning just number or None. It showed error. So is there any way to use flatmap without Option lists and just list. For example, List(1, None, 9, None, 25) should be returned as List(1, 9, 25). A. No, this won’t work because List(1, None, 9, None, 25) mixes Options with Ints. scala> val mixedup = List(1, None, 9, None, 25) mixedup: List[Any] = List(1, None, 9, None, 25)So, you should have your function return an Option which means returning Somes or Nones. Then flatMap will work happily. One way of think of Options is that they are like Lists with zero or one element, as can be noted by the parallels in the following snippet. scala> val foo = List(List(1),Nil,List(3),List(6),Nil) foo: List[List[Int]] = List(List(1), List(), List(3), List(6), List())scala> foo.flatten res12: List[Int] = List(1, 3, 6)scala> val bar = List(Option(1),None,Option(3),Option(6),None) bar: List[Option[Int]] = List(Some(1), None, Some(3), Some(6), None)scala> bar.flatten res13: List[Int] = List(1, 3, 6)Q. Does scala have generic templates (like C++, Java)? eg. in C++, we can use vector<int>, vector<string> etc. Is that possible in scala? If so, how? A. Yes, every collection type is parameterized. Notice that each of the following variables is parameterized by the type of the elements they are initialized with. scala> val foo = List(1,2,3) foo: List[Int] = List(1, 2, 3)scala> val bar = List("a","b","c") bar: List = List(a, b, c)scala> val baz = List(true, false, true) baz: List[Boolean] = List(true, false, true)You can create your own parameterized classes straightforwardly. scala> class Flexible[T] (val data: T) defined class Flexiblescala> val foo = new Flexible(1) foo: Flexible[Int] = Flexible@7cd0570escala> val bar = new Flexible("a") bar: Flexible = Flexible@31b6956fscala> val baz = new Flexible(true) baz: Flexible[Boolean] = Flexible@5b58539fscala> foo.data res0: Int = 1scala> bar.data res1: java.lang.String = ascala> baz.data res2: Boolean = trueQ. How can we easily create, initialize and work with multi-dimensional arrays (and dictionaries)? A. Use the fill function of the Array object to create them. scala> Array.fill(2)(1.0) res8: Array[Double] = Array(1.0, 1.0)scala> Array.fill(2,3)(1.0) res9: Array[Array[Double]] = Array(Array(1.0, 1.0, 1.0), Array(1.0, 1.0, 1.0))scala> Array.fill(2,3,2)(1.0) res10: Array[Array[Array[Double]]] = Array(Array(Array(1.0, 1.0), Array(1.0, 1.0), Array(1.0, 1.0)), Array(Array(1.0, 1.0), Array(1.0, 1.0), Array(1.0, 1.0)))Once you have these in hand, you can iterate over them as usual. scala> val my2d = Array.fill(2,3)(1.0) my2d: Array[Array[Double]] = Array(Array(1.0, 1.0, 1.0), Array(1.0, 1.0, 1.0))scala> my2d.map(row => row.map(x=>x+1)) res11: Array[Array[Double]] = Array(Array(2.0, 2.0, 2.0), Array(2.0, 2.0, 2.0))For dictionaries (Maps), you can use mutable HashMaps to create an empty Map and then add elements to it. For that, see this blog post: http://bcomposes.wordpress.com/2011/09/19/first-steps-in-scala-for-beginning-programmers-part-8/ Q. Is the apply function similar to constructor in C++, Java? Where will the apply function be practically used? Is it for intialising values of attributes? A. No, the apply function is like any other function except that it allows you to call it without writing out “apply”. Consider the following class. class AddX (x: Int) { def apply(y: Int) = x+y override def toString = "My number is " + x }Here’s how we can use it. scala> val add1 = new AddX(1) add1: AddX = My number is 1scala> add1(4) res0: Int = 5scala> add1.apply(4) res1: Int = 5scala> add1.toString res2: java.lang.String = My number is 1So, the apply method is just (very handy) syntactic sugar that allows you to specify one function as fundamental to a class you have designed (actually, you can have multiple apply methods as long as each one has a unique parameter list). For example, with Lists, the apply method returns the value at the index provided, and for Maps it returns the value associated with the given key. scala> val foo = List(1,2,3) foo: List[Int] = List(1, 2, 3)scala> foo(2) res3: Int = 3scala> foo.apply(2) res4: Int = 3scala> val bar = Map(1->2,3->4) bar: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)scala> bar(1) res5: Int = 2scala> bar.apply(1) res6: Int = 2Q. In the SBT tutorial you discuss “Node” and “Value” as being case classes. What is the alternative to a case class? A. A normal class. Case classes are the special case. They do two things (and more) for you. The first is that you don’t have to use “new” to create a new object. Consider the following otherwise identical classes. scala> class NotACaseClass (val data: Int) defined class NotACaseClassscala> case class IsACaseClass (val data: Int) defined class IsACaseClassscala> val foo = new NotACaseClass(4) foo: NotACaseClass = NotACaseClass@a5c0f8fscala> val bar = IsACaseClass(4) bar: IsACaseClass = IsACaseClass(4)That may seem like a little thing, but it can significantly improve code readability. Consider creating Lists within Lists within Lists if you had to use “new” all the time, for example. This is definitely true for Node and Value, which are used to build trees. Case classes also support matching, as in the following. scala> val IsACaseClass(x) = bar x: Int = 4A normal class cannot do this. scala> val NotACaseClass(x) = foo <console>:13: error: not found: value NotACaseClass val NotACaseClass(x) = foo ^ <console>:13: error: recursive value x needs type val NotACaseClass(x) = foo ^If you mix the case class into a List and map over it, you can match it like you can with other classes, like Lists and Ints. Consider the following heterogeneous List. scala> val stuff = List(IsACaseClass(3), List(2,3), IsACaseClass(5), 4) stuff: List[Any] = List(IsACaseClass(3), List(2, 3), IsACaseClass(5), 4)We can convert this to a List of Ints by processing each element according to its type by matching. scala> stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x } <console>:13: warning: match is not exhaustive! missing combination * Nil * *stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x } ^warning: there were 1 unchecked warnings; re-run with -unchecked for details res10: List[Any] = List(3, 2, 5, 4)If you don’t want to see the warning in the REPL, add a case for things that don’t match that throws a MatchError. scala> stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x; case _ => throw new MatchError } warning: there were 1 unchecked warnings; re-run with -unchecked for details res13: List[Any] = List(3, 2, 5, 4)Better yet, return Options (using None for the unmatched case) and flatMapping instead. scala> stuff.flatMap { case List(x,y) => Some(x); case IsACaseClass(x) => Some(x); case x: Int => Some(x); case _ => None } warning: there were 1 unchecked warnings; re-run with -unchecked for details res14: List[Any] = List(3, 2, 5, 4)Q. In C++ the default access specifier is private; in Java one needs to specify private or public for each class member where as in Scala the default access specifier for a class is public. What could be the design motivation behind this when one of the purpose of the class is data hiding? A. The reason is that Scala has a much more refined access specification scheme than Java that makes public the rational choice. See the discussion here: http://stackoverflow.com/questions/4656698/default-public-access-in-scala Another key aspecte of this is that the general emphasis in Scala is on using immutable data structures, so there isn’t any danger of someone changing the internal state of your objects if you have designed them in this way. This in turn gets rid of the ridiculous getter and setter methods that breed and multiply in Java programs. See “Why getters and setters are evil” for more discussion: http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html After you get used to programming in Scala, the whole getter/setter thing that is so common in Java code is pretty much gag worthy. In general, it is still a good idea to use private[this] as a modifier to methods and variables whenever they are only needed by an object itself. Q. How do we define overloaded constructors in Scala? Q. The way a class is defined in Scala introduced in the tutorial, seems to have only one constructor. Is there any way to provide multiple constructors like Java? A. You can add additional constructors with this declarations. class SimpleTriple (x: Int, y: Int, z: String) { def this (x: Int, z: String) = this(x,0,z) def this (x: Int, y: Int) = this(x,y,"a") override def toString = x + ":" + y + ":" + z }scala> val foo = new SimpleTriple(1,2,"hello") foo: SimpleTriple = 1:2:helloscala> val bar = new SimpleTriple(1,"goodbye") bar: SimpleTriple = 1:0:goodbyescala> val baz = new SimpleTriple(1,3) baz: SimpleTriple = 1:3:aNotice that you must supply an initial value for every one of the parameters of the class. This contrasts with Java, which allows you to leave some fields uninitialized (and which tends to lead to nasty bugs and bad design). Note that you can also provide defaults to parameters. class SimpleTripleWithDefaults (x: Int, y: Int = 0, z: String = "a") { override def toString = x + ":" + y + ":" + z }scala> val foo = new SimpleTripleWithDefaults(1) foo: SimpleTripleWithDefaults = 1:0:ascala> val bar = new SimpleTripleWithDefaults(1,2) bar: SimpleTripleWithDefaults = 1:2:aHowever, you can’t omit a middle parameter while specifying the last one. scala> val foo = new SimpleTripleWithDefaults(1,"xyz") <console>:12: error: type mismatch; found : java.lang.String("xyz") required: Int Error occurred in an application involving default arguments. val foo = new SimpleTripleWithDefaults(1,"xyz") ^But, you can name the parameters in the initialization if you want to be able to do this. scala> val foo = new SimpleTripleWithDefaults(1,z="xyz") foo: SimpleTripleWithDefaults = 1:0:xyzYou then have complete freedom to change the parameters around. scala> val foo = new SimpleTripleWithDefaults(z="xyz",x=42,y=3) foo: SimpleTripleWithDefaults = 42:3:xyzQ. I’m still not clear on the difference between classes and traits. I guess I see a conceptual difference but I don’t really understand what the functional difference is — how is creating a “trait” different from creating a class with maybe fewer methods associated with it? A. Yes, they are different. First off, traits are abstract, which means you cannot create any members. Consider the following contrast. scala> class FooClass defined class FooClassscala> trait FooTrait defined trait FooTraitscala> val fclass = new FooClass fclass: FooClass = FooClass@1b499616scala> val ftrait = new FooTrait <console>:8: error: trait FooTrait is abstract; cannot be instantiated val ftrait = new FooTrait ^You can extend a trait to make a concrete class, however. scala> class FooTraitExtender extends FooTrait defined class FooTraitExtenderscala> val ftraitExtender = new FooTraitExtender ftraitExtender: FooTraitExtender = FooTraitExtender@53d26552This gets more interesting if the trait has some methods, of course. Here’s a trait, Animal, that declares two abstract methods, makeNoise and doBehavior. trait Animal { def makeNoise: String def doBehavior (other: Animal): String }We can extend this trait with new class definitions; each extending class must implement both of these methods (or else be declared abstract). case class Bear (name: String, defaultBehavior: String = "Regard warily...") extends Animal { def makeNoise = "ROAR!" def doBehavior (other: Animal) = other match { case b: Bear => makeNoise + " I'm " + name + "." case m: Mouse => "Eat it!" case _ => defaultBehavior } override def toString = name }case class Mouse (name: String) extends Animal { def makeNoise = "Squeak?" def doBehavior (other: Animal) = other match { case b: Bear => "Run!!!" case m: Mouse => makeNoise + " I'm " + name + "." case _ => "Hide!" } override def toString = name }Notice that Bear and Mouse have different parameter lists, but both can be Animals because they fully implement the Animal trait. We can now start creating objects of the Bear and Mouse classes and have them interact. We don’t need to use “new” because they are case classes (and this also allowed them to be used in the match statements of the doBehavior methods). val yogi = Bear("Yogi", "Hello!") val baloo = Bear("Baloo", "Yawn...") val grizzly = Bear("Grizzly") val stuart = Mouse("Stuart")println(yogi + ": " + yogi.makeNoise) println(stuart + ": " + stuart.makeNoise) println("Grizzly to Stuart: " + grizzly.doBehavior(stuart))We can also create a singleton object that is of the Animal type by using the following declaration. object John extends Animal { def makeNoise = "Hullo!" def doBehavior (other: Animal) = other match { case b: Bear => "Nice bear... nice bear..." case _ => makeNoise } override def toString = "John" }Here, John is an object, not a class. Because this object implements the Animal trait, it successfully extends it and can act as an Animal. This means that a Bear like baloo can interact with John. println("Baloo to John: " + baloo.doBehavior(John))The output of the above code when run as a script is the following. Yogi: ROAR! Stuart: Squeak? Grizzly to Stuart: Eat it! Baloo to John: Yawn… The closer distinction is between traits and abstract classes. In fact, everything shown above could have been done with Animal as an abstract class rather than as a trait. One difference is that an abstract class can have a constructor while traits cannot. Another key difference between them is that traits can be used to support limited multiple inheritance, as shown in the next question/answer. Q. Does Scala support multiple inheritance? A. Yes, via traits with implementations of some methods. Here’s an example, with a trait Clickable that has an abstract (unimplemented) method getMessage, an implemented method click, and a private, reassignable variable numTimesClicked (the latter two show clearly that traits are different from Java interfaces). trait Clickable { private var numTimesClicked = 0 def getMessage: String def click = { val output = numTimesClicked + ": " + getMessage numTimesClicked += 1 output } }Now let’s say we have a MessageBearer class (that we may have wanted for entirely different reasons having nothing to do with clicking). class MessageBearer (val message: String) { override def toString = message }A new class can be now created by extending MessageBearer and “mixing in” the Clickable trait. class ClickableMessageBearer(message: String) extends MessageBearer(message) with Clickable { def getMessage = message }ClickableMessageBearer now has the abilities of both MessageBearers (which is to be able to retrieve its message) and Clickables. scala> val cmb1 = new ClickableMessageBearer("I'm number one!") cmb1: ClickableMessageBearer = I'm number one!scala> val cmb2 = new ClickableMessageBearer("I'm number two!") cmb2: ClickableMessageBearer = I'm number two!scala> cmb1.click res3: java.lang.String = 0: I'm number one!scala> cmb1.message res4: String = I'm number one!scala> cmb1.click res5: java.lang.String = 1: I'm number one!scala> cmb2.click res6: java.lang.String = 0: I'm number two!scala> cmb1.click res7: java.lang.String = 2: I'm number one!scala> cmb2.click res8: java.lang.String = 1: I'm number two!Q. Why are there toString, toInt, and toList functions, but there isn’t a toTuple function? A. This is a basic question that leads directly to the more advanced topic of implicits. There are a number of reasons behind this. To start with, it is important to realize that there are many types of Tuples, starting with a Tuple with a single element (a Tuple1) up to 22 elements (a Tuple22). Note that when you use (,) to create a tuple, it is implicitly invoking a constructor for the corresponding TupleN of the correct arity. scala> val b = (1,2,3) b: (Int, Int, Int) = (1,2,3)scala> val c = Tuple3(1,2,3) c: (Int, Int, Int) = (1,2,3)scala> b==c res4: Boolean = trueGiven this, it is obviously not meaningful to have a function toTuple on Seqs (sequences) that are longer than 22. This means there is no generic way to have, say a List or Array, and then call toTuple on it and expect reliable behavior to happen. However, if you want this functionality (even though limited by the above constraint of 22 elements max), Scala allows you to “add” methods to existing classes by using implicit definitions. You can find lots of discussions about implicits by search for “scala implicits”. But, here’s an example that shows how it works for this particular case. val foo = List(1,2) val bar = List(3,4,5) val baz = List(6,7,8,9)foo.toTupleclass TupleAble[X] (elements: Seq[X]) { def toTuple = elements match { case Seq(a) => Tuple1(a) case Seq(a,b) => (a,b) case Seq(a,b,c) => (a,b,c) case _ => throw new RuntimeException("Sequence too long to be handled by toTuple: " + elements) } }foo.toTupleimplicit def seqToTuple[X](x: Seq[X]) = new TupleAble(x)foo.toTuple bar.toTuple baz.toTupleIf you put this into the Scala REPL, you’ll see that the first invocation of foo.toTuple gets an error: scala> foo.toTuple <console>:9: error: value toTuple is not a member of List[Int] foo.toTuple ^Note that class TupleAble takes a Seq in its constructor and then provides the method toTuple, using that Seq. It is able to do so for Seqs with 1, 2 or 3 elements, and above that it throws an exception. (We could of course keeping listing more cases out and go up to 22 element tuples, but this shows the point.) The second invocation of foo.toTuple still doesn’t work — and that is because foo is a List (a kind of Seq) and there isn’t a toTuple method for Lists. That’s where the implicit function seqToTuple comes in — once it is declared, Scala notes that you are trying to call toTuple on a Seq, notes that there is no such function for Seqs, but sees that there is an implicit conversion from Seqs to TupleAbles via seqToTuple, and then it sees that TupleAble has a toTuple method. Based on that, it compiles and the produces the desired behavior. This is a very handy ability of Scala that can really simplify your code if you use it well and with care. Reference: Student Questions about Scala, Part 1 from our JCG partner Jason Baldridge at the Bcomposes blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books