KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > corba > NSbinder


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.util.corba;
29
30
31 import java.io.*;
32 import java.net.URL JavaDoc;
33 import java.util.Properties JavaDoc;
34 import org.omg.CORBA.ORB JavaDoc;
35 import mparser.*;
36
37
38 /**
39  * MobiliTools $Name: $, $Id: NSbinder.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
40  * <P>
41  * Tool for making automatic bindings in CORBA naming service.
42  * The input can be a property file, an XML file, or Java properties.
43  * Binding names must be given according to "id!kind/.../id!kind" format.
44  * A binding object may be given as a stringified IOR, as a file containing
45  * a stringified IOR, or as a URL containing a stringified IOR.
46  */

47 public class NSbinder
48 {
49     NameService ns;
50     ORB JavaDoc orb;
51
52
53     /**
54         Parses a property file or an xml file describing a set
55         of CORBA name service bindings to do.
56         @param args shall contain ORB-specific switches first, and
57         then either <code>-xml xml_file</code> or <code>-prop property_file</code>
58         switch.
59         @see #bindFromXMLFile(String)
60         @see #bindFromPropFile(String)
61     */

62     static public void main(String JavaDoc[] args)
63     {
64         try
65         {
66             NSbinder binder = new NSbinder(ORB.init(args, System.getProperties()));
67             if (args[args.length-2].equalsIgnoreCase("-prop"))
68             {
69                 binder.bindFromPropFile(args[args.length-1]);
70             }
71             else if(args[args.length-2].equalsIgnoreCase("-xml"))
72             {
73                 binder.bindFromXMLFile(args[args.length-1]);
74             }
75             else
76             {
77                 System.out.println("usage: [ORB-specific args...] [-prop file] [-xml file]");
78             }
79         }
80         catch (Throwable JavaDoc ex)
81         {
82             System.err.println(ex.toString());
83         }
84     }
85
86
87     /**
88         Creates a new name service binder utility, using default/singleton ORB object.
89         @throws NameServiceException if no valid initial reference to a naming service
90         could be obtained.
91     */

92     public NSbinder()
93         throws NameServiceException
94     {
95         this(ORB.init());
96     }
97
98
99     /**
100         Creates a new name service binder utility, using the provided ORB object.
101         @param orb the ORB object to use.
102         @throws NameServiceException if no valid initial reference to a naming service
103         could be obtained.
104     */

105     public NSbinder(ORB JavaDoc orb)
106         throws NameServiceException
107     {
108         this.orb = orb;
109         ns = new NameService(orb);
110     }
111
112
113     /**
114         Parses the specified XML file in order to make the specified bindings.
115         This file should contain only bind tags, with the following attributes:
116         <UL>
117             <LI><code>&lt;bind name="some_new_context" /&gt;</code> <I>rebinds name to a new naming context</I>
118             <LI><code>&lt;bind name="some_object" ior="IOR:...." /&gt;</code> <I>rebinds name to the specified IOR</I>
119             <LI><code>&lt;bind name="some_object" file="IOR_file" /&gt;</code> <I>rebinds name to IOR contained in the specified file</I>
120             <LI><code>&lt;bind name="some_object" url="IOR_URL" /&gt;</code> <I>rebinds name to IOR contained at the specified URL</I>
121         </UL>
122         @param file the name of the XML file to be parsed.
123         @throws Exception a variety of exceptions if a parsing error occurs,
124         because of an invalid file format (check the XML content)
125     */

126     public void bindFromXMLFile(String JavaDoc file)
127         throws Exception JavaDoc
128     {
129         WMparser parser = new WMparser(file);
130         ItemParsed item;
131         Properties JavaDoc props = new Properties JavaDoc();
132         int bindCount = 0;
133
134         while (parser.hasMoreItems())
135         {
136             item = parser.getNextItem();
137             System.out.println(item.getName());
138             if ((item.getType() == ItemParsed.ITEM_TYPE_ELEMT) && item.getName().equalsIgnoreCase("bind"))
139             {
140                 ++bindCount;
141             }
142             else if (item.getType() == ItemParsed.ITEM_TYPE_ATTRB)
143             {
144                 props.setProperty(
145                     item.getName().substring("bind.".length()) + "." + String.valueOf(bindCount),
146                     item.getValue());
147             }
148             else if (item.getType() == ItemParsed.ITEM_TYPE_TEXTE)
149             {
150                 System.out.println(item.getValue());
151             }
152             else
153             {
154                 System.out.println("XML deployment warning: ignoring \"" + item.getName() + "\"");
155             }
156         }
157         props.list(System.out);
158         bind(props);
159     }
160
161
162     /**
163         Parses the specified property file in order to make the specified bindings.
164         @param file the name of the property file to be parsed.
165         @throws IOException if the specified file could not be read.
166         @see #bind(Properties)
167     */

168     public void bindFromPropFile(String JavaDoc file)
169         throws IOException
170     {
171         Properties JavaDoc props = new Properties JavaDoc();
172         props.load(new FileInputStream(new File(file)));
173         bind(props);
174     }
175
176
177     /**
178         Performs CORBA name service bindings specified by the provided properties.
179         Properties are:
180         <UL>
181             <LI>name.<I>n</I> for <I>n</I>th binding name
182             <LI>url.<I>n</I> for URL of <I>n</I>th binding IOR
183             <LI>ior.<I>n</I> for <I>n</I>th stringified binding IOR
184             <LI>file.<I>n</I> for file containing <I>n</I>th binding IOR
185         </UL>
186         Where <I>n</I> increments from 1 and stops as soon as property name.<I>n</I> is undefined.
187         If no URL, file or IOR is specified for a name, then the name is bound to a new naming context.
188     */

189     public void bind(Properties JavaDoc props)
190     {
191         String JavaDoc name, url, ior, file;
192         String JavaDoc suffix;
193
194         for (int i=1 ; (name = props.getProperty("name." + (suffix = String.valueOf(i)))) != null ; ++i)
195         {
196             url = props.getProperty("url." + suffix);
197             ior = props.getProperty("ior." + suffix);
198             file = props.getProperty("file." + suffix);
199             try
200             {
201                 if (url != null)
202                 {
203                     System.out.println("binding " + name + " to IOR from URL " + url);
204                     ns.rebind(
205                         name,
206                         orb.string_to_object((new BufferedReader(new InputStreamReader((new URL JavaDoc(url)).openStream()))).readLine()));
207                 }
208                 else if (file != null)
209                 {
210                     System.out.println("binding " + name + " to IOR from file " + file);
211                     ns.rebind(
212                         name,
213                         orb.string_to_object((new BufferedReader(new InputStreamReader((new URL JavaDoc("file:" + file)).openStream()))).readLine()));
214                 }
215                 else if (ior != null)
216                 {
217                     System.out.println("binding " + name + " to IOR " + ior);
218                     ns.rebind(name, orb.string_to_object(ior));
219                 }
220                 else
221                 {
222                     System.out.println("binding " + name + " to new context");
223                     ns.makePath(name);
224                 }
225             }
226             catch (Exception JavaDoc e)
227             {
228                 System.err.println(e.toString());
229             }
230         }
231     }
232 }
233
Popular Tags