001: package demo.dii;
002:
003: import org.omg.CORBA.Any;
004: import org.omg.CosNaming.*;
005:
006: public class DiiServer extends
007: org.omg.PortableServer.DynamicImplementation {
008: private String[] ids = { "IDL:dii/server:1.0" };
009:
010: // singleton ORB as any factory
011: org.omg.CORBA.ORB orb = null;
012:
013: serverImpl impl = new serverImpl();
014:
015: public DiiServer(org.omg.CORBA.ORB orb) {
016: this .orb = orb;
017: }
018:
019: /** from Servant */
020:
021: public String[] _all_interfaces(org.omg.PortableServer.POA poa,
022: byte[] objectId) {
023: return ids;
024: }
025:
026: public void invoke(org.omg.CORBA.ServerRequest request) {
027: String op = request.operation();
028: try {
029: if (op.equals("_get_long_number")) {
030: Any a = orb.create_any();
031: a.insert_long(impl.long_number());
032: request.set_result(a);
033: } else if (op.equals("_set_long_number")) {
034: /* set up an argument list */
035: org.omg.CORBA.NVList params = orb.create_list(0);
036: /* there is only on argument to this call, i.e. a long */
037: Any numAny = orb.create_any();
038: numAny
039: .type(orb
040: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
041: params
042: .add_value("", numAny,
043: org.omg.CORBA.ARG_IN.value);
044:
045: /* extract the argugments */
046: request.arguments(params);
047:
048: /* make the call */
049: impl.long_number(numAny.extract_long());
050:
051: /* set up the any for the result */
052: Any s = orb.create_any();
053: s
054: .type(orb
055: .get_primitive_tc(org.omg.CORBA.TCKind.tk_void));
056: request.set_result(s);
057: } else if (op.equals("writeNumber")) {
058: org.omg.CORBA.NVList params = orb.create_list(0);
059: Any numAny = orb.create_any();
060: numAny
061: .type(orb
062: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
063: params
064: .add_value("", numAny,
065: org.omg.CORBA.ARG_IN.value);
066: request.arguments(params);
067: Any a = orb.create_any();
068: a
069: .insert_string(impl.writeNumber(numAny
070: .extract_long()));
071: request.set_result(a);
072: } else if (op.equals("add")) {
073: org.omg.CORBA.NVList params = orb.create_list(0);
074: Any argOneAny = orb.create_any();
075: Any argTwoAny = orb.create_any();
076: Any outArgAny = orb.create_any();
077: argOneAny
078: .type(orb
079: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
080: argTwoAny
081: .type(orb
082: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
083: outArgAny
084: .type(orb
085: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
086:
087: /* add these anys to the parameter list */
088: params.add_value("", argOneAny,
089: org.omg.CORBA.ARG_IN.value);
090: params.add_value("", argTwoAny,
091: org.omg.CORBA.ARG_IN.value);
092: params.add_value("", outArgAny,
093: org.omg.CORBA.ARG_OUT.value);
094:
095: /* read in and inout arguments */
096: request.arguments(params);
097:
098: /* do the computation and fill it into the out arg */
099: org.omg.CORBA.IntHolder iHolder = new org.omg.CORBA.IntHolder();
100: impl.add(argOneAny.extract_long(), argTwoAny
101: .extract_long(), iHolder);
102:
103: outArgAny.insert_long(iHolder.value);
104:
105: Any resultAny = orb.create_any();
106: resultAny
107: .type(orb
108: .get_primitive_tc(org.omg.CORBA.TCKind.tk_void));
109: request.set_result(resultAny);
110: } else if (op.equals("writeNumberWithEx")) {
111: org.omg.CORBA.NVList params = orb.create_list(0);
112: Any numAny = orb.create_any();
113: numAny
114: .type(orb
115: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
116: params
117: .add_value("", numAny,
118: org.omg.CORBA.ARG_IN.value);
119: request.arguments(params);
120: Any a = orb.create_any();
121: a.insert_string(impl.writeNumberWithEx(numAny
122: .extract_long()));
123: request.set_result(a);
124: } else if (op.equals("notify")) {
125: org.omg.CORBA.NVList params = orb.create_list(0);
126: Any stringAny = orb.create_any();
127: stringAny
128: .type(orb
129: .get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
130: params.add_value("", stringAny,
131: org.omg.CORBA.ARG_IN.value);
132: request.arguments(params);
133: impl._notify(stringAny.extract_string());
134: Any s = orb.create_any();
135: s
136: .type(orb
137: .get_primitive_tc(org.omg.CORBA.TCKind.tk_void));
138: request.set_result(s);
139: } else if (op.equals("_non_existent")) {
140: Any s = orb.create_any();
141: s
142: .type(orb
143: .get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean));
144: s.insert_boolean(_non_existent());
145: request.set_result(s);
146: }
147: /**
148: * the following operations would also have to be implemented
149: * by delegating to the superclass DynamicImplementation or Servant
150: * but are omitted here for brevity
151: */
152: else if (op.equals("_all_interfaces")) {
153: throw new org.omg.CORBA.BAD_OPERATION(
154: "Object reference operations not implemented in example!");
155: } else if (op.equals("_get_interface")) {
156: throw new org.omg.CORBA.BAD_OPERATION(
157: "Object reference operations not implemented in example!");
158: } else if (op.equals("_is_a")) {
159: throw new org.omg.CORBA.BAD_OPERATION(
160: "Object reference operations not implemented in example");
161: } else {
162: throw new org.omg.CORBA.BAD_OPERATION(op
163: + " not found.");
164: }
165: } catch (org.omg.CORBA.UserException e) {
166: System.out.println("Caught: " + e);
167:
168: Any exceptAny = orb.create_any();
169: try {
170: Class helperClass = Class.forName(e.getClass()
171: .getName()
172: + "Helper");
173: Class anyClass = Class.forName("org.omg.CORBA.Any");
174: java.lang.reflect.Method insert = helperClass
175: .getDeclaredMethod("insert", new Class[] {
176: anyClass, e.getClass() });
177:
178: insert.invoke(null, new java.lang.Object[] { exceptAny,
179: e });
180: } catch (Exception sfe) {
181: sfe.printStackTrace();
182: }
183: request.set_exception(exceptAny);
184: } catch (org.omg.CORBA.SystemException e) {
185: try {
186: ((org.jacorb.orb.dsi.ServerRequest) request)
187: .setSystemException(e);
188: } catch (Exception sfe) {
189: sfe.printStackTrace();
190: }
191: } catch (Exception e) {
192: e.printStackTrace();
193: }
194: }
195:
196: public static void main(String[] args) {
197: try {
198: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
199: org.omg.PortableServer.POA poa = org.omg.PortableServer.POAHelper
200: .narrow(orb.resolve_initial_references("RootPOA"));
201:
202: poa.the_POAManager().activate();
203:
204: org.omg.CORBA.Object o = poa
205: .servant_to_reference(new DiiServer(orb));
206:
207: // register server with naming context
208: NamingContextExt nc = NamingContextExtHelper.narrow(orb
209: .resolve_initial_references("NameService"));
210: nc.rebind(nc.to_name("dii.example"), o);
211: orb.run();
212: } catch (Exception e) {
213: e.printStackTrace();
214: }
215: }
216: }
|