1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +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

@@ -18,7 +18,7 @@ package com.l2jserver;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.l2jserver.db.dao.MySQL5DAOModule;
import com.l2jserver.db.dao.H2DAOModule;
import com.l2jserver.model.id.provider.IDProviderModule;
import com.l2jserver.service.ServiceModule;
@@ -32,6 +32,6 @@ public class GameServerModule extends AbstractModule {
protected void configure() {
install(new ServiceModule());
install(new IDProviderModule());
install(new MySQL5DAOModule());
install(new H2DAOModule());
}
}

View File

@@ -59,15 +59,11 @@ public class L2JGameServerMain {
// spawn
serviceManager.get(NPCService.class).spawnAll();
// staticSpawn(server.getInjector());
} catch (Exception e) {
System.out.println("GameServer could not be started!");
e.printStackTrace();
System.exit(0);
}
// Thread.sleep(60 * 60 * 1000);
}
//
// /**

View File

@@ -87,9 +87,7 @@ public class AuthLoginPacket extends AbstractClientPacket {
final List<L2Character> chars = characterDao.selectByAccount(accountId);
conn.write(CharacterSelectionListPacket.fromL2Session(
conn.getSession(), chars.get(0)));
// conn.setCharacterID(chars.get(0).getID());
// conn.write(new CharacterEnterWorldPacket(chars.get(0), playKey1));
conn.getSession(), chars.toArray(new L2Character[chars.size()])));
}
/**

View File

@@ -70,7 +70,7 @@ public class CharacterCreatePacket extends AbstractClientPacket {
/**
* The {@link CharacterTemplateID} factory
*/
private final CharacterTemplateIDProvider characterTemplateIdFactory;
private final CharacterTemplateIDProvider characterTemplateIdProvider;
// packet
/**
@@ -140,7 +140,7 @@ public class CharacterCreatePacket extends AbstractClientPacket {
CharacterTemplateIDProvider characterTemplateIdFactory) {
this.characterDao = characterDao;
this.characterIdFactory = characterIdFactory;
this.characterTemplateIdFactory = characterTemplateIdFactory;
this.characterTemplateIdProvider = characterTemplateIdFactory;
}
@Override
@@ -192,7 +192,7 @@ public class CharacterCreatePacket extends AbstractClientPacket {
}
// create template id and lookup for the template instance
final CharacterTemplateID templateId = characterTemplateIdFactory
final CharacterTemplateID templateId = characterTemplateIdProvider
.createID(characterClass.id);
final CharacterTemplate template = templateId.getTemplate();
log.debug("Creating character with template {}", template);

View File

@@ -76,13 +76,13 @@ public class RequestCharacterTemplatesPacket extends AbstractClientPacket {
@Override
public void process(final Lineage2Connection conn) {
log.debug("Requested character templates");
final CharacterTemplate[] templates = new CharacterTemplate[TEMPLATE_CLASSES.length];
int i = 0;
for (final CharacterClass charClass : TEMPLATE_CLASSES) {
final CharacterTemplateID id = idFactory.createID(charClass.id);
final CharacterTemplate template = id.getTemplate();
final CharacterTemplatePacket templatePacket = new CharacterTemplatePacket(
template);
conn.write(templatePacket);
templates[i++] = id.getTemplate();
}
conn.write(new CharacterTemplatePacket(templates));
}
}

View File

@@ -32,7 +32,7 @@ public abstract class AbstractModel<T extends ID<?>> implements Model<T> {
/**
* The database object state
*/
protected ObjectState state = ObjectState.NOT_STORED;
protected transient ObjectState state = ObjectState.NOT_STORED;
@Override
public T getID() {

View File

@@ -31,7 +31,7 @@ public final class CharacterID extends ActorID<L2Character> {
/**
* Data Access Object (DAO) for characters
*/
private final CharacterDAO characterDao;
private transient final CharacterDAO characterDao;
@Inject
public CharacterID(@Assisted int id, CharacterDAO characterDao) {

View File

@@ -29,11 +29,12 @@ import com.l2jserver.service.game.template.TemplateService;
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ActorTemplateID<T extends ActorTemplate<?>> extends TemplateID<T, Integer> {
public class ActorTemplateID<T extends ActorTemplate<?>> extends
TemplateID<T, Integer> {
/**
* The template service
*/
private final TemplateService templateService;
private transient final TemplateService templateService;
@Inject
protected ActorTemplateID(@Assisted int id, TemplateService templateService) {

View File

@@ -30,7 +30,7 @@ import com.l2jserver.service.game.template.TemplateService;
*/
public class CharacterTemplateID extends ActorTemplateID<CharacterTemplate> {
@Inject
protected CharacterTemplateID(@Assisted int id,
public CharacterTemplateID(@Assisted int id,
TemplateService templateService) {
super(id, templateService);
}

View File

@@ -31,7 +31,8 @@ import com.l2jserver.model.world.character.CharacterClass;
import com.l2jserver.util.jaxb.CharacterTemplateIDAdapter;
/**
* The {@link L2Character} template. Each character instance is backed by an template.
* The {@link L2Character} template. Each character instance is backed by an
* template.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
@@ -43,9 +44,6 @@ public class CharacterTemplate extends ActorTemplate<L2Character> {
@XmlAttribute(name = "class")
protected CharacterTemplateID id = null;
@XmlAttribute(name = "class")
protected CharacterClass characterClass = null;
@XmlElement(name = "stats")
protected CharacterStatsMetadata stats = null;
@@ -181,14 +179,14 @@ public class CharacterTemplate extends ActorTemplate<L2Character> {
* @return the character Class
*/
public CharacterClass getCharacterClass() {
return characterClass;
return CharacterClass.fromID(id.getID());
}
/**
* @return the character race
*/
public ActorRace getRace() {
return characterClass.race;
return getCharacterClass().race;
}
/**

View File

@@ -97,11 +97,6 @@ public class L2Character extends Player {
*/
private Date lastAccess;
/**
* The character stat
*/
private final CharacterStats stats = new CharacterStats(this);
/**
* The character karma points
*/
@@ -118,12 +113,16 @@ public class L2Character extends Player {
// ////////////////////////////////////
// / RUNTIME
// ////////////////////////////////////
/**
* The character stat
*/
private transient final CharacterStats stats = new CharacterStats(this);
/**
* The character walk mode.
* <p>
* This field is not persisted.
*/
private CharacterMoveType moveType = CharacterMoveType.WALK;
private transient CharacterMoveType moveType = CharacterMoveType.WALK;
/**
* The character walking mode
@@ -143,11 +142,11 @@ public class L2Character extends Player {
/**
* The character target, if any.
*/
private ActorID<?> targetID;
private transient ActorID<?> targetID;
/**
* State of the character. Will be null if it is idle
*/
private CharacterState state;
private transient CharacterState state;
/**
* The valid states for an character
@@ -180,7 +179,7 @@ public class L2Character extends Player {
/**
* The point the player is moving, teleporting etc...
*/
private Point targetLocation;
private transient Point targetLocation;
/**
* Creates a new instance

View File

@@ -16,8 +16,8 @@
*/
package com.l2jserver.model.world.character;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.l2jserver.model.world.Item;
@@ -76,7 +76,7 @@ public class CharacterInventory implements Iterable<Item> {
* @param items
* the items to be added
*/
public void load(List<Item> items) {
public void load(Collection<Item> items) {
this.items.clear();
this.items.addAll(items);
}

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")

View File

@@ -71,8 +71,9 @@ public class XMLMappingTest {
//
// System.out.println("Took " + ((end - start) / 1000) + " seconds");
final NPCTemplate t = (NPCTemplate) u
.unmarshal(new File("data/templates/npc/teleporter/30059-Trisha.xml"));
System.out.println(t.getControllerClass());
final CharacterTemplate t = (CharacterTemplate) u.unmarshal(new File(
"data/templates/character/HumanFighter.xml"));
System.out.println(t.getID());
System.out.println(t.getCharacterClass());
}
}