001: package fr.aliacom.dbmjui;
002:
003: import java.beans.PropertyChangeListener;
004: import java.beans.PropertyChangeSupport;
005: import java.io.Serializable;
006: import java.util.Properties;
007:
008: import org.apache.log4j.Logger;
009:
010: import com.sap.dbtech.powertoys.DBM;
011:
012: import fr.aliacom.dbmjui.beans.InstanceInformations;
013:
014: /**
015: * Most of the dbm code should be moved elsewhere
016: *
017: * @author tom
018: *
019: * (c) 2001, 2003 Thomas Cataldo
020: */
021: public final class DbInstance implements Serializable {
022:
023: private String version;
024: private String fullName;
025: private String name;
026: private DatabaseServer dbs;
027: private Properties connectionParams;
028:
029: private transient InstanceInformations infos;
030:
031: private transient BackupHistory history;
032: private transient PropertyChangeSupport changes;
033: private transient static final Logger logger = Logger
034: .getLogger(DbInstance.class);
035:
036: public DbInstance(DatabaseServer dbs) {
037: this .dbs = dbs;
038: infos = new InstanceInformations();
039: history = new BackupHistory();
040: }
041:
042: private final PropertyChangeSupport getChanges() {
043: if (changes == null) {
044: changes = new PropertyChangeSupport(this );
045: }
046: return changes;
047: }
048:
049: /**
050: * Add a listener on a given property
051: *
052: * @param prop the property changes you want to be notified of.
053: * @param l the listener notified of the property change
054: */
055: public void addPropertyChangeListener(String prop,
056: PropertyChangeListener l) {
057: getChanges().addPropertyChangeListener(prop, l);
058: }
059:
060: public String getVersion() {
061: return version;
062: }
063:
064: /**
065: * Removes a listener on a given property
066: *
067: */
068: public void removePropertyChangeListener(String prop,
069: PropertyChangeListener l) {
070: getChanges().removePropertyChangeListener(prop, l);
071: }
072:
073: public DatabaseServer getServer() {
074: return dbs;
075: }
076:
077: /**
078: * @see DbInstance#setDbmCredential(DbmCredential)
079: */
080: public void setDbmCredential(DbmCredential creds)
081: throws DBMLoginException {
082: try {
083: Properties props = new Properties();
084: props.put("host", dbs.getHost());
085: props.put("dbname", infos.getName());
086: props.put("user", creds.getLogin() + ","
087: + creds.getPassword());
088:
089: DBM aDbm = new DBM(props);
090:
091: logger.debug("Logging in...");
092: aDbm.cmd("user_logon " + props.get("user"));
093: logger.debug("logger in");
094:
095: connectionParams = props;
096: aDbm.release();
097: infos = HelperFactory.getInstance().getInstanceHelper(this )
098: .updateInformations(this );
099: } catch (Exception e) {
100: logger.fatal(
101: "Exception occured while checking credentials", e);
102: throw new DBMLoginException(e.toString());
103: }
104: }
105:
106: /**
107: * Get a DBM connection. The user given at registration time
108: * is already logged on.
109: *
110: * Error generated by commands executed with the returned connection
111: * will be reported in the UI.
112: *
113: * @return a dbm connection
114: */
115: public DBM getDbmConnection() {
116: DBM aDbm = null;
117: try {
118: aDbm = new JuiDbmConnection(connectionParams);
119: aDbm.cmd("user_logon " + connectionParams.get("user"));
120: } catch (Exception e) {
121: logger.fatal("user login failed", e);
122: throw new RuntimeException("user_logon failed !");
123: }
124:
125: return aDbm;
126: }
127:
128: /**
129: * Get a DBM connection. The user given at registration time
130: * is already logged on.
131: *
132: * Error generated by commands executed with the returned connection
133: * will not be reported in the UI.
134: *
135: * @return a dbm connection
136: */
137: public DBM getPlainDbmConnection() {
138: DBM aDbm = null;
139: try {
140: aDbm = new DBM(connectionParams);
141: aDbm.cmd("user_logon " + connectionParams.get("user"));
142: } catch (Exception e) {
143: logger.fatal("user login failed", e);
144: throw new RuntimeException("user_logon failed !");
145: }
146:
147: return aDbm;
148: }
149:
150: /**
151: * @param listener
152: */
153: public void addPropertyChangeListener(
154: PropertyChangeListener listener) {
155: getChanges().addPropertyChangeListener(listener);
156: }
157:
158: /**
159: * @param listener
160: */
161: public void removePropertyChangeListener(
162: PropertyChangeListener listener) {
163: getChanges().removePropertyChangeListener(listener);
164: }
165:
166: /**
167: * @return the cached informations about the instance
168: */
169: public InstanceInformations getInformations() {
170: return infos;
171: }
172:
173: /**
174: * @param infos
175: */
176: public void setInformations(InstanceInformations infos) {
177: this .infos = infos;
178: }
179:
180: public void reloadState() {
181: infos = HelperFactory.getInstance().getInstanceHelper(this )
182: .updateInformations(this );
183: }
184:
185: /**
186: * @return the name used to display the instance
187: */
188: public String getFullName() {
189: return fullName;
190: }
191:
192: /**
193: * @param string
194: */
195: public void setFullName(String string) {
196: fullName = string;
197: infos.setFullName(fullName);
198: }
199:
200: /**
201: * @return the instance name
202: */
203: public String getName() {
204: return name;
205: }
206:
207: /**
208: * @param string
209: */
210: public void setName(String string) {
211: name = string;
212: infos.setName(name);
213: }
214:
215: }
|