Commit e2d2dd18 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

stub ofseperate databases

parent 2e7589af
......@@ -22,7 +22,6 @@ import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.ektorp.CouchDbConnector;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import java.io.IOException;
......@@ -73,7 +72,6 @@ public class CTFApplication extends Application<ApplicationConfiguration>
{
injector = createInjector(applicationConfiguration);
CouchDbConnector connector = injector.getInstance(CouchDbConnector.class);
Team team = new Team();
team.setName("asdf");
......
package com.telephoners.krakyournet.ctf.core;
import com.google.inject.Inject;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.support.CouchDbDocument;
import javax.inject.Singleton;
@Singleton
public class DataConnector<T extends CouchDbDocument>
{
private final CouchDbConnector connector;
@Inject
public DataConnector(final CouchDbInstance databaseInstance, final DatabaseNameProvider<T> databaseNameProvider)
{
//todo: temporary
String databaseName = databaseNameProvider.get();
// databaseInstance.deleteDatabase(databaseName);
final StdCouchDbConnector connector = new StdCouchDbConnector(databaseName, databaseInstance); //todo: to configuration
connector.createDatabaseIfNotExists();
//todo: as task while application start (configurable)
this.connector = connector;
}
//todo: name
public CouchDbConnector getConnector()
{
return connector;
}
}
package com.telephoners.krakyournet.ctf.core;
import com.google.inject.Inject;
import org.ektorp.support.CouchDbDocument;
public class DatabaseNameProvider<T extends CouchDbDocument>
{
private Class<T> type;
@Inject
public DatabaseNameProvider(final Class<T> type)
{
this.type = type;
}
public String get()
{
return String.format("%s_database", formatName(type.getTypeName()));
}
private String formatName(final String className)
{
return className.replace(".","_");
}
}
......@@ -6,11 +6,9 @@ import com.google.inject.TypeLiteral;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import java.net.MalformedURLException;
......@@ -34,16 +32,19 @@ public class ApplicationModule extends AbstractModule
bind(new TypeLiteral<Class<Team>>(){}).toInstance(Team.class);
}
@Provides
//todo: reuqires seperate connectors for each repository
// @Provides
//todo: remove
//todo: hide behind interface
public CouchDbConnector provideDatastore(final CouchDbInstance databaseInstance)
/*public CouchDbConnector provideDatastore(final CouchDbInstance databaseInstance)
{
databaseInstance.deleteDatabase("database"); //todo: temp
final StdCouchDbConnector database = new StdCouchDbConnector("database", databaseInstance); //todo: to configuration
database.createDatabaseIfNotExists();
//todo: as task while application start (configurable)
return database;
}
}*/
@Provides
public CouchDbInstance provideDatabaseInstance(final HttpClient httpClient)
......
package com.telephoners.krakyournet.ctf.repositories;
import com.google.inject.Inject;
import org.ektorp.CouchDbConnector;
import com.google.inject.Singleton;
import com.telephoners.krakyournet.ctf.core.DataConnector;
import org.ektorp.support.CouchDbDocument;
import org.ektorp.support.CouchDbRepositorySupport;
//@Singleton
@Singleton
public class Repository<T extends CouchDbDocument> extends CouchDbRepositorySupport<T>
{
@Inject
public Repository(final Class<T> type, final CouchDbConnector connector)
public Repository(final Class<T> type, final DataConnector<T> connector)
{
super(type, connector);
super(type, connector.getConnector());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment