package com.apalya.action;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.codehaus.jettison.json.JSONObject;
import com.apalya.util.ApalyaProperties;
import com.visural.common.IOUtil;
import com.visural.common.StringUtil;
/**
*
* @author Ravi
*
*/
public class Facebook extends BaseAction {
/**
* Register your web application here https://developers.facebook.com/apps
* to get below credential.
*/
private static final String api_key = 1608443106749145;
private static final String secret = c21bf0af0d9479e8582e46ed36fe114av;
private static final String client_id = 1608443106749145;
/**
* set this to your servlet URL for the authentication servlet/filter
*/
private static final String redirect_uri = "http://your_application_url/redirect_action_to_call_method";
/**
* set this to the list of extended permissions you want
*/
private static final String[] perms = new String[] { "publish_stream",
"email" };
public static String getLoginRedirectURL() {
return "https://graph.facebook.com/oauth/authorize?client_id="
+ client_id + "&display=page&redirect_uri=" + redirect_uri
+ "&scope=" + StringUtil.delimitObjectsToString(",", perms);
}
public static String getAuthURL(String authCode) {
return "https://graph.facebook.com/oauth/access_token?client_id="
+ client_id + "&redirect_uri=" + redirect_uri
+ "&client_secret=" + secret + "&code=" + authCode;
}
/**
* Redirect url method which handle the facebook request.
*
* @return success when the user successfully authenticated by facebook
*/
public String getFbauth() {
String code = getServletRequest().getParameter("code");
if (StringUtil.isNotBlankStr(code)) {
try {
String authURL = Facebook.getAuthURL(getServletRequest()
.getParameter("code"));
URL url = new URL(authURL);
String result = readURL(url);
String accessToken = null;
Integer expires = null;
String[] pairs = result.split("&");
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv.length != 2) {
// log.error("Unexpected auth response");
}
if (kv[0].equals("access_token")) {
accessToken = kv[1];
}
if (kv[0].equals("expires")) {
expires = Integer.valueOf(kv[1]);
}
}
if (accessToken != null && expires != null) {
try {
authFacebookLogin(accessToken, expires);
} catch (Exception e) {
// log.error("Auth Facebook Login failed",e);
return "success";
}
} else {
// log.error("Access token and expires not found");
}
} catch (IOException e) {
// log.error("Access token and expires not found",e);
return "success";
}
}
return "success";
}
/**
* After successfully login facebook return the json response object with
* loggedin user details.
*
* @param accessToken
* @param expires
*/
public void authFacebookLogin(String accessToken, int expires) {
String userName = null;
try {
JSONObject resp = new JSONObject(
IOUtil.urlToString(new URL(
"https://graph.facebook.com/me?access_token="
+ accessToken)));
// get user details and save in data base as per your requirement.
} catch (Throwable ex) {
// handle the exception
} finally {
getServletRequest().getSession().setAttribute("userName", userName);
getServletRequest().getSession().setAttribute("fbAccessToken",accessToken);
}
}
private String readURL(URL url) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = url.openStream();
int r;
while ((r = is.read()) != -1) {
baos.write(r);
}
return new String(baos.toByteArray());
}
public static String getAPIKey() {
return api_key;
}
public static String getSecret() {
return secret;
}
}
Tag: software-development
IBATIS vs Hibernate
There are major differences between iBatis and Hibernate but both the solutions work well, given their specific domain. Personally I would suggest you should use iBATIS if:
- You want to create your own SQL‘s and are willing to maintain them.
- your environment is driven by relational data model.
- you have to work existing and complex schema’s.
And simply use Hibernate if:
- Your environment is driven by object model and wants generates SQL automatically.
To count there are few differences:
- iBATIS is:
- Simpler
- Faster development time
- Flixable
- Much smaller in package size
- Hibernate:
- Generates SQL for you which means you don’t spend time on SQL
- Provides much more advance cache
- Highly scalable
Other difference is that iBATIS makes use of SQL which could be database dependent where as Hibernate makes use of HQL which is relatively independent of databases and it is easier to change db in Hibernate.
Hibernate maps your Java POJO objects to the Database tables where as iBatis maps the ResultSet from JDBC API to your POJO Objets.
If you are using stored procedures, well you can do it in Hibernate but it is little difficult in comparision of iBATIS. As an alternative solution iBATIS maps results sets to objects, so no need to care about table structures. This works very well for stored procedures, works very well for reporting applications, etc
Finally, Hibernate and iBATIS both are open source Object Relational Mapping(ORM) tools available in the industry. Use of each of these tools depends on the context you are using them. Hibernate and iBatis both also have good support from SPRING framework so it should not be a problem to chose one of them.
Android SDK: Unit Testing with the JUnit Testing Framework
In this tutorial, you will learn how to use Eclipse to create an Android JUnit Test Project, create automated unit tests and run them under a variety of conditions.
Before You Begin
The authors are assuming the reader has some basic knowledge of Android and have all of the tools such as Eclipse and the Android SDK installed and working. The examples given here are engineered to show how the Android JUnit framework can be used to test Android applications. Specifically, this tutorial will show you how to test aspects of an Activity and identify program errors. These errors are determined, but not addressed, as part of this tutorial.
Note: The code for the SimpleCalc application is available on Google Code as well as from the above link.
Step 1: Review the SimpleCalc Application
First, let’s take a few moments to look over the SimpleCalc application. It’s a very simple application with just one screen. The screen has two simple functions: it allows the user to input two values, adds or multiplies these values together and displays the result as shown below.

Step 2: Review the SimpleCalc Screen Layout
The SimpleCalc application has just one screen, so we have only one layout resource file called /res/layout/main.xml.
Here is a listing of the main.xml layout resource:
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
- android:orientation=”vertical” android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”>
- <TextView android:layout_width=”fill_parent”
- android:layout_height=”wrap_content” android:text=”@string/hello”
- android:gravity=”center_horizontal” android:textSize=”48px”
- android:padding=”12px” />
- <EditText android:layout_height=”wrap_content” android:id=”@+id/value1″
- android:hint=”@string/hint1″ android:inputType=”numberDecimal”
- android:layout_width=”fill_parent” android:textSize=”48px”></EditText>
- <EditText android:layout_height=”wrap_content” android:id=”@+id/value2″
- android:hint=”@string/hint2″ android:inputType=”numberDecimal”
- android:layout_width=”fill_parent” android:textSize=”48px”></EditText>
- <FrameLayout android:id=”@+id/FrameLayout01″
- android:layout_width=”wrap_content” android:layout_height=”wrap_content”
- android:padding=”12px” android:background=”#ff0000″>
- <LinearLayout android:id=”@+id/LinearLayout02″
- android:layout_width=”wrap_content” android:layout_height=”wrap_content”
- android:orientation=”horizontal” android:background=”#000000″
- android:padding=”4px”>
- <TextView android:layout_width=”wrap_content”
- android:layout_height=”wrap_content” android:text=”@string/resultLabel”
- android:textSize=”48px” android:id=”@+id/resultLabel”></TextView>
- <TextView android:layout_width=”wrap_content”
- android:layout_height=”wrap_content” android:id=”@+id/result”
- android:textSize=”48px” android:textStyle=”bold”
- android:layout_marginLeft=”16px”></TextView>
- </LinearLayout>
- </FrameLayout>
- <LinearLayout android:id=”@+id/LinearLayout03″
- android:layout_height=”wrap_content” android:layout_width=”fill_parent”>
- <Button android:layout_height=”wrap_content” android:id=”@+id/addValues”
- android:text=”@string/add” android:textSize=”32px”
- android:layout_width=”wrap_content”></Button>
- <Button android:layout_height=”wrap_content” android:id=”@+id/multiplyValues”
- android:text=”@string/multiply” android:textSize=”32px”
- android:layout_width=”wrap_content”></Button>
- </LinearLayout>
- </LinearLayout>
This layout is fairly straightforward. The entire content of the screen is stored within a LinearLayout, allowing the controls to display one after another vertically. Within this parent layout, we have the following controls:
- A TextView control displaying the header “Unit Testing Sample.”
- Two EditText controls to collect user input in the form of two numbers.
- A FrameLayout control which contains a horizontal LinearLayout with the result label TextView and resulting sum or product TextView. The FrameLayout displays a red border around these controls, highlighting the result.
- Finally, another horizontal LinearLayout with two child controls: a Button control for addition and a Button control for multiplication.
This layout is designed to look right in the Android Layout Designer when the Nexus One option (which has a screen size of 800×480) is chosen in both portrait and landscape modes. You’ll soon see that this is not a bullet-proof way of designing layouts. As with the code you’ll see in the next step, it isn’t designed to work perfectly.
Step 3: Review the SimpleCalc Activity
The SimpleCalc application has just one screen, so we have only one Activity as well: MainActivity.java. The MainActivity.java class controls the behavior of the one screen, whose user interface is dictated by the main.xml layout.
Here is a listing of the MainActivity.java class:
- package com.mamlambo.article.simplecalc;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- final String LOG_TAG = “MainScreen”;
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final EditText value1 = (EditText) findViewById(R.id.value1);
- final EditText value2 = (EditText) findViewById(R.id.value2);
- final TextView result = (TextView) findViewById(R.id.result);
- Button addButton = (Button) findViewById(R.id.addValues);
- addButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- try {
- int val1 = Integer.parseInt(value1.getText().toString());
- int val2 = Integer.parseInt(value2.getText().toString());
- Integer answer = val1 + val2;
- result.setText(answer.toString());
- } catch (Exception e) {
- Log.e(LOG_TAG, “Failed to add numbers”, e);
- }
- }
- });
- Button multiplyButton = (Button) findViewById(R.id.multiplyValues);
- multiplyButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- try {
- int val1 = Integer.parseInt(value1.getText().toString());
- int val2 = Integer.parseInt(value2.getText().toString());
- Integer answer = val1 * val2;
- result.setText(answer.toString());
- } catch (Exception e) {
- Log.e(LOG_TAG, “Failed to multiply numbers”, e);
- }
- }
- });
- }
- }
When a Button is pressed, the Activity retrieves the values stored in the two EditText controls, calculates the result, and displays it in the TextView control called R.id.result.
Note: There are bugs in this code! These bugs have been designed specifically to illustrate unit testing.
Step 4: Creating an Android Test Project
You can add a test project to any Android project in two ways. You can add a test project while creating a new Android project with the Android Project Wizard or you can add a test project to an existing Android project. (The steps are basically the same.)
For this example, we have an existing project. To add a test project to the SimpleCalc project in Eclipse, take the following steps:
From the Project Explorer, choose the Android project you wish to add a test project to. Right-click on the project and choose Android Tools->New Test Project…

Step 5: Configuring the Android Test Project
Now you need to configure the test project settings, including the test project name, file location, Android project to test and build target (SDK version).
For this example, we can use the following settings:
- Test project names generally end in “Test”, so let’s name this test project SimpleCalcTest
- The project location can be wherever you normally store source files on your hard drive.
- Choose the Android project to test: SimpleCalc
- The appropriate build target will be selected automatically once you select the project to test. In this case, since the Android project is built for Android 2.1 + Google APIs (API Level 7), it makes sense to use the same build target for the test project.
- It makes sense to name the test project accordingly: SimpleCalcTest, with the appropriate package name suffix simplecalc.test.

Hit Finish.
Step 6: Review the SimpleCalcTest Project
The SimpleCalcTest project is an Android project. You will notice that it has all the normal things you’d expect of an Android project, including a Manifest file and resources.

