Search in sources :

Example 1 with DataSourceFactory

use of org.osgi.service.jdbc.DataSourceFactory in project aries by apache.

the class AbstractTransactionTest method programaticConnection.

private Connection programaticConnection(Properties jdbc) {
    JDBCConnectionProviderFactory resourceProviderFactory = getService(JDBCConnectionProviderFactory.class, 5000);
    DataSourceFactory dsf = getService(DataSourceFactory.class, 5000);
    provider = resourceProviderFactory.getProviderFor(dsf, jdbc, resourceProviderConfig());
    return provider.getResource(txControl);
}
Also used : DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) JDBCConnectionProviderFactory(org.osgi.service.transaction.control.jdbc.JDBCConnectionProviderFactory)

Example 2 with DataSourceFactory

use of org.osgi.service.jdbc.DataSourceFactory in project karaf by apache.

the class DSFactoriesCommand method execute.

@Override
public Object execute() throws Exception {
    ShellTable table = new ShellTable();
    table.column("Name");
    table.column("Class");
    table.column("Version");
    Collection<ServiceReference<DataSourceFactory>> refs = context.getServiceReferences(DataSourceFactory.class, null);
    for (ServiceReference<DataSourceFactory> ref : refs) {
        String driverName = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_NAME);
        String driverClass = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS);
        String driverVersion = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION);
        table.addRow().addContent(driverName, driverClass, driverVersion);
    }
    table.print(System.out);
    return null;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with DataSourceFactory

use of org.osgi.service.jdbc.DataSourceFactory in project aries by apache.

the class TxDBServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    pw = response.getWriter();
    pw.write(LOGO_HEADER);
    // Get the bundle context from ServletContext attributes
    BundleContext ctx = (BundleContext) getServletContext().getAttribute(OSGI_BUNDLECONTEXT_ATTRIBUTE);
    pw.println("<html>");
    pw.println("<head>");
    pw.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"tableTemplate.css\"/>");
    pw.println("<body>");
    pw.println("<h2 align=center><p><font face=\"Tahoma\">Sample Application for JDBC usage in JTA Transactions.</font></h2>");
    pw.println("<p><p>");
    ServiceReference tmServiceRef = ctx.getServiceReference("javax.transaction.TransactionManager");
    ServiceReference derbyServiceRef = ctx.getServiceReference(DataSourceFactory.class.getName());
    if (tmServiceRef == null || derbyServiceRef == null) {
        pw.println("<font face=\"Tahoma\">TransactionManager or Derby driver are not available in the OSGI registry.</font><br>");
    } else {
        TransactionManager tm = (TransactionManager) ctx.getService(tmServiceRef);
        DataSourceFactory derbyRegistry = (DataSourceFactory) ctx.getService(derbyServiceRef);
        try {
            // Set the needed properties for the database connection
            Properties props = new Properties();
            props.put(DataSourceFactory.JDBC_URL, "jdbc:derby:txDemo");
            props.put(DataSourceFactory.JDBC_DATABASE_NAME, "txDemo");
            props.put(DataSourceFactory.JDBC_USER, "");
            props.put(DataSourceFactory.JDBC_PASSWORD, "");
            XADataSource xaDataSource = derbyRegistry.createXADataSource(props);
            pw.println("<center><form><table><tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndCommit' size='100'/></td></tr>");
            pw.println("<tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndRollback' size='100'/></center></td></tr>");
            pw.println("<tr colspan='2' align='center'><td>&nbsp;</td><td><input type='submit' name='action' value='cleanTable' size='100' />&nbsp;<input type='submit' name='action' value='printTable' size='100'/></td><tr></table></form></center>");
            String value = request.getParameter("value");
            String action = request.getParameter("action");
            if (action != null && action.equals("InsertAndCommit")) {
                insertIntoTransaction(xaDataSource, tm, value, true);
            } else if (action != null && action.equals("InsertAndRollback")) {
                insertIntoTransaction(xaDataSource, tm, value, false);
            } else if (action != null && action.equals("cleanTable")) {
                cleanTable(xaDataSource);
            }
            printTable(xaDataSource);
        } catch (Exception e) {
            pw.println("<font face=\"Tahoma\">Unexpected exception occurred " + e.toString() + ".</font><br>");
            e.printStackTrace(pw);
        }
    }
    pw.println("</body>");
    pw.println("</html>");
    pw.flush();
}
Also used : XADataSource(javax.sql.XADataSource) DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) TransactionManager(javax.transaction.TransactionManager) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with DataSourceFactory

use of org.osgi.service.jdbc.DataSourceFactory in project aries by apache.

the class AbstractManagedJPADataSourceSetup method addingService.

@Override
public AbstractManagedJPAEMFLocator addingService(ServiceReference<DataSourceFactory> reference) {
    DataSourceFactory service = context.getService(reference);
    AbstractManagedJPAEMFLocator toReturn;
    try {
        Map<String, Object> jpaProps = decorateJPAProperties(service, unmodifiableMap(providerProperties), (Properties) jdbcProperties.clone(), new HashMap<>(baseJPAProperties));
        toReturn = getManagedJPAEMFLocator(context, pid, jpaProps, providerProperties, () -> cleanupOnClose(jpaProps));
    } catch (Exception e) {
        LOG.error("An error occured creating the Resource provider for pid {}", pid, e);
        return null;
    }
    updateService(reference, toReturn);
    return toReturn;
}
Also used : DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) ConfigurationException(org.osgi.service.cm.ConfigurationException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

DataSourceFactory (org.osgi.service.jdbc.DataSourceFactory)4 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1 XADataSource (javax.sql.XADataSource)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 TransactionManager (javax.transaction.TransactionManager)1 ShellTable (org.apache.karaf.shell.support.table.ShellTable)1 BundleContext (org.osgi.framework.BundleContext)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 JDBCConnectionProviderFactory (org.osgi.service.transaction.control.jdbc.JDBCConnectionProviderFactory)1