OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / CORBA / NamingService / Demo.java
1 /* Demo.java -- Shows how to use Classpath transient naming service.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.classpath.examples.CORBA.NamingService;
40
41 import gnu.CORBA.IOR;
42
43 import org.omg.CORBA.ORB;
44 import org.omg.CORBA.Object;
45 import org.omg.CosNaming.Binding;
46 import org.omg.CosNaming.BindingHolder;
47 import org.omg.CosNaming.BindingIterator;
48 import org.omg.CosNaming.BindingIteratorHolder;
49 import org.omg.CosNaming.BindingListHolder;
50 import org.omg.CosNaming.NameComponent;
51 import org.omg.CosNaming.NamingContext;
52 import org.omg.CosNaming.NamingContextExt;
53 import org.omg.CosNaming.NamingContextExtHelper;
54 import org.omg.CosNaming.NamingContextHelper;
55
56 /**
57  * A simple test of the naming service.
58  *
59  * The main class of the GNU Classpath transient naming service is
60  * {@link gnu.CORBA.NamingService}. This class must be started
61  * before starting this example.
62  *
63  * This example should interoperate as with GNU Classpath naming
64  * service, as with Sun Microsystems transient and persistent
65  * naming services, included in releases 1.3 and 1.4 (tnameserv and
66  * orbd). To work with this example, the naming service must
67  * be started on the local host, at the port 900.
68  *
69  * The persistent naming service is currently under development.
70  *
71  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
72  */
73 public class Demo
74 {
75   public static void main(String[] an_args)
76   {
77     // We create the following naming graph:
78     // <ROOT CONTEXT>
79     //  |
80     //  +--- <c.d context>
81     //  |     |
82     //  |     +--- obj
83     //  |
84     //  +--- xobj
85     //
86     // Where both obj and xobj are CORBA objects, representing the
87     // default naming service.
88     //
89     System.out.println("Starting the GNU Classpath " +
90                        "built-in transient naming service"
91                       );
92     
93     final String[] args = an_args;
94
95     new Thread()
96       {
97         public void run()
98         {
99           gnu.classpath.tools.tnameserv.Main.main(args);
100         }
101       }.start();
102
103     System.out.println("Waiting for three seconds for naming service to start:");
104     try
105       {
106         Thread.sleep(3000);
107       }
108     catch (InterruptedException ex)
109       {
110       }
111
112     try
113       {
114         ORB orb = ORB.init(args, null);
115
116         Object no = orb.resolve_initial_references("NameService");
117
118         System.out.println("Naming service IOR:" + orb.object_to_string(no));
119
120         System.out.println(IOR.parse(orb.object_to_string(no)));
121
122         NamingContextExt namer = NamingContextExtHelper.narrow(no);
123
124         System.out.println("Naming service: " + namer.getClass().getName());
125
126         NamingContext second = namer.new_context();
127
128         namer.rebind_context(namer.to_name("c.d"), second);
129         namer.rebind(namer.to_name("xobj"), no);
130
131         second.rebind(namer.to_name("obj"), no);
132
133         NamingContext nsec =
134           NamingContextHelper.narrow(namer.resolve_str("c.d"));
135
136         System.out.println(namer.resolve(namer.to_name("c.d/obj")));
137
138         // In all cases, this must be the same object (the naming
139         // service itself).
140         System.out.println(nsec.resolve(new NameComponent[]
141                                         {
142                                           new NameComponent("obj", "")
143                                         }
144                                        )
145                           );
146         System.out.println(namer.resolve_str("xobj"));
147
148         // In all cases, this must be the same object (the naming
149         // service itself).
150         System.out.println(namer.resolve(new NameComponent[]
151                                          {
152                                            new NameComponent("c", "d"),
153                                            new NameComponent("obj", "")
154                                          }
155                                         )
156                           );
157
158         System.out.println(namer.resolve_str("c.d/obj"));
159
160         System.out.println("Test binding list iterator:");
161
162         BindingListHolder lh = new BindingListHolder();
163         BindingIteratorHolder lih = new BindingIteratorHolder();
164
165         namer.list(0, lh, lih);
166
167         BindingIterator iter = lih.value;
168         BindingHolder binding = new BindingHolder();
169
170         while (iter.next_one(binding))
171           {
172             Binding b = binding.value;
173             System.out.println("NAME: " + namer.to_string(b.binding_name) +
174                                " TYPE " + b.binding_type.value()
175                               );
176           }
177
178         System.out.println("Testing binding list:");
179
180         iter.destroy();
181
182         namer.list(Integer.MAX_VALUE, lh, lih);
183
184         for (int i = 0; i < lh.value.length; i++)
185           {
186             Binding b = lh.value [ i ];
187             System.out.println("NAME: " + namer.to_string(b.binding_name) +
188                                " TYPE " + b.binding_type.value()
189                               );
190           }
191       }
192     catch (Exception ex)
193       {
194         ex.printStackTrace();
195         System.exit(1);
196       }
197
198     System.exit(0);
199   }
200 }