001: package net.sourceforge.squirrel_sql.plugins.derby;
002:
003: /*
004: * Copyright (C) 2006 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import net.sourceforge.squirrel_sql.client.IApplication;
023: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
024: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
025: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
026: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
027: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
028: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.expanders.TableWithChildNodesExpander;
031: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.DatabaseObjectInfoTab;
032: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
033: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
034: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
035: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
036: import net.sourceforge.squirrel_sql.fw.util.StringManager;
037: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
038: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
039: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
040: import net.sourceforge.squirrel_sql.plugins.derby.exp.DerbyTableTriggerExtractorImpl;
041: import net.sourceforge.squirrel_sql.plugins.derby.tab.TriggerDetailsTab;
042: import net.sourceforge.squirrel_sql.plugins.derby.tab.TriggerSourceTab;
043: import net.sourceforge.squirrel_sql.plugins.derby.tab.ViewSourceTab;
044: import net.sourceforge.squirrel_sql.plugins.derby.tokenizer.DerbyQueryTokenizer;
045:
046: import java.net.MalformedURLException;
047: import java.sql.Driver;
048: import java.sql.SQLException;
049: import java.util.Properties;
050: import net.sourceforge.squirrel_sql.client.session.event.SessionAdapter;
051: import net.sourceforge.squirrel_sql.client.session.event.SessionEvent;
052: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
053: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
054: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverManager;
055:
056: /**
057: * The main controller class for the Derby plugin.
058: *
059: * @author manningr
060: */
061: public class DerbyPlugin extends DefaultSessionPlugin {
062:
063: private static final StringManager s_stringMgr = StringManagerFactory
064: .getStringManager(DerbyPlugin.class);
065:
066: /** Logger for this class. */
067: private final static ILogger s_log = LoggerController
068: .createLogger(DerbyPlugin.class);
069:
070: /** API for the Obejct Tree. */
071: private IObjectTreeAPI _treeAPI;
072:
073: static interface i18n {
074: //i18n[DerbyPlugin.showViewSource=Show view source]
075: String SHOW_VIEW_SOURCE = s_stringMgr
076: .getString("DerbyPlugin.showViewSource");
077:
078: //i18n[DerbyPlugin.showProcedureSource=Show procedure source]
079: String SHOW_PROCEDURE_SOURCE = s_stringMgr
080: .getString("DerbyPlugin.showProcedureSource");
081: }
082:
083: /**
084: * Return the internal name of this plugin.
085: *
086: * @return the internal name of this plugin.
087: */
088: public String getInternalName() {
089: return "derby";
090: }
091:
092: /**
093: * Return the descriptive name of this plugin.
094: *
095: * @return the descriptive name of this plugin.
096: */
097: public String getDescriptiveName() {
098: return "Derby Plugin";
099: }
100:
101: /**
102: * Returns the current version of this plugin.
103: *
104: * @return the current version of this plugin.
105: */
106: public String getVersion() {
107: return "0.12";
108: }
109:
110: /**
111: * Returns the authors name.
112: *
113: * @return the authors name.
114: */
115: public String getAuthor() {
116: return "Rob Manning";
117: }
118:
119: /**
120: * Returns a comma separated list of other contributors.
121: *
122: * @return Contributors names.
123: */
124: public String getContributors() {
125: return "Alex Pivovarov";
126: }
127:
128: /**
129: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getChangeLogFileName()
130: */
131: public String getChangeLogFileName() {
132: return "changes.txt";
133: }
134:
135: /**
136: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getHelpFileName()
137: */
138: public String getHelpFileName() {
139: return "readme.html";
140: }
141:
142: /**
143: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getLicenceFileName()
144: */
145: public String getLicenceFileName() {
146: return "licence.txt";
147: }
148:
149: /**
150: * Load this plugin.
151: *
152: * @param app Application API.
153: */
154: public synchronized void load(IApplication app)
155: throws PluginException {
156: super .load(app);
157: }
158:
159: /**
160: * Initialize this plugin.
161: */
162: public synchronized void initialize() throws PluginException {
163: super .initialize();
164: //Add session ended listener -- needs for Embedded Derby DB
165: _app.getSessionManager().addSessionListener(
166: new SessionListener());
167: }
168:
169: /**
170: * Application is shutting down so save preferences.
171: */
172: public void unload() {
173: super .unload();
174: }
175:
176: public boolean allowsSessionStartedInBackground() {
177: return true;
178: }
179:
180: /**
181: * Session has been started. Update the tree api in using the event thread
182: *
183: * @param session Session that has started.
184: *
185: * @return <TT>true</TT> if session is Oracle in which case this plugin
186: * is interested in it.
187: */
188: public PluginSessionCallback sessionStarted(final ISession session) {
189: if (!isPluginSession(session)) {
190: return null;
191: }
192: s_log.info("Installing Derby query tokenizer");
193: IQueryTokenizer orig = session.getQueryTokenizer();
194: DerbyQueryTokenizer tokenizer = new DerbyQueryTokenizer(orig
195: .getSQLStatementSeparator(),
196: orig.getLineCommentBegin(), orig
197: .isRemoveMultiLineComment());
198: session.setQueryTokenizer(tokenizer);
199: GUIUtils.processOnSwingEventThread(new Runnable() {
200: public void run() {
201: updateTreeApi(session);
202: }
203: });
204:
205: return new PluginSessionCallback() {
206: public void sqlInternalFrameOpened(
207: SQLInternalFrame sqlInternalFrame, ISession sess) {
208: // Supports Session main window only
209: }
210:
211: public void objectTreeInternalFrameOpened(
212: ObjectTreeInternalFrame objectTreeInternalFrame,
213: ISession sess) {
214: // Supports Session main window only
215: }
216: };
217:
218: }
219:
220: @Override
221: protected boolean isPluginSession(ISession session) {
222: return DialectFactory.isDerby(session.getMetaData());
223: }
224:
225: private void updateTreeApi(ISession session) {
226:
227: _treeAPI = session.getSessionInternalFrame().getObjectTreeAPI();
228:
229: //_treeAPI.addDetailTab(DatabaseObjectType.PROCEDURE,
230: // new ProcedureSourceTab(i18n.SHOW_PROCEDURE_SOURCE));
231:
232: _treeAPI.addDetailTab(DatabaseObjectType.VIEW,
233: new ViewSourceTab(i18n.SHOW_VIEW_SOURCE));
234:
235: //_treeAPI.addDetailTab(DatabaseObjectType.INDEX, new DatabaseObjectInfoTab());
236: //_treeAPI.addDetailTab(DatabaseObjectType.INDEX, new IndexDetailsTab());
237: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
238: new DatabaseObjectInfoTab());
239: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER_TYPE_DBO,
240: new DatabaseObjectInfoTab());
241: //_treeAPI.addDetailTab(DatabaseObjectType.SEQUENCE, new DatabaseObjectInfoTab());
242: //_treeAPI.addDetailTab(DatabaseObjectType.SEQUENCE, new SequenceDetailsTab());
243:
244: // Expanders - trigger and index expanders are added inside the table
245: // expander
246: //_treeAPI.addExpander(DatabaseObjectType.SCHEMA, new SchemaExpander());
247: TableWithChildNodesExpander trigExp = new TableWithChildNodesExpander();
248: trigExp
249: .setTableTriggerExtractor(new DerbyTableTriggerExtractorImpl());
250: _treeAPI.addExpander(DatabaseObjectType.TABLE, trigExp);
251:
252: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
253: new TriggerDetailsTab());
254: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
255: new TriggerSourceTab("The source of the trigger"));
256:
257: }
258:
259: /**
260: * A session listener that shutdown Embedded Derby when session and
261: * connection are already closed
262: *
263: * @author Alex Pivovarov
264: */
265: private class SessionListener extends SessionAdapter {
266: @Override
267: public void sessionClosed(SessionEvent evt) {
268: ISession session = evt.getSession();
269: shutdownEmbeddedDerby(session);
270: }
271: }
272:
273: /**
274: * Shutdown Embedded Derby DB and reload JDBC Driver
275: *
276: * @param session
277: * Current session.
278: *
279: * @author Alex Pivovarov
280: */
281: private void shutdownEmbeddedDerby(final ISession session) {
282: try {
283: ISQLDriver iSqlDr = session.getDriver();
284: if (!(iSqlDr.getDriverClassName()
285: .startsWith("org.apache.derby.jdbc.EmbeddedDriver"))) {
286: return;
287: }
288: //the code bellow is only for Embedded Derby Driver
289: IIdentifier drId = iSqlDr.getIdentifier();
290: SQLDriverManager sqlDrMan = _app.getSQLDriverManager();
291: //Getting java.sql.Driver to run shutdown command
292: Driver jdbcDr = sqlDrMan.getJDBCDriver(drId);
293: //Shutdown Embedded Derby DB
294: try {
295: jdbcDr.connect("jdbc:derby:;shutdown=true",
296: new Properties());
297: } catch (SQLException e) {
298: //it is always thrown as said in Embedded Derby API.
299: //So it is not error it is info
300: s_log.info(e.getMessage());
301: }
302: //Re-registering driver is necessary for Embedded Derby
303: sqlDrMan.registerSQLDriver(iSqlDr);
304: } catch (RuntimeException e) {
305: s_log.error(e.getMessage(), e);
306: } catch (MalformedURLException e) {
307: s_log.error(e.getMessage(), e);
308: } catch (IllegalAccessException e) {
309: s_log.error(e.getMessage(), e);
310: } catch (InstantiationException e) {
311: s_log.error(e.getMessage(), e);
312: } catch (ClassNotFoundException e) {
313: s_log.error(e.getMessage(), e);
314: }
315: }
316:
317: }
|