1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-10 09:22:49 +00:00

Support for H2 databases

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-27 00:13:59 -03:00
parent 81dea2def4
commit aabe375b49
45 changed files with 1585 additions and 236 deletions

View File

@@ -28,7 +28,7 @@ import com.l2jserver.service.core.LoggingService;
import com.l2jserver.service.core.vfs.VFSService;
import com.l2jserver.service.core.vfs.VFSServiceImpl;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.MySQLDatabaseService;
import com.l2jserver.service.database.JDBCDatabaseService;
import com.l2jserver.service.game.character.CharacterService;
import com.l2jserver.service.game.character.CharacterServiceImpl;
import com.l2jserver.service.game.chat.ChatService;
@@ -69,7 +69,7 @@ public class ServiceModule extends AbstractModule {
bind(ConfigurationService.class).to(ProxyConfigurationService.class)
.in(Scopes.SINGLETON);
bind(CacheService.class).to(EhCacheService.class).in(Scopes.SINGLETON);
bind(DatabaseService.class).to(MySQLDatabaseService.class).in(
bind(DatabaseService.class).to(JDBCDatabaseService.class).in(
Scopes.SINGLETON);
bind(WorldIDService.class).to(CachedWorldIDService.class).in(
Scopes.SINGLETON);

View File

@@ -24,6 +24,8 @@ import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.impl.DefaultFileSystemManager;
import org.apache.commons.vfs.impl.StandardFileSystemManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
@@ -35,6 +37,11 @@ import com.l2jserver.service.ServiceStopException;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class VFSServiceImpl extends AbstractService implements VFSService {
/**
* The logger
*/
private final Logger log = LoggerFactory.getLogger(this.getClass());
/**
* The CommonsVFS {@link FileSystemManager}
*/
@@ -62,6 +69,7 @@ public class VFSServiceImpl extends AbstractService implements VFSService {
try {
return manager.resolveFile(uri);
} catch (FileSystemException e) {
log.error("Error resolving file", e);
return null;
}
}

View File

@@ -1,42 +0,0 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database;
import java.io.File;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
/**
* Configuration for DB4O Database Service
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
@ConfigurationName("db4o")
public interface DB4ODatabaseConfiguration extends DatabaseConfiguration {
/**
* @return the database file
*/
@ConfigurationPropertyGetter(name = "db4o.file", defaultValue = "database.bin")
File getDatabaseFile();
/**
* @param jdbcUrl
* the new database file
*/
@ConfigurationPropertySetter(name = "db4o.file")
void setDatabaseFile(File file);
}

View File

@@ -1,53 +0,0 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database;
import com.l2jserver.service.AbstractService;
/**
* Database service implementation for DB4O database engine.
* <p>
* Note that this is not implemented yet!
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DB4ODatabaseService extends AbstractService implements
DatabaseService {
@Override
public Object getCachedObject(Object id) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasCachedObject(Object id) {
// TODO Auto-generated method stub
return false;
}
@Override
public void updateCache(Object key, Object value) {
// TODO Auto-generated method stub
}
@Override
public void removeCache(Object key) {
// TODO Auto-generated method stub
}
}

View File

@@ -16,8 +16,8 @@
*/
package com.l2jserver.service.database;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.l2jserver.model.Model;
import com.l2jserver.model.id.ID;
@@ -56,7 +56,7 @@ public interface DataAccessObject<O extends Model<?>, I extends ID<?>> extends
* @return the list containing all {@link ID} objects
*/
@IgnoreCaching
List<I> selectIDs();
Collection<I> selectIDs();
/**
* Save the instance to the database. If a new database entry was created

View File

@@ -25,40 +25,5 @@ import com.l2jserver.service.Service;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface DatabaseService extends Service {
/**
* Get the database object cached. Will return null if the object is not in
* cache
*
* @param id
* the object id
* @return the cached object
*/
Object getCachedObject(Object id);
/**
* Tests if the object is cached
*
* @param id
* the object id
* @return true if object is in cache
*/
boolean hasCachedObject(Object id);
/**
* Adds or update the cache object
*
* @param key
* the cache key
* @param value
* the object
*/
void updateCache(Object key, Object value);
/**
* Removes an object from the cache
*
* @param key
* the cache key
*/
void removeCache(Object key);
void install();
}

View File

@@ -17,60 +17,60 @@
package com.l2jserver.service.database;
/**
* Configuration interface for {@link MySQLDatabaseService}.
* Configuration interface for {@link JDBCDatabaseService}.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface MySQLDatabaseConfiguration extends DatabaseConfiguration {
public interface JDBCDatabaseConfiguration extends DatabaseConfiguration {
/**
* @return the jdbc url
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.url", defaultValue = "jdbc:mysql://localhost/l2jserver2")
@ConfigurationPropertyGetter(name = "jdbc.url", defaultValue = "jdbc:mysql://localhost/l2jserver2")
String getJdbcUrl();
/**
* @param jdbcUrl
* the new jdbc url
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.url")
@ConfigurationPropertySetter(name = "jdbc.url")
void setJdbcUrl(String jdbcUrl);
/**
* @return the jdbc driver class
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.driver", defaultValue = "com.mysql.jdbc.Driver")
@ConfigurationPropertyGetter(name = "jdbc.driver", defaultValue = "com.jdbc.jdbc.Driver")
String getDriver();
/**
* @param driver
* the new jdbc driver
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.driver")
@ConfigurationPropertySetter(name = "jdbc.driver")
void setDriver(Class<?> driver);
/**
* @return the mysql database username
* @return the jdbc database username
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.username", defaultValue = "l2j")
@ConfigurationPropertyGetter(name = "jdbc.username", defaultValue = "l2j")
String getUsername();
/**
* @param username
* the mysql database username
* the jdbc database username
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.username")
@ConfigurationPropertySetter(name = "jdbc.username")
void setUsername(String username);
/**
* @return the mysql database password
* @return the jdbc database password
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.password", defaultValue = "changeme")
@ConfigurationPropertyGetter(name = "jdbc.password", defaultValue = "changeme")
String getPassword();
/**
* @param password
* the mysql database password
* the jdbc database password
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.password")
@ConfigurationPropertySetter(name = "jdbc.password")
void setPassword(String password);
}

View File

@@ -16,10 +16,13 @@
*/
package com.l2jserver.service.database;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -34,6 +37,7 @@ import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
@@ -63,18 +67,18 @@ import com.l2jserver.util.factory.CollectionFactory;
*/
@Depends({ LoggingService.class, CacheService.class,
ConfigurationService.class, TemplateService.class })
public class MySQLDatabaseService extends AbstractService implements
public class JDBCDatabaseService extends AbstractService implements
DatabaseService {
/**
* The configuration object
*/
private final MySQLDatabaseConfiguration config;
private final JDBCDatabaseConfiguration config;
/**
* The logger
*/
private final Logger logger = LoggerFactory
.getLogger(MySQLDatabaseService.class);
// services
.getLogger(JDBCDatabaseService.class);
/**
* The cache service
*/
@@ -104,9 +108,9 @@ public class MySQLDatabaseService extends AbstractService implements
private Cache objectCache;
@Inject
public MySQLDatabaseService(ConfigurationService configService,
public JDBCDatabaseService(ConfigurationService configService,
CacheService cacheService) {
config = configService.get(MySQLDatabaseConfiguration.class);
config = configService.get(JDBCDatabaseConfiguration.class);
this.cacheService = cacheService;
}
@@ -129,6 +133,30 @@ public class MySQLDatabaseService extends AbstractService implements
cacheService.register(objectCache);
}
@Override
public void install() {
@SuppressWarnings("unchecked")
Collection<File> files = FileUtils.listFiles(new File("dist/sql/h2"),
new String[] { "sql" }, false);
try {
final Connection conn = dataSource.getConnection();
try {
for (final File file : files) {
conn.createStatement().execute(
FileUtils.readFileToString(file));
}
} finally {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
/**
* Executes an <tt>query</tt> in the database.
*
@@ -156,7 +184,6 @@ public class MySQLDatabaseService extends AbstractService implements
}
}
@Override
public Object getCachedObject(Object id) {
Preconditions.checkNotNull(id, "id");
final Element element = objectCache.get(id);
@@ -165,20 +192,17 @@ public class MySQLDatabaseService extends AbstractService implements
return element.getObjectValue();
}
@Override
public boolean hasCachedObject(Object id) {
Preconditions.checkNotNull(id, "id");
return objectCache.get(id) != null;
}
@Override
public void updateCache(Object key, Object value) {
Preconditions.checkNotNull(key, "key");
Preconditions.checkNotNull(value, "value");
objectCache.put(new Element(key, value));
}
@Override
public void removeCache(Object key) {
Preconditions.checkNotNull(key, "key");
objectCache.remove(key);
@@ -189,6 +213,7 @@ public class MySQLDatabaseService extends AbstractService implements
if (objectCache != null)
objectCache.dispose();
objectCache = null;
try {
if (connectionPool != null)
connectionPool.close();
@@ -473,7 +498,7 @@ public class MySQLDatabaseService extends AbstractService implements
/**
* The database service instance
*/
private final MySQLDatabaseService database;
private final JDBCDatabaseService database;
private final Mapper<I> idMapper;
@@ -483,7 +508,7 @@ public class MySQLDatabaseService extends AbstractService implements
* @param database
* the database service
*/
public CachedMapper(MySQLDatabaseService database, Mapper<I> idMapper) {
public CachedMapper(JDBCDatabaseService database, Mapper<I> idMapper) {
this.database = database;
this.idMapper = idMapper;
}

View File

@@ -16,7 +16,7 @@
*/
package com.l2jserver.service.game.npc;
import java.util.List;
import java.util.Collection;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction;
@@ -69,7 +69,7 @@ public interface NPCService extends Service {
* @throws SpawnPointNotFoundServiceException
* if one of the NPCs does not have an spawn point defined
*/
List<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
Collection<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException;
/**

View File

@@ -16,7 +16,7 @@
*/
package com.l2jserver.service.game.npc;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import com.google.common.base.Preconditions;
@@ -123,9 +123,9 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
}
@Override
public List<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
public Collection<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException {
final List<NPC> npcs = npcDao.loadAll();
final Collection<NPC> npcs = npcDao.loadAll();
for (final NPC npc : npcs) {
spawnService.spawn(npc, null);
}

View File

@@ -23,7 +23,7 @@ import com.l2jserver.service.configuration.Configuration.ConfigurationName;
@ConfigurationName("template")
public interface XMLTemplateServiceConfiguration extends Configuration {
@ConfigurationPropertyGetter(name = "template.directory", defaultValue = "zip:data/templates.zip")
@ConfigurationPropertyGetter(name = "template.directory", defaultValue = "data/templates")
URI getTemplateDirectory();
@ConfigurationPropertySetter(name = "template.directory")