01: package com.sun.portal.sample.j2ee.connector.provider;
02:
03: import com.sun.portal.providers.context.ProviderContext;
04: import com.sun.portal.providers.jsp.JSPProvider;
05: import com.sun.portal.providers.ProviderException;
06:
07: import com.sun.portal.sample.j2ee.connector.comet.*;
08: import javax.resource.cci.*;
09: import javax.resource.ResourceException;
10: import javax.naming.InitialContext;
11:
12: import javax.servlet.http.HttpSession;
13: import javax.servlet.http.HttpServletRequest;
14: import javax.servlet.http.HttpServletResponse;
15:
16: public class ConnectorProvider extends JSPProvider {
17:
18: public StringBuffer getContent(HttpServletRequest req,
19: HttpServletResponse res) throws ProviderException {
20:
21: String name = (String) req.getParameter("NAME");
22: if (name != null && name.length() != 0) {
23: execute(name);
24: }
25:
26: return super .getContent(req, res);
27:
28: }
29:
30: private void execute(String name) throws ProviderException {
31: ProviderContext ctx = getProviderContext();
32: boolean ok = false;
33: ConnectionFactory cf = null;
34: String message = null;
35:
36: try {
37: javax.naming.Context nc = new InitialContext();
38: cf = (ConnectionFactory) nc
39: .lookup("java:comp/env/eis/Comet");
40: } catch (Exception e) {
41: throw new ProviderException(e.getMessage());
42: }
43: try {
44:
45: RecordFactory rf = cf.getRecordFactory();
46: MappedRecord input = rf.createMappedRecord("INPUT");
47: MappedRecord output = rf.createMappedRecord("OUTPUT");
48: input.put("NAME", getFString(10, name));
49: output.put("WELCOME_MESSAGE", "");
50:
51: Connection con = cf.getConnection();
52:
53: Interaction inter = con.createInteraction();
54:
55: CometInteractionSpec spec = new CometInteractionSpec();
56: ok = inter.execute(spec, input, output);
57: message = (String) output.get("WELCOME_MESSAGE");
58: con.close();
59:
60: if (ok) {
61: ctx.setSessionProperty("message", message);
62: } else {
63: ctx.setSessionProperty("message", "Failed to Execute");
64: }
65: } catch (ResourceException re) {
66: throw new ProviderException(
67: "ConnectionProvider.execute():ResourceException caught",
68: re);
69: }
70:
71: }
72:
73: private String getFString(int length, String val) {
74: int diff = length - val.length();
75: if (diff > 0) {
76: byte[] tmp = new byte[diff];
77: for (int i = 0; i < diff; i++)
78: tmp[i] = (byte) ' ';
79: return (val + new String(tmp));
80: } else {
81: if (diff < 0)
82: return (val.substring(0, length));
83: else
84: return (val);
85: }
86: }
87: }
|