Step 7: Determine Unit Tests for the SimpleCalc Application
Now that you have a test project configured for the SimpleCalc application, it makes sense to determine some reasonable unit tests for the application.
Many software methodologies these days work compatibly with unit testing. Unit tests can greatly increase the stability and quality of your application and reduce testing costs. Unit tests come in many forms.
Unit tests can:
- Improve testing coverage
- Test application assumptions
- Validate application functionality
Unit tests can be used to test core functionality, such as whether or not the SimpleCalc math is being performed correctly. They can also be used to verify if the user interface is displaying correctly. Now let’s look at some specific test cases.
Step 8: Create Your First Test Case
To create your first test case, right-click on the simplecalc.test package and choose New->JUnit Test Case.

Step 9: Configure Your First Test Case
Now you need to configure the test case settings.
For this example, we can use the following settings:
- Both JUnit 3 and 4 are supported. We’ll use the default JUnit 3 here.
- The source folder should be the location of the SimpleCalcTest project files.
- The package should be the package name of the SimpleCalcTest project.
- In this case, we will name the test case MathValidation.
- For the SuperClass, choose “android.test.ActivityInstrumentationTestCase2.” This is the test case you use for testing activities.
- Check the boxes to add method stubs for setUp() and constructor.

Hit Finish. (You can safely ignore the warning saying, “Superclass does not exist.”)
Step 10: Review the MathValidation Test Case
The MathValidation.java test case source file is then created.
The lifecycle of a test case is basically this: construction, setUp(), tests run, tearDown(), and destruction. The setUp() method is used to do any general initialization used by all of specific tests. Each test to be run in the test case is implemented as its own method, where the method name begins with “test”. The tearDown() method is then used to uninitialize any resources acquired by the setUp() method.
- package com.mamlambo.article.simplecalc.test;
- import android.test.ActivityInstrumentationTestCase2;
- public class MathValidation extends
- ActivityInstrumentationTestCase2<MainActivity> {
- public MathValidation(String name) {
- super(name);
- }
- protected void setUp() throws Exception {
- super.setUp();
- }
- }
Now it’s time to implement the MathValidation Test Case
Step 11: Modify the MathValidation Class Constructor
First, modify the MathValidation class constructor. This constructor ties configures the internals of the Android test superclass we’re using.
- public MathValidation() {
- super(“com.mamlambo.article.simplecalc”, MainActivity.class);
- }
Step 12: Implement the MathValidation setUp() method
Now you need to gather the data required for validating the SimpleCalc math calculations. Begin by implementing the setUp() method. You can retrieve the Activity being tested using the getActivity() method as follows:
- MainActivity mainActivity = getActivity();
Next, you need to retrieve an instance of the TextView control called R.id.result. This is the control that will hold the resulting sum or product from the math calculations used by the application.
The full updated code listing (MathValidation.java) with these modifications is shown below:
- package com.mamlambo.article.simplecalc.test;
- import android.test.ActivityInstrumentationTestCase2;
- import android.widget.TextView;
- import com.mamlambo.article.simplecalc.MainActivity;
- import com.mamlambo.article.simplecalc.R;
- public class MathValidation extends ActivityInstrumentationTestCase2<MainActivity> {
- private TextView result;
- public MathValidation() {
- super (“com.mamlambo.article.simplecalc”, MainActivity.class);
- }
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- MainActivity mainActivity = getActivity();
- result = (TextView) mainActivity.findViewById(R.id.result);
- }
- }
Step 13: Consider Tests for the SimpleCalc Application
Now that everything is set up, what tests do you want to perform? Let’s begin by checking the math used by the SimpleCalc application. Specifically, let’s check that the numbers are retrieved correctly, as well as added and multiplied correctly. Did we set the right types for our number values? Are we retrieving and performing mathematical calculations using the correct types?
To answer these questions, we must add some testing code to the MathValidation class. Each specific test will have it’s own method beginning with “test” – the “test” method name prefix is case sensitive! This naming scheme is how JUnit determines what methods to run.
Step 14: Implement a Method to Test SimpleCalc’s Addition
Let’s begin by testing the addition calculation of SimpleCalc. To do this, add a method called testAddValues() to the MathValidation class.
This test will enter two numbers (24 and 74) into the screen and press Enter, which acts as a click on the first Button control which is in focus. Then it will retrieve the sum displayed by the application in the result TextView control and test to see if the result is the expected one (98).
To supply the EditText controls with two numbers to sum, use the sendKeys() method. This method mimics how keys are sent to Android applications. If you use the setText() method of the EditText control to set the text in each control, then you are bypassing the validation of the numeric entry that user’s would encounter. This method of providing key strokes assumes that the focus starts on the first control and that using the Enter key goes to the next one (you’ll see that the enter key is sent at the end of each number). If neither of those assumptions is true, the test will also fail. This is not a bad thing, but it might be failing for the wrong reasons (e.g. focus or layout issues, rather than math issues).
Finally, you use the assertTrue() method to compare the actual result displayed on the screen to the expected result. ). We compare against a string, since the result is displayed as a string. This way, we can also make sure we don’t duplicate any math or type errors in the application logic within the test framework.
Here is the full listing of the testAddValues() method:
- private static final String NUMBER_24 = “2 4 ENTER “;
- private static final String NUMBER_74 = “7 4 ENTER “;
- private static final String ADD_RESULT = “98”;
- public void testAddValues() {
- sendKeys(NUMBER_24);
- // now on value2 entry
- sendKeys(NUMBER_74);
- // now on Add button
- sendKeys(“ENTER”);
- // get result
- String mathResult = result.getText().toString();
- assertTrue(“Add result should be 98”, mathResult.equals(ADD_RESULT));
- }
Congratulations! You’ve created your first test!
Step 15: Enhancing the Tests for Addition
Now let’s add a few more tests to make sure all different types of numbers can be added, resulting in the display of the proper sum.
Because the activity is launched for each test, you do not need to clear the values or anything like that between tests. You also do not need to change the focus within the form, since it begins at value1. Therefore, you can simplify tests by concatenating key presses together in a single sendKeys() method call, like such:
- sendKeys(NUMBER_24 + NUMBER_74 + “ENTER”);
For example, here is the code for the testAddDecimalValues() method, which tests the addition of a decimal value 5.5 with the number 74, which should result in 79.5:
- public void testAddDecimalValues() {
- sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + “ENTER”);
- String mathResult = result.getText().toString();
- assertTrue(“Add result should be ” + ADD_DECIMAL_RESULT + ” but was “
- + mathResult, mathResult.equals(ADD_DECIMAL_RESULT));
- }
Similarly, you can perform a test of adding a negative number -22 to the number 74, which should result in a sum of 52. This test is implemented in the testSubtractValues() method, as follows:
- public void testAddDecimalValues() {
- sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + “ENTER”);
- String mathResult = result.getText().toString();
- assertTrue(“Add result should be ” + ADD_DECIMAL_RESULT + ” but was “
- + mathResult, mathResult.equals(ADD_DECIMAL_RESULT));
- }
Step 16: Implement a Method to Test SimpleCalc’s Multiplication
It should be quite straightforward to implement a similar test for SimpleCalc’s multiplication called testMuliplyValues().
The only tricky part is that the Multiply Button control is not in focus when we’re done entering the numbers (instead, the Add Button is).
You might think to just call the requestFocus() method on the multiply button. Unfortunately, this won’t work because requestFocus() has to be run on the UI thread in Android. Running methods on the UI Thread can be done as part of a test case, but it’s done asynchronously so you can’t guarantee when it will be complete.
Instead, we’ll again use the sendKeys() method. Since we defined the Multiply Button to always display to the right of the Add Button, we can just send the “DPAD_RIGHT” key followed by “ENTER” to click the Multiply Button.
- public void testMultiplyValues() {
- sendKeys(NUMBER_24+NUMBER_74+ ” DPAD_RIGHT ENTER”);
- String mathResult = result.getText().toString();
- assertTrue(“Multiply result should be ” + MULTIPLY_RESULT + ” but was “
- + mathResult, mathResult.equals(MULTIPLY_RESULT));
- }
As an exercise, you might try adding more multiplication tests with various sizes of numbers. Try to engineer a test that might fail with the existing code. Can you guess if each test will be successful or not? (Hint: Look at the type of the variable that the string is being converted to.)
Step 17: Running Unit Tests with the Android Emulator or Device
Unit test frameworks such as the one you’re building are Android applications like any other. They must be installed on the emulator or device you wish to test, along with the application to be tested (in this case, SimpleCalc).
To run the unit tests you have created so far from Eclipse, choose the Debug drop down, then Debug As, then Android JUnit Test. Make sure the file you just created is the active file (shown in the window) as that is the test case that will be launched.

Step 18: Examining the Test Results
The tests may take some time to complete, especially if the emulator was not already running. When the tests have completed, you should see results similar to those shown here:

You’ll notice that all four of the tests you’ve just created run. Two of them are successful, while two of them have failed. Do you know why they’ve failed? (Fixing these calculation bugs is left as a learning exercise for the reader.)
Besides successful tests and failed tests, errors are also shown. A failure is when a tested for assertion fails. An error, on the other hand, is a thrown exception. Errors can either be untested for edge cases or simply mistakes in the testing code. If you have errors, check your testing code carefully to make sure it is working correctly.
Step 19: Create a Test Case for Screen Display Testing
Unit tests need not be limited to validating core functionality such as the addition and multiplication of values. Tests can also validate whether or not a screen layout is displayed properly.
For example, you might want to validate that all of the layout controls display properly on all target screens. The SimpleCalc’s screen was designed in the layout designer in Eclipse for an 800×480 screen in either landscape or portrait mode, but will it work on other screen sizes and devices? Were we then too specific in our design? Automated testing can tell us the answer to this question very quickly.
To create another test case, right-click on the SimpleCalc.Test package and choose New->JUnit Test Case. Call this new test LayoutTests. Configure this test case much as you did the MathValidation class in Step 8.

