How to use the Server Administration API to enumerate objects on the server.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on using the Server Administration API in the Administrator's Guide |
The following code gets the URL of the current database, from which it gets and sets server parameters in appropriate fields ( which may be edited by the user.) Then the code activates code that displays the top level server objects (databases) in a tree control.
protected void formLoaded() { // Get the database URL. java.net.URL dbURL = agGeneral.getDatabaseURL(); // Get the host, port, and protocol from the URL and // set the fields on the form. fldServer.setText(dbURL.getHost()); fldPort.setValue(dbURL.getPort()); fldProtocol.setText(dbURL.getProtocol()); // Get the server objects. getServerObjects(); }
getServerObjects()
a custom (user-defined) method that enumerates all databases on the server in a tree control (see the next description.)
The following code uses the server parameters loaded on to the main form to get the current server and enumerates the database objects on the server. It then uses a hashtable with some user-defined variables to get the database properties needed to display the result in a tree control.
For information about creating a tree control, see the Application Technique, Using Tree Controls.
private void getServerObjects() { String sDBName, sHost, sProtocol; int iPort; sHost = fldServer.getText(); sProtocol = fldProtocol.getText(); iPort = fldPort.getValue(); if (sProtocol == null || sProtocol.equals("")) sProtocol = "http"; if (iPort == 0) iPort = -1; if (sHost.equals("")) { agDialog.showMessage("Required value", "You must enter a host name."); fldServer.requestFocus(); return; } // Remove any items in the tree control. tcObjects.removeAll(); // Get the server's children. Enumeration serverChildren = null; try { // Get the server object. m_server = AgAdmin.getServer(sProtocol, sHost, iPort); // Use GET_CHILDREN_SORTED flag to get a sorted list // of the server's children. serverChildren = m_server.getChildren (AgiAdmContainer.GET_CHILDREN_SORTED); } catch (Exception e) { agDialog.displayError(e); return; } // If we have no children, we don't need to continue, // so return false. if (serverChildren == null) return; // Loop through the children looking for databases. while (serverChildren.hasMoreElements()) { // Get the next element. AgiAdmElement element = (AgiAdmElement) serverChildren.nextElement(); // Is it a database? (It should be) if (element instanceof AgiAdmDatabase) { // Create a database object (db) from the element. AgiAdmDatabase db = (AgiAdmDatabase) element; // Create a Hashtable (props) and enumerate the // database properties into it. Hashtable props = null; // Yes, so add the database name to the tree as a // top-level node. sDBName = element.getName(); Image dbIcon = getImage("SmallWebbaseImages.gif 60 0 18 18"); Hashtable dbUserData = new Hashtable(); dbUserData.put(DATABASE, sDBName); dbUserData.put(TYPE, AgiAdmDatabase.DATABASE); dbUserData.put(RETRIEVE, new Boolean(true)); dbUserData.put(ELEMENT, element); // Add the node. AgoTreeControlNode childNode = tcObjects.add(null, AgcTreeControl.NEXT, sDBName, dbUserData, dbIcon); tcObjects.add(childNode,AgcTreeControl.CHILD,"empty"); } else tcObjects.add(null, AgcTreeControl.NEXT, element.getName()); } }
getChildren()
at the server level to get the child objects of the server (that is, databases.) The method uses the constant argument GET_CHILDREN_SORTED, which sorts the result in ascending order.
getChildren()
the code uses the JAVA method nextElement()
, casting the result to an object of type AgiAdmElement. It then verifies that the element is of type AgiAdmDatabase.
The server ServerObjects application uses several other user-defined methods to define the objects at each level. The following user-defined method shows how directory objects are enumerated at the database level. Given a database name, the method enumerates the SilverStream directories and adds them as nodes to an AgcTreeControl.
public void populateDatabase(String psDBName) { AgoTreeControlNode node = tcObjects.getSelectedNode(); removeChildren(node); // Get the database's children and add them to the tree // as children of the database. Enumeration dbChildren = null; try { AgiAdmDatabase db = (AgiAdmDatabase) m_server.getElement (psDBName, AgiAdmDatabase.DATABASE, null); // The flag GET_CHILDREN_SORTED is used to get a sorted list // of the database's children. dbChildren = db.getChildren( AgiAdmContainer.GET_CHILDREN_SORTED); } catch (Exception e) { agDialog.displayError(e); return; } // If we have no children, we don't need to continue, // so return. if (dbChildren == null) return; // We now have an enumeration of the database's children, // so loop through them and add them to the tree control. while (dbChildren.hasMoreElements()) { // Get the next directory. AgiAdmElement element = (AgiAdmElement) dbChildren.nextElement(); // Create the user data for the node. Hashtable hshUserData = new Hashtable(); hshUserData.put(DATABASE, psDBName); hshUserData.put(RETRIEVE, new Boolean(true)); hshUserData.put(ELEMENT, element); // Get the directory's name. String sName = element.getName(); // Tables directory. if (sName.equalsIgnoreCase(AgiAdmDirectory.TABLES)) { Image imgDisplay = getImage("SmallServerImages.gif 296 0 25 20"); hshUserData.put(TYPE, AgiAdmDirectory.TABLES); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, sName, hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } // Forms directory. else if (sName.equalsIgnoreCase(AgiAdmDirectory.FORMS)) { Image imgDisplay = getImage("SmallServerImages.gif 0 0 20 20"); hshUserData.put(TYPE, AgiAdmDirectory.FORMS); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, sName, hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } // Views directory. else if (sName.equalsIgnoreCase(AgiAdmDirectory.VIEWS)) { Image imgDisplay = getImage("SmallServerImages.gif 17 0 22 20"); hshUserData.put(TYPE, AgiAdmDirectory.VIEWS); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, sName, hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } // Media directory. else if (sName.equalsIgnoreCase(AgiAdmDirectory.MEDIA)) { Image imgDisplay = getImage("SmallServerImages.gif 345 0 30 20"); hshUserData.put(TYPE, AgiAdmDirectory.MEDIA); hshUserData.put(RETRIEVE, new Boolean(true)); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, sName, hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } // Objects directory. else if (sName.equalsIgnoreCase(AgiAdmDirectory.APP_OBJECTS)) { Image imgDisplay = getImage("SmallServerImages.gif 373 0 30 20"); hshUserData.put(TYPE, AgiAdmDirectory.APP_OBJECTS); hshUserData.put(RETRIEVE, new Boolean(true)); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, "Objects", hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } // Pages directory. else if (sName.equalsIgnoreCase(AgiAdmDirectory.PAGES)) { Image imgDisplay = getImage("SmallServerImages.gif 321 0 24 20"); hshUserData.put(TYPE, AgiAdmDirectory.PAGES); AgoTreeControlNode parent = tcObjects.add(node, AgcTreeControl.CHILD, sName, hshUserData, imgDisplay); tcObjects.add(parent, AgcTreeControl.CHILD, "DUMMY"); } } }
getElement()
to get the cureently selected database and casts the result to an object of type AgiAdmDatabase. It then uses getChildren()
to get all of the child objects of the database, sorted in ascending order.
getChildren()
to get the types of directories in the database. First it uses the Java method nextElement()
and casts the result to an object of type AgiAdmElement. Then the code tests for the directory type of each element, using the constant values available in the API. The resulting enumeration is added to the tree control.