Packt Publishing have just agreed to send me a copy of JBoss AS 7 Configuration, Deployment and Administration by Francesco Marchioni.
I'll be reviewing the book shortly and posting my review. This looks like an interesting book as it covers management and administration of JBoss AS 7, something not often found in books.
Monday, 9 January 2012
Monday, 2 January 2012
Getting Logged On User in a Spring-Web Application
In a web application, it can be useful to get the logged on user's name and display it within a web page, for example as a link to allow the user to edit their profile. In a Spring Web application the username can easily be obtained in a controller and passed via a map to the user interface.
To get the username in a controller class, we would use the
The username can then be displayed in an HTML page as:
To get the username in a controller class, we would use the
SecurityContextHolder.getContext().getAuthentication().getPrincipal() method to get hold of the principal. We can then call the .getUsername() method to get the username of the currently logged on user.@Controller
public class PageController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest() {
User user = (User) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
Map<String, Object> userModel = new HashMap<String, Object>();
userModel.put("username", user.getUsername());
return new ModelAndView("page", "model", userModel);
}
}The username can then be displayed in an HTML page as:
<c:out value="${model.username}" />
Tuesday, 20 December 2011
Creating Calendar Based Timers in Java EE 6
Java EE 6 allows developers to create application timers that are initialized when either a Stateless Session Bean, a Singleton Bean or a Message Driven Bean are deployed to the application server.
To indicate that a method on any of these beans is to be invoked on a timed basis, the method must be annotated with either the @Schedule annotation (for single timer schedules), or the @Schedules annotation (for multiple timer schedules).
The code below shows a very simple Stateless Session Bean configured with 2 scheduled timers. The first timer is configured with one schedule whereas the second is configured with 2 schedules.
As can be seen, the first timer is annotated with the @Schedule annotation. This annotation takes several parameters that define the timer schedule:
The table above shows the allowable values that can be used for each expression used to build up a schedule. These values can also be expanded into expressions to make more complex schedules.
Wildcard: A wildcard character (*) is used to indicate that the schedule will fire for every valid value of the specific operand. For example, setting the value second="0", minute="*" would cause a timer to be invoked every minute at 0 seconds.
Lists: Comma separated lists of values allow timers to occur at every value in the list rather than at all valid values as specified by the wildcard character. For example second="0", minute="0, 15, 30, 45" would cause a timer to be invoked every quarter of an hour.
Ranges: Hypen separated ranges allow timers to occur within the specified range. For example dayOfMonth="1-5" would cause a timer to be invoked every day for the first 5 days of each month.
Intervals: Intervals are defined in the format start/interval and are valid only for hours, minutes and seconds. An interval is defined as the start value for a timer and then the interval at which a timer will be invoked. For example hour="12/1" would cause a timer to be invoked on the hour, every hour in an afternoon. It's possible to combine the wildcard and interval expressions to cause a timer to be invoked every x hours, minutes or seconds. For example minute="*/10" would cause a timer to be invoked every 10 minutes.
The second method in the example above shows how 2 different schedules can be applied to a timer. In this instance, the method is annotated with the @Schedules annotation rather than the @Schedule annotation.
To indicate that a method on any of these beans is to be invoked on a timed basis, the method must be annotated with either the @Schedule annotation (for single timer schedules), or the @Schedules annotation (for multiple timer schedules).
The code below shows a very simple Stateless Session Bean configured with 2 scheduled timers. The first timer is configured with one schedule whereas the second is configured with 2 schedules.
package com.acme.timer;
import javax.ejb.Schedule;
import javax.ejb.Schedules;
import javax.ejb.Stateless;
import javax.ejb.Timer;
@Stateless
public class CalendarTimer {
@SuppressWarnings("unused")
@Schedule(second = "*/10", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "Scheduled Timer")
private void scheduledTimeout(final Timer t) {
System.out.println(t.getInfo().toString() + " called at: "
+ new java.util.Date());
}
@SuppressWarnings("unused")
@Schedules({
@Schedule(second = "15", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "2nd Scheduled Timer"),
@Schedule(second = "45", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "2nd Scheduled Timer") })
private void scheduledTimeout2(final Timer t) {
System.out.println(t.getInfo().toString() + " called at: "
+ new java.util.Date());
System.out.println();
}
}
As can be seen, the first timer is annotated with the @Schedule annotation. This annotation takes several parameters that define the timer schedule:
| second | Number of seconds: 0 through 59 |
| minute | Number of minutes: 0 through 59 |
| hour | Number of hours: 0 through 23 |
| dayOfWeek | Day of the week. This can take textual values (Sun, Mon, Tue, Wed, Thu, Fri, Sat) or numerical values 0 through 7 (both 0 and 7 indicate Sunday) |
| dayOfMonth | Day of the month. This can take textual values (1st, 2nd etc), or numeric values 1 through 31. Negative values can also be used to indicate days before the end of the month. The value Last can also be used to indicate the last day of the month. |
| month | Month of the year. This can take textual values (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) or numerical values 1 through 12 |
| year | The year. This can take numeric years in the format yyyy. |
| info | Additional information passed to the timer function. |
The table above shows the allowable values that can be used for each expression used to build up a schedule. These values can also be expanded into expressions to make more complex schedules.
Wildcard: A wildcard character (*) is used to indicate that the schedule will fire for every valid value of the specific operand. For example, setting the value second="0", minute="*" would cause a timer to be invoked every minute at 0 seconds.
Lists: Comma separated lists of values allow timers to occur at every value in the list rather than at all valid values as specified by the wildcard character. For example second="0", minute="0, 15, 30, 45" would cause a timer to be invoked every quarter of an hour.
Ranges: Hypen separated ranges allow timers to occur within the specified range. For example dayOfMonth="1-5" would cause a timer to be invoked every day for the first 5 days of each month.
Intervals: Intervals are defined in the format start/interval and are valid only for hours, minutes and seconds. An interval is defined as the start value for a timer and then the interval at which a timer will be invoked. For example hour="12/1" would cause a timer to be invoked on the hour, every hour in an afternoon. It's possible to combine the wildcard and interval expressions to cause a timer to be invoked every x hours, minutes or seconds. For example minute="*/10" would cause a timer to be invoked every 10 minutes.
The second method in the example above shows how 2 different schedules can be applied to a timer. In this instance, the method is annotated with the @Schedules annotation rather than the @Schedule annotation.
Using GlassFish from Eclipse
When I think of developing apps using GlassFish, I usually think of using NetBeans for the development. As you'd expect however, Oracle provides some good tooling to allow you to develop against GlassFish from within Eclipse - even to the point of installing the application server directly from within Eclipse. So, how is this done?
Installing GlassFish Server Tools
Contrary to what you'd expect, to install GlassFish tooling, you don't use the Eclipse Marketplace for installation. To install, right click within the "Servers" pane and select "New | Server" to display the "Define a New Server" dialog. On this dialog, select "Download additional server adaptors" and in the resulting dialog select "Oracle GlassFish Server Tools".
After accepting the licence conditions, the GlassFish Server tools will be downloaded upon which you'll need to restart Eclipse to complete the installation.
Defining a GlassFish Instance
After a restart, opening the "Define a New Server" dialog will show that GlassFish support has been added.
Choose the GlassFish server you wish to use and press the "Next" button. On the resulting dialog, you can choose to locate an existing copy of GlassFish, or install an new instance via the "Install Server" button if you don't already have GlassFish installed.
One point to note here is that, if you install a new instance of GlassFish, you may then need to browse through the install folder to locate the "glassfish" folder within. You will see that this is necessary because the "Next>" button will be disabled as you've installed an instance of GlassFish, but not correctly identified its location to Eclipse.
When you've selected (and installed if necessary) a GlassFish instance, complete the wizard to define the admninistrator credentials of the server.
GlassFish Tools
After creating a GlassFish server instance in Eclipse, you can develop and test Java EE 6 applications against it. The GlassFish tools provides additional support to assist in this. For example, right clicking on the server in the "Servers" tab allows you to manage the server or get additional information or support.
New wizards are also installed into Eclipse to assist when creating GlassFish facets such as JMS or JDBC resources as shown below. These wizards are available on the standard "File | New | Other" dialog under the "GlassFish" folder.
Installing GlassFish Server Tools
Contrary to what you'd expect, to install GlassFish tooling, you don't use the Eclipse Marketplace for installation. To install, right click within the "Servers" pane and select "New | Server" to display the "Define a New Server" dialog. On this dialog, select "Download additional server adaptors" and in the resulting dialog select "Oracle GlassFish Server Tools".
After accepting the licence conditions, the GlassFish Server tools will be downloaded upon which you'll need to restart Eclipse to complete the installation.
Defining a GlassFish Instance
After a restart, opening the "Define a New Server" dialog will show that GlassFish support has been added.
Choose the GlassFish server you wish to use and press the "Next" button. On the resulting dialog, you can choose to locate an existing copy of GlassFish, or install an new instance via the "Install Server" button if you don't already have GlassFish installed.
One point to note here is that, if you install a new instance of GlassFish, you may then need to browse through the install folder to locate the "glassfish" folder within. You will see that this is necessary because the "Next>" button will be disabled as you've installed an instance of GlassFish, but not correctly identified its location to Eclipse.
When you've selected (and installed if necessary) a GlassFish instance, complete the wizard to define the admninistrator credentials of the server.
GlassFish Tools
After creating a GlassFish server instance in Eclipse, you can develop and test Java EE 6 applications against it. The GlassFish tools provides additional support to assist in this. For example, right clicking on the server in the "Servers" tab allows you to manage the server or get additional information or support.
New wizards are also installed into Eclipse to assist when creating GlassFish facets such as JMS or JDBC resources as shown below. These wizards are available on the standard "File | New | Other" dialog under the "GlassFish" folder.
Thursday, 24 November 2011
Eclipse - 'Building workspace' has encountered a problem
I've got a web project that uses some 3rd party JavaScript libraries. Unfortunately whenever Eclipse (Indigo) builds the project, it gives an error "'Building workspace' has encountered a problem".
The details of the error read:
One solution though appears to be to edit the project Builders (Right click on Project, select "Properties" | "Builders") and un-tick the JavaScript Validator option.
When you do this, Eclipse complains that this is an advanced operation and may cause side effects, so it doesn't feel like the correct way to progress to me. Having said that, it does eliminate the problem and allows Eclipse to build the project without NPEs.
Has anyone else experienced this problem and have a workaround that doesn't involve disabling the 'JavaScript Validator' Builder, or is this the best approach in this situation?
The details of the error read:
Errors occurred during the build.
Errors running builder 'JavaScript Validator' on project 'project'.
java.lang.NullPointerException
On first inspection, it looks as though the solution would be to edit the JavaScript validator preferences and make it a bit less verbose. Unfortunately though, there doesn't seem to be any options in Eclipse to do this.
One solution though appears to be to edit the project Builders (Right click on Project, select "Properties" | "Builders") and un-tick the JavaScript Validator option.
When you do this, Eclipse complains that this is an advanced operation and may cause side effects, so it doesn't feel like the correct way to progress to me. Having said that, it does eliminate the problem and allows Eclipse to build the project without NPEs.
Has anyone else experienced this problem and have a workaround that doesn't involve disabling the 'JavaScript Validator' Builder, or is this the best approach in this situation?
Labels:
Eclipse,
JavaScript
Friday, 29 July 2011
Review of EJB 3.1 Cookbook by Richard M. Reese
I've just posted my review of "EJB 3.1 Cookbook" over on Develop In Java.
In summary:
I very much liked this book and can imagine using it frequently. You can read the whole review here.
In summary:
EJB 3.1 Cookbook is an excellent resource in an EJB developer’s library. It’s not the sort of book you pick up and read from cover to cover rather a useful resource that you pick up when you think “How do I do xxx in EJB?”. With over 100 recipes this book is recommended to developers using EJB technologies.
I very much liked this book and can imagine using it frequently. You can read the whole review here.
Labels:
Book Review,
EJB
Tuesday, 12 July 2011
Forthcoming Book Reviews
Packt Publishing have recently released two new books of interest to Java EE developers.
EJB 3.1 Cookbook and Java EE6 With NetBeans 7 both look like good reads and Packt have graciously agreed to send me a copy of each of these books for review, which I'll post on the site as soon as I've reviewed them.
Thanks to Nicole for sorting this out for me.
EJB 3.1 Cookbook and Java EE6 With NetBeans 7 both look like good reads and Packt have graciously agreed to send me a copy of each of these books for review, which I'll post on the site as soon as I've reviewed them.
Thanks to Nicole for sorting this out for me.
Labels:
Book Review,
EE6,
EJB,
NetBeans
Subscribe to:
Posts (Atom)





