001: package fr.aliacom.dbmjui;
002:
003: import java.io.Serializable;
004: import java.net.InetAddress;
005: import java.net.UnknownHostException;
006: import java.util.ArrayList;
007: import java.util.Properties;
008:
009: import org.apache.log4j.Logger;
010:
011: import com.sap.dbtech.powertoys.DBM;
012: import com.sap.dbtech.powertoys.DBMException;
013: import com.sap.dbtech.rte.comm.RTEException;
014:
015: import fr.aliacom.dbmjui.beans.InstanceInformations;
016: import fr.aliacom.form.common.ToolkitManager;
017:
018: public class DatabaseServer implements Serializable {
019:
020: private String host;
021: private transient static final Logger log = Logger
022: .getLogger(DatabaseServer.class);
023:
024: public DatabaseServer(String host) throws UnknownHostException {
025: this .host = getIpAddress(host);
026: }
027:
028: private String getIpAddress(String host)
029: throws UnknownHostException {
030: return InetAddress.getByName(host).getHostAddress();
031: }
032:
033: public String getHost() {
034: return host;
035: }
036:
037: /**
038: * @return a list of InstanceInformations
039: */
040: public ArrayList getInstances() {
041: Properties props = new Properties();
042: props.put("host", host);
043:
044: String ret = null;
045: try {
046: DBM aDbm = new DBM(props);
047: ret = aDbm.cmd("db_enum");
048: ArrayList instances = getDbList(ret);
049: aDbm.release();
050: return instances;
051: } catch (RTEException rtee) {
052: ToolkitManager.getToolkit().handleError(
053: ToolkitManager.getToolkit().getMainWindow(), rtee);
054: return new ArrayList();
055: } catch (DBMException dbme) {
056: ToolkitManager.getToolkit().handleError(
057: ToolkitManager.getToolkit().getMainWindow(), dbme);
058: return new ArrayList();
059: }
060: }
061:
062: public DBM getRawConnection() throws RTEException {
063: Properties props = new Properties();
064: props.put("host", host);
065:
066: return new DBM(props);
067: }
068:
069: private ArrayList getDbList(String dbs) {
070: log.debug(dbs);
071:
072: String[] dbRows = dbs.split("\n");
073: ArrayList ret = new ArrayList(dbRows.length);
074: log.debug("Found " + dbRows.length + " instances");
075: String previousIName = "";
076: for (int i = 0; i < dbRows.length; i++) {
077: String[] params = dbRows[i].split("\t");
078: if (!params[0].trim().equals(previousIName)) {
079: DbInstance dbi = new DbInstance(this );
080: InstanceInformations ii = dbi.getInformations();
081: ii.setDbi(dbi);
082: dbi.setName(params[0].trim());
083: dbi.setFullName(ii.getName());
084: ii.setPath(params[1].trim());
085: ii.setVersion(params[2].trim());
086: ret.add(ii);
087: previousIName = ii.getName();
088: }
089: }
090: return ret;
091: }
092:
093: /**
094: * Method getInstallations.
095: * @return Object[]
096: */
097: public DBInstallation[] getInstallations() {
098: String row = null;
099: try {
100: DBM dbm = getRawConnection();
101: row = dbm.cmd("inst_enum");
102: dbm.release();
103: } catch (Exception e) {
104: return new DBInstallation[0];
105: }
106: String[] rows = row.split("\n");
107: DBInstallation[] ret = new DBInstallation[rows.length];
108: for (int i = 0, n = rows.length; i < n; i++) {
109: String[] params = rows[i].split(" ");
110: DBInstallation inst = new DBInstallation();
111: int field = 1;
112: inst.setVersion(params[0]);
113: while (params[field].trim().length() == 0) {
114: field++;
115: }
116: inst.setInstallationRoot(params[field]);
117: inst.setDefaultInstallation(i == 0 ? Boolean.TRUE
118: : Boolean.FALSE);
119: ret[i] = inst;
120: }
121: return ret;
122: }
123:
124: }
|