/*
Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
This file is part of the JODB (Java Objects Database) open source project.
JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.mobixess.jodb.tests.clientserver;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import com.mobixess.jodb.core.ISessionCloseListener;
import com.mobixess.jodb.core.ITransactionResolver;
import com.mobixess.jodb.core.JODB;
import com.mobixess.jodb.core.JODBConfig;
import com.mobixess.jodb.core.JODBServer;
import com.mobixess.jodb.core.JODBSessionContainer;
import com.mobixess.jodb.tests.SimpleAddTest;
import com.mobixess.jodb.tests.testobjects.ObjectA;
import com.mobixess.jodb.util.rmi.RegistryManager;
public class ServerClientConcurent {
private static int _testCounter =0;
private static String TEST_DATA_DIR = "./testData/IndexingTests/";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
runConcurentTest();
System.exit(0);
}
public static void runConcurentTest() throws Exception{
File testFileDir = new File(TEST_DATA_DIR);
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
if(testFile.exists()){
throw new Exception();
}
JODBConfig.setNewClientPerRequest(true);
JODBSessionContainer[] containers = getContainerForFile(testFile);
JODBSessionContainer sessionContainer0 = containers[0];
JODBSessionContainer sessionContainer1 = containers[1];
ResolverImpl resolver0 = (ResolverImpl) sessionContainer0.getTransactionResolver();
ResolverImpl resolver1 = (ResolverImpl) sessionContainer1.getTransactionResolver();
ObjectA objectA = new ObjectA((byte)0,(short)0,null);
sessionContainer0.set(objectA);
sessionContainer0.commit();
List list = sessionContainer1.getAllObjects();
ObjectA objectA2 = (ObjectA) list.get(0);
objectA.setVal2((short) 1);
sessionContainer0.set(objectA);
sessionContainer0.commit();
if(resolver0._resolveRequests.size()!=0 || resolver1._resolveRequests.size()!=0){
throw new RuntimeException();
}
objectA2.setVal2((short)3);
sessionContainer1.set(objectA2);
sessionContainer1.commit();
if(resolver1._resolveRequests.size()!=1){
throw new RuntimeException();
}
String[] regList = RegistryManager.getInstance().list();
for (int i = 0; i < regList.length; i++) {
System.err.println(regList[0]);
}
for (int i = containers.length-1; i >=0 ; --i) {
containers[i].close();
}
}
public static JODBSessionContainer[] getContainerForFile(File file) throws IOException
{
JODBSessionContainer[] sessionContainers = new JODBSessionContainer[2];
JODBServer server = JODB.openServer(file);
SessionCloseListenerImpl closeListener = new SessionCloseListenerImpl(server);
JODBSessionContainer container = (JODBSessionContainer) JODB.openClient("//127.0.0.1",null, "rw", new ResolverImpl());
container.addCloseListener(closeListener);
sessionContainers[0] = container;
container = (JODBSessionContainer) JODB.openClient("//127.0.0.1",null, "rw", new ResolverImpl());
sessionContainers[1] = container;
return sessionContainers;
}
private static class ResolverImpl implements ITransactionResolver{
Vector _resolveRequests = new Vector();
@SuppressWarnings("unchecked")
public boolean resolve(Object objectToResolve, JODBSessionContainer sessionContainer, IResolverContext resolverContext) throws IOException {
_resolveRequests.add(objectToResolve);
return true;
}
}
private static class SessionCloseListenerImpl implements ISessionCloseListener{
JODBServer _server;
/**
* @param server
*/
public SessionCloseListenerImpl(JODBServer server) {
super();
_server = server;
}
public void sessionClosed(JODBSessionContainer closedSession) {
try {
_server.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}