001: package de.ixdb.squirrel_sql.plugins.cache;
002:
003: import com.intersys.cache.jbind.JBindDatabase;
004: import com.intersys.objects.CacheDatabase;
005: import com.intersys.objects.CacheQuery;
006: import com.intersys.objects.Database;
007: import net.sourceforge.squirrel_sql.client.IApplication;
008: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
009: import net.sourceforge.squirrel_sql.client.session.ISession;
010: import net.sourceforge.squirrel_sql.client.session.action.ISessionAction;
011: import net.sourceforge.squirrel_sql.client.util.IdentifierFactory;
012: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
013: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
014: import net.sourceforge.squirrel_sql.fw.util.Resources;
015:
016: import javax.swing.*;
017: import java.awt.event.ActionEvent;
018: import java.sql.ResultSet;
019: import java.util.*;
020:
021: public class ShowNamespacesCommand {
022: private ISession _session;
023:
024: public ShowNamespacesCommand(ISession session) {
025: _session = session;
026: }
027:
028: public void execute() {
029: try {
030: String curServerName = getUrlBegin(_session
031: .getSQLConnection().getConnection().getMetaData()
032: .getURL());
033:
034: Database conn = (JBindDatabase) CacheDatabase
035: .getDatabase(_session.getSQLConnection()
036: .getConnection());
037: CacheQuery qry = new CacheQuery(conn,
038: "%Library.RoutineMgr", "NamespaceList");
039: ResultSet resNamespaces = qry.execute();
040:
041: Hashtable aliasesByNamespaces = new Hashtable();
042: while (resNamespaces.next()) {
043: aliasesByNamespaces.put(resNamespaces.getString(1)
044: .toUpperCase(), "");
045: }
046: resNamespaces.close();
047:
048: ISQLDriver cacheDriver = null;
049: for (Iterator i = _session.getApplication().getDataCache()
050: .drivers(); i.hasNext();) {
051: ISQLDriver drv = (ISQLDriver) i.next();
052: if (drv.getDriverClassName().equals(
053: "com.intersys.jdbc.CacheDriver")) {
054: cacheDriver = drv;
055: }
056: }
057:
058: if (null == cacheDriver) {
059: String msg = "Could not find driver com.intersys.jdbc.CacheDriver";
060: _session.showErrorMessage(msg);
061: throw new IllegalStateException(msg);
062: }
063:
064: for (Iterator i = _session.getApplication().getDataCache()
065: .getAliasesForDriver(cacheDriver); i.hasNext();) {
066: ISQLAlias alias = (ISQLAlias) i.next();
067:
068: String serverName = getUrlBegin(alias.getUrl());
069:
070: if (false == curServerName.equalsIgnoreCase(serverName)) {
071: continue;
072: }
073:
074: String nameSpaceOfAlias = alias.getUrl().substring(
075: alias.getUrl().lastIndexOf('/') + 1);
076:
077: String aliasNames = (String) aliasesByNamespaces
078: .get(nameSpaceOfAlias.toUpperCase());
079:
080: if (null != aliasNames) {
081: if (0 == aliasNames.length()) {
082: aliasNames += alias.getName();
083: } else {
084: aliasNames += ";" + alias.getName();
085: }
086: aliasesByNamespaces.put(nameSpaceOfAlias,
087: aliasNames);
088:
089: }
090: }
091:
092: String[][] nameSpacesAndAliases = new String[aliasesByNamespaces
093: .size()][2];
094: int index = 0;
095: for (Enumeration e = aliasesByNamespaces.keys(); e
096: .hasMoreElements();) {
097: nameSpacesAndAliases[index][0] = (String) e
098: .nextElement();
099: nameSpacesAndAliases[index][1] = (String) aliasesByNamespaces
100: .get(nameSpacesAndAliases[index][0]);
101: ++index;
102: }
103:
104: NamespaceCtrlListener nl = new NamespaceCtrlListener() {
105: public String nameSpaceSelected(ISession session,
106: String nameSpace, String aliasNameTemplate) {
107: return onNameSpaceSelected(session, nameSpace,
108: aliasNameTemplate);
109: }
110: };
111:
112: Arrays.sort(nameSpacesAndAliases, new Comparator() {
113: public int compare(Object o1, Object o2) {
114: return ((String[]) o1)[0]
115: .compareTo(((String[]) o2)[0]);
116: }
117: });
118:
119: new NamespaceCtrl(_session, nameSpacesAndAliases, nl);
120:
121: } catch (Exception e) {
122: throw new RuntimeException(e);
123: }
124: }
125:
126: private String getUrlBegin(String url) {
127: //jdbc:Cache://cachensw-and:1972/SHDTest
128:
129: return url.substring(0, url.lastIndexOf('/'));
130:
131: }
132:
133: private String onNameSpaceSelected(ISession session,
134: String nameSpace, String aliasNameTemplate) {
135:
136: try {
137: ISQLAlias curAlias = session.getAlias();
138:
139: String urlPrefix = "jdbc:Cache://";
140:
141: if (false == curAlias.getUrl().startsWith(urlPrefix)) {
142: String msg = "URL of this session does not start with "
143: + urlPrefix + ".\nCannot create alias";
144: JOptionPane.showMessageDialog(session.getApplication()
145: .getMainFrame(), msg);
146: return "";
147: }
148:
149: if (urlPrefix.length() >= curAlias.getUrl()
150: .lastIndexOf(':')) {
151: String msg = "Could not find server in this session's URL "
152: + curAlias.getUrl()
153: + ".\nURL must match jdbc:Cache://<server>:<port>/<namespace>\n"
154: + "Cannot create alias.";
155: JOptionPane.showMessageDialog(session.getApplication()
156: .getMainFrame(), msg);
157: return "";
158: }
159:
160: String server = curAlias.getUrl().substring(
161: urlPrefix.length(),
162: curAlias.getUrl().lastIndexOf(':'));
163:
164: ISQLAlias alias = session.getApplication().getDataCache()
165: .createAlias(
166: IdentifierFactory.getInstance()
167: .createIdentifier());
168:
169: alias.setName(aliasNameTemplate.replaceAll("%server",
170: server).replaceAll("%namespace", nameSpace));
171: alias.setDriverIdentifier(curAlias.getDriverIdentifier());
172: alias.setUserName(curAlias.getUserName());
173: alias.setPassword(curAlias.getPassword());
174: alias.setUrl(curAlias.getUrl().substring(0,
175: curAlias.getUrl().lastIndexOf('/') + 1)
176: + nameSpace);
177: alias.setAutoLogon(curAlias.isAutoLogon());
178:
179: session.getApplication().getDataCache().addAlias(alias);
180:
181: return alias.getName();
182: } catch (Exception e) {
183: throw new RuntimeException(e);
184: }
185: }
186:
187: }
|