/** * Tutorial demo code. * * @version $Revision: 1.2 $ $Author: arossi * $ $Date: 2001/12/23 16:23:10 $ (GMT) * @author Albert L. Rossi [mailto:arossi@indiana.edu] */ package demo; import java.io.*; import xcat.framework.ccacore.Component; import xcat.framework.ccacore.Services; import xcat.framework.ccacore.PortInfoImpl; import xcat.framework.ccacore.ProvidesPortMapping; import soaprmi.events.*; import soaprmi.events.util.Util; /** * Corresponds to the functionality of printenv.c. * Can be initialized with the names of System properties, * then called to write them out with their current values * to a file. * * Implements the CCA Component interface. * * Uses event channel to send messages concerning state. */ public class PrintEnvComponent implements Component { // for use by the publish method private static final String ECNM = "myPEGC"; private static final String ECCTX = "ldap://palomar.extreme.indiana.edu/" + "ou=events,o=soaprmi,dc=cs,dc=indiana,dc=edu"; private String [] args; private PrintWriter pw; /** * Empty constructor, as required by CCA specification. */ public PrintEnvComponent () {} /** * Method defined by the Component interface. * Registers and adds ports (in this case, a parameterized * control provides port and a PrintEnv provides port). * @param core reference to XCAT framework Services. */ public void setServices (Services core) { try { // register Provides port ProvidesPortMapping.addMapping ("printEnvProvidesPort", "http://www.extreme.indiana.edu/demo/wsdl#provides", new Class [] {ProvidesPrintEnv.class}); core.addProvidesPort (new PrintEnvProvidesImpl(this), new PortInfoImpl ("printEnvProvidesPort", "http://www.extreme.indiana.edu/demo/wsdl#provides")); // register ProvidesParamControl port ProvidesPortMapping.addMapping ("controlProvidesPort", "http://www.extreme.indiana.edu/demo/wsdl#control", new Class [] {ProvidesParamControl.class}); core.addProvidesPort (new PrintEnvControlImpl(this), new PortInfoImpl ("controlProvidesPort", "http://www.extreme.indiana.edu/demo/wsdl#control")); } catch (Exception e) { e.printStackTrace(); } } // setServices /** * Sets the internal args field to the array reference. * Constructs a print writer to write to a file called * "env-vars" placed in the current working directory. * @param _args array of Strings, the names of the * System properties to be written to file. */ public void init (String [] _args) throws Exception { args = _args; pw = null; try { FileOutputStream fos = new FileOutputStream("env-vars"); pw = new PrintWriter(fos,true); } catch (IOException ioex) { throw new Exception("PrintEnvComponent.init: " + ioex.toString()); } } // init /** * Writes the current set of System properties names and * their corresponding values to the file "env-vars". * * If the PrintWriter was not constructed, returns. */ public void write() { String msg = null; if (pw == null) { msg = "PrintEnv.write: PrintWriter was not initialized"; publish(msg); return; } for (int i = 0; i < args.length; i++) { pw.print(args[i] + ":\t"); pw.println(System.getProperty(args[i], "Undefined")); if (pw.checkError()) { msg = "PrintEnv.write: error printing value of args["+i+"]"; publish(msg); return; } } } // write // Private Methods /////////////////////////////////////////////////////// private void publish (String msg) { // retrieve the channel; perhaps less efficient than // having the component spin off a thread which // periodically checks the channel reference, // but we have left this out for the sake of simplicity EventChannel channel = null; Event event = null; try { EventMappings.init(); } catch (Exception e) { System.err.println ("PrintEnv.publish, could not initialize EventMappings: "+e); return; } try { channel = Util.getPEGCRef(ECNM, ECCTX); } catch (Exception e) { System.err.println ("PrintEnv.publish, could not retrieve channel ref: " + e); System.err.println ("PrintEnv.publish, msg = " + msg); return; } // construct and send event event = new Event(msg); event.setEventType("demo"); event.setSource("PrintEnvComponent"); try { channel.handleEvent(event); } catch (Exception e) { System.err.println ("PrintEnv.publish: " + e); System.err.println ("PrintEnv.publish, msg = " + msg); } } // publish } // class PrintEnvComponent