Hit Finish.
Step 20: Review and Update the LayoutTests Test Case
The LayoutTests.java test case source file is then created. Modify the class to look like the code listing below, modifying the constructor, retrieving the Button controls and the layout as a whole:
- package com.mamlambo.article.simplecalc.test;
- import android.test.ActivityInstrumentationTestCase2;
- import android.view.View;
- import android.widget.Button;
- import com.mamlambo.article.simplecalc.MainActivity;
- import com.mamlambo.article.simplecalc.R;
- public class LayoutTests extends ActivityInstrumentationTestCase2<MainActivity> {
- private Button addValues;
- private Button multiplyValues;
- private View mainLayout;
- public LayoutTests() {
- super(“com.mamlambo.article.simplecalc”, MainActivity.class);
- }
- protected void setUp() throws Exception {
- super.setUp();
- MainActivity mainActivity = getActivity();
- addValues = (Button) mainActivity.findViewById(R.id.addValues);
- multiplyValues = (Button) mainActivity
- .findViewById(R.id.multiplyValues);
- mainLayout = (View) mainActivity.findViewById(R.id.mainLayout);
- }
- }
Now let’s implement some specific tests for LayoutTests.
Step 20: Consider Layout Tests for the SimpleCalc Application
Now that everything is set up, what tests do you want to perform? One common bug in application design is for controls not to display properly in all screen sizes and orientations. Therefore, it makes sense to try to build a test case to verify the location of certain controls. You can then add other checks to make sure other View controls display and behave appropriately.
Step 21: Implement a Method to Test Button Display
Let’s begin by testing that the Add Button control of the SimpleCalc screen is visible. To do this, add a method called testAddButtonOnScreen() to the LayoutTests class.
This test checks to see if the Add Button control is displayed within the visible rectangle representing the overall screen size.
To implement the testAddButtonOnScreen() method, you must first determine the screen size. There are a number of ways to do this. One simple way is to retrieve the View that represents the entire screen layout and use the getWidth() and getHeight() methods. Doing this also takes in to account any screen real estate being used by other items, such as the title bar or information bar that are often at the top of an Android screen.
Determining whether or not the Add Button control is drawn within those bounds is as simple as comparing the layout bounds to the bounds of the drawing rectangle for the Add Button control.
Here is the full listing of the testAddButtonOnScreen() method:
- public void testAddButtonOnScreen() {
- int fullWidth = mainLayout.getWidth();
- int fullHeight = mainLayout.getHeight();
- int[] mainLayoutLocation = new int[2];
- mainLayout.getLocationOnScreen(mainLayoutLocation);
- int[] viewLocation = new int[2];
- addValues.getLocationOnScreen(viewLocation);
- Rect outRect = new Rect();
- addValues.getDrawingRect(outRect);
- assertTrue(“Add button off the right of the screen”, fullWidth
- + mainLayoutLocation[0] > outRect.width() + viewLocation[0]);
- assertTrue(“Add button off the bottom of the screen”, fullHeight
- + mainLayoutLocation[1] > outRect.height() + viewLocation[1]);
- }
At this point, you can see how you could also test the display of the Multiply Button, or the Result text, or any other control on the SimpleCalc screen.
Step 22: Running the LayoutTests Test Case
In order for layout testing to provide useful results, we can’t just run the test once. Instead, the tests must be run on multiple emulator configurations and screen orientations. This is different from the logic tests of above where a messy layout or a layout that doesn’t match the design pattern doesn’t necessarily impede functionality.
For example, if you create emulator configurations (using AVDs) for the following configurations, the LayoutTests test case will yield the following results:
- 480×800, portrait mode (will pass)
- 800×480, landscape mode (will fail)
- 320×480, portrait mode (will fail)
- 480×320, landscape (will fail)
- 480×854, portrait mode (will pass)
- 854×480, landscape mode (will fail) Can you figure out why it fails in all landscape modes, but draws fine in the creator for the landscape mode (#2 above)?
Can you figure out why it fails in all landscape modes, but draws fine in the creator for the landscape mode (#2 above)?
Hint: What’s shown on the screen when the application actually runs (see the figure below)?

Step 23: Where to Go From Here
Now that you have some tests in place—some of which pass and some of which fail—you can imagine how unit testing can improve the quality of your application.
The more thorough you are with your unit testing coverage, the better. You’ve seen how unit testing can uncover bugs in code and layout designs. The next step would be to identify the failure points and fix those bugs. Once you’ve fixed the bugs, you should re-run the unit tests to ensure that they pass in all test cases.
Conclusion
In this tutorial, you learned how to create unit tests using the Android JUnit framework for your Android projects. You also learned how to create different kinds of unit tests to test a variety of application features, including underlying program functions as well as display characteristics.
Related articles
- Android Development Tutorials[Various Links] (amitcs.wordpress.com)
- Live Camera Previews in Android (zeeshanakhter.com)
- Quickstart Android 4.0 mobile app development (iprofs.nl)
- Beyond JUnit – Testing Frameworks alternatives (javacodegeeks.com)
Calling Stored Procedures from ASP.NET and VB.NET
Let us see how can we create and call Stored procedures, in a .NET Environment, i.e Visual Studio.We use .NET 2.0 and Visual Studio 2005 for all the examples.fs
Writing stored procedures has never been easy as Microsoft has almost integrated SQL Server with Visual Studio 2005. In the past most of the developers has wondered can’t we have a good editor for creating stored procedures. One of the main advantage of creating procedures in Visual Studio is it creates the basic stub for you and further more, it has inbuilt syntax checking which makes the job easier for us.
In order to create a stored procedure from Visual Studio, first you need to create a data connection from the Server Explorer and follow the below steps.
Step 1: Open Visual Studio 2005.
Step 2: Create a VB.NET / C# Windows / Web Application Project.
Step 3: Open the Server Explorer by Selecting View -> Server Explorer.

Step 4: Create a Data Connection to your server you can do this by Right Clicking on the Data Connection Tree and Selecting “Add New Connection”.
Step 5: It will Prompt for the Provider Type you can select .NET SQL Server Provider as it gives more performance.
Step 6: After giving all the credentials once the connection is active expand the database that you are having.
Step 7: Expand the Stored Procedure Tree.
Step 8: To Create a New Procedure Right Click and Select “Add New Procedure”.
Step 9: The IDE will give you a Stub where you can replace the Name of the Procedure and Arguments.
Those who are familiar with Visual Studio IDE would love to create procedures here rather then doing it in Query Analyzer or in SQL Enterprise Manager, though it doesn’t provide any fancy auto complete drop downs its still the best I believe to create stored procedures.
TIP: The Maximum number of parameters in a stored procedure is 2100.
Hope everyone have used SQLCommand / OLEDB Command objects in .NET. Here we can call stored procedures in two different forms, one without using parameter objects which is not recommended for conventional development environments, the other one is the familiar model of using Parameters.
In the first method you can call the procedure using Exec command followed by the procedure name and the list of parameters, which doesn’t need any parameters.
Dim SQLCon As New SqlClient.SqlConnection
SQLCon.ConnectionString = “Data Source=Server;User ID=User;Password=Password;”
SQLCon.Open()
Calling Stored Procedures with Exec command
SQLCmd.CommandText = “Exec SelectRecords ‘Test’, ‘Test’, ‘Test'”
SQLCmd.Connection = SQLCon ‘Active Connection
The second most conventional method of calling stored procedures is to use the parameter objects and get the return values using them. In this method we need to set the “SQLCommandType” to “StoredProcedure” remember you need to set this explicitly as the the default type for SQLCommand is SQLQuery”.
Here is an example to call a simple stored procedure.
In order to get XML Results from the Stored Procedure you need to first ensure that your stored procedure is returning a valid XML. This can be achieved using FOR XML [AUTO | RAW | EXPLICIT] clause in the select statements. You can format XML using EXPLICIT Keyword, you need to alter your Query accordingly
Dim SQLCon As New SqlClient.SqlConnection
SQLCon.ConnectionString = “Data Source=Server;User ID=User;Password=Password;”
SQLCon.Open()SQLCmd.CommandText = “SelectRecords” ‘ Stored Procedure to Call
SQLCmd.CommandType = CommandType.StoredProcedure ‘Setup Command Type
SQLCmd.Connection = SQLCon ‘Active Connection
The procedure can be called by adding Parameters in at least two different methods, the simplest way to add parameters and respective values is using
SQLCmd.Parameters.AddWithValue(“S_Mobile”, “Test”)
SQLCmd.Parameters.AddWithValue(“S_Mesg”, “Test”)
SQLCmd.Parameters.AddWithValue(“LastMsgID”, “”)
In this above method, you doesn’t necessarily know the actually data type that you had in your procedure and all parameters are validated according to the type declared in your procedure but only thing is all the validations will occur in SQL and not in your client code.
We still need to declare the last parameter as Output and we need to do that explicitly as the default type is Input. So here we are going to declare the last parameter as Output by
SQLCmd.Parameters(“LastMsgID”).Direction = ParameterDirection.Outputfs
If you want to declare parameters properly then you need to use the below method to add all the parameters with its data type, direction. Also if you are using stored procedures to update all the rows in a dataset then you need to declare parameters in the below fashion and give SouceColumn value as the Column name in the DataTable.
SQLCmd.Parameters.Add(New SqlClient.SqlParameter(“LastMsgID”, SqlDbType.BigInt, 5, ParameterDirection.Output, False, 5, 0, “”, DataRowVersion.Current, 0))
‘ The Above Procedure has two input parameters and one output parameter you can notice the same in the “Parameter Direction”
SQLCmd.ExecuteNonQuery() ‘We are executing the procedure here by calling Execute Non Query.
MsgBox(SQLCmd.Parameters(“LastMsgID”).Value) ‘You can have the returned value from the stored procedure from this statement. Its all similar to ASP / VB as the only difference is the program structure.
In order to get the result sets from the stored procedure, the best way is to use a DataReader to get the results. In this example we are getting the results from the Stored Procedure and filling the same in a DataTable.
Here we need to additionally declare a SQLDataReader and DataTable
Dim SQLDataTable As New DataTableSQLCmd.CommandText = “GetAuthors”
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Connection = SQLCon
SQLCmd.Parameters.Add(New SqlClient.SqlParameter(“AuthorName”, SqlDbType.VarChar, 100, ParameterDirection.Input, False, 30, 0, “”, DataRowVersion.Current, “Y%”)) SQLDBDataReader = SQLCmd.ExecuteReader() SQLDataTable.Columns.Add(“AuthorName”, GetType(Int32), “”) SQLDataTable.Columns.Add(“AuthorLocation”, GetType(String), “”)
Dim FieldValues(1) As Object ‘A Temporary Variable to retrieve all columns in a row and fill them in Object array
While (SQLDBDataReader.Read)
SQLDBDataReader.GetValues(FieldValues)
SQLDataTable.Rows.Add(FieldValues)
End While
In order to get XML Results from the Stored Procedure you need to first ensure that your stored procedure is returning a valid XML. This can be achieved using FOR XML [AUTO | RAW | EXPLICIT] clause in the select statements. You can format XML using EXPLICIT Keyword, you need to alter your Query accordingly.
ASSelect Author_ID, Author_Name, Author_Location Where Author_Name LIKE @AuthorName from Authors FOR XML AUTO
RETURN
When you use the above procedure you can get XML Results with TableName as Element and Fields as Attributes
Dim SQLXMLReader As Xml.XmlReader
SQLCmd.CommandText = “GetAuthorsXML”
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Connection = SQLCon
SQLCmd.Parameters.Add(New SqlClient.SqlParameter(“AuthorName”, SqlDbType.VarChar, 100, ParameterDirection.Input, False, 30, 0, “”, DataRowVersion.Current, “Y%”))
SQLDBDataReader = SQLCmd.ExecuteReader()
SQLXMLReader = SQLCmd.ExecuteXmlReader()
While (SQLXMLReader.Read)
MsgBox(SQLXMLReader.ReadOuterXml)
End While
You can further process this XML or write XSL to display results in a formatted manner. But in order to get formatted XML Results, we need to use EXPLICIT case which we can see in our next article on SQL Queries & XML.
Related articles
- What are Stored Procedure (vkagarwal.wordpress.com)
- Sql Server Stored Procedures (docserve.wordpress.com)
- SQL Azure 101: Creating a basic stored procedure with SQL Azure Database (parasdoshi.com)
- Stored Procedures with Asp.net (udaysingh86.wordpress.com)
- linq if statement, or stored procedure if statements (stackoverflow.com)
- SQL Server – Building the Appendix – Building Data Dictionary Part 3 (dattatreysindol.com)
Spring Framework Architecture
Spring could potentially be a one-stop shop for all your enterprise applications, however, Spring is modular, allowing you to pick and choose which modules are applicable to you, without having to bring in the rest. Following section gives detail about all the modules available in Spring Framework.
The Spring Framework provides about 20 modules which can be used based on an application requirement.
Core Container:
The Core Container consists of the Core, Beans, Context, and Expression Language modules whose detail is as follows:
- The Core module provides the fundamental parts of the framework, including the IoC and Dependency Injection features.
- The Bean module provides BeanFactory which is a sophisticated implementation of the factory pattern.
- The Context module builds on the solid base provided by the Core and Beans modules and it is a medium to access any objects defined and configured. The ApplicationContext interface is the focal point of the Context module.
- The Expression Language module provides a powerful expression language for querying and manipulating an object graph at runtime.
Data Access/Integration:
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules whose detail is as follows:
- The JDBC module provides a JDBC-abstraction layer that removes the need to do tedious JDBC related coding.
- The ORM module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.
- The OXM module provides an abstraction layer that supports Object/XML mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
- The Java Messaging Service JMS module contains features for producing and consuming messages.
- The Transaction module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs.
Web:
The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet modules whose detail is as follows:
- The Web module provides basic web-oriented integration features such as multipart file-upload functionality and the initialization of the IoC container using servlet listeners and a web-oriented application context.
- The Web-Servlet module contains Spring’s model-view-controller (MVC) implementation for web applications.
- The Web-Struts module contains the support classes for integrating a classic Struts web tier within a Spring application.
- The Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module.
Miscellaneous:
There are few other important modules like AOP, Aspects, Instrumentation, Web and Test modules whose detail is as follows:
- The AOP module provides aspect-oriented programming implementation allowing you to define method-interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated.
- The Aspects module provides integration with AspectJ which is again a powerful and mature aspect oriented programming (AOP) framework.
- The Instrumentation module provides class instrumentation support and class loader implementations to be used in certain application servers.
- The Test module supports the testing of Spring components with JUnit or TestNG frameworks
Related articles
- Filter irrelevant stack trace lines in logs (javacodegeeks.com)
- Spring Framework – the model model driven target? (modeldrivers.us)
- Tutorial: Struts Portlet in Weblogic Portal Server (clean-clouds.com)
- Spring Framework moves to GitHub (oio.de)
- Spring Injection Example (shanisk.wordpress.com)
Introduction to Spring’s Aspect Oriented Programming(AOP)
1) Introduction
One of the major features available in the Spring Distribution is the provision for separating the cross-cutting concerns in an Application through the means of Aspect Oriented Programming. Aspect Oriented Programming is sensibly new and it is not a replacement for Object Oriented Programming. In fact, AOP is another way of organizing your Program Structure. This first section of this article looks into the various terminologies that are commonly used in the AOP Environment. Then it moves into the support that is available in the Spring API for embedding Aspects into an Application . Finally the article concludes by giving a Sample Application.
2) Introduction to AOP
2.1) The Real Problem
Since AOP is relatively new, this section devotes time in explaining the need for Aspect Oriented Programming and the various terminologies that are used within. Let us look into the traditional model of before explaining the various concepts.
Consider the following sample application,
Account.java
public class Account{ public long deposit(long depositAmount){ newAmount = existingAccount + depositAccount; currentAmount = newAmount; return currentAmount; } public long withdraw(long withdrawalAmount){ if (withdrawalAmount <= currentAmount){ currentAmount = currentAmount – withdrawalAmount; } return currentAmount; } }
The above code models a simple Account
Object that provides services for deposit and withdrawal operation in the form of Account.deposit()
and Account.withdraw()
methods. Suppose say we want to add some bit of the security to the Account
class, telling that only users with BankAdmin
privilege is allowed to do the operations. With this new requirement being added, let us see the modified class structure below.
Account.java
public class Account{ public long deposit(long depositAmount){ User user = getContext().getUser(); if (user.getRole().equals("BankAdmin"){ newAmount = existingAccount + depositAccount; currentAmount = newAmount; } return currentAmount; } public long withdraw(long withdrawalAmount){ User user = getContext().getUser(); if (user.getRole().equals("BankAdmin"){ if (withdrawalAmount <= currentAmount){ currentAmount = currentAmount – withdrawalAmount; } } return currentAmount; } }
Assume that getContext().getUser()
someway gives the current User
object who is invoking the operation. See the modified code mandates the use of adding additional if condition before performing the requested operation. Assume that another requirement for the above Account
class is to provide some kind of Logging and Transaction Management Facility. Now the code expands as follows,
Account.java
public class Account{ public long deposit(long depositAmount){ logger.info("Start of deposit method"); Transaction trasaction = getContext().getTransaction(); transaction.begin(); try{ User user = getContext().getUser(); if (user.getRole().equals("BankAdmin"){ newAmount = existingAccount + depositAccount; currentAmount = newAmount; } transaction.commit(); }catch(Exception exception){ transaction.rollback(); } logger.info("End of deposit method"); return currentAmount; } public long withdraw(long withdrawalAmount){ logger.info("Start of withdraw method"); Transaction trasaction = getContext().getTransaction(); transaction.begin(); try{ User user = getContext().getUser(); if (user.getRole().equals("BankAdmin"){ if (withdrawalAmount <= currentAmount){ currentAmount = currentAmount – withdrawalAmount; } } transaction.commit(); }catch(Exception exception){ transaction.rollback(); } logger.info("End of withdraw method"); return currentAmount; } }
The above code has so many dis-advantages. The very first thing is that as soon as new requirements are coming it is forcing the methods and the logic to change a lot which is against the Software Design. Remember every piece of newly added code has to undergo the Software Development Lifecycle of Development, Testing, Bug Fixing, Development, Testing, …. This, certainly cannot be encouraged in particularly big projects where a single line of code may have multiple dependencies between other Components or other Modules in the Project.
2.2) The Solution through AOP
Let us re-visit the Class Structure and the Implementation to reveal the facts. The Account class provides services for depositing and withdrawing the amount. But when you look into the implementation of these services, you can find that apart from the normal business logic, it is doing so many other stuffs like Logging, User Checking and Transaction Management. See the pseudo-code below that explains this.
public void deposit(){ // Transaction Management // Logging // Checking for the Privileged User // Actual Deposit Logic comes here } public void withdraw(){ // Transaction Management // Logging // Checking for the Privileged User // Actual Withdraw Logic comes here }
From the above pseudo-code, it is clear that Logging, Transaction Management and User Checking which are never part of the Deposit or the Service functionality are made to embed in the implementation for completeness. Specifically, AOP calls this kind of logic that cross-cuts or overlaps the existing business logic as Concerns or Cross-Cutting Concerns. The main idea of AOP is to isolate the cross-cutting concerns from the application code thereby modularizing them as a different entity. It doesn’t mean that because the cross-cutting code has been externalized from the actual implementation, the implementation now doesn’t get the required add-on functionalities. There are ways to specify some kind of relation between the original business code and the Concerns through some techniques which we will see in the subsequent sections.
3) AOP Terminologies
It is hard to get used with the AOP terminologies at first but a thorough reading of the following section along with the illustrated samples will make it easy. Let us look into the majorly used AOP jargons.
3.1) Aspects
An Aspect is a functionality or a feature that cross-cuts over objects. The addition of the functionality makes the code to Unit Test difficult because of its dependencies and the availability of the various components it is referring. For example, in the below example, Logging and Transaction Management are the aspects.
public void businessOperation(BusinessData data){ // Logging logger.info("Business Method Called"); // Transaction Management Begin transaction.begin(); // Do the original business operation here transaction.end(); }
3.2) JoinPoint
Join Points defines the various Execution Points where an Aspect can be applied. For example, consider the following piece of code,
public void someBusinessOperation(BusinessData data){ //Method Start -> Possible aspect code here like logging. try{ // Original Business Logic here. }catch(Exception exception){ // Exception -> Aspect code here when some exception is raised. }finally{ // Finally -> Even possible to have aspect code at this point too. } // Method End -> Aspect code here in the end of a method. }
In the above code, we can see that it is possible to determine the various points in the execution of the program like Start of the Method, End of the Method, the Exception Block, the Finally Block where a particular piece of Aspect can be made to execute. Such Possible Execution Points in the Application code for embedding Aspects are called Join Points. It is not necessary that an Aspect should be applied to all the possible Join Points.
3.3) Pointcut
As mentioned earlier, Join Points refer to the Logical Points wherein a particular Aspect or a Set of Aspects can be applied. A Pointcut or a Pointcut Definition will exactly tell on which Join Points the Aspects will be applied. To make the understanding of this term clearer, consider the following piece of code,
aspect LoggingAspect {} aspect TransactionManagementAspect {}
Assume that the above two declarations declare something of type Aspect. Now consider the following piece of code,
public void someMethod(){ //Method Start try{ // Some Business Logic Code. }catch(Exception exception){ // Exception handler Code }finally{ // Finally Handler Code for cleaning resources. } // Method End }
In the above sample code, the possible execution points, i.e. Join Points, are the start of the method, end of the method, exception block and the finally block. These are the possible points wherein any of the aspects, Logging Aspect or Transaction Management Aspect can be applied. Now consider the following Point Cut definition,
pointcut method_start_end_pointcut(){ // This point cut applies the aspects, logging and transaction, before the // beginning and the end of the method. } pointcut catch_and_finally_pointcut(){ // This point cut applies the aspects, logging and transaction, in the catch // block (whenever an exception raises) and the finally block. }
As clearly defined, it is possible to define a Point Cut that binds the Aspect to a particular Join Point or some Set of Join Points.
3.4) Advice
Now that we are clear with the terms like Aspects, Point Cuts and Join Points, let us look into what actually Advice is. To put simple, Advice is the code that implements the Aspect. In general, an Aspect defines the functionality in a more abstract manner. But, it is this Advice that provides a Concrete code Implementation for the Aspect.
In the subsequent sections, we will cover the necessary API in the form of classes and interfaces for supporting the Aspect Oriented Programming in Spring.
4) Creating Advices in Spring
As mentioned previously, Advice refers to the actual implementation code for an Aspect. Other Aspect Oriented Programming Languages also provide support for Field Aspect, i.e. intercepting a field before its value gets affected. But Spring provides support only Method Aspect. The following are the different types of aspects available in Spring.
- Before Advice
- After Advice
- Throws Advice
- Around Advice
4.1) Before Advice
Before Advice is used to intercept before the method execution starts. In AOP, Before Advice is represented in the form of org.springframework.aop.BeforeAdvice
. For example, a System should make security check on users before allowing them to accessing resources. In such a case, we can have a Before Advice that contains code which implements the User Authentication Logic.
Consider the following piece of Code,
Authentication.java
public class Authentication extends BeforeAdvice{ public void before(Method method, Object[] args, Object target) throws Throwable{ if (args[0] instanceof User){ User user = (User)args[0]; // Authenticate if he/she is the right user. } } }
The above class extends BeforeAdvice, thereby telling that before()
method will be called before the execution of the method call. Note that the java.lang.reflect.Method
method object represents target method to be invoked, Object[] args
refers to the various arguments that are passed on to the method and target
refers to the object which is calling the method.
System.java
public class System{ public void login(){ // Apply Authentication Advice here. } public void logout(){ // Apply Authentication Advice here too. } }
Note that, till now we have not seen any code that will bind the Advice implementation to the actual calling method. We will see how to do that in the Samples section.
4.2) After Advice
After Advice will be useful if some logic has to be executed before Returning the Control within a method execution. This advice is represented by the interface org.springframework.aop.AfterReturningAdvice
. For example, it is common in Application to Delete the Session Data and the various information pertaining to a user, after he has logged out from the Application. These are ideal candidates for After Advice.
CleanUpOperation.java
public class CleanUpOperation implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable{ // Clean up session and user information. } }
Note that, afterReturning()
will be method that will be called once the method returns normal execution. If some exception happens in the method execution the afterReturning()
method will never be called.
4.3) Throws Advice
When some kind of exception happens during the execution of a method, then to handle the exception properly, Throws Advice can be used through the means of org.springframework.aop.ThrowsAdvice
. Note that this interface is a marker interface meaning that it doesn’t have any method within it. The method signature inside the Throws Advice can take any of the following form,
public void afterThrowing(Exception ex) public void afterThrowing(Method method, Object[] args, Object target, Exception exception)
For example, in a File Copy program, if some kind of exception happens in the mid-way then the newly created target file has to be deleted as the partial content in the file doesn’t carry any sensible meaning. It can be easily achieved through the means of Throws Advice.
DeleteFile.java
public class DeleteFile implements ThrowsAdvice{ public void afterThrowing(Method method, Object[] args, Object target, IOException exception){ String targetFileName = (String)args[2]; // Code to delete the target file. } }
Note that the above method will be called when an Exception, that too of type IOException
is thrown by the File Copy Program.
4.4) Around Advice
This Advice is very different from the other types of Advice that we have seen before, because of the fact that, this Advice provides finer control whether the target method has to be called or not. Considering the above advices, the return type of the method signature is always void
meaning that, the Advice itself cannot change the return arguments of the method call. But Around Advice can even change the return type, thereby returning a brand new object of other type if needed.
Consider the following code,
pubic void relate(Object o1, Object o2){ o1.establishRelation(o2); }
Assume that we have a method that provides an Association link between two objects. But before that, we have to we want to ensure that the type of the Objects being passed must conform to a standard, by implementing some interfaces. We can have this arguments check in the Around Advice rather than having it in the actual method implementation. The Around Advice is represented by org.aopalliance.intercept.MethodInterceptor
.
ValidateArguments.java
public class ValidateArguments implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { Object arguments [] = invocation.getArguments() if ((arguments[0] instanceof Parent) && (arguments[1] instanceof Child) ){ Object returnValue = invocation.proceed(); return returnValue; } throw new Exception ("Arguments are of wrong type"); } }
In the above code, the validation happens over the arguments to check whether they implement the right interface. It is important to make a call to MethodInvocation.proceed()
, if we are happy with the arguments validation, else the target method will never gets invoked.
5) Creating Point Cuts in Spring
Point Cuts define where exactly the Advices have to be applied in various Join Points. Generally they act as Filters for the application of various Advices into the real implementation. Spring defines two types of Point Cuts namely the Static and the Dynamic Point Cuts. The following section covers only about the Static Point Cuts as Dynamic Point Cuts are rarely used.
5.1) The Point Cut Interface
Point Cuts in Spring are represented by org.springframework.aop.Pointcut
. Let us look into the various components of this interface. Looking at the interface definition will have something like the following,
Pointcut.java
public interface Pointcut{ ClassFilter getClassFilter() MethodMatcher getMethodMatcher() }
The getClassFilter()
method returns a ClassFilter
object which determines whether the classObject argument passed to the matches()
method should be considered for giving Advices. Following is a typical implementation of the ClassFilter
interface.
MyClassFilter.java
public class MyClassFilter implements ClassFilter{ public boolean matches(Class classObject){ String className = classObject.getName(); // Check whether the class objects should be advised based on their name. if (shouldBeAdviced(className) ){ return true; } return false; } }
The next interface in consideration is the MethodMatcher
which will filter whether various methods within the class should be given Advices. For example, consider the following code,
MyMethodMatcher.java
class MyMethodMatcher implements MethodMatcher{ public boolean matches(Method m, Class targetClass){ String methodName = m.getName(); if (methodName.startsWith("get")){ return true; } return false; } public boolean isRuntime(){ return false; } public boolean matches(Method m, Class target, Object[] args); // This method wont be called in our case. So, just return false. return false; } }
In the above code, we have 3 methods defined inside in MethodMatcher
interface. The isRuntime()
method should return true when we want to go for Dynamic Point cut Inclusion by depending on the values of the arguments, which usually happens at run-time. In our case, we can return false, which means that matches(Method method, Class target, Object[] args)
wont be called. The implementation of the 2 argument matches()
method essentially says that we want only the getter methods to be advised.
We have convenient concrete classes’ implementation the Point Cut classes which are used to give advices to methods statically. They are
- NameMatchMethod Pointcut
- Regular Expression Pointcut
5.2) NameMatchMethod Pointcut
Here the name of the methods that are too be given advices can me directly mentioned in the Configuration File. A '*'
represents that all the methods in the class should be given Advice. For example consider the following class,
MyClass.java
public class MyClass{ public void method1(){} public void method2(){} public void getMethod1() public void getMethod2() }
Suppose we wish that only the methods getMethod1()
and getMethod2()
should be given Advice by some aspect. In that case, we can have the following Configuration file that achieves this,
<bean id="getMethodsAdvisor" > <property name="mappedName"> <value>get*</value> </property> </bean>
The Expression get*
tells that all method names starting with the method name get
will be given Advices. If we want all the methods in the MyClass to be adviced, then the 'value'
tag should be given '*'
meaning all the methods in the Class.
5.3) Regular Expression Pointcut
This kind is used if you want to match the name of the methods in the Class based on Regular Expression. Spring distribution already comes with two supported flavors of Regular Expression namely Perl Regular Expression (represented by org.springframework.aop.support.Perl5RegexpMethodPointcut
) and Jdk Regular Expression (represented by org.springframework.aop.support.JdkRegexpMethodPointcut
).
Considering the following class,
MyClass.java
public class MyClass{ public void method1(){} public void method11(){} public void method2(){} public void getMethod1() public void getMethod11() public void getMethod2() }
The Expression 'm*1'
matches method1()
and method11()
, 'getMethod.'
matches only getMethod1()
and getMethod2()
. The Configuration File should be populated with the following information for gaining this kind of support.
<bean id="regExpAdvisor" > <property name="pattern"> <value>m*1<value> </property> </bean>
6) Sample Application
6.1) Introduction
Let is illustrate the various types of Advices (Before Advice, After Advice, Throws Advice and Around Advice) that we saw before in this sample Application. For this sample application let us define Adder Service which provides logic for adding two numbers. The various classes involved in Application along with the Advices in the subsequent sections.
6.2) Addder.java
This is the interface definition for the Add Service. The interface name is Adder
and it has one single method called add()
taking two arguments both of type int.
Adder.java
package net.javabeat.spring.aop.introduction.test; public interface Adder { public int add(int a,int b); }
6.3) AdderImpl.java
The implementation class for the Add Service. The logic is as simple as it returns the summation of the two numbers given as arguments. Note that, in the later section we will see how Advices get bound with this Implementation Class.
AdderImpl.java
package net.javabeat.spring.aop.introduction.test; public class AdderImpl implements Adder { public int add(int a, int b){ return a+b; } }
6.4) Before Advice Implementation
This is the Before Advice for the Adder Implmentation class. This class implements the before()
method in the MethodBeforeAdvice
interface by simply outputting a message telling that this advice is called.
LogBeforeCallAdvice.java
package net.javabeat.spring.aop.introduction.test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class LogBeforeCallAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] args, Object target) { System.out.println("Before Calling the Method"); } }
6.5) After Advice Implementation
The After Method Call Advice implements the AfterReturningAdvice
interface providing implementation for the afterReturning()
method. Like the Before Advice implementation, this Advice also outputs a simple message to the console.
LogAfterReturningAdvice.java
package net.javabeat.spring.aop.introduction.test; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class LogAfterReturningAdvice implements AfterReturningAdvice{ public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("After Normal Return from Method"); } }
6.6) Throws Advice Implementation
This Advice will be called when some kind of Exception
is caught during the method invocation. We have added a simple logic to simlate the exception when the user inputs are 0 and 0.
LogAfterThrowsAdvice.java
package net.javabeat.spring.aop.introduction.test; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; public class LogAfterThrowsAdvice implements ThrowsAdvice{ public void afterThrowing(Method method, Object[] args, Object target, Exception exception){ System.out.println("Exception is thrown on method " + method.getName()); } }
6.7) Around Advice Implementation
This Advice takes the entire control during the Method Execution. It decides whether the add()
method should be called or not based on the user inputs. Note that, only if the user inputs are not 0 and 0, then the add()
method will be called through MethodInvocation.proceed()
.
LogAroundAdvice.java
package net.javabeat.spring.aop.introduction.test; import org.aopalliance.intercept.*; public class LogAroundAdvice implements MethodInterceptor{ public Object invoke(MethodInvocation methodInvocation) throws Throwable { Object arguments[] = methodInvocation.getArguments(); int number1 = ((Integer)arguments[0]).intValue(); int number2 = ((Integer)arguments[1]).intValue(); if (number1 == 0 && number2 == 0){ throw new Exception("Dont know how to add 0 and 0!!!"); } return methodInvocation.proceed(); } }
6.8) Configuration File
The Configuration File has 3 sections. One section is the Advice Bean Definition Section which is the definition set for all the 4 advices which we saw before. All the advices are given identifiers like 'beforeCall'
, 'afterCall'
, 'throwCall'
and 'aroundCall'
. Then contains the Bean Definition for the Add implementation class which is giving the identifier 'adderImpl'
.
The next interesting section is how to bind these advices to the implementation code. For this, we have to depend on ProxyFactory Bean. This Bean is used to create Proxy objects for the Add Implementation class along with the Advice implementation. Note that the property 'proxyInterfaces'
contains the Interface Name for which the proxy class has to ge generated. In our case, it is going to be the Adder
interface. The 'interceptorNames'
property takes a list of Advices to be applied to the dynamically generated proxy class. We have given all the 4 advices to this property. Finally the implementation class for the Adder service is given in the 'target'
property.
aop-test.xml
<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-2.0.xsd"> <!-- Advices --> <bean id = "beforeCall" class = "net.javabeat.spring.aop.introduction.test.LogBeforeCallAdvice" /> <bean id = "afterCall" class = "net.javabeat.spring.aop.introduction.test.LogAfterReturningAdvice" /> <bean id = "throwCall" class = "net.javabeat.spring.aop.introduction.test.LogAfterThrowsAdvice" /> <bean id = "aroundCall" class = "net.javabeat.spring.aop.introduction.test.LogAroundAdvice" /> <!-- Implementation Class --> <bean id = "adderImpl" class = "net.javabeat.spring.aop.introduction.test.AdderImpl" /> <!-- Proxy Implementation Class --> <bean id = "adder" class = "org.springframework.aop.framework.ProxyFactoryBean"> <property name = "proxyInterfaces"> <value>net.javabeat.spring.aop.introduction.test.Adder</value> </property> <property name = "interceptorNames"> <list> <value>beforeCall</value> <value>afterCall</value> <value>throwCall</value> <value>aroundCall</value> </list> </property> <property name = "target"> <ref bean = "adderImpl"/> </property> </bean> </beans>
6.9) Test Class
Following is the test class for the Adder
Service. The code loads the Bean Definition File by depending on the BeanFactory
class. Watch carefully in the output for the various Advices getting called. Also, we have made to activate the Simulated Exception by passing 0
and 0
as arguments to the add()
method call thereby making use of the Throws Advice.
AdderTest.java
package net.javabeat.spring.aop.introduction.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.*; public class AdderTest { public static void main(String args[]){ Resource resource = new FileSystemResource("./src/aop-test.xml"); BeanFactory factory = new XmlBeanFactory(resource); Adder adder = (Adder)factory.getBean("adder"); int result = adder.add(10,10); System.out.println("Result = " + result); result = adder.add(0,0); System.out.println("Result = " + result); } }
7) Conclusion
This article provided information on how to use Spring AOP for programming the Aspects in an Application. It started with defining what Aspects are and what are problems in having the Aspects directly embedded into the Application and how to separate them using AOP. It then looked briefly into the various AOP Terminologies like Advice, Point Cut, Join Points etc. Then it moved on into the various support for creating Advices using Spring AOP. Also covered in brief are the Static Point Cuts like Name Method Match and Regular Expression Point Cuts. Finally the article concluded with a Sample Application that illustrates the usage of different Advices.
Related articles
- Plastic: Advanced Example (nofluffjuststuff.com)
- Create Your Own T4 Building Blocks (pluralsight.com)
- Transaction configuration with JPA and Spring 3.1 (baeldung.com)
- A Theory of Aspects as Latent Topics (tagide.com)
- Logging design pattern in a Zend framework application (stackoverflow.com)
- Why I will use Java EE instead of Spring in new Enterprise Java Projects in 2012 (javacodegeeks.com)
- Install JBoss 7.0.2 Application Server on Ubuntu 11.10 or Ubuntu 10.04 LTS (clean-clouds.com)
- Does Java Suck? (zef.me)
- Something Aspected-Oriented in Javascript (stackoverflow.com)
- XDDs: stay healthily skeptical and don’t drink the kool-aid (pixelmonkey.org)
What Is JAX-RPC?
JAX-RPC stands for Java API for XML-based RPC. It’s an API for building Web services and clients that used remote procedure calls (RPC) and XML. Often used in a distributed client/server model, an RPC mechanism enables clients to execute procedures on other systems.
In JAX-RPC, a remote procedure call is represented by an XML-based protocol such as SOAP. The SOAP specification defines envelope structure, encoding rules, and a convention for representing remote procedure calls and responses. These calls and responses are transmitted as SOAP messages over HTTP. In this release, JAX-RPC relies on SOAP 1.1 and HTTP 1.1.
Although JAX-RPC relies on complex protocols, the API hides this complexity from the application developer. On the server side, the developer specifies the remote procedures by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods. Client programs are also easy to code. A client creates a proxy, a local object representing the service, and then simply invokes methods on the proxy.
With JAX-RPC, clients and Web services have a big advantage–the platform independence of the Java programming language. In addition, JAX-RPC is not restrictive: a JAX-RPC client can access a Web service that is not running on the Java platform and vice versa. This flexibility is possible because JAX-RPC uses technologies defined by the World Wide Web Consortium (W3C): HTTP, SOAP, and the Web Service Description Language (WSDL). WSDL specifies an XML format for describing a service as a set of endpoints operating on messages.
A Simple Example: HelloWorld
This example shows you how to use JAX-RPC to create a Web service named HelloWorld
. A remote client of the HelloWorld
service can invoke the sayHello
method, which accepts a string parameter and then returns a string.
HelloWorld at Runtime
Figure 9-1 shows a simplified view of the HelloWorld
service after it’s been deployed. Here’s a more detailed description of what happens at runtime:
- To call a remote procedure, the
HelloClient
program invokes a method on a stub, a local object that represents the remote service. - The stub invokes routines in the JAX-RPC runtime system.
- The runtime system converts the remote method call into a SOAP message and then transmits the message as an HTTP request.
- When the server receives the HTTP request, the JAX-RPC runtime system extracts the SOAP message from the request and translates it into a method call.
- The JAX-RPC runtime system invokes the method on the tie object.
- The tie object invokes the method on the implementation of the
HelloWorld
service. - The runtime system on the server converts the method’s response into a SOAP message and then transmits the message back to the client as an HTTP response.
- On the client, the JAX-RPC runtime system extracts the SOAP message from the HTTP response and then translates it into a method response for the
HelloClient
program.

