Advertisement
Guest User

Untitled

a guest
Mar 18th, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /*******************************************************************************
  2. * Copyright (c) 2010 Oracle.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * and Apache License v2.0 which accompanies this distribution.
  6. * The Eclipse Public License is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. * and the Apache License v2.0 is available at
  9. * http://www.opensource.org/licenses/apache2.0.php.
  10. * You may elect to redistribute this code under either of these licenses.
  11. *
  12. * Contributors:
  13. * mkeith - Gemini DBAccess examples
  14. ******************************************************************************/
  15. package org.eclipse.gemini.dbaccess.samples;
  16.  
  17. import java.util.Properties;
  18.  
  19. import java.sql.Connection;
  20. import java.sql.DatabaseMetaData;
  21. import java.sql.SQLException;
  22. import javax.sql.DataSource;
  23.  
  24. import org.osgi.framework.BundleActivator;
  25. import org.osgi.framework.BundleContext;
  26. import org.osgi.framework.ServiceReference;
  27.  
  28. import org.osgi.util.tracker.ServiceTracker;
  29. import org.osgi.util.tracker.ServiceTrackerCustomizer;
  30.  
  31. import org.osgi.service.jdbc.DataSourceFactory;
  32.  
  33. /**
  34. * Example of how to access a DataSource from a client program
  35. *
  36. * @author mkeith
  37. */
  38. public class DataSourceClientExample implements BundleActivator, ServiceTrackerCustomizer {
  39.  
  40. public static final String EMBEDDED_DERBY_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
  41. public static final String JDBC_4_VERSION = "4.0";
  42.  
  43. ServiceTracker dsfTracker;
  44. BundleContext ctx;
  45.  
  46. /* === Activator methods === */
  47.  
  48. public void start(BundleContext context) throws Exception {
  49. log("Sample Gemini DBAccess Client starting");
  50. ctx = context;
  51.  
  52. dsfTracker = new ServiceTracker(ctx, DataSourceFactory.class.getName(), this);
  53. dsfTracker.open();
  54. }
  55.  
  56. public void stop(BundleContext context) throws Exception {
  57. log("Sample Gemini DBAccess Client stopping");
  58. dsfTracker.close();
  59. }
  60.  
  61. /* === ServiceTracker methods === */
  62.  
  63. public Object addingService(ServiceReference ref) {
  64. Object service = ctx.getService(ref);
  65.  
  66. String driver = (String)ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS);
  67. String version = (String)ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION);
  68.  
  69. if (driver != null && driver.equalsIgnoreCase(EMBEDDED_DERBY_DRIVER_NAME) &&
  70. version != null && version.equalsIgnoreCase(JDBC_4_VERSION)) {
  71. log("Sample Gemini DBAccess client notified of service: " + driver);
  72.  
  73. // We have a JDBC service, now do something with it
  74. DataSourceFactory dsf = (DataSourceFactory) service;
  75. useEmbeddedDataSource(dsf);
  76. }
  77. return service;
  78. }
  79.  
  80. public void modifiedService(ServiceReference ref, Object service) { /* Do nothing */ }
  81.  
  82. public void removedService(ServiceReference ref, Object service) { ctx.ungetService(ref); }
  83.  
  84. /* === Supporting methods === */
  85.  
  86. void useEmbeddedDataSource(DataSourceFactory dsf) {
  87. Properties props = new Properties();
  88. props.put(DataSourceFactory.JDBC_URL, "jdbc:derby:testDB;create=true");
  89. DataSource ds = null;
  90. Connection conn = null;
  91. try {
  92. ds = dsf.createDataSource(props);
  93. conn = ds.getConnection();
  94. DatabaseMetaData metadata = conn.getMetaData();
  95. log("Driver accessed by sample Gemini DBAccess client:" +
  96. "\n\tName = " + metadata.getDriverName() +
  97. "\n\tVersion = " + metadata.getDriverVersion() +
  98. "\n\tUser = " + metadata.getUserName());
  99. conn.close();
  100. } catch (SQLException sqlEx) {
  101. log("Sample Gemini DBAccess client - Error creating or using data source: " + sqlEx);
  102. }
  103. }
  104.  
  105. void log(String msg) { System.out.println("===== " + msg); }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement