?? 10.txt
字號:
102
103 if (methodParameters != null) {
104 int parametersCount = methodParameters.size ();
105 args = new Object[parametersCount];
106 argTypes = new Class[parametersCount];
107
108 for (int i = 0; i < parametersCount; i++) {
109 Parameter param = (Parameter) methodParameters.elementAt (i);
110 args[i] = param.getValue ();
111 argTypes[i] = param.getType ();
112
113 if (respEncStyle == null) {
114 respEncStyle = param.getEncodingStyleURI ();
115 }
116 }
117 }
118
119 if (respEncStyle == null) {
120 respEncStyle = Constants.NS_URI_SOAP_ENC;
121 }
122
123 try {
124
125 Method m = MethodUtils.getMethod (remoteObjRef, methodName, argTypes);
126 Bean result = new Bean (m.getReturnType (),
127 m.invoke (remoteObjRef, args));
128
129
130 if (result.type != void.class) {
131 ret = new Parameter (RPCConstants.ELEM_RETURN, result.type,
132 result.value, null);
133 }
134
135
136 } catch (InvocationTargetException e) {
137 System.err.println("Exception Caught upon method invocation attempt: "
138 + e.getMessage());
139 Throwable t = e.getTargetException ();
140 throw new SOAPException (Constants.FAULT_CODE_SERVER,
141 t.getMessage(), t);
142 }
143 catch (Throwable t) {
144 System.err.println("Exception Caught upon method invocation attempt: "
145 + t.toString());
146 throw new SOAPException (Constants.FAULT_CODE_SERVER,
147 t.getMessage(), t);
148 }
149
150 try {
151 Response resp = new Response( targetObjectURI,
152 // URI
153 call.getMethodName(),
154 // Method
155 (Parameter) ret,
156 // ReturnValue
157 null,
158 // Params
159 null,
160 // Header
161 respEncStyle,
162 // encoding
163 resContext );
164 // response soapcontext - not supported yet
165 Envelope env = resp.buildEnvelope();
166 StringWriter sw = new StringWriter();
167 env.marshall( sw, call.getSOAPMappingRegistry(), resContext );
168 resContext.setRootPart( sw.toString(),
169 Constants.HEADERVAL_CONTENT_TYPE_UTF8);
170 }
171 catch( Exception e ) {
172 if ( e instanceof SOAPException )
173 throw (SOAPException ) e ;
174 throw new SOAPException( Constants.FAULT_CODE_SERVER,
175 e.toString() );
176 }
177 }
178
179 /**
180 * locate method comment.
181 */
182 public void locate(DeploymentDescriptor dd,
183 Envelope env,
184 Call call,
185 String methodName,
186 String targetObjectURI,
187 SOAPContext reqContext)
188 throws org.apache.soap.SOAPException {
189
190 HttpServlet servlet = (HttpServlet)
191 reqContext.getProperty( Constants.BAG_HTTPSERVLET );
192 HttpSession session = (HttpSession)
193 reqContext.getProperty( Constants.BAG_HTTPSESSION );
194
195 System.err.println( "===========================================" );
196 System.err.println( "In TemplateProvider.locate()" );
197 System.err.println( "URI: " + targetObjectURI );
198 System.err.println( "DD.ServiceClass: " + dd.getServiceClass() );
199 System.err.println( "DD.ProviderClass: " + dd.getProviderClass() );
200 System.err.println( "Call.MethodName: " + call.getMethodName() );
201
202 this.dd = dd ;
203 this.envelope = env ;
204 this.call = call ;
205 this.methodName = methodName ;
206 this.targetObjectURI = targetObjectURI ;
207 this.servlet = servlet ;
208 this.session = session ;
209
210 Hashtable props = dd.getProps();
211
212
213 String ContxtProviderURL = (String) props.get("ContextProviderURL");
214 String ContxtFactoryName = (String) props.get("FullContextFactoryName");
215
216 if ((ContxtProviderURL != null) && (ContxtFactoryName != null))
217 initialize(ContxtProviderURL, ContxtFactoryName);
218 else
219 initialize();
220
221 String homeInterfaceName = (String) props.get("FullHomeInterfaceName");
222 if (homeInterfaceName == null)
223 throw new SOAPException(Constants.FAULT_CODE_SERVER,
224 "Error in Deployment Descriptor Property Settings");
225
226 // From the Deployment Descriptor get the JNDI name
227 String jndiName = (String) props.get("JNDIName");
228 if ( jndiName == null ) jndiName = dd.getProviderClass();
229
230 if ((jndiName != null) && (contxt != null)) {
231
232 try {
233
234 // Use service name to locate EJB home object via the contxt
235 // EJBHome home = (EJBHome) contxt.lookup(jndiName);
236 EJBHome home = (EJBHome) PortableRemoteObject.narrow(
237 contxt.lookup(jndiName),Class.forName(homeInterfaceName));
238 // call the 'create' method on the EJB home object, and store the
239 // ref to the EJB object.
240 Method createMethod = home.getClass().getMethod("create", new Class[0]);
241 remoteObjRef = (EJBObject) createMethod.invoke((Object) home, new Object[0]);
242
243 } catch (Exception e) {
244
245 System.out.println("Exception caught: " + e.toString());
246 throw new SOAPException(Constants.FAULT_CODE_SERVER,"Error in
247 connecting to EJB", e); }
248 }
249
250 // Once previous steps have been successful, then we take the Call Object
251 // and extract the method name from it, and any parameters, and store them.
252 methodName = call.getMethodName();
253 methodParameters = call.getParams();
254
255
256 }
257 }
例程10-20
01 package samples.weblogic_ejb;
02
03 /**
04 * This is an Enterprise Java Bean Remote Interface
05 */
06 public interface HelloService extends Javax.ejb.EJBObject {
07
08 /**
09 *
10 * @return Java.lang.String
11 * @param phrase Java.lang.String
12 * @exception String The exception description.
13 */
14 Java.lang.String hello(Java.lang.String phrase) throws Java.rmi.RemoteException;
15 }
例程10-21
01 package samples.weblogic_ejb;
02
03 /**
04 * This is a Home interface for the Session Bean
05 */
06 public interface HelloServiceHome extends Javax.ejb.EJBHome {
07
08 /**
09 * create method for a session bean
10 * @return samples.HelloService
11 * @exception Javax.ejb.CreateException The exception description.
12 * @exception Java.rmi.RemoteException The exception description.
13 */
14 HelloService create() throws Javax.ejb.CreateException, Java.rmi.RemoteException;
15 }
例程10-22
01 package samples.weblogic_ejb;
02
03 import Java.rmi.RemoteException;
04 import Java.security.Identity;
05 import Java.util.Properties;
06 import Javax.ejb.*;
07
08 /**
09 * This is a Session Bean Class.
10 * It can be used with a stateless or stateful SOAP EJB provider.
11 *
12 * @author <a href="mailto:olivier@intraware.com">Olivier Brand</a>
13 * @version 1.0
14 * @since 1.0
15 * @see SessionBean
16 */
17 public class HelloServiceBean implements SessionBean {
18 private Javax.ejb.SessionContext mySessionCtx = null;
19
20 // keeps track of the calls
21 private int nbcall = 0;
22
23 /**
24 * ejbActivate method comment
25 * @exception Java.rmi.RemoteException The exception description.
26 */
27 public void ejbActivate() throws Java.rmi.RemoteException {}
28 /**
29 * ejbCreate method comment
30 * @exception Javax.ejb.CreateException The exception description.
31 * @exception Java.rmi.RemoteException The exception description.
32 */
33 public void ejbCreate() throws
34 Javax.ejb.CreateException, Java.rmi.RemoteException {}
35
36 /**
37 * ejbPassivate method comment
38 * @exception Java.rmi.RemoteException The exception description.
39 */
40 public void ejbPassivate() throws Java.rmi.RemoteException {}
41 /**
42 * ejbRemove method comment
43 * @exception Java.rmi.RemoteException The exception description.
44 */
45 public void ejbRemove() throws Java.rmi.RemoteException {}
46 /**
47 * getSessionContext method comment
48 * @return Javax.ejb.SessionContext
49 */
50 public Javax.ejb.SessionContext getSessionContext() {
51 return mySessionCtx;
52 }
53 /**
54 * Insert the method's description here.
55 * Creation date: (10/31/00 1:44:19 PM)
56 * @return Java.lang.String
57 * @param phrase Java.lang.String
58 */
59 public String hello(String phrase)
60 {
61 try
62 {
63 Thread.sleep(0);
64 }
65 catch(InterruptedException ex){}
66 finally
67 {
68 return "HELLO!! You just said :" + phrase
69 + " nb time: " + nbcall++;
70 }
71 }
72 /**
73 * setSessionContext method comment
74 * @param ctx Javax.ejb.SessionContext
75 * @exception Java.rmi.RemoteException The exception description.
76 */
77 public void setSessionContext(Javax.ejb.SessionContext ctx)
78 throws Java.rmi.RemoteException {
79 mySessionCtx = ctx;
80 }
81 }
例程10-23
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://Java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>helloServiceSession</ejb-name>
<home>samples.HelloServiceHome</home>
<remote>samples.HelloService</remote>
<ejb-class>samples.HelloServiceBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
例程10-24
01 <?xml version="1.0"?>
02 <isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
03 id="urn:ejbhello">
04 <isd:provider type="org.apache.soap.providers.StatefulEJBProvider"
05 scope="Application"
06 methods="create">
07 <isd:Java class="samples.HelloService"/>
08 <isd:option key="FullHomeInterfaceName" value="samples.HelloServiceHome" />
09 <isd:option key="ContextProviderURL" value="t3://localhost:7001" />
10 <isd:option key="FullContextFactoryName"
11 value="weblogic.jndi.WLInitialContextFactory" />
12 </isd:provider>
13 <isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>
14 </isd:service>
例程10-25
01 //samples.weblogic_ejb.ejbtest.Java
02 package samples.weblogic_ejb;
03
04 import Java.io.*;
05 import Java.net.*;
06 import Java.util.*;
07 import org.apache.soap.*;
08 import org.apache.soap.rpc.*;
09
10 /**
11 * This sample class makes use of a stateful ejb service.
12 * The code contains a loop of 10 on one of the service's method to show that
13 * we are in a stateful mode.On the server side an instance variable (counter)
14 * keeps track on how many time the service has been called.
15 *
16 * @author <a href="mailto:olivier@intraware.com">Olivier Brand</a>
17 * @version 1.0
18 * @since 1.0
19 */
20 public class ejb
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -