/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.ops4j.pax.transx.jdbc;

import org.ops4j.pax.transx.jdbc.impl.ConnectionPoolDataSourceMCF;
import org.ops4j.pax.transx.jdbc.impl.JDBCDriverMCF;
import org.ops4j.pax.transx.jdbc.impl.LocalDataSourceMCF;
import org.ops4j.pax.transx.jdbc.impl.XADataSourceMCF;

import javax.resource.ResourceException;
import javax.resource.spi.ManagedConnectionFactory;
import javax.sql.CommonDataSource;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.XADataSource;

public class ManagedConnectionFactoryFactory {

    public static ManagedConnectionFactory create(String driver, String connectionUrl) throws ResourceException {
        JDBCDriverMCF mcf = new JDBCDriverMCF();
        mcf.setDriver(driver);
        mcf.setConnectionURL(connectionUrl);
        return mcf;
    }

    public static ManagedConnectionFactory create(CommonDataSource dataSource) {
        if (dataSource instanceof XADataSource) {
            return create((XADataSource) dataSource);
        }
        else if (dataSource instanceof ConnectionPoolDataSource) {
            return create((ConnectionPoolDataSource) dataSource);
        }
        else if (dataSource instanceof DataSource) {
            return create((DataSource) dataSource);
        }
        else {
            throw new UnsupportedOperationException();
        }
    }

    public static ManagedConnectionFactory create(DataSource dataSource) {
        return new LocalDataSourceMCF(dataSource);
    }

    public static ManagedConnectionFactory create(ConnectionPoolDataSource dataSource) {
        return new ConnectionPoolDataSourceMCF(dataSource);
    }

    public static ManagedConnectionFactory create(XADataSource dataSource) {
        return new XADataSourceMCF(dataSource);
    }
}

@@ -594,16 +594,80 @@
 .jdbc;%0A%0A
+import org.ops4j.pax.transx.connector.ConnectionManagerFactory;%0A
 import o
@@ -895,48 +895,144 @@
 CF;%0A
-%0Aimport javax.resource.ResourceException
+import org.ops4j.pax.transx.tm.TransactionManager;%0A%0Aimport javax.resource.ResourceException;%0Aimport javax.resource.spi.ConnectionManager
 ;%0Aim
@@ -2493,11 +2493,1783 @@
 );%0A    %7D
+%0A%0A    public static Builder builder() %7B%0A        return new Builder();%0A    %7D%0A%0A    public static class Builder %7B%0A%0A        private ConnectionManagerFactory.Builder builder = ConnectionManagerFactory.builder();%0A        private CommonDataSource dataSource;%0A        private ManagedConnectionFactory managedConnectionFactory;%0A%0A        private Builder() %7B%0A        %7D%0A%0A        public Builder name(String name) %7B%0A            builder.name(name);%0A            return this;%0A        %7D%0A%0A        public Builder dataSource(CommonDataSource dataSource) %7B%0A            this.dataSource = dataSource;%0A            return this;%0A        %7D%0A%0A        public Builder transaction(ConnectionManagerFactory.TransactionSupportLevel tx) %7B%0A            builder.transaction(tx);%0A            return this;%0A        %7D%0A%0A        public Builder transactionManager(TransactionManager transactionManager) %7B%0A            builder.transactionManager(transactionManager);%0A            return this;%0A        %7D%0A%0A        public Builder pooling(boolean pooling) %7B%0A            builder.pooling(pooling);%0A            return this;%0A        %7D%0A%0A        public Builder partition(ConnectionManagerFactory.Partition partition) %7B%0A            builder.partition(partition);%0A            return this;%0A        %7D%0A%0A        public DataSource build() throws Exception %7B%0A            if (dataSource == null) %7B%0A                throw new NullPointerException(%22dataSource must be set%22);%0A            %7D%0A            if (managedConnectionFactory == null) %7B%0A                managedConnectionFactory = create(dataSource);%0A            %7D%0A            builder.managedConnectionFactory(managedConnectionFactory);%0A            ConnectionManager cm = builder.build();%0A            return (DataSource) managedConnectionFactory.createConnectionFactory(cm);%0A        %7D%0A%0A    %7D
 %0A%7D%0A

Add a builder to hide the dependency on javax.resource.spi package