|
Before moving on to editing the BlazeDS configuration files for the object remoting, it is appropriate to create the Java objects that will be remotely accessed. Listing 10 shows the Java class (TopArticlesServer) that the Flex client will interact with.
Listing 10. TopArticlesServer.java: Java class to be remotely accessed by Flex
package javaworld.dustin.remoting;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import static javaworld.dustin.remoting.TopArticleConstants.*;
/**
* Simple Java class to be used in Flex/Java remoting example. Represents the
* top articles on JavaWorld.
*
* @author Dustin
*/
public class TopArticlesServer
{
/** Map of year+rank key to article corresponding to that year and rank. */
private static final Map<String, TopArticle> topArticles =
new HashMap<String, TopArticle>();
static
{
final TopArticle article200801 =
new TopArticle(
"Hello, OSGi, Part 1: Bundles for Beginners",
"Sunil Patil",
2008,
"http://www.javaworld.com/javaworld/jw-03-2008/jw-03-osgi1.html",
1);
topArticles.put("2008_01", article200801);
final TopArticle article200802 =
new TopArticle(
"Eclipse 3.3 or NetBeans 6.0?",
"Andrew Binstock",
2008,
"http://www.javaworld.com/javaworld/jw-03-2008/jw-03-java-ides0308.html",
2);
topArticles.put("2008_02", article200802);
final TopArticle article200803 =
new TopArticle(
"Is Tomcat Enterprise Ready?",
"Jeff Hanson",
2008,
"http://www.javaworld.com/javaworld/jw-01-2008/jw-01-tomcat6.html",
3);
topArticles.put("2008_03", article200803);
final TopArticle article200804 =
new TopArticle(
"iBATIS, Hibernate, and JPA: Which is Right for You?",
"K. L. Nitin et al",
2008,
"http://www.javaworld.com/javaworld/jw-07-2008/jw-07-orm-comparison.html",
4);
topArticles.put("2008_04", article200804);
final TopArticle article200805 =
new TopArticle(
"Merging and Branching in Subversion 1.5",
"John Ferguson Smart",
2008,
"http://www.javaworld.com/javaworld/jw-01-2008/jw-01-svnmerging.html",
5);
topArticles.put("2008_05", article200805);
final TopArticle article200806 =
new TopArticle(
"Understanding JPA, Part 1",
"Aditi Das",
2008,
"http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html",
6);
topArticles.put("2008_06", article200806);
final TopArticle article200807 =
new TopArticle(
"Four Harmful Java Idioms, and How to Fix Them",
"John O'Hanley",
2008,
"http://www.javaworld.com/javaworld/jw-07-2008/jw-07-harmful-idioms.html",
7);
topArticles.put("2008_07", article200807);
final TopArticle article200808 =
new TopArticle(
"Asynchronous HTTP Comet Architectures",
"Gregor Roth",
2008,
"http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp-test.html",
8);
topArticles.put("2008_08", article200808);
final TopArticle article200809 =
new TopArticle(
"Introduction to Hibernate Search",
"Dr. Xinyu Liu",
2008,
"http://www.javaworld.com/javaworld/jw-07-2008/jw-07-hibernate-search.html",
9);
topArticles.put("2008_09", article200809);
final TopArticle article200810 =
new TopArticle(
"Open Source Java Projects: Java Native Access",
"Jeff Friesen",
2008,
"http://www.javaworld.com/javaworld/jw-02-2008/jw-02-opensourcejava-jna.html",
10);
topArticles.put("2008_10", article200810);
}
/** No-arguments constructor necessary for object binding. */
public TopArticlesServer() {}
/**
* Provide the article corresponding to the provided year and rank desired.
*
* @param year Publication year of article.
* @param rank Rank of desired article.
* @return Article matching provided year and rank; the article will have a
* rank of zero (0) if no actual matching article was found.
* @throws IllegalArgumentException Thrown if the publication year or rank
* are not valid.
*/
public TopArticle getRankedArticle(final int year, final int rank)
{
System.err.println("getRankedArticle() received ranking of " + rank);
TopArticleUtil.validateArticleRankAndYearInput(year, rank);
final String extraDigit = rank < 10 ? "0" : "";
final String key =
String.valueOf(year) + "_" + extraDigit + String.valueOf(rank);
TopArticle topArticle = topArticles.get(key);
if (topArticle == null)
{
final Calendar today = Calendar.getInstance();
topArticle =
new TopArticle(
DEFAULT_TITLE + " (No Match!)",
DEFAULT_AUTHOR,
DEFAULT_YEAR,
DEFAULT_URL,
NO_MATCHING_ARTICLE_RANK);
}
return topArticle;
}
}
The getRankedArticle(int,int) method in TopArticlesServer returns an instance of TopArticle. Listing 11 shows the definition of TopArticle. |
|