001: package fr.aliacom.dbmjui;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.ObjectInputStream;
006: import java.io.ObjectOutputStream;
007: import java.util.Enumeration;
008: import java.util.Hashtable;
009: import java.util.LinkedList;
010: import java.util.ListIterator;
011: import java.util.prefs.Preferences;
012:
013: import org.apache.log4j.Logger;
014:
015: import fr.aliacom.dbmjui.beans.InstanceInformations;
016: import fr.aliacom.dbmjui.driver.IInstanceHelper;
017:
018: /**
019: * Tracks instances managed by the database manager.
020: * This class loads and saves the registered instances at application
021: * shutdown/startup
022: *
023: */
024: public class DbRegistry {
025:
026: /** holds "host/dbname" <--> DbInstance mapping */
027: private Hashtable hash;
028:
029: /** <code>RegistryListener</code> list **/
030: private LinkedList registryListeners;
031:
032: /** <code>IInstanceSelectionListener</code> list **/
033: private LinkedList selectionListeners;
034:
035: private static final Logger log = Logger
036: .getLogger(DbRegistry.class);
037:
038: /** singleton pattern support */
039: private static DbRegistry singleton;
040:
041: static {
042: singleton = new DbRegistry();
043: }
044:
045: /**
046: * Provides access to dbmjui registry.
047: *
048: * @return the registry instance
049: */
050: public static DbRegistry getInstance() {
051: return singleton;
052: }
053:
054: private DbRegistry() {
055: hash = new Hashtable();
056: registryListeners = new LinkedList();
057: selectionListeners = new LinkedList();
058: }
059:
060: public void fireRegisterEvent(DbInstance dbi) {
061: ListIterator li = registryListeners.listIterator(0);
062: while (li.hasNext()) {
063: ((IRegistryListener) li.next()).instanceRegistered(dbi);
064: }
065: }
066:
067: public void fireUnregisterEvent(DbInstance dbi) {
068: ListIterator li = registryListeners.listIterator(0);
069: while (li.hasNext()) {
070: ((IRegistryListener) li.next()).instanceUnregistered(dbi);
071: }
072: }
073:
074: public void addRegistryListener(IRegistryListener rl) {
075: registryListeners.add(rl);
076: }
077:
078: public void removeRegistryListener(IRegistryListener rl) {
079: registryListeners.remove(rl);
080: }
081:
082: /**
083: * Returns <code>true</code> if the given database instance
084: * is already registered.
085: *
086: * Used by the registry wizard to prevent double registration
087: * of a given instance.
088: *
089: * @param dbi the instance to check.
090: * @return true if the instance is already registered.
091: */
092: public boolean isRegistered(DbInstance dbi) {
093: return hash.containsKey(getHashKey(dbi));
094: }
095:
096: public void register(DbInstance dbi) {
097: hash.put(getHashKey(dbi), dbi);
098: fireRegisterEvent(dbi);
099: }
100:
101: public void unregister(DbInstance dbi) {
102: DbInstance i = (DbInstance) hash.remove(getHashKey(dbi));
103: fireUnregisterEvent(i);
104: }
105:
106: private final String getHashKey(DbInstance dbi) {
107: log.debug("dbi=" + dbi);
108: log.debug("dbi.server=" + dbi.getServer());
109: log.debug("dbi.server.host=" + dbi.getServer().getHost());
110: return dbi.getServer().getHost() + "/" + dbi.getName();
111: }
112:
113: public void loadPreferences() {
114: try {
115: log.info("Loading registered instances...");
116: Preferences prefs = Preferences
117: .userNodeForPackage(getClass());
118: byte[] instancesArray = prefs.getByteArray("instances",
119: null);
120: if (instancesArray == null) {
121: return;
122: }
123: ObjectInputStream in = new ObjectInputStream(
124: new ByteArrayInputStream(instancesArray));
125: Object array = in.readObject();
126: DbInstance[] instances = (DbInstance[]) array;
127: for (int i = 0; i < instances.length; i++) {
128: IInstanceHelper helper = HelperFactory.getInstance()
129: .getInstanceHelper(instances[i]);
130: InstanceInformations ii = helper
131: .updateInformations(instances[i]);
132: instances[i].setInformations(ii);
133: hash.put(getHashKey(instances[i]), instances[i]);
134: log.info(instances[i].getFullName() + " registered");
135: ii.setName(instances[i].getName());
136: ii.setFullName(instances[i].getFullName());
137: fireRegisterEvent(instances[i]);
138: }
139: log.info("Registered instances loaded");
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: }
144:
145: public void savePreferences() {
146: try {
147: log.info("Saving registered instances...");
148: DbInstance[] dbi = new DbInstance[hash.size()];
149: Enumeration dbis = hash.elements();
150: for (int i = 0; dbis.hasMoreElements(); i++) {
151: dbi[i] = (DbInstance) dbis.nextElement();
152: }
153: ByteArrayOutputStream str = new ByteArrayOutputStream();
154: ObjectOutputStream out = new ObjectOutputStream(str);
155: out.writeObject(dbi);
156: out.flush();
157: byte[] thePref = str.toByteArray();
158: Preferences prefs = Preferences
159: .userNodeForPackage(getClass());
160: prefs.putByteArray("instances", thePref);
161: log.info("Instances saved");
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165: }
166:
167: }
|