Figure 9-1 The HelloWorld
Example at Runtime
The application developer only provides the top layers in the stacks depicted by Figure 9-1. Table 9-1 shows where the layers originate.
HelloWorld Files
To create a service with JAX-RPC, an application developer needs to provide a few files. For the HelloWorld
example, these files are in the <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/hello
directory:
-
HelloIF.java
– the service definition interface -
HelloImpl.java
– the service definition implementation class, it implements theHelloIF
interface -
HelloClient.java
– the remote client that contacts the service and then invokes thesayHello
method -
config.xml
– a configuration file read by thewscompile
tool -
jaxrpc-ri.xml
– a configuration file read by thewsdeploy
tool -
web.xml
– a deployment descriptor for the Web component (a servlet) that dispatches to the service
Setting Up
If you haven’t already done so, follow these instructions in the chapter Getting Started With Tomcat:
Building and Deploying the Service
The basic steps for developing a JAX-RPC Web service are as follows.
- Code the service definition interface and implementation class.
- Compile the service definition code of step 1.
- Package the code in a WAR file.
- Generate the ties and the WSDL file.
- Deploy the service.
The sections that follow describe each of these steps in more detail.
Coding the Service Definition Interface and Implementation Class
A service definition interface declares the methods that a remote client may invoke on the service. The interface must conform to a few rules:
- It extends the
java.rmi.Remote
interface. - It must not have constant declarations, such as
public final static
. - The methods must throw the
java.rmi.RemoteException
or one of its subclasses. (The methods may also throw service-specific exceptions.) - Method parameters and return types must be supported JAX-RPC types. See the section Types Supported By JAX-RPC.
In this example, the service definition interface is HelloIF.java
:
package hello; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloIFextends Remote
{ public String sayHello(String s)throws RemoteException
; }
In addition to the interface, you’ll need to code the class that implements the interface. In this example, the implementation class is called HelloImpl
:
package hello; public class HelloImplimplements HelloIF
{ public String message ="Hello"; public String sayHello(String s) { return message + s; } }
Compiling the Service Definition Code
To compile HelloIF.java
and HelloImpl.java
, go to the <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/hello
directory and type the following:
ant compile-server
This command places the resulting class files in the build/shared
subdirectory.
Packaging the WAR File
To create the WAR file that contains the service code, type these commands:
ant setup-web-inf ant package
The setup-web-inf
target copies the class and XML files to the build/WEB-INF sub
directory. The package
target runs the jar
command and bundles the files into a WAR file named dist/hello-portable.war
. This WAR file is not ready for deployment because it does not contain the tie classes. You’ll learn how to create a deployable WAR file in the next section. The hello-portable.war
contains the following files:
WEB-INF/classes/hello/HelloIF.class WEB-INF/classes/hello/HelloImpl.class WEB-INF/jaxrpc-ri.xml WEB-INF/web.xml
The class files were created by the compile-server
target shown in the previous section. The web.xml
file is the deployment descriptor for the Web application that implements the service. Unlike the web.xml
file, the jaxrpc-ri.xml
file is not part of the specifications and is implementation-specific. The jaxrpc-ri.xml
file for this example follows:
<webServices xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd" version="1.0" targetNamespaceBase="http://com.test/wsdl" typeNamespaceBase="http://com.test/types" urlPatternBase="/ws">
Several of the webServices
attributes, such as targetNamespaceBase
, are used in the WSDL file, which you’ll create in the next section. (WSDL files can be complex and are not discussed in this tutorial. See Further Information.) Note that the urlPattern
value (/hello
) is part of the service’s URL, which is described in the section Verifying the Deployment).
For more information about the syntax of the jaxrpc-ri.xml
file, see the XML Schema file: <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/common/jax-rpc-ri-dd.xsd
.
Generating the Ties and the WSDL File
To generate the ties and the WSDL file, type the following:
ant process-war
This command runs the wsdeploy
tool as follows:
wsdeploy -tmpdir build/wsdeploy-generated -o dist/hello-deployable.war dist/hello-portable.war
This command runs the wsdeploy
tool, which performs these tasks:
- Reads the
dist/hello-portable.war
file as input - Gets information from the
jaxrpc-ri.xml
file that’s inside thehello-portable.war
file - Generates the tie classes for the service
- Generates a WSDL file named
MyHello.wsdl
- Packages the tie classes, the
Hello.wsdl
file, and the contents ofhello-portable.war
file into a deployable WAR file nameddist/hello-jaxrpc.war
The –tmpdir
option specifies the directory where wsdeploy
stores the files that it generates, including the WSDL file, tie classes, and intermediate source code files. If you specify the –keep
option, these files are not deleted.
There are several ways to access the WSDL file generated by wsdeploy
:
- Run
wsdeploy
with the –keep
option and locate the WSDL file in the directory specified by the –tmpdir
option. - Unpack (
jar
-x
) the WAR file output bywsdeploy
and locate the WSDL file in theWEB-INF
directory. - Deploy and verify the service as described in the following sections. A link to the WSDL file is on the HTML page of the URL shown in Verifying the Deployment.
Note that the wsdeploy
tool does not deploy the service; instead, it creates a WAR file that is ready for deployment. In the next section, you will deploy the service in the hello-jaxrpc.war
file that was created by wsdeploy
.
Deploying the Service
To deploy the service, type the following:
ant deploy
For subsequent deployments , run ant
redeploy
as described in the section Iterative Development.
Verifying the Deployment
To verify that the service has been successfully deployed, open a browser window and specify the service endpoint’s URL:
http://localhost:8080/hello-jaxrpc/hello
The browser should display a page titled Web Services, which lists the port name MyHello
with a status of ACTIVE
. This page also has a URL to the service’s WSDL file.
The hello-jaxrpc
portion of the URL is the context path of the servlet that implements the HelloWorld
service. This portion corresponds to the prefix of the hello-jaxrpc.war
file. The /hello
string of the URL matches the value of the urlPattern
attribute of the jaxrpc-ri.xml
file. Note that the forward slash in the /hello
value of urlPattern
is required. For a full listing of the jaxrpc-ri.xml
file, see Packaging the WAR File.
Undeploying the Service
At this point in the tutorial, do not undeploy the service. When you are finished with this example, you can undeploy the service by typing this command:
ant undeploy
Building and Running the Client
To develop a JAX-RPC client, you follow these steps:
- Generate the stubs.
- Code the client.
- Compile the client code.
- Package the client classes into a JAR file.
- Run the client.
The following sections describe each of these steps.
Generating the Stubs
Before generating the stubs, be sure to install the Hello.wsdl
file according to the instructions in Deploying the Service. To create the stubs, go to the <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/hello
directory and type the following:
ant generate-stubs
This command runs the wscompile
tool as follows:
wscompile -gen:client -d build/client -classpath build/shared config.xml
The -gen:client
option instructs wscompile
to generate client-side classes such as stubs. The -d
option specifies the destination directory of the generated files.
The wscompile
tool generates files based on the information it reads from the Hello.wsdl
and config.xml
files. The Hello.wsdl
file was intalled on Tomcat when the service was deployed. The location of Hello.wsdl
is specified by the <wsdl
> element of the config.xml
file, which follows:
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
The tasks performed by the wscompile
tool depend on the contents of the config.xml
file. For more information about the syntax of the config.xml
file, see the XML Schema file: <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/common/jax-rpc-ri-config.xsd.
Coding the Client
HelloClient
is a stand-alone program that calls the sayHello
method of the HelloWorld
service. It makes this call through a stub, a local object which acts as a proxy for the remote service. Because the stubs is created before runtime (by wscompile
), it is usually called a static stub.
To create the stub, HelloClient
invokes a private method named createProxy
. Note that the code in this method is implementation-specific and might not be portable because it relies on the MyHello_Impl
object. (The MyHello_Impl
class was generated by wscompile
in the preceding section.) After it creates the stub, the client program casts the stub to the type HelloIF
, the service definition interface.
The source code for HelloClient
follows:
package hello; import javax.xml.rpc.Stub; public class HelloClient { public static void main(String[] args) { try {Stub stub = createProxy(); HelloIF hello = (HelloIF)stub; System.out.println(hello.sayHello("Duke!"));
} catch (Exception ex) { ex.printStackTrace(); } } private static Stub createProxy() { // Note: MyHello_Impl is implementation-specific. return (Stub)(new MyHello_Impl().getHelloIFPort()); } }
Compiling the Client Code
Because the client code refers to the stub classes, be sure to follow the instructions in Generating the Stubs before compiling the client. To compile the client, go to the <JWSDP_HOME>
/docs/tutorial/examples/jaxrpc/hello
directory and type the following:
ant compile-client
Packaging the Client
To package the client into a JAR file, type the following command:
ant jar-client
This command creates the dist/hello-client.jar
file.
Running the Client
To run the HelloClient
program, type the following:
ant run
The program should display this line:
Hello Duke!
The ant run
target executes this command:
java -classpath <cpath>
hello.HelloClient
The classpath includes the hello-client.jar
file that you created in the preceding section, as well as several JAR files that belong to the Java WSDP. In order to run the client remotely, all of these JAR files must reside on the remote client’s computer.
Iterative Development
In order to show you each step of development, the previous sections instructed you to type several ant
commands. However, it would be inconvenient to type all of those commands during iterative development. To save time, after you’ve initially deployed the service, you can iterate through these steps:
- Test the application.
- Edit the source files.
- Execute
ant
build
to create the deployable WAR file. - Execute
ant
redeploy
to undeploy and deploy the service. - Execute
ant
build-static
to create the JAR file for a client with static stubs. - Execute
ant
run
.
Implementation-Specific Features
To implement the JAX-RPC Specification, the Java WSDP requires some features that are not described in the specification. These features are specific to the Java WSDP and might not be compatible with implementations from other vendors. For JAX-RPC, the implementation-specific features of the Java WSDP follow:
-
config.xml
– See Generating the Stubs for an example. -
jaxrpc-ri.xml
– See Packaging the WAR File for an example. - ties – In the preceding example, the ties are in the
hello-jaxrpc.war
file, which is implementation-specific. (Thehello-portable.war
file, however, is not implementation-specific.) - stubs – The stubs are in the
hello-client.jar
file. Note that theHelloClient
program instantiatesMyHelloImpl
, a static stub class that is implementation-specific. Because they do not contain static stubs, dynamic clients do not have this limitation. For more information about dynamic clients, see the sections A Dynamic Proxy Client Example and A Dynamic Invocation Interface (DII) Client Example . - tools – The
wsdeploy
,wscompile
, anddeploytool
utilities. - support for collections – See Table 9-1.
Using JMX within a spring application
Lately I have been doing a lot with JMX. I use it more and more to check what my application is doing. I use it to monitor tomcat, the cache, queue’s and other libraries and components. Now I wanted to use jmx to monitor my own application. Using the standard JMX stuff coming with the JDK is not hard, but since I use a lot of spring, I wanted to know more about spring support.
The most important question in the end will be, is it easier to use spring with jmx than the standard jmx stuff from the jdk.
Read on to find out about jmx and my answer to the question which is easier, the spring way or the standard jmx way.
Background
Java Management Extension was introduced to create a uniform way to manage your java applications. A very basic console was introduced jConsole. You can now monitor the cpu usage of the jvm, memory consumption and lots of other characteristics. Frameworks and tools like tomcat, ActiveMQ and EHcache provided a lot of information through jmx.
Allard has written a whitepaper about jmx that you can find on the jteam website:
http://www.jteam.nl/dms/whitepapers/JavaApplicationMonitoringAndManagement.pdf
I want to have a better look at providing your own management information through jmx. For a project I was interested in the internals of a connection pool. I wanted to learn about the amount of active connections, the idle connections, passivations and activations. For the Axon Framework we wanted to have a better insight in the amount of event listeners, command handlers, received events and handled commands. For Axon we want to use as little springframework as possible and for the other project with the connections pool I want the easiest solution.
I big advantage of jmx is that you are able to use a generic client to access it. Disadvantage is that you must have this generic client and that firewalls must enable you to connect to the jmx server. Therefore I want to have a very basic web client that provides the most basic jmx information through a web interface.
Introducing the example
The sample is a very basic service that is used to store contacts. The service provides a statistics object that keeps data about the amount of stored contacts. If you are looking for code, check the code at my source repository at google code.
http://code.google.com/p/gridshore/source/browse/#svn/trunk/JMXMonitor
MBeans the jdk way
The jdk comes with jmx out of the box. It is not hard to use. You must create an interface that ends with MXBean or use the annotation. Of course there must be an implementation as well. The implementation is than used to manage facets of your application. In our example we expose one parameter, the amount of registered contacts. The implementation uses the ContactServiceStatistics from the the ContactService. This statistics object exposes the amount of registered contacts.
Now we need to actually do something with jmx. There are two major steps:
- Obtain the platform mbeanserver
- Register the MXBean with the mbeanserver
Now we can interact with the service by adding contacts. Using the jdk provided jmx client, we can see the amount of registered contacts. The following code block shows the code for the Runner class that executes the example and provides the command line interface to add new contacts.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
public class Runner { public static void main(String[] args) throws Exception { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); ContactService service = new InMemoryContactService(); ContactServiceMonitorMXBean mbean = new ContactServiceMonitor(service); ObjectName monitorName = new ObjectName( "Gridshore:name=contactServiceMonitor" ); mbeanServer.registerMBean(mbean, monitorName); String input = "" ; do { System.out.println( "Type stop if you want to, well euh, stop !" ); input = ( new BufferedReader( new InputStreamReader(System.in))).readLine(); service.addContact( new Contact( 0 , input)); } while (! "stop" .equals(input)); } } |
The amount of code is oke, not very hard to implement. The following image gives a screendump of jconsole with the MXBean in it. Notice the location of the MXBean. The ObjectName is used to determine the location for the bean. The configured name is “Gridshore:name=contactServiceMonitor” which can easily be identified in the jconsole image.
MBeans the spring way
Now let us have a look at what spring can do for us when doing jmx. The support for jmx from spring is extensive. A complete section in the reference manual is dedicated to jmx. We are only touching a small part, but an important part. You can find out more about the spring jmx way here:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jmx.html
If you know your way with spring, the first thing that comes to mind is xml. Oke, nowadays a lot of annotations make life easier. With spring, the jdk way to find the mbeanserver and to register the beans is made easier. The javacode is replaced with some xml and annotations. I agree that in this specific case with the very basic sample the jdk way might even look easier. Still it fit’s nice with our own spring applications and there is a very big advantage using spring. That is when you want to create your own client. Spring comes with something nice to support that, this is explained in the next section.
The spring solution again consists of an interface and an implementation. The interface is actually only used for the client, so we could do with only the java class. Than we need an xml config file with only two items:
1
2
3
4
|
< beans > < context:component-scan base-package = "nl.gridshore.monitoring" /> < context:mbean-export registration = "replaceExisting" /> </ beans > |
The runner now becomes incredible easy. The following lines of code show the complete runner
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
public class Runner { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "nl/gridshore/monitoring/springannotation/spring-config.xml" ); ContactService service = context.getBean(ContactService. class ); String name = "" ; do { System.out.println( "Type stop if you want to, well euh, stop !" ); name = ( new BufferedReader( new InputStreamReader(System.in))).readLine(); service.addContact( new Contact( 0 ,name)); } while (! "stop" .equals(name)); } } |
Now the jconsole looks like the following image. Check that we now have an additional folder in the folder tree. This is the type of the bean that is registered with the mbeanserver. More on this name when we discuss the client.
JMX clients
Nice to show the data through the jconsole application. There is a catch. Of course you need to have java to start the console. But you also need to explicitly enable it for remote access. Usually this mean firewall changes. Not the easiest way. If you want basic information it might be easier to create a client yourself.
Of course the jdk comes with client capabilities as well. I did however not spend a lot of time looking at them. With spring it becomes so easy to create a client that I only look at the spring way to create a client. Check out the following page if you want more information.
http://java.sun.com/docs/books/tutorial/jmx/remote/custom.html
Spring proxy
Spring comes with a class that acts as a proxy to an MBean. This can be a bean of yourself, a bean provided by a library but also a bean running in a remote jmx server. The following lines show the addition xml configuration for the proxy. You need an interface that is implemented by the proxy. It is easy if the MXBean implements this interface, but as long as the methods are available it is not required. Using this interface you can inject the proxy into another class, the JMXTestClient. Which in itself can be a controller or a service in your own application.
1
2
3
4
5
|
< bean id = "proxyContactServiceMonitor" class = "org.springframework.jmx.access.MBeanProxyFactoryBean" > < property name = "objectName" value = "nl.gridshore.monitoring.springannotation:name=contactServiceMonitorSpring,type=ContactServiceMonitorSpring" /> < property name = "proxyInterface" value = "nl.gridshore.monitoring.springannotation.ContactServiceMonitor" /> </ bean > |
Have a good look at the value for objectName. It consists of the package name of the class that is registered as an mbean. The name is is the registeren spring bean name and the type is the actual class name.
Now the client becomes very easy. The following code block shows the client as well as the new Runner class that makes use of the client.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@Component ( "client" ) public class JmxTestClient { private ContactServiceMonitor contactServiceMonitor; public int obtainAmountOfContact() { return contactServiceMonitor.getAmountOfContacts(); } @Autowired public void setContactServiceMonitor( @Qualifier ( "proxyContactServiceMonitor" )ContactServiceMonitor contactServiceMonitor) { this .contactServiceMonitor = contactServiceMonitor; } } public class Runner { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "nl/gridshore/monitoring/springannotation/spring-config.xml" ); ContactService service = context.getBean(ContactService. class ); String name = "" ; do { int amountOfContacts = context.getBean(JmxTestClient. class ).obtainAmountOfContact(); System.out.println( "Amount of contacts now is : " + amountOfContacts); System.out.println( "Type stop if you want to, well euh, stop !" ); name = ( new BufferedReader( new InputStreamReader(System.in))).readLine(); service.addContact( new Contact( 0 ,name)); } while (! "stop" .equals(name)); } } |
The following code block shows you a screendump of a jmx client that is used to monitor the amount of items and sessions in a connection pool as well as basic information about the cache in use.
Conclusion
I like the way spring helps you in hiding some complexity. To be honest with these simple examples spring is not less code than with the plain jdk way of jmx interaction. But if the amount of beans increase, to my opinion spring becomes easier. If you are already creating a spring application I would also do the spring way, it just fits better.
What is Spring?
Spring is great framework for development of Enterprise grade applications. Spring is a light-weight framework for the development of enterprise-ready applications. Spring can be used to configure declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database. Spring framework can be used in modular fashion, it allows to use in parts and leave the other components which is not required by the application.
Features of Spring Framework:
- Transaction Management: Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in container less environments.
- JDBC Exception Handling: The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy
- Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS.
- AOP Framework: Spring is best AOP framework
- MVC Framework: Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework..
Spring Architecture
Spring is well-organized architecture consisting of seven modules. Modules in the Spring framework are:
- Spring AOP
One of the key components of Spring is the AOP framework. AOP is used in Spring:- To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on Spring’s transaction abstraction.
- To allow users to implement custom aspects, complementing their use of OOP with AOP
- Spring ORM
The ORM package is related to the database access. It provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis.
- Spring Web
The Spring Web module is part of Spring?s web application development stack, which includes Spring MVC.
- Spring DAO
The DAO (Data Access Object) support in Spring is primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO.
- Spring Context
This package builds on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API.
- Spring Web MVC
This is the Module which provides the MVC implementations for the web applications.
- Spring Core
The Core package is the most import component of the Spring Framework.
This component provides the Dependency Injection features. The BeanFactory provides a factory pattern which separates the dependencies like initialization, creation and access of the objects from your actual program logic.
The following diagram represents the Spring Framework Architecture
Spring Framework Architecture
MyEclipse for Spring 10.0: Spring 3.0 MVC Scaffolding
1. Introduction
One of the goals of MyEclipse for Spring (ME4S) is to help developers write less code from scratch and use code generation more broadly, and ME4S offers the richest set of code generation options available anywhere. ME4S can generate software components for Spring MVC, Spring Web Flow, Spring Security, REST, JAX-WS, Adobe Flex, Google Web Toolkit (GWT), JPA, DAO and JSF 2.0/Primefaces. As an alternative to writing software components from scratch, ME4S helps developers generate software components that are specific to their project. In many cases the software components may be used as-is, or they may be used as a starting point for further development. Furthermore the developer can edit the ME4S templates to further customize how the software components are generated.
Scaffolding refers to the superset of ME4S’s broad code generation functions, from generating a specific set of requested software components to generating fully implemented ready-to-run applications. Besides the obvious benefits of reducing application development effort and sparing developers from some of the chores of development, scaffolding is also frequently used for rapidly developing functional prototypes, learning new technologies, and application modernization projects. The ability to quickly generate a full application is very beneficial to prototyping because you can iterate on a domain model and regenerate a functional prototype in minutes. The prototype can be used with business analysts as a tool for elaborating requirements and as the starting point for development. ME4S helps many developers learn new technologies by generating contextual reference projects, which are personalized projects that are based on their own data model. This is a considerably better approach than hunting for sample projects that are often incomplete, don’t follow modern best practices, and are based on trivial data models. ME4S can also be used to jumpstart an application modernization effort by leveraging application artifacts from the current system for generating a significant portion of the application components for the new system.
The objective of this tutorial is to walk you through the generation of software components for Spring MVC, which is a web framework from the creators of the Spring framework. In addition to generating the Spring MVC software components, this tutorial will also generate the software components for the service, domain, and data access layers. This will result in a fully implemented ready-to-run Spring MVC application that can be immediately used for validating and testing the generated Spring MVC components, and it will also serve a reference implementation for how the generated Spring MVC components integrate with the other components of an application.
Application Architecture:
- Web Layer – Spring MVC (@Controller)
- Service Layer – Spring (@Service)
- Domain Layer – JPA (@Entity)
- Data Access Layer – Spring (@Repository)
Spring MVC – Annotation-based Programming Model
Spring MVC supports multiple approaches for implementing the web layer, and Spring 2.5 added support for an annotation-based programming model. The annotation-based approach has quickly become the predominant way of using Spring MVC, and it’s generally regarded as the best practice in Spring MVC development. The scaffolding functions of MyEclipse for Spring will generate the web layer using the annotation-based programming model.
What about other web technologies?
This tutorial is focused on Spring MVC, but the scaffolding wizard can also generate the web layer implementation for Spring Web Flow, JavaServer Faces (JSF 2.0), Adobe Flex, Google Web Toolkit (GWT) and iPhone.
MyEclipse for Spring uses the Create-Read-Update-Delete (CRUD) application pattern for generating applications that allows the end-user to manage application data. While CRUD isn’t the only application pattern, it’s a fairly typical application pattern. While not all web applications are satisfied solely by the CRUD application pattern, developers find that the resulting generated application artifacts lend themselves to being easily re-used, customized and extended. CRUD applications are tied to an application domain model that is used as the input into the scaffolding engine. The domain model can exist in many forms, and the MyEclipse for Spring scaffolding functionality supports the use of Java beans, JPA entities, or database tables as inputs.
This tutorial is going to walk you through producing a ready-to-run Spring MVC application that implements the CRUD application pattern for a domain model.
- Domain model: CUSTOMERS table from the MyEclipse Derby database.
- Target Container: MyEclipse Tomcat
- Target Database: MyEclipse Derby
MyEclipse for Spring will be used to generate the entire Spring 3.0 application within a matter of minutes that includes:
- A JPA entity corresponding to domain model (CUSTOMERS)
- A DAO for managing the JPA entity,
- Finder methods (JPA named queries) in the DAO based on domain model fields,
- A Service with fully implemented CRUD operations for managing domain model,
- A Controller with fully implemented request handlers for supporting web application,
- All the necessary Spring annotations and configuration files for a Spring MVC app,
- CRUD JSP pages using Spring Form tag library and JSTL
- Layout managed user interface using Sitemesh,
- Client-side validation implemented Spring JS with DOJO,
- CSS for UI styling
- JUnits for every Service and Controller,
- SpringSource certified code and configuration files,
- Generated code that follows Spring Recipes,
The prerequisites needed to complete this tutorial are:
- Download MyEclipse for Spring 10.0
- Create a MyEclipse Web Project (or Eclipse Dynamic Web Project) called CustomersApp.
Incompatible Compiler Compliance Level
If you get a popup dialog regarding the project compiler compliance level not matching the workspace default, specify that you want to use a custom setting for the project [click the Yes button].
It’s now time to generate all the software components and configuration files required to implement the CRUD application.
- Right-click on the CustomersApp project, and choose MyEclipse > Scaffold Spring CRUD application…
- The first step is to select the type of artifact you want to scaffold from. As mentioned in the introduction there are a variety of possible inputs into scaffolding. For this tutorial we’re going to scaffold from a pre-existing database table that comes with the MyEclipse Derby database. Choose the Database Schema option on the Select Artifact Type(s) panel. Click the Next button.
- The next step is to select the DB connection for accessing the MyEclipse Derby database. This panel will show you all configured DB connections in the MyEclipse workspace, and you must select the MyEclipse Derby connection, which is a preconfigured DB connection in MyEclipse. Click the Next button.
- The next step is to specify which database table(s) to scaffold an application from. Start by selecting the desired schema, and the select the database table(s) that should be used for scaffolding. When the CLASSICCARS schema is selected, the tables list will be populated with a list of tables. The CUSTOMER table should be added to the scaffolding list. Click the Next button.
- The next panel will prompt you to select parent objects, and this panel also lets you override the derived name of the Java Object that will be created from the database table. Since we’re only scaffolding from a single database table, the Customer Java object must be the parent. For this tutorial there’s nothing that needs to be changed on this panel. Just click the Next button.
Overriding derived Java object names
Java Object names are automatically derived from table names, but the name can be overridden by double-clicking on the name and typing a new name.
- The next step is to specify which layers of the application should be scaffolded and which package names should be used for each layer. All the layers are enabled by default. Enter org.customerapp as the base package. The package names for the different layers will be automatically derived from the base package. A sub-package (i.e. web, service, DAO, and domain) will be added to the end of the base package.
- The next step is to specify which web clients should be generated for the web layer. As you can see, there are a variety of different web clients available, including Spring MVC, Spring Web Flow, Adobe Flex, GWT, and iPhone. This tutorial is focused on Spring MVC, so click on the Generate checkbox for Spring MVC.
- The next step is an optional step to specify the REST scaffolding options. Since REST scaffolding is beyond the scope of this tutorial, we’ll just skip to the next step of the wizard.
- The next step is in an optional step to customize the UI. For this tutorial we’ll go with the defaults.
Overriding field names for user interface
The field names are automatically derived from the column names of the selected database tables, but the field name can be overridden by double-clicking on the name and typing a new name.
- The next step is to specify where the application (source code, configuration files, JSP, etc…) should be generated to in the current project and a few additional scaffolding options. For this panel the defaults are fine. Click the Next button.
- The final configuration step is to specify the libraries that should be added to the current project, how they should be added (classpath containers or copied into project), and the version of Spring MVC to use. For this panel the defaults are fine. Click the Next button.
- The final panel will give you a summary of everything that will be generated for you. Click the Finish button to scaffold the application from the information you provided in the wizard.
That’s it. Once the wizard is complete you have a ready-to-run Spring MVC application that implements the CRUD application pattern for the domain model (CUSTOMERS DB table).
Scaffold Spring DSL
If you want to use the Spring DSL, then you will need enable the project for Spring DSL support prior to scaffolding. See Enable Spring DSL in the reference guide for more info.
The next step is to see the application in operation by deploying it to Tomcat. The running application can be used for validating the generated Spring MVC components and observing the integration with the other components of the application.
- To deploy the application, right-click on the CustomersApp project and select Run As –> MyEclipse Server Application.
- MyEclipse Tomcat will start up. The first page will be a dashboard for accessing all scaffolded web clients. Since this tutorial only scaffolded a single database table for Spring MVC, the only option under Spring MVC is View Customers. Click on it to see generated Spring MVC application in operation.
Run in a separate web browser?
If you want to run the Spring MVC application in a separate browser, use the following URL: http://localhost/CustomersApp/
- The Spring MVC application can be used to (a) list all customers, (b) view customer details, (c) edit customer details, (d) delete customers and (e) add new customers. The following screen shots show some of the views.
The scaffolded application is compromised of many integrated software components, and the following diagrams provide an overview of which software components were generated and where they can be found in the project. Understanding what gets generated (and where) will help you find the individual or set of software components that you want use in your own developent projects.
Scaffolding Note
In order to ease your way into code generation with ME4S, this tutorial only involved scaffolding from a single database table. Therefore only the software components for managing a single entity were generated, and there were no entity relationships. When you start generating for multiple entities, potentially with entity relationships, there will be a lot more software components generated than what was generated with this tutorial. Even though there’s a lot more code, it will all be grouped and organized as shown in the following diagrams.
The Spring MVC scaffolding blueprint provides a snapshot of what gets generated during scaffolding. The Spring MVC blueprint and the blueprints for the other ME4S code generations capabilities are covered in more detail in the MyEclipse for Spring reference guide. (see Additional Developer Resources).
Now that you have a running Spring MVC application, you may want re-run the tutorial and scaffold different web clients, including Spring Web Flow, JavaServer Faces (JSF 2.0), Adobe Flex, Google Web Toolkit (GWT), and iPhone.
You may also want to try the Spring Annotator, JAX-WS Annotator and JPA annotator tutorials which are available in the Eclipse help system and online (see Additional Developer Resources).
What was your experience with this tutorial? Share your experience with us by completing a very brief survey.