In this post I will make the Skyway Services from (part 3) available as Appcelerator services that can be used by an Appcelerator-based RIA application. I was hoping I could just use Spring services, but at SpringOne 2008, Kevin Whinery said the recommended approach is to use Appcelerator services and wire in Spring services. It's an extra step, but it turns out that it gave me a place where I could workaround some issues I had with Appcelerator's automatic JSON serialization logic. While I could've updated the Spring beans to return JSON to the Appcelerator service, I wanted to avoid tweaking my Spring services to accomodate Appcelerator.
22: private BeanFactory factory;
23:
24: public WeatherService()
25: {
26: String[] configFiles = new String[]
27: { "NOAAWeather-generated-web-context.xml",
28: "NOAAWeather-generated-service-context.xml",
29: "NOAAWeather-generated-domain-context.xml",
30: "NOAAWeather-generated-dao-context.xml"};
31:
32: setFactory(new ClassPathXmlApplicationContext(configFiles));
33:
34: }35:
36: @Service(request = "app.locations.get.message.request",
37: response = "app.locations.get.message.response",
38: version = "1.0")
39: protected void getLocations (Message request, Message response) throws Exception
40: {
41: Set<CityCoordinates> locations = null;
42: try {
43: locations = ((noaaweather.service.weather.WeatherService) getFactory().
44: getBean("WeatherService")).loadLocations();
45:
46: }
47: catch(Exception e)
48: {
49: System.out.println("EXCEPTION = "+e.toString());
50: }
51: net.sf.json.JSONArray ja = net.sf.json.JSONArray.fromObject(locations);
52:
53: response.getData().put("locations", ja);
54: return;
55:
56: }
5758: @Service(request = "app.forecast.get.request",
59: response = "app.forecast.get.response",
60: version = "1.0")
61: protected void getForecast (Message request, Message response) throws Exception
62: {
63: Set<WeatherData> forecast = null;
64:
65: Calendar now = Calendar.getInstance();
66: Calendar later = Calendar.getInstance();
67: later.add(Calendar.DATE, 5);
68: try {
69: forecast = ((noaaweather.service.weather.WeatherService) getFactory()
70: .getBean("WeatherService"))
71: .getWeatherForecast((String)request.getData().getString("citystate"),now,later);
72: }
73: catch(Exception e)
74: {
75: System.out.println("EXCEPTION = "+e.toString());
76: }
77: net.sf.json.JSONArray ja = net.sf.json.JSONArray.fromObject(forecast);
78: response.getData().put("forecast", ja);
79: return;
80: }


