// Path: couchbase-as-a-datasource/src/main/java/org/teiid/example/JDBCUtils.java
// public static void execute(Connection connection, String sql) throws Exception {
//     execute(connection, sql, false);
// }


import org.teiid.translator.file.FileExecutionFactory;
import org.teiid.translator.jdbc.h2.H2ExecutionFactory;
import org.teiid.transport.SocketConfiguration;
import org.teiid.transport.WireProtocol;
import static org.teiid.example.JDBCUtils.execute;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.sql.DataSource;
import org.h2.tools.RunScript;
import org.teiid.resource.adapter.file.FileManagedConnectionFactory;
import org.teiid.runtime.EmbeddedConfiguration;
import org.teiid.runtime.EmbeddedServer;
/*
 * Copyright Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags and
 * the COPYRIGHT.txt file distributed with this work.
 *
 * 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.teiid.example;






/**
 * This example is same as "embedded-portfolio" example, except this demonstrates exposing teiid embedded
 * using sockets, so that a client application running in another VM can connect.
 * 
 * This example will start teiid embedded as the "Server" and then start a "Client" thread that will connect to 
 * the server and issue the same queries issued in the "embedded-portfolio" example.
 */
@SuppressWarnings("nls")
public class TeiidEmbeddedPortfolio {
	
	
	public static void main(String[] args) throws Exception {
		Server s = new Server();
		new Thread(s).start();

		Thread.sleep(10000);
		
		Client c = new Client();
		new Thread(c).start();

	}


}

final class Server implements Runnable {

	/**
	 * {@inheritDoc}
	 *
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run()  {
		
		try {
			SocketConfiguration s = new SocketConfiguration();
			s.setBindAddress("localhost");
			s.setPortNumber(31000);
			s.setProtocol(WireProtocol.teiid);
			
			DataSource ds = EmbeddedHelper.newDataSource("org.h2.Driver", "jdbc:h2:mem://localhost/~/account", "sa", "sa");
// Path: couchbase-as-a-datasource/src/main/java/org/teiid/example/JDBCUtils.java
// public static void execute(Connection connection, String sql) throws Exception {
//     execute(connection, sql, false);
// }


// Path: embedded-portfolio-sockets/src/main/java/org/teiid/example/TeiidEmbeddedPortfolio.java
import org.teiid.translator.file.FileExecutionFactory;
import org.teiid.translator.jdbc.h2.H2ExecutionFactory;
import org.teiid.transport.SocketConfiguration;
import org.teiid.transport.WireProtocol;
import static org.teiid.example.JDBCUtils.execute;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.sql.DataSource;
import org.h2.tools.RunScript;
import org.teiid.resource.adapter.file.FileManagedConnectionFactory;
import org.teiid.runtime.EmbeddedConfiguration;
import org.teiid.runtime.EmbeddedServer;

/*
 * Copyright Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags and
 * the COPYRIGHT.txt file distributed with this work.
 *
 * 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.teiid.example;






/**
 * This example is same as "embedded-portfolio" example, except this demonstrates exposing teiid embedded
 * using sockets, so that a client application running in another VM can connect.
 * 
 * This example will start teiid embedded as the "Server" and then start a "Client" thread that will connect to 
 * the server and issue the same queries issued in the "embedded-portfolio" example.
 */
@SuppressWarnings("nls")
public class TeiidEmbeddedPortfolio {
	
	
	public static void main(String[] args) throws Exception {
		Server s = new Server();
		new Thread(s).start();

		Thread.sleep(10000);
		
		Client c = new Client();
		new Thread(c).start();

	}


}

final class Server implements Runnable {

	/**
	 * {@inheritDoc}
	 *
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run()  {
		
		try {
			SocketConfiguration s = new SocketConfiguration();
			s.setBindAddress("localhost");
			s.setPortNumber(31000);
			s.setProtocol(WireProtocol.teiid);
			
			DataSource ds = EmbeddedHelper.newDataSource("org.h2.Driver", "jdbc:h2:mem://localhost/~/account", "sa", "sa");
			RunScript.execute(ds.getConnection(), new InputStreamReader(TeiidEmbeddedPortfolio.class.getClassLoader().getResourceAsStream("data/customer-schema.sql")));