/* * Autor: Christian Schiller * Datum: 15. August 1998 */ package schiller.jmapi; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import sunw.admin.avm.base.*; import sunw.admin.arm.rmi.*; import sunw.admin.arm.common.*; import sunw.admin.arm.manager.*; import schiller.jmapi.*; /** * Applet used for testing the NomadicSystemContentManager * */ public class NomadicSystemContentManagerApplet extends Applet { TopLevelContentManager mgr; ContentManager nomadicSystems; Image userImg; public void init() { setLayout(new BorderLayout()); Context.setProperty("headerBackground", new Color(70,70,170)); Context.setProperty("headerForeground", Color.white); try { /* * This is just a dummy image. We're not using it, * but an image is required for setting up the * SimpleContentManager */ userImg = Context.getImage("images/user.gif", this); } catch (Exception e) { e.printStackTrace(); } add("Center", mgr = new TopLevelContentManager()); mgr.add("Center", nomadicSystems = new NomadicSystemContentManager("Nomadic Systems", userImg)); nomadicSystems.addItemListener(mgr); mgr.select(nomadicSystems); } } /** * This command is used when "Delete .." is selected from the "Selected" menu */ class DeleteCmd implements Command, ActionListener { SecurityContext sc = new SecurityContext("nobody", null); RemoteEnumeration moEnum = null; Session session = null; QuestionDialog questionDialog; NomadicSystemMO mo = null; static final String className = "schiller.jmapi.NomadicSystemMO"; public DeleteCmd() { try { session = MOFactory.newSession(sc); } catch (Exception e) { e.printStackTrace(); } } public String getName() { return "Delete ..."; } public String getLabel() { return "Delete ..."; } /** * This method gets the first MO from the database which * matches the given ethernet address and domain name. * It then pops up a question dialog asking whether the object * should be deleted. When deletion is confirmed, the object is * removed from the database. */ public void execute(Object executor) { SimpleContentManager scm = (SimpleContentManager) executor; Table t = scm.getTable(); int i = t.getSelectedIndex(); String domainName = (String) t.getItem(i, 1); String ethernetAddress = (String) t.getItem(i, 0); System.err.println("Domain Name: " + domainName); System.err.println("Ethernet Address: " + ethernetAddress); if (session == null) { sc = new SecurityContext("nobody", null); try { Session session = MOFactory.newSession(sc); } catch (Exception e) { e.printStackTrace(); } } QueryExp queryExp = Query.and( Query.eq(Query.attr("domainName"), Query.value(domainName)), Query.eq(Query.attr("ethernetAddress"), Query.value(ethernetAddress)) ); try { moEnum = MOFactory.enumerateMOClass(className, queryExp); if (moEnum == null || ! moEnum.hasMoreElements()) { System.err.println("No matching MO found in database."); System.err.println("Domain Name: " + domainName); System.err.println("Ethernet Address: " + ethernetAddress); return; } mo = (NomadicSystemMO) moEnum.nextElement(); System.err.println("MO found in database"); // pop up question dialog and ask for confirmation System.err.println("Popping up question dialog ..."); Frame f = new Frame(); // dummy frame for constructor questionDialog = new QuestionDialog(f, "Really Delete this NomadicSystemMO?"); questionDialog.addActionListener(this); questionDialog.setSize(400, 200); questionDialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } // action listener for question dialog public void actionPerformed(ActionEvent ae) { String cmd = ae.getActionCommand(); if (cmd.equals("yes")) { System.err.println("Deleting MO ..."); try { session.beginUpdate(); mo.deleteObject(session); session.commitUpdate(); System.err.println("Deletion successful."); questionDialog.dispose(); } catch (Exception e) { System.err.println("Couldn't delete MO"); e.printStackTrace(); } } else { System.err.println("Nothing deleted."); questionDialog.dispose(); } } } /** * This command is used when "Properties ..." is selected from the * "Selected" menu */ class PropCmd implements Command { SecurityContext sc = new SecurityContext("nobody", null); RemoteEnumeration moEnum = null; Session session = null; static final String className = "schiller.jmapi.NomadicSystemMO"; public PropCmd() { try { session = MOFactory.newSession(sc); } catch (Exception e) { e.printStackTrace(); } } public String getName() { return "Properties ..."; } public String getLabel() { return "Properties ..."; } /** * This method displays the property book corresponding to the * first MO found in the database matching the selected ethernet * address and domain name. */ public void execute(Object executor) { SimpleContentManager scm = (SimpleContentManager) executor; Table t = scm.getTable(); int i = t.getSelectedIndex(); String domainName = (String) t.getItem(i, 1); String ethernetAddress = (String) t.getItem(i, 0); System.err.println("Domain Name: " + domainName); System.err.println("Ethernet Address: " + ethernetAddress); if (session == null) { sc = new SecurityContext("nobody", null); try { Session session = MOFactory.newSession(sc); } catch (Exception e) { e.printStackTrace(); } } QueryExp queryExp = Query.and( Query.eq(Query.attr("domainName"), Query.value(domainName)), Query.eq(Query.attr("ethernetAddress"), Query.value(ethernetAddress)) ); try { moEnum = MOFactory.enumerateMOClass(className, queryExp); if (moEnum == null || ! moEnum.hasMoreElements()) { System.err.println("No matching MO found in database."); System.err.println("Domain Name: " + domainName); System.err.println("Ethernet Address: " + ethernetAddress); return; } NomadicSystemMO mo = (NomadicSystemMO) moEnum.nextElement(); PropertyBook pb = new NomadicSystemPropertyBook(mo); pb.setCreateFlag(true); pb.display("Existing Nomadic System", (Container) executor); } catch (Exception e) { e.printStackTrace(); } } } /** * Used when "Create ..." is selected from the "Create" menu. */ class CreateCmd implements Command { public String getName() { return "Create ..."; } public String getLabel() { return "Create ..."; } public void execute(Object executor) { PropertyBook pb = new NomadicSystemPropertyBook(); pb.setCreateFlag(true); pb.display("New Nomadic System", (Container) executor); } } /** * The content manager user for displaying all nomadic systems * in the database. */ class NomadicSystemContentManager extends SimpleContentManager implements JMAPIObserver { static private Command[] viewCmds = new Command[3]; static private Command[] selCmds = new Command[2]; static private Command[] createCmds = new Command[1]; static final String className = "schiller.jmapi.NomadicSystemMO"; static final String moServerHost = "sunhegering2.nm.informatik.uni-muenchen.de"; static final int moServerPort = 7401; private Vector tableRows = new Vector(); private SecurityContext sc = null; private Session session = null; private ObserverProxy observerProxy = null; public NomadicSystemContentManager(String title, Image userImg) { super(title, userImg); Table table = getTable(); table.setNumColumns(2); table.setColumnWidthInChars(0, 14); table.setColumnWidthInChars(1, 20); getTableRows(); setTableData(new TableData(tableRows)); System.out.println("data: " + getTableData()); // the commands for the view menu viewCmds[0] = new FilterCommand(); viewCmds[1] = new SortCommand(); viewCmds[2] = new DisplayCommand(); // the commands for the selected menu selCmds[0] = new DeleteCmd(); selCmds[1] = new PropCmd(); // the command for the create menu createCmds[0] = new CreateCmd(); setCommands(Selectable.VIEW_COMMANDS, viewCmds); setCommands(Selectable.CREATE_COMMANDS, createCmds); String cols[] = new String[2]; cols[0] = "Ethernet Address"; cols[1] = "Domain Name"; int widths[] = new int[2]; widths[0] = 12; widths[1] = 18; /* * The table data are first piped through a filter pipe, * then through a sort pipe and finally through a view pipe. */ setFilterPipe(new TableFilterPipe(getTableData(), cols)); setQuerySpace(makeQuerySpace()); setSortPipe(new TableSortPipe(getFilterPipe(), cols)); setViewPipe(new TableViewPropertiesPipe(getSortPipe(), cols, widths)); getViewPipe().addObserver(this); getTableData().changed(); /* * The following code should set up an observer proxy which * notifies the content manager of any changes made to the * NomadicSystemMO class or any of its instances. * In case of a change, a notification should be sent to the * observer proxy which in turn should call the update() method * of the content manager (see below). * For some reason it doesn't seem to work as it should, though. */ try { observerProxy = new ObserverProxy(sc, this); } catch (Exception e) { observerProxy.addClassObserver(className); } } /* * Access the database and get the ethernet addresses and * domain names. Insert the data into a vector, which is then used to * construct the table data. */ private void getTableRows() { RemoteEnumeration moEnum = null; NomadicSystemMO mo = null; Vector moVector = null; try { if (sc == null) { System.err.println("Initializing MOFactory ..."); MOFactory.initialize(moServerHost, moServerPort); System.err.println("o.k."); sc = new SecurityContext("nobody", null); session = MOFactory.newSession(sc); System.err.println("New session created."); } // enumerate MOs moEnum = MOFactory.enumerateMOClass(className); if (moEnum == null) { System.err.println("No instances found in database."); return; } System.err.println("Adding MOs to table data ..."); while (moEnum.hasMoreElements()) { mo = (NomadicSystemMO) moEnum.nextElement(); moVector = new Vector(); moVector.addElement(mo.getEthernetAddress(session)); moVector.addElement(mo.getDomainName(session)); tableRows.addElement(moVector); System.err.println("MO added"); } System.err.println("Done adding MOs to table data."); return; } catch (Exception e) { e.printStackTrace(); } } /* * Construct the query space for the filter pipe. */ private QuerySpace makeQuerySpace() { QuerySpace qs = new QuerySpace(); QueryRelation relation[] = {QueryRelation.MATCHES}; Vector values; // Ethernet Address values = new Vector(); values.addElement("*"); qs.add("Nomadic System", "Ethernet Address", QueryType.STRING, relation, values); // Domain Name values = new Vector(); values.addElement("*"); qs.add("Nomadic System", "Domain Name", QueryType.STRING, relation, values); return qs; } /* * Get the commands which are available when an MO is selected and * the "Selected" menu is pulled down. */ public Command[] getCommands(String commandType) { if (commandType.equals(Selectable.SELECTED_COMMANDS)) { Object[] a = getSelectedObjects(); return (a.length > 0) ? selCmds : null; } return super.getCommands(commandType); } /* * This is needed for the JMAPIObserver interface. It should be * called by the observer proxy, but for some reason it's never called. */ public void update(Observation o) { System.err.println("update() method invoked"); getTableRows(); getTableData().changed(); } }