001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.portlet.samples.weatherportlet;
039:
040: import java.util.Date;
041:
042: import javax.xml.rpc.ServiceException;
043: import javax.xml.rpc.ServiceFactory;
044: import javax.xml.rpc.Service;
045: import javax.xml.rpc.Call;
046: import javax.xml.rpc.ParameterMode;
047: import javax.xml.namespace.QName;
048: import java.io.PrintWriter;
049:
050: /**
051: * This class generates the JAXRPC invocation call for the weather service
052: */
053: public class WeatherService {
054:
055: private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri";
056: private static String NS_XSD = "http://www.w3.org/2001/XMLSchema";
057: private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/";
058: private static String NAMESPACE_URI = "urn:xmethods-Temperature-Demo";
059: private static String WEATHER_SERVICE = "TemperatureService";
060: private static String WEATHER_PORT = "TemperaturePort";
061:
062: private Service _service;
063: private QName _port;
064: private String _endPointAddr;
065: private QName _qnameTypeFloat;
066: private QName _qnameNamespaceURI;
067: private QName _qnameNS_XSD;
068: private Float _defTemp;
069:
070: public WeatherService(String endPointAddr)
071: throws WeatherServiceException {
072: try {
073: ServiceFactory factory = ServiceFactory.newInstance();
074: _service = factory
075: .createService(new QName(WEATHER_SERVICE));
076: _port = new QName(WEATHER_PORT);
077: } catch (ServiceException se) {
078: throw new WeatherServiceException(
079: "Can not create weather service: "
080: + se.getMessage());
081: }
082:
083: _endPointAddr = endPointAddr;
084: _qnameTypeFloat = new QName(NS_XSD, "float");
085: _qnameNamespaceURI = new QName(NAMESPACE_URI, "getTemp");
086: _qnameNS_XSD = new QName(NS_XSD, "string");
087: _defTemp = new Float(-999);
088: }
089:
090: public Weather getWeather(String zip, char unit)
091: throws WeatherServiceException {
092: Object result = null;
093: String[] params = { zip };
094:
095: try {
096: Call call = _service.createCall(_port);
097: call.setTargetEndpointAddress(_endPointAddr);
098: call
099: .setProperty(Call.SOAPACTION_USE_PROPERTY,
100: Boolean.TRUE);
101: call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
102: call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING);
103: call.setOperationName(_qnameNamespaceURI);
104: call.addParameter("String_1", _qnameNS_XSD,
105: ParameterMode.IN);
106:
107: call.setReturnType(_qnameTypeFloat);
108: result = call.invoke((Object[]) params);
109: } catch (Exception ex) {
110: throw new WeatherServiceException(
111: "Can not get temperature: " + ex.getMessage());
112:
113: }
114:
115: Float temp = _defTemp;
116: if (result != null) {
117: temp = new Float(result.toString());
118: }
119:
120: return new Weather(zip, unit, new Date(), temp.intValue());
121: }
122:
123: public void destroy() {
124: _service = null;
125: _port = null;
126: _endPointAddr = null;
127: _qnameTypeFloat = null;
128: _qnameNamespaceURI = null;
129: _qnameNS_XSD = null;
130: _defTemp = null;
131: }
132:
133: }
|