?? 10.txt
字號:
}
function divide (x, y) {
return x / y;
}
</isd:script>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>
</isd:service>
例程10-18
001 //samples.calculator.Calculator.Java
002 package samples.calculator;
003
004 import Java.io.*;
005 import Java.net.*;
006 import Java.util.*;
007 import Java.awt.*;
008 import Java.awt.event.*;
009 import org.apache.soap.util.xml.*;
010 import org.apache.soap.*;
011 import org.apache.soap.rpc.*;
012
013 /**
015 * do the real operations.
016 */
017 public class Calculator extends Frame {
018 String encodingStyleURI;
019 URL url;
020 TextField ef = new TextField ();
021 int state = 0; // 0 => got 0 args, 1 => got 1 arg
022 double arg1 = Double.NaN;
023 boolean dotpressed = false;
024 String lastop;
025 Label status;
026 boolean freshstart = true;
027
028 public Calculator (String title) {
029 super (title);
030
031 addWindowListener (new WindowAdapter () {
032 public void windowClosing (WindowEvent e) {
033 System.exit (0);
034 }
035 });
036
037 ef.setEditable (false);
038 add ("North", ef);
039
040 Panel p = new Panel ();
041 p.setLayout (new GridLayout (-1, 4, 5, 5));
042 String bs[] = {"7", "8", "9", "/",
043 "4", "5", "6", "*",
044 "1", "2", "3", "-",
045 "0", "+/-", ".", "+"};
046 for (int i = 0; i < bs.length; i++) {
047 Button b = new Button (" " + bs[i] + " ");
048 ActionListener al = null;
049 if ((i != 3) && (i != 7) && (i != 11) && (i != 13) && (i != 14) &&
050 (i != 15)) {
051 // digit pressed
052 b.setActionCommand (bs[i]);
053 al = new ActionListener () {
054 public void actionPerformed (ActionEvent e) {
055 if (freshstart) {
056 ef.setText ("");
057 freshstart = false;
058 }
059 Button bb = (Button) e.getSource ();
060 ef.setText (ef.getText () + bb.getActionCommand ());
061 status.setText ("");
062 }
063 };
064 } else if (i == 14) {
065 // '.' pressed
066 al = new ActionListener () {
067 public void actionPerformed (ActionEvent e) {
068 if (freshstart) {
069 ef.setText ("");
070 freshstart = false;
071 }
072 status.setText ("");
073 if (dotpressed) {
074 return;
075 } else {
076 dotpressed = true;
077 ef.setText (ef.getText () + '.');
078 }
079 }
080 };
081 } else if (i == 13) {
082 // +/- pressed
083 al = new ActionListener () {
084 public void actionPerformed (ActionEvent e) {
085 if (freshstart) {
086 ef.setText ("");
087 freshstart = false;
088 }
089 String t = ef.getText ();
090 if (t.charAt (0) == '-') {
091 ef.setText (t.substring (1));
092 } else {
093 ef.setText ('-' + t);
094 }
095 status.setText ("");
096 }
097 };
098 } else {
099 // operation
100 String ac = null;
101 if (bs[i].equals ("/")) {
102 ac = "divide";
103 } else if (bs[i].equals ("*")) {
104 ac = "times";
105 } else if (bs[i].equals ("-")) {
106 ac = "minus";
107 } else {
108 ac = "plus";
109 }
110 b.setActionCommand (ac);
111 al = new ActionListener () {
112 public void actionPerformed (ActionEvent e) {
113 Button bb = (Button) e.getSource ();
114 double arg = stringToDouble (ef.getText ());
115 if (state == 0) {
116 arg1 = arg;
117 lastop = bb.getActionCommand ();
118 freshstart = true;
119 state = 1;
120 } else {
121 try {
122 status.setText ("Working ..");
123 arg1 = doOp (lastop, arg1, arg);
124 lastop = bb.getActionCommand ();
125 ef.setText ("" + arg1);
126 freshstart = true;
127 status.setText ("");
128 } catch (SOAPException e2) {
129 status.setText ("Ouch, excepted: " + e2.getMessage ());
130 e2.printStackTrace ();
131 }
132 }
133 }
134 };
135 }
136 b.addActionListener (al);
137 p.add (b);
138 }
139
140 add ("Center", p);
141 add ("South", status = new Label ("Ready .."));
142
143 pack ();
144 show ();
145 }
146
147 private double doOp (String op, double arg1, double arg2)
148 throws SOAPException {
149 // Build the call.
150 Call call = new Call ();
151 call.setTargetObjectURI ("urn:xml-soap-demo-calculator");
152 call.setMethodName (op);
153 call.setEncodingStyleURI(encodingStyleURI);
154 Vector params = new Vector ();
155 params.addElement (new Parameter("arg1", double.class,
156 new Double (arg1), null));
157 params.addElement (new Parameter("arg2", double.class,
158 new Double (arg2), null));
159 call.setParams (params);
160
161 // make the call: note that the action URI is empty because the
162 // XML-SOAP rpc router does not need this. This may change in the
163 // future.
164 Response resp = call.invoke (/* router URL */ url, /* actionURI */ "" );
165
166 // Check the response.
167 if (resp.generatedFault ()) {
168 Fault fault = resp.getFault ();
169 System.out.println ("Ouch, the call failed: ");
170 System.out.println (" Fault Code = " + fault.getFaultCode ());
171 System.out.println (" Fault String = " + fault.getFaultString ());
172 return Double.NaN;
173 } else {
174 Parameter result = resp.getReturnValue ();
175 return ((Double)result.getValue ()).doubleValue ();
176 }
177 }
178
179 private double stringToDouble (String s) {
180 // try as a double, float or by appending a ".0" to it
181 try {
182 return Double.valueOf (s).doubleValue ();
183 } catch (NumberFormatException e1) {
184 try {
185 return Float.valueOf (s).floatValue () * 1.0;
186 } catch (NumberFormatException e2) {
187 if (s.indexOf (".") == -1) {
188 return stringToDouble (s + '.' + '0');
189 } else {
190 return Double.NaN;
191 }
192 }
193 }
194 }
195
196 public static void main (String[] args) throws Exception {
197 int maxargs = 2;
198 if (args.length != (maxargs-1)
199 && (args.length != maxargs || !args[0].startsWith ("-"))) {
200 System.err.println ("Usage: Java " + Calculator.class.getName () +
201 " [-encodingStyleURI] SOAP-router-URL");
202 System.exit (1);
203 }
204
205 Calculator c = new Calculator ("XML-SOAP Calculator");
206
207 int offset = maxargs - args.length;
208 c.encodingStyleURI = (args.length == maxargs)
209 ? args[0].substring(1)
210 : Constants.NS_URI_SOAP_ENC;
211 c.url = new URL (args[1 - offset]);
212 }
213 }
例程10-19
001 //org.apache.soap.providers.StatelessEJBProvider.Java
002 package org.apache.soap.providers;
003
004 import Java.io.* ;
005 import Java.util.* ;
006 import Javax.servlet.* ;
007 import Javax.servlet.http.* ;
008 import org.apache.soap.* ;
009 import org.apache.soap.rpc.* ;
010 import org.apache.soap.server.* ;
011 import org.apache.soap.util.* ;
012
013 import Java.lang.reflect.*;
014 import Javax.rmi.*;
015 import Javax.ejb.*;
016 import Javax.naming.*;
017 import Javax.naming.Context.*;
018
019 public class StatelessEJBProvider implements org.apache.soap.util.Provider {
020
021 private DeploymentDescriptor dd ;
022 private Envelope envelope ;
023 private Call call ;
024 private String methodName ;
025 private String targetObjectURI ;
026 private HttpServlet servlet ;
027 private HttpSession session ;
028
029 private Javax.naming.Context contxt = null;
030 private EJBObject remoteObjRef = null;
031 public static Java.lang.String CNTXT_PROVIDER_URL = "iiop://localhost:900";
032 public static Java.lang.String CNTXT_FACTORY_NAME
033 = "com.ibm.ejs.ns.jndi.CNInitialContextFactory";
034 private Vector methodParameters = null;
035 private String respEncStyle = null;
036 /**
037 * StatelessEJBProvider constructor comment.
038 */
039 public StatelessEJBProvider() {
040 super();
041 }
042
043 private void initialize() throws SOAPException {
044
045 if(contxt == null) {
046
047 Java.util.Properties properties = new Java.util.Properties();
048 properties.put(Javax.naming.Context.PROVIDER_URL,
049 CNTXT_PROVIDER_URL);
050 properties.put(Javax.naming.Context.INITIAL_CONTEXT_FACTORY,
051 CNTXT_FACTORY_NAME);
052 try {
053 contxt = new Javax.naming.InitialContext(properties);
054 } catch (NamingException ne) {
055 // ErrorListener?
056 System.out.println("Naming Exception caught during InitialContext creation @ "
057 + CNTXT_PROVIDER_URL);
058 throw new SOAPException(Constants.FAULT_CODE_SERVER,
059 "Unable to initialize context" );
060 }
061 }
062
063 }
064
065 private void initialize(String url, String factory) throws SOAPException {
066
067 if(contxt == null) {
068
069 Java.util.Properties properties = new Java.util.Properties();
070
071 properties.put(Javax.naming.Context.PROVIDER_URL, url);
072 properties.put(Javax.naming.Context.INITIAL_CONTEXT_FACTORY,
073 factory);
074
075 try {
076 contxt = new Javax.naming.InitialContext(properties);
077 } catch (NamingException ne) {
078 // ErrorListener?
079 System.out.println("Naming Exception caught during InitialContext creation @ "
080 + CNTXT_PROVIDER_URL);
081 throw new SOAPException(Constants.FAULT_CODE_SERVER,
082 "Unable to initialize context");
083 }
084 }
085
086 }
087
088
089 /**
090 * invoke method comment.
091 */
092 public void invoke(SOAPContext reqContext, SOAPContext resContext)
093 throws SOAPException {
094 System.err.println( "============================================" );
095 System.err.println("In TemplateProvider.invoke()" );
096
097 Parameter ret = null;
098 Object[] args = null;
099 Class[] argTypes = null;
100
101 respEncStyle = call.getEncodingStyleURI();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -