/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. /********** CORBAServer.java  **************************/ 

2.  

3. import IDLgenerated.*; 

4. import org.omg.CORBA.*; 

5. import org.omg.CosNaming.*; 

6.  

7. class TheServant extends _CORBAServantImplBase 

8.

9.   private int sum ; 

10.  

11.   public String 

12.   sayHi() 

13.   { 

14.     return "Hi guys!"

15.   } 

16.  

17.   public int 

18.   increment ( int plus ) 

19.   { 

20.     sum += plus ; 

21.     return sum ; 

22.   } 

23.

24. // end class TheServant 

25. public class CORBAServer 

26.

27.  

28.   public static void 

29.   main( String args[] ) 

30.   { 

31.   try

32.   ORB orb = ORB.init ( args, null ); 

33.  

34.   TheServant tsref = new TheServant(); 

35.  

36.   orb.connect ( tsref ); 

37.  

38.   org.omg.CORBA.Object objRef = 

39.     orb.resolve_initial_references ( "NameService" ); 

40.  

41.   NamingContext ncRef = NamingContextHelper.narrow (objRef) ; 

42.  

43.   NameComponent nc = new NameComponent ("CORBAServant", ""); 

44.   NameComponent path[] = { nc }; 

45.   ncRef.rebind ( path, tsref ); 

46.   System.out.println( "Ready to serve" ); 

47.  

48.   java.lang.Object sync = new java.lang.Object(); 

49.   synchronized(sync) 

50.   { 

51.     sync.wait(); 

52.   } 

53.   } 

54.   catch(Exception e) { 

55.     System.err.println ( "Error : " + e ); 

56.   } 

57.   } 

58.

59.  

60. ====== CORBAClient.java ========= 

61.  

62. import IDLgenerated.*; 

63. import org.omg.CosNaming.*; 

64. import org.omg.CosNaming.NamingContextPackage.*; 

65. import org.omg.CORBA.*; 

66.  

67.  

68. public class CORBAClient 

69.

70.   public static void 

71.   main ( String args[] ) 

72.   { 

73.   try

74.  

75.   ORB orb = ORB.init(args, null); 

76.  

77.   org.omg.CORBA.Object objRef = 

78.     orb.resolve_initial_references( "NameService" ); 

79.  

80.   NamingContext ncRef = NamingContextHelper.narrow(objRef); 

81.  

82.   NameComponent nc = new NameComponent ("CORBAServant", ""); 

83.   NameComponent path[] = { nc } ; 

84.  

85.   org.omg.CORBA.Object srv = ncRef.resolve(path) ; 

86.  

87.   CORBAServant csref = CORBAServantHelper.narrow ( srv ); 

88.  

89.   String str = csref.sayHi (); 

90.   System.out.println (str); 

91.  

92.   int cnt = csref.increment ( 5 ); 

93.   System.out.println ( cnt ); 

94.  

95.   cnt = csref.increment ( 25 ); 

96.   System.out.println ( cnt ); 

97.  

98.   } 

99.   catch(Exception e) { 

100.     System.out.println("Error : " + e); 

101.   } 

102.   } 

103.

104.  

105. ================ 

106. ExCORBA.idl : 

107. ================ 

108.   module IDLgenerated 

109.   { 

110.     interface CORBAServant 

111.     { 

112.     long increment ( in long plus ); 

113.     string sayHi (); 

114.     }; 

115.   }; 

116. ======= data types  =============== 

117. IDL    ---     Java 

118.

119. module  ---   package 

120. interface  ---   (same) 

121. boolean  ---   (same) 

122. char (8-bit)  ---   (same) 

123.  

124. octet  ---   byte 

125. string  ---   String 

126. short  ---   (same) 

127. long  ---   int 

128.  

129. long long  ---   long 

130. float  ---   float 

131. double  ---   double 

132. const  ---   final 

133.  

134. TRUE  ---   true 

135. FALSE  ---   false 

136.  

137. ! unsigned long 

138. ! unsigned short 

139. ====================== 

140. ?\jdk1.3\bin\idlj  -fclient -fserver  ExCORBA.idl 

141. == generated : ================ 

142. === CORBAServant.java === 

143. package IDLgenerated

144. public interface CORBAServant extends CORBAServantOperations, org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity 

145. {} 

146. === CORBAServantOperations.java === 

147. package IDLgenerated

148. public interface CORBAServantOperations 

149.

150.   int increment (int plus); 

151.   String sayHi (); 

152.

153. ... 

154. === _CORBAServantImplBase.java === 

155. package IDLgenerated

156. public abstract class _CORBAServantImplBase 

157.  extends org.omg.CORBA.portable.ObjectImpl 

158.   implements IDLgenerated.CORBAServant, org.omg.CORBA.portable.InvokeHandler 

159. ... 

160. ======= 

161. ?\jdk1.3\bin\tnameserv.exe  -ORBInitialPort 900 

162. ====== 

163. Properties props = new Properties(); 

164. props.put("org.omg.CORBA.ORBInitialPort", "2345"); 

165. props.put("org.omg.CORBA.ORBInitialHost", "znogl.com"); 

166. ORB orb = ORB.init( args, props ); 

167.