001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import org.cougaar.util.log.Logger;
032: import org.omg.PortableServer.POA;
033: import org.omg.PortableServer.POAHelper;
034: import org.omg.CORBA.ORB;
035: import org.omg.CosNaming.NameComponent;
036: import org.omg.CosNaming.NamingContext;
037: import org.omg.CosNaming.NamingContextHelper;
038: import org.omg.CosNaming.NamingContextExt;
039: import org.omg.CosNaming.NamingContextExtHelper;
040: import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
041: import org.omg.CosNaming.NamingContextPackage.CannotProceed;
042: import org.omg.CosNaming.NamingContextPackage.InvalidName;
043: import org.omg.CosNaming.NamingContextPackage.NotFound;
044: import org.omg.PortableServer.Servant;
045:
046: import java.io.BufferedReader;
047: import java.io.FileInputStream;
048: import java.io.FileReader;
049: import java.io.FileWriter;
050: import java.io.InputStream;
051: import java.io.PrintWriter;
052: import java.net.InetAddress;
053: import java.net.URL;
054: import java.util.Properties;
055:
056: public class CorbaUtils {
057:
058: public static ORB orb;
059: public static POA poa;
060: public static NamingContextExt ns_root;
061:
062: private static NamingContext findOrMakeContext(
063: NamingContext parent, NameComponent cname) throws NotFound,
064: CannotProceed, InvalidName {
065: Logger logger = Logging.getLogger(CorbaUtils.class);
066: NameComponent[] name = { cname };
067: try {
068: parent.bind_new_context(name);
069: if (logger.isInfoEnabled()) {
070: logger.info("Made context " + cname.id);
071: }
072: } catch (AlreadyBound ab) {
073: // ignore this error
074: if (logger.isInfoEnabled()) {
075: logger.info("Context " + cname.id + " already exists");
076: }
077: }
078: org.omg.CORBA.Object ref = parent.resolve(name);
079: return NamingContextHelper.narrow(ref);
080: }
081:
082: private static void bind(NamingContext root, NameComponent[] name,
083: org.omg.CORBA.Object ref) throws NotFound, CannotProceed,
084: InvalidName {
085: int context_length = name.length - 1;
086: NamingContext context = root;
087: for (int i = 0; i < context_length; i++) {
088: context = findOrMakeContext(context, name[i]);
089: }
090: NameComponent[] tname = { name[context_length] };
091: context.rebind(tname, ref);
092: }
093:
094: public static void nsBind(NameComponent[] name, Servant servant) {
095: try {
096: org.omg.CORBA.Object ref = poa
097: .servant_to_reference(servant);
098: nsBind(name, ref);
099: } catch (Exception ex) {
100: Logger logger = Logging.getLogger(CorbaUtils.class);
101: logger.error(null, ex);
102: }
103: }
104:
105: public static void nsBind(NameComponent[] name,
106: org.omg.CORBA.Object ref) {
107: ensure_ns();
108: if (ns_root == null) {
109: return;
110: }
111: Logger logger = Logging.getLogger(CorbaUtils.class);
112: try {
113: // Depends on -ORBInitRef having been set properly.
114: bind(ns_root, name, ref);
115: if (logger.isInfoEnabled()) {
116: StringBuffer buffer = new StringBuffer();
117: buffer.append(ref.toString());
118: buffer.append(" bound to ");
119: buffer.append(name[0].id);
120: for (int i = 1; i < name.length; i++) {
121: buffer.append("/");
122: buffer.append(name[i].id);
123: }
124: logger.info(buffer.toString());
125: }
126: } catch (Exception ex) {
127: logger.error(null, ex);
128: }
129: }
130:
131: public static org.omg.CORBA.Object nsResolve(NameComponent[] name) {
132: ensure_ns();
133: if (ns_root == null) {
134: return null;
135: }
136: Logger logger = Logging.getLogger(CorbaUtils.class);
137: org.omg.CORBA.Object ref = null;
138: try {
139: ref = ns_root.resolve(name);
140: if (logger.isInfoEnabled()) {
141: StringBuffer buffer = new StringBuffer();
142: buffer.append(name[0].id);
143: for (int i = 1; i < name.length; i++) {
144: buffer.append("/");
145: buffer.append(name[i].id);
146: }
147: buffer.append(" resolves to ");
148: buffer.append(ref);
149: logger.info(buffer.toString());
150: }
151: } catch (Exception ex) {
152: // no stack dump here, since this is often an expected
153: // condition
154: StringBuffer buffer = new StringBuffer();
155: buffer.append(name[0].id);
156: for (int i = 1; i < name.length; i++) {
157: buffer.append("/");
158: buffer.append(name[i].id);
159: }
160: buffer.append(" not found: ");
161: buffer.append(ex.toString());
162: logger.error(buffer.toString());
163: }
164: return ref;
165: }
166:
167: public static void writeIOR(String iorfile,
168: org.omg.CORBA.Object reference) {
169: try {
170: FileWriter file = new FileWriter(iorfile);
171: PrintWriter writer = new PrintWriter(file);
172: String ior = orb.object_to_string(reference);
173: writer.println(ior);
174: writer.close();
175: } catch (java.io.IOException e) {
176: Logger logger = Logging.getLogger(CorbaUtils.class);
177: logger.error(null, e);
178: }
179: }
180:
181: public static org.omg.CORBA.Object readIOR(String iorfile) {
182: org.omg.CORBA.Object reference = null;
183: try {
184: FileReader reader = new FileReader(iorfile);
185: BufferedReader rdr = new BufferedReader(reader);
186: String ior = rdr.readLine();
187: reference = orb.string_to_object(ior);
188: rdr.close();
189: } catch (java.io.IOException e) {
190: Logger logger = Logging.getLogger(CorbaUtils.class);
191: logger.error(null, e);
192: }
193: return reference;
194: }
195:
196: public static void registerContexts() {
197: AlarmDS.register();
198: ClassDS.register();
199: HostDS.register();
200: IntegraterDS.register();
201: IpFlowDS.register();
202: MethodDS.register();
203: MvaDS.register();
204: ObjectDS.register();
205: ObjectMethodDS.register();
206: ProcessDS.register();
207: SiteFlowDS.register();
208: }
209:
210: static private String hostname;
211: static private String hostaddr;
212:
213: static {
214: try {
215: InetAddress addr = InetAddress.getLocalHost();
216: hostname = addr.getCanonicalHostName();
217: hostaddr = addr.getHostAddress();
218: } catch (Exception ex) {
219: }
220: }
221:
222: public static String hostname() {
223: return hostname;
224: }
225:
226: public static String hostaddr() {
227: return hostaddr;
228: }
229:
230: private static synchronized void ensure_ns() {
231: if (ns_root != null) {
232: return;
233: }
234: try {
235: org.omg.CORBA.Object o = orb
236: .resolve_initial_references("NameService");
237: ns_root = NamingContextExtHelper.narrow(o);
238: } catch (Exception ex) {
239: Logger logger = Logging.getLogger(CorbaUtils.class);
240: logger.warn("Unable to resolve NameService ORBInitRef:"
241: + ex.getMessage());
242: }
243: }
244:
245: public static void makeORB(String[] args) {
246: orb = ORB.init(args, null);
247: try {
248: org.omg.CORBA.Object raw = orb
249: .resolve_initial_references("RootPOA");
250: poa = POAHelper.narrow(raw);
251: poa.the_POAManager().activate();
252: ensure_ns();
253: } catch (Exception ex) {
254: Logger logger = Logging.getLogger(CorbaUtils.class);
255: logger
256: .warn("Unable to resolve RootPOA: "
257: + ex.getMessage());
258: }
259: }
260:
261: public static void main(String[] args,
262: ResourceStatusServiceImpl impl) {
263: String iorfile = null;
264: String config = null;
265: boolean gui = false;
266:
267: int i = 0;
268: while (i < args.length) {
269: String arg = args[i++];
270: if (arg.equals("-ior")) {
271: iorfile = args[i++];
272: } else if (arg.equals("-gui")) {
273: gui = true;
274: } else if (arg.equals("-rss.config")) {
275: config = args[i++];
276: } else {
277: System.out.println("Do not understand switch " + arg);
278: }
279: }
280:
281: Logger logger = Logging.getLogger(CorbaUtils.class);
282: if (logger.isDebugEnabled()) {
283: logger.debug("Arg Switches " + " -gui=" + gui
284: + " -rss.config=" + config + " -ior=" + iorfile);
285: }
286:
287: // make RSS, using conf
288: Properties props = new Properties();
289: if (gui && impl != null) {
290: String gui_title = impl.guiTitle() + " on " + hostname();
291: props.setProperty(RSS.GUI_PROPERTY, gui_title);
292: }
293:
294: if (config != null) {
295: InputStream is = null;
296: try {
297: URL url = new URL(config);
298: is = url.openStream();
299: } catch (Exception ex) {
300: // try it as a filename
301: try {
302: is = new FileInputStream(config);
303: } catch (Exception e) {
304: logger.error("Error opening " + config + ": " + ex);
305: }
306: }
307:
308: if (is != null) {
309: try {
310: props.load(is);
311: is.close();
312: } catch (java.io.IOException ex) {
313: logger.error("Error loading RSS properties from "
314: + config + ": " + ex);
315: }
316: }
317: }
318:
319: if (logger.isDebugEnabled()) {
320: logger.debug("Properties=" + props);
321: }
322:
323: RSS.makeInstance(props);
324:
325: registerContexts();
326:
327: if (impl != null) {
328: makeORB(args);
329: Servant servant = impl.getServant();
330: org.omg.CORBA.Object reference = null;
331: try {
332: poa.activate_object(servant);
333: reference = poa.servant_to_reference(servant);
334: } catch (Exception ex) {
335: logger.error(ex.getMessage(), ex);
336: }
337:
338: if (iorfile != null) {
339: writeIOR(iorfile, reference);
340: }
341:
342: impl.postInit();
343:
344: System.out.println("RSS ready");
345: if (iorfile != null) {
346: System.out.println("IOR in " + iorfile);
347: }
348: orb.run();
349: }
350: }
351:
352: }
|