/plugins/SQL/tags/v_0_93/sql/SqlPlugin.java

# · Java · 705 lines · 380 code · 112 blank · 213 comment · 58 complexity · feea92fca0b9c0aa9bbbd196fa931c8a MD5 · raw file

  1. /**
  2. * SqlPlugin.java - Sql Plugin
  3. * Copyright (C) 26 á×Ç?ÓÔ 2001 Ç. Sergey V. Udaltsov
  4. * svu@users.sourceforge.net
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package sql;
  22. import java.awt.*;
  23. import java.io.*;
  24. import java.sql.*;
  25. import java.text.*;
  26. import java.util.*;
  27. import javax.swing.*;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.jedit.gui.*;
  30. import org.gjt.sp.jedit.io.*;
  31. import org.gjt.sp.jedit.msg.*;
  32. import org.gjt.sp.jedit.textarea.*;
  33. import org.gjt.sp.util.*;
  34. import sessions.*;
  35. import sql.*;
  36. import sql.options.*;
  37. /**
  38. * Description of the Class
  39. *
  40. * @author svu
  41. * @created 26 á×Ç?ÓÔ 2001 Ç.
  42. */
  43. public class SqlPlugin extends EBPlugin
  44. {
  45. /**
  46. * Description of the Field
  47. *
  48. * @since
  49. */
  50. public final static String NAME = "Sql Plugin";
  51. /**
  52. * Description of the Field
  53. *
  54. * @since
  55. */
  56. public final static String DEFAULT_EDIT_MODE_NAME = "transact-sql";
  57. /**
  58. * Description of the Field
  59. *
  60. * @since
  61. */
  62. public final static String resultSetWinName = "sql.resultSet";
  63. /**
  64. * Description of the Field
  65. *
  66. * @since
  67. */
  68. public static ImageIcon Icon;
  69. protected static Properties props = null;
  70. protected static boolean configModified = false;
  71. protected static String currentSession;
  72. protected static SqlVFS sqlVFS;
  73. /**
  74. * Description of the Method
  75. *
  76. * @since
  77. */
  78. public void start()
  79. {
  80. final File settingsDir = new File( MiscUtilities.constructPath(
  81. jEdit.getSettingsDirectory(), "sql" ) );
  82. if ( !settingsDir.exists() )
  83. settingsDir.mkdirs();
  84. VFSManager.registerVFS( SqlVFS.PROTOCOL, sqlVFS = new SqlVFS() );
  85. registerJdbcClassPath();
  86. SqlUtils.init();
  87. }
  88. /**
  89. * Description of the Method
  90. *
  91. * @param menuItems Description of Parameter
  92. * @since
  93. */
  94. public void createMenuItems( Vector menuItems )
  95. {
  96. menuItems.addElement( GUIUtilities.loadMenu( "sqlMenu" ) );
  97. }
  98. /**
  99. * Description of the Method
  100. *
  101. * @param optionsDialog Description of Parameter
  102. * @since
  103. */
  104. public void createOptionPanes( OptionsDialog optionsDialog )
  105. {
  106. final OptionGroup group = new OptionGroup( "SQL" );
  107. group.addOptionPane( new GeneralOptionPane() );
  108. group.addOptionPane( new ServersOptionPane() );
  109. group.addOptionPane( new JdbcOptionPane() );
  110. final OptionGroup pgroup = new OptionGroup( "sql.preprocessors" );
  111. final java.util.List l = SqlUtils.getPreprocessors();
  112. for ( Iterator i = l.iterator(); i.hasNext(); )
  113. {
  114. final Preprocessor pr = (Preprocessor) i.next();
  115. final OptionPane op = pr.getOptionPane();
  116. if ( op != null )
  117. pgroup.addOptionPane( op );
  118. }
  119. group.addOptionGroup( pgroup );
  120. optionsDialog.addOptionGroup( group );
  121. }
  122. /**
  123. * Description of the Method
  124. *
  125. * @param message Description of Parameter
  126. * @since
  127. */
  128. public void handleMessage( EBMessage message )
  129. {
  130. if ( message instanceof SessionChanging )
  131. handleSessionChange( (SessionChanging) message );
  132. }
  133. /**
  134. * Sets the Property attribute of the SqlPlugin class
  135. *
  136. * @param name The new Property value
  137. * @param value The new Property value
  138. * @since
  139. */
  140. public static void setProperty( String name, String value )
  141. {
  142. props.setProperty( name, value );
  143. configModified = true;
  144. }
  145. /**
  146. * Sets the BufferMode attribute of the SqlPlugin class
  147. *
  148. * @param buf The new BufferMode value
  149. * @param name The new BufferMode value
  150. * @since
  151. */
  152. public static void setBufferMode( Buffer buf, String name )
  153. {
  154. final Mode mode = jEdit.getMode( name );
  155. if ( mode != null )
  156. buf.setMode( mode );
  157. }
  158. /**
  159. *Sets the JdbcClassPath attribute of the SqlPlugin class
  160. *
  161. * @param jdbcClassPath The new JdbcClassPath value
  162. * @since
  163. */
  164. public static void setJdbcClassPath( String[] jdbcClassPath )
  165. {
  166. final String[] oldCp = getJdbcClassPath();
  167. unregisterJdbcClassPath();
  168. for ( int i = oldCp.length; --i >= 0; )
  169. unsetProperty( "sql.jdbcClassPath." + i );
  170. if ( jdbcClassPath != null )
  171. {
  172. for ( int i = jdbcClassPath.length; --i >= 0; )
  173. setProperty( "sql.jdbcClassPath." + i, jdbcClassPath[i] );
  174. }
  175. registerJdbcClassPath();
  176. }
  177. /**
  178. * Gets the Property attribute of the SqlPlugin class
  179. *
  180. * @param name Description of Parameter
  181. * @return The Property value
  182. * @since
  183. */
  184. public static String getProperty( String name )
  185. {
  186. if ( props == null )
  187. loadProperties();
  188. return props.getProperty( name );
  189. }
  190. /**
  191. *Gets the JdbcClassPath attribute of the SqlPlugin class
  192. *
  193. * @return The JdbcClassPath value
  194. * @since
  195. */
  196. public static String[] getJdbcClassPath()
  197. {
  198. final java.util.List v = new ArrayList();
  199. int i = 0;
  200. while ( true )
  201. {
  202. final String s = getProperty( "sql.jdbcClassPath." + i++ );
  203. if ( s == null )
  204. break;
  205. v.add( s );
  206. }
  207. return (String[]) v.toArray( new String[0] );
  208. }
  209. /**
  210. * Gets the PropertyNames attribute of the SqlPlugin class
  211. *
  212. * @return The PropertyNames value
  213. * @since
  214. */
  215. public static Iterator getPropertyNames()
  216. {
  217. if ( props == null )
  218. loadProperties();
  219. return props.keySet().iterator();
  220. }
  221. /**
  222. *Gets the ConfigFileName attribute of the SqlPlugin class
  223. *
  224. * @param sessionName Description of Parameter
  225. * @return The ConfigFileName value
  226. * @since
  227. */
  228. public static String getConfigFileName( String sessionName )
  229. {
  230. return MiscUtilities.constructPath( jEdit.getSettingsDirectory(),
  231. "sql",
  232. ( sessionName == null || "default".equals( sessionName ) ) ?
  233. "properties" :
  234. "properties." + sessionName );
  235. }
  236. /**
  237. *Description of the Method
  238. *
  239. * @since
  240. */
  241. public static void clearProperties()
  242. {
  243. ResultSetWindow.clearProperties();
  244. SqlServerRecord.clearProperties();
  245. }
  246. /**
  247. * Description of the Method
  248. *
  249. * @since
  250. */
  251. public static void loadProperties()
  252. {
  253. String path = getConfigFileName( getCurrentSession() );
  254. if ( !( new File( path ).exists() ) )
  255. path = getConfigFileName( null );
  256. try
  257. {
  258. props = new Properties();
  259. final InputStream is = new BufferedInputStream( new FileInputStream( path ) );
  260. props.load( is );
  261. is.close();
  262. configModified = false;
  263. } catch ( IOException ex )
  264. {
  265. Log.log( Log.ERROR, SqlPlugin.class,
  266. "Error loading SqlPlugin properties" + ex );
  267. }
  268. }
  269. /**
  270. * Description of the Method
  271. *
  272. * @since
  273. */
  274. public static void commitProperties()
  275. {
  276. if ( !configModified )
  277. return;
  278. final String path = getConfigFileName( getCurrentSession() );
  279. try
  280. {
  281. final OutputStream os = new BufferedOutputStream( new FileOutputStream( path ) );
  282. props.store( os, "Sql Plugin properties" );
  283. os.close();
  284. FileVFS.setPermissions( path, 0600 );
  285. configModified = false;
  286. } catch ( IOException ex )
  287. {
  288. Log.log( Log.ERROR, SqlPlugin.class,
  289. "Error saving SqlPlugin properties:" );
  290. Log.log( Log.ERROR, SqlPlugin.class, ex );
  291. }
  292. }
  293. /**
  294. * Description of the Method
  295. *
  296. * @param name Description of Parameter
  297. * @since
  298. */
  299. public static void unsetProperty( String name )
  300. {
  301. props.remove( name );
  302. configModified = true;
  303. }
  304. /**
  305. * Description of the Method
  306. *
  307. * @param view Description of Parameter
  308. * @return Description of the Returned Value
  309. * @since
  310. */
  311. public static ResultSetWindow showResultSetWindow( View view )
  312. {
  313. final DockableWindowManager dockableWindowManager = view.getDockableWindowManager();
  314. if ( !dockableWindowManager.isDockableWindowVisible( resultSetWinName ) )
  315. dockableWindowManager.addDockableWindow( resultSetWinName );
  316. dockableWindowManager.showDockableWindow( resultSetWinName );
  317. return (ResultSetWindow) dockableWindowManager.getDockable( resultSetWinName );
  318. }
  319. /**
  320. * Description of the Method
  321. *
  322. * @param view Description of Parameter
  323. * @param serverName Description of Parameter
  324. * @since
  325. */
  326. public static void loadObject( final View view, final String serverName )
  327. {
  328. SqlUtils.getThreadGroup().runInGroup(
  329. new Runnable()
  330. {
  331. public void run()
  332. {
  333. final SqlServerRecord rec = SqlUtils.getServerRecord( view, serverName );
  334. if ( rec == null )
  335. return;
  336. Connection conn = null;
  337. try
  338. {
  339. conn = rec.allocConnection();
  340. final String user = rec.getProperty( rec.USER ).toUpperCase();
  341. final Object[] objs = SqlUtils.loadObjectList( view, conn, rec, user );
  342. if ( objs == null )
  343. return;
  344. final DbCodeObject dbobj = chooseCodeObjectInAWTThread( view, objs );
  345. if ( dbobj == null )
  346. return;
  347. final String text = SqlUtils.loadObjectText( conn, rec, user, dbobj.name, dbobj.type );
  348. if ( text == null )
  349. {
  350. Log.log( Log.NOTICE, SqlPlugin.class,
  351. "Got null retrieving the object text for " + dbobj.name );
  352. return;
  353. }
  354. SqlUtils.runInAWTThreadNoWait(
  355. new Runnable()
  356. {
  357. public void run()
  358. {
  359. final Buffer buf = jEdit.newFile( view );
  360. buf.insert( 0, text );
  361. setBufferMode( buf, rec.getServerType().getEditModeName() );
  362. }
  363. } );
  364. } catch ( SQLException ex )
  365. {
  366. SqlUtils.processSqlException( view, ex, "??", rec );
  367. } finally
  368. {
  369. rec.releaseConnection( conn );
  370. }
  371. }
  372. } );
  373. }
  374. /**
  375. * Description of the Method
  376. *
  377. * @param view Description of Parameter
  378. * @since
  379. */
  380. public static void loadObjectFromServer( View view )
  381. {
  382. final String name = SqlUtils.getServerForPublishing( view );
  383. if ( name != null )
  384. loadObject( view, name );
  385. }
  386. /**
  387. * Description of the Method
  388. *
  389. * @param view Description of Parameter
  390. * @param serverName Description of Parameter
  391. * @since
  392. */
  393. public static void publishSelection( View view, String serverName )
  394. {
  395. final Buffer buffer = view.getBuffer();
  396. final JEditTextArea tArea = view.getTextArea();
  397. final Selection[] sels = tArea.getSelection();
  398. int start;
  399. int end;
  400. if ( sels.length != 1 )
  401. {
  402. start = 0;
  403. end = buffer.getLength();
  404. }
  405. else
  406. {
  407. start = sels[0].getStart();
  408. end = sels[0].getEnd();
  409. }
  410. SqlUtils.publishText( view, buffer, start, end - start, serverName );
  411. }
  412. /**
  413. * Description of the Method
  414. *
  415. * @param view Description of Parameter
  416. * @since
  417. */
  418. public static void publishSelectionToServer( View view )
  419. {
  420. final String name = SqlUtils.getServerForPublishing( view );
  421. if ( name != null )
  422. publishSelection( view, name );
  423. }
  424. /**
  425. * Description of the Method
  426. *
  427. * @param view Description of Parameter
  428. * @param serverName Description of Parameter
  429. * @since
  430. */
  431. public static void publishBuffer( View view, String serverName )
  432. {
  433. final Buffer buffer = view.getBuffer();
  434. SqlUtils.publishText( view, buffer, 0, buffer.getLength(), serverName );
  435. }
  436. /**
  437. * Description of the Method
  438. *
  439. * @param view Description of Parameter
  440. * @since
  441. */
  442. public static void publishBufferToServer( View view )
  443. {
  444. final String name = SqlUtils.getServerForPublishing( view );
  445. if ( name != null )
  446. publishBuffer( view, name );
  447. }
  448. /**
  449. *Constructor for the registerJdbcClass object
  450. *
  451. * @since
  452. */
  453. public static void registerJdbcClassPath()
  454. {
  455. final String[] jdbcClassPath = getJdbcClassPath();
  456. if ( jdbcClassPath != null )
  457. for ( int i = jdbcClassPath.length; --i >= 0; )
  458. {
  459. final String path = jdbcClassPath[i];
  460. if ( !( new File( path ).exists() ) )
  461. {
  462. Log.log( Log.ERROR, SqlPlugin.class,
  463. "JDBC classpath component " + path + " does not exist" );
  464. continue;
  465. }
  466. final EditPlugin.JAR jar = jEdit.getPluginJAR( path );
  467. if ( jar == null )
  468. {// not registered yet
  469. try
  470. {
  471. jEdit.addPluginJAR( new EditPlugin.JAR( path,
  472. new JARClassLoader( path ) ) );
  473. } catch ( IOException ex )
  474. {
  475. Log.log( Log.ERROR, SqlPlugin.class,
  476. "Error loading the jdbc driver from " + path + ": " );
  477. Log.log( Log.ERROR, SqlPlugin.class, ex );
  478. continue;
  479. }
  480. }
  481. }
  482. SqlServerType.loadAll();
  483. VFSManager.sendVFSUpdate( sqlVFS, SqlVFS.PROTOCOL + ":/", false );
  484. }
  485. /**
  486. *Description of the Method
  487. *
  488. * @since
  489. */
  490. public static void unregisterJdbcClassPath()
  491. {
  492. SqlServerType.dropAll();
  493. final String[] jdbcClassPath = getJdbcClassPath();
  494. if ( jdbcClassPath != null )
  495. for ( int i = jdbcClassPath.length; --i >= 0; )
  496. {
  497. final String path = jdbcClassPath[i];
  498. if ( !( new File( path ).exists() ) )
  499. {
  500. Log.log( Log.ERROR, SqlPlugin.class,
  501. "JDBC classpath component " + path + " does not exist" );
  502. continue;
  503. }
  504. final EditPlugin.JAR jar = jEdit.getPluginJAR( path );
  505. if ( jar == null )
  506. {
  507. Log.log( Log.ERROR, SqlPlugin.class,
  508. "Strange, classpath element " + path + " was not registered" );
  509. }
  510. //!! TODO
  511. }
  512. }
  513. /**
  514. *Gets the CurrentSession attribute of the SqlPlugin class
  515. *
  516. * @return The CurrentSession value
  517. * @since
  518. */
  519. protected static String getCurrentSession()
  520. {
  521. if ( currentSession == null )
  522. currentSession = SessionManager.getInstance().getCurrentSession();
  523. return currentSession;
  524. }
  525. /**
  526. *Description of the Method
  527. *
  528. * @param message Description of Parameter
  529. * @since
  530. */
  531. protected static void handleSessionChange( SessionChanging message )
  532. {
  533. Log.log( Log.DEBUG, SqlPlugin.class,
  534. "Changing the session from " +
  535. message.getOldSession() + " to " + message.getNewSession() );
  536. commitProperties();
  537. setJdbcClassPath( null );
  538. clearProperties();
  539. props = null;
  540. currentSession = message.getNewSession();
  541. registerJdbcClassPath();
  542. }
  543. /**
  544. * Description of the Method
  545. *
  546. * @param view Description of Parameter
  547. * @param objs Description of Parameter
  548. * @return Description of the Returned Value
  549. * @since
  550. */
  551. protected static DbCodeObject chooseCodeObjectInAWTThread( final View view,
  552. final Object objs[] )
  553. {
  554. final java.util.List rv = Collections.synchronizedList( new ArrayList() );
  555. final Runnable r =
  556. new Runnable()
  557. {
  558. public void run()
  559. {
  560. final JComboBox combo = new JComboBox( objs );
  561. final Object controls[] = new Object[2];
  562. controls[0] = jEdit.getProperty( "sql.objectchooser.prompt" );
  563. controls[1] = combo;
  564. final JOptionPane p = new JOptionPane(
  565. controls,
  566. JOptionPane.INFORMATION_MESSAGE,
  567. JOptionPane.OK_CANCEL_OPTION,
  568. Icon );
  569. final JDialog dlg = p.createDialog( view,
  570. jEdit.getProperty( "sql.objectchooser.title" ) );
  571. combo.setRenderer( new DbCodeObject.CellRenderer() );
  572. dlg.show();
  573. final Object val = p.getValue();
  574. if ( !new Integer( JOptionPane.OK_OPTION ).equals( val ) )
  575. return;
  576. if ( combo.getSelectedIndex() == -1 )
  577. return;
  578. final Object obj = combo.getItemAt( combo.getSelectedIndex() );
  579. rv.add( obj );
  580. }
  581. };
  582. SqlUtils.runInAWTThreadAndWait( r );
  583. if ( rv.size() == 0 )
  584. return null;
  585. return (DbCodeObject) rv.get( 0 );
  586. }
  587. static
  588. {
  589. Icon = new ImageIcon(
  590. Toolkit.getDefaultToolkit().getImage(
  591. SqlPlugin.class.getClassLoader().getResource( "SqlPlugin.gif" ) ) );
  592. }
  593. }