1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-05 23:22:47 +00:00

Removed "core" project dependency

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-13 17:12:29 -03:00
parent bb3f24e8f4
commit 73d793c28c
47 changed files with 1402 additions and 39 deletions

View File

@@ -7,7 +7,7 @@
<classpathentry kind="src" path="data/script/template"/>
<classpathentry kind="src" path="data/script/quest"/>
<classpathentry kind="src" path="data/script/ai"/>
<classpathentry kind="src" path=" data/plugin"/>
<classpathentry kind="src" path="data/plugin"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="src" path="src/tool/java"/>

View File

@@ -0,0 +1,4 @@
#Fri May 13 16:44:30 BRT 2011
default.configuration=
eclipse.preferences.version=1
hibernate3.enabled=false

View File

@@ -10,6 +10,8 @@ import com.google.inject.Inject;
import com.l2jserver.model.template.Template;
import com.l2jserver.service.game.scripting.classlistener.Loader;
import com.l2jserver.service.game.scripting.classlistener.Unloader;
import com.l2jserver.service.game.template.StaticTemplateService;
import com.l2jserver.service.game.template.TemplateService;
import com.l2jserver.util.ClassUtils;
import com.l2jserver.util.factory.CollectionFactory;
@@ -24,25 +26,27 @@ public class PluginLoader implements Loader, Unloader {
private static final Logger log = LoggerFactory
.getLogger(PluginLoader.class);
private final StaticTemplateService templateService;
@Inject
public PluginLoader() {
public PluginLoader(TemplateService templateService) {
this.templateService = (StaticTemplateService) templateService;
}
@Override
public void load(Class<?>[] classes) {
log.debug("Loading plugins from {} classes", classes.length);
log.debug("Loading templates from {} classes", classes.length);
for (final Class<? extends Template<?>> template : getSuitableClasses(classes)) {
log.debug("Found loadable plugin class: {}", template);
//templateService.addTemplate(template);
log.debug("Found loadable template class: {}", template);
templateService.addTemplate(template);
}
}
@Override
public void unload(Class<?>[] classes) {
log.debug("Unloading plugins from {} classes", classes.length);
log.debug("Unloading templates from {} classes", classes.length);
for (final Class<? extends Template<?>> template : getSuitableClasses(classes)) {
log.debug("Found unloadable plugin class: {}", template);
log.debug("Found unloadable template class: {}", template);
// TODO unloading
}
}

141
pom.xml
View File

@@ -4,18 +4,151 @@
<groupId>com.l2jserver</groupId>
<artifactId>l2jserver2-gameserver</artifactId>
<version>2.0.0-DEVEL</version>
<name>L2JServer - Game Server Module</name>
<name>L2JServer - Game Server</name>
<description>Lineage II server emulator</description>
<inceptionYear>2011</inceptionYear>
<dependencies>
<dependency>
<groupId>com.l2jserver</groupId>
<artifactId>l2jserver2-core</artifactId>
<version>2.0.0-DEVEL</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.4.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javacc</groupId>
<artifactId>javacc</artifactId>
<version>4.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.kenai.nbpwr</groupId>
<artifactId>org-apache-commons-dbcp</artifactId>
<version>1.2.2-201002241055</version>
<type>nbm</type>
<scope>compile</scope>
</dependency>
</dependencies>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/Rogiel/l2jserver2-gs/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub</system>
<url>https://github.com/Rogiel/l2jserver2-gs/wiki</url>
</ciManagement>
<developers>
<developer>
<id>rogiel</id>
<name>Rogiel</name>
<email>rogiel@rogiel.com</email>
<url>http://www.rogiel.com</url>
<timezone>GMT - 3</timezone>
<roles>
<role>Creator</role>
<role>API Designer</role>
</roles>
</developer>
</developers>
<contributors>
</contributors>
<scm>
<connection>git://github.com/Rogiel/l2jserver2-gs.git</connection>
<developerConnection>ssh://git@github.com:Rogiel/l2jserver2-gs.git</developerConnection>
<tag>master</tag>
<url>https://github.com/Rogiel/l2jserver2-gs</url>
</scm>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repository.jboss.org</id>

View File

@@ -4,13 +4,11 @@ import com.google.inject.AbstractModule;
import com.l2jserver.db.dao.DAOModuleMySQL5;
import com.l2jserver.model.id.factory.IDFactoryModule;
import com.l2jserver.routines.GameServerInitializationRoutine;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceModule;
public class GameServerModule extends AbstractModule {
@Override
protected void configure() {
install(new BasicServiceModule());
install(new ServiceModule());
install(new IDFactoryModule());
install(new DAOModuleMySQL5());

View File

@@ -0,0 +1,6 @@
package com.l2jserver.routines;
import java.util.concurrent.Callable;
public interface Routine<T> extends Callable<T> {
}

View File

@@ -0,0 +1,17 @@
package com.l2jserver.service;
public class AbstractService implements Service {
@Override
public void start() throws ServiceStartException {
}
@Override
public void stop() throws ServiceStopException {
}
@Override
public void restart() throws ServiceException {
this.stop();
this.start();
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.service;
public interface Service {
/**
* Start this service
*
* @throws ServiceStartException
* if an error occurred
*/
void start() throws ServiceStartException;
/**
* Stop this service
*
* @throws ServiceStopException
* if an error occurred
*/
void stop() throws ServiceStopException;
/**
* Stop this service
*
* @throws ServiceException
* if an error occurred
*/
void restart() throws ServiceException;
}

View File

@@ -0,0 +1,21 @@
package com.l2jserver.service;
public class ServiceException extends Exception {
private static final long serialVersionUID = 1L;
public ServiceException() {
super();
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
public ServiceException(String message) {
super(message);
}
public ServiceException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,93 @@
package com.l2jserver.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.l2jserver.service.logging.LoggingService;
public class ServiceManager {
/**
* The logger
*/
private final Logger logger;
/**
* The Guice Injector
*/
private final Injector injector;
@Inject
public ServiceManager(Injector injector) {
this.injector = injector;
final LoggingService service = injector
.getInstance(LoggingService.class);
try {
service.start();
} catch (ServiceStartException e) {
throw new RuntimeException(e);
}
logger = LoggerFactory.getLogger(ServiceManager.class);
}
public <T extends Service> T start(Class<T> serviceClass)
throws ServiceStartException {
final T service = injector.getInstance(serviceClass);
if (service == null)
return null;
try {
logger.info("{}: Starting service...",
serviceClass.getCanonicalName());
service.start();
logger.info("{}: Service started!",
serviceClass.getCanonicalName());
return service;
} catch (ServiceStartException e) {
logger.error("{}: Error starting service: {}",
serviceClass.getCanonicalName(), e.getCause());
throw e;
}
}
public void stop(Class<? extends Service> serviceClass)
throws ServiceStopException {
final Service service = injector.getInstance(serviceClass);
if (service == null)
return;
try {
logger.info("{0}: Stopping service...",
serviceClass.getCanonicalName());
service.stop();
logger.info("{0}: Service stopped!",
serviceClass.getCanonicalName());
} catch (ServiceStopException e) {
logger.error("{0}: Error stopping service: {1}",
serviceClass.getCanonicalName(), e.getCause());
throw e;
}
}
public <T extends Service> T restart(Class<T> serviceClass)
throws ServiceStartException, ServiceStopException {
final T service = injector.getInstance(serviceClass);
if (service == null)
return null;
try {
logger.info("{0}: Restaring service...",
serviceClass.getCanonicalName());
service.stop();
service.start();
logger.info("{0}: Service restarted!",
serviceClass.getCanonicalName());
return service;
} catch (ServiceStartException e) {
logger.error("{0}: Error starting service: {1}",
serviceClass.getCanonicalName(), e.getCause());
throw e;
} catch (ServiceStopException e) {
logger.error("{0}: Error stopping service: {1}",
serviceClass.getCanonicalName(), e.getCause());
throw e;
}
}
}

View File

@@ -2,6 +2,10 @@ package com.l2jserver.service;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.configuration.ProxyConfigurationService;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.MySQLDatabaseService;
import com.l2jserver.service.game.scripting.ScriptingService;
import com.l2jserver.service.game.scripting.ScriptingServiceImpl;
import com.l2jserver.service.game.template.StaticTemplateService;
@@ -10,12 +14,22 @@ import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.WorldServiceImpl;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.game.world.event.WorldEventDispatcherImpl;
import com.l2jserver.service.logging.Log4JLoggingService;
import com.l2jserver.service.logging.LoggingService;
import com.l2jserver.service.network.NettyNetworkService;
import com.l2jserver.service.network.NetworkService;
public class ServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(ServiceManager.class).in(Scopes.SINGLETON);
bind(LoggingService.class).to(Log4JLoggingService.class).in(
Scopes.SINGLETON);
bind(ConfigurationService.class).to(ProxyConfigurationService.class)
.in(Scopes.SINGLETON);
bind(DatabaseService.class).to(MySQLDatabaseService.class).in(
Scopes.SINGLETON);
bind(NetworkService.class).to(NettyNetworkService.class).in(
Scopes.SINGLETON);
bind(ScriptingService.class).to(ScriptingServiceImpl.class).in(

View File

@@ -0,0 +1,21 @@
package com.l2jserver.service;
public class ServiceRestartException extends ServiceException {
private static final long serialVersionUID = 1L;
public ServiceRestartException() {
super();
}
public ServiceRestartException(String message, Throwable cause) {
super(message, cause);
}
public ServiceRestartException(String message) {
super(message);
}
public ServiceRestartException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,21 @@
package com.l2jserver.service;
public class ServiceStartException extends ServiceException {
private static final long serialVersionUID = 1L;
public ServiceStartException() {
super();
}
public ServiceStartException(String message, Throwable cause) {
super(message, cause);
}
public ServiceStartException(String message) {
super(message);
}
public ServiceStartException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,21 @@
package com.l2jserver.service;
public class ServiceStopException extends ServiceException {
private static final long serialVersionUID = 1L;
public ServiceStopException() {
super();
}
public ServiceStopException(String message, Throwable cause) {
super(message, cause);
}
public ServiceStopException(String message) {
super(message);
}
public ServiceStopException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,29 @@
package com.l2jserver.service.configuration;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public interface Configuration {
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(value = ElementType.TYPE)
public @interface ConfigurationName {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ConfigurationPropertyGetter {
String name();
String defaultValue() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ConfigurationPropertySetter {
String name();
}
}

View File

@@ -0,0 +1,7 @@
package com.l2jserver.service.configuration;
import com.l2jserver.service.Service;
public interface ConfigurationService extends Service {
<C extends Configuration> C get(Class<C> config);
}

View File

@@ -0,0 +1,185 @@
package com.l2jserver.service.configuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
import com.l2jserver.service.configuration.Configuration.ConfigurationPropertyGetter;
import com.l2jserver.service.configuration.Configuration.ConfigurationPropertySetter;
import com.l2jserver.util.transformer.Transformer;
import com.l2jserver.util.transformer.TransformerFactory;
public class ProxyConfigurationService extends AbstractService implements
ConfigurationService {
private File directory = new File("./config");
private final Logger logger = LoggerFactory
.getLogger(ProxyConfigurationService.class);
private Map<Class<?>, Object> cache = new WeakHashMap<Class<?>, Object>();
@Override
public void start() throws ServiceStartException {
if (!directory.exists())
directory.mkdirs();
}
@Override
@SuppressWarnings("unchecked")
public <C extends Configuration> C get(Class<C> config) {
if (cache.containsKey(config))
return (C) cache.get(config);
logger.info("Trying to create {} proxy", config);
Properties properties;
try {
properties = findProperties(config);
} catch (IOException e) {
properties = new Properties();
logger.info(
"Configuration file for {} not found, falling back to default values",
config);
}
C proxy = (C) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class<?>[] { config }, new ConfigInvocationHandler(
properties));
cache.put(config, proxy);
return proxy;
}
private class ConfigInvocationHandler implements InvocationHandler {
private final Properties properties;
private Map<String, Object> cache = new WeakHashMap<String, Object>();
public ConfigInvocationHandler(Properties properties) {
this.properties = properties;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
logger.debug("Configuration service, method invoked: {}", method.getName());
if (args == null || args.length == 0) {
final ConfigurationPropertyGetter getter = method
.getAnnotation(ConfigurationPropertyGetter.class);
if (getter == null)
return null;
return get(getter, method.getReturnType());
} else if (args.length == 1) {
final ConfigurationPropertySetter setter = method
.getAnnotation(ConfigurationPropertySetter.class);
if (setter == null)
return null;
set(setter, args[0], method.getParameterTypes()[0]);
}
return null;
}
private Object get(ConfigurationPropertyGetter getter, Class<?> type) {
if (cache.containsKey(getter.name()))
return cache.get(getter.name());
Object o = untransform(
getRaw(getter.name(), getter.defaultValue()), type);
cache.put(getter.name(), o);
return o;
}
private void set(ConfigurationPropertySetter setter, Object value,
Class<?> type) {
if (value != null) {
properties.setProperty(setter.name(),
transform(value.toString(), type));
cache.remove(setter.name());
} else {
properties.remove(setter.name());
cache.remove(setter.name());
}
}
private Object untransform(String value, Class<?> type) {
if (value == null)
return null;
if (type == String.class)
return value;
final Transformer<?> transformer = TransformerFactory
.getTransfromer(type);
if (transformer == null)
return null;
return transformer.untransform(value);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private String transform(Object value, Class<?> type) {
if (value == null)
return null;
if (value instanceof String)
return (String) value;
final Transformer transformer = TransformerFactory
.getTransfromer(type);
if (transformer == null)
return null;
return transformer.transform(value);
}
private String getRaw(String key, String defaultValue) {
if (properties == null)
return defaultValue;
if (properties.containsKey(key)) {
return (String) properties.get(key);
}
return defaultValue;
}
}
private Properties findProperties(Class<?> clazz) throws IOException {
ConfigurationName config = findAnnotation(ConfigurationName.class,
clazz);
if (config == null)
return null;
final Properties prop = new Properties();
final File file = new File(directory, config.value() + ".properties");
final InputStream in = new FileInputStream(file);
try {
prop.load(in);
} finally {
in.close();
}
return prop;
}
private <T extends Annotation> T findAnnotation(Class<T> annotationClass,
Class<?> clazz) {
T ann = clazz.getAnnotation(annotationClass);
if (ann != null)
return ann;
for (Class<?> clazz2 : annotationClass.getInterfaces()) {
if (clazz2 == clazz)
continue;
ann = findAnnotation(annotationClass, clazz2);
if (ann != null)
return ann;
}
return null;
}
public File getDirectory() {
return directory;
}
public void setDirectory(File directory) {
this.directory = directory;
}
}

View File

@@ -0,0 +1,16 @@
package com.l2jserver.service.database;
import com.google.inject.Inject;
public abstract class AbstractDAO<T> implements DataAccessObject<T> {
protected final DatabaseService database;
@Inject
protected AbstractDAO(DatabaseService database) {
this.database = database;
}
public DatabaseService getDatabase() {
return database;
}
}

View File

@@ -0,0 +1,21 @@
package com.l2jserver.service.database;
import java.io.File;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
@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

@@ -0,0 +1,7 @@
package com.l2jserver.service.database;
import com.l2jserver.service.AbstractService;
public class DB4ODatabaseService extends AbstractService implements
DatabaseService {
}

View File

@@ -0,0 +1,17 @@
package com.l2jserver.service.database;
/**
* The DAO interface
* <p>
* TODO make DAO an {@link Iterable}. So if we want to select all objects we can
* do like this: <code><pre>
* for(final Object o : daoInstance) {
* ...
* }
* </pre></code>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface DataAccessObject<T> {
}

View File

@@ -0,0 +1,9 @@
package com.l2jserver.service.database;
import com.l2jserver.service.configuration.Configuration;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
@ConfigurationName("database")
public interface DatabaseConfiguration extends Configuration {
}

View File

@@ -0,0 +1,7 @@
package com.l2jserver.service.database;
import com.l2jserver.service.Service;
public interface DatabaseService extends Service {
}

View File

@@ -0,0 +1,55 @@
package com.l2jserver.service.database;
public interface MySQLDatabaseConfiguration extends DatabaseConfiguration {
/**
* @return the jdbc url
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.url", defaultValue = "jdbc:mysql://localhost/l2jserver2")
String getJdbcUrl();
/**
* @param jdbcUrl
* the new jdbc url
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.url")
void setJdbcUrl(String jdbcUrl);
/**
* @return the jdbc driver class
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.driver", defaultValue = "com.mysql.jdbc.Driver")
String getDriver();
/**
* @param driver
* the new jdbc driver
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.driver")
void setDriver(Class<?> driver);
/**
* @return the mysql database username
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.username", defaultValue = "l2j")
String getUsername();
/**
* @param username
* the mysql database username
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.username")
void setUsername(String username);
/**
* @return the mysql database password
*/
@ConfigurationPropertyGetter(name = "jdbc.mysql.password", defaultValue = "changeme")
String getPassword();
/**
* @param password
* the mysql database password
*/
@ConfigurationPropertySetter(name = "jdbc.mysql.password")
void setPassword(String password);
}

View File

@@ -0,0 +1,177 @@
package com.l2jserver.service.database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
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.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.util.ArrayIterator;
import com.l2jserver.util.factory.CollectionFactory;
public class MySQLDatabaseService extends AbstractService implements
DatabaseService {
private final MySQLDatabaseConfiguration config;
private final Logger logger = LoggerFactory
.getLogger(MySQLDatabaseService.class);
private ObjectPool connectionPool;
private ConnectionFactory connectionFactory;
@SuppressWarnings("unused")
private PoolableConnectionFactory poolableConnectionFactory;
private PoolingDataSource dataSource;
@Inject
public MySQLDatabaseService(ConfigurationService configService) {
config = configService.get(MySQLDatabaseConfiguration.class);
}
@Override
public void start() throws ServiceStartException {
connectionPool = new GenericObjectPool(null);
connectionFactory = new DriverManagerConnectionFactory(
config.getJdbcUrl(), config.getUsername(), config.getPassword());
poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, connectionPool, null, null, false, true);
dataSource = new PoolingDataSource(connectionPool);
}
public <T> T query(Query<T> query) {
try {
final Connection conn = dataSource.getConnection();
try {
return query.query(conn);
} catch (SQLException e) {
logger.error("Error executing query", e);
return null;
} finally {
conn.close();
}
} catch (SQLException e) {
logger.error("Could not open database connection", e);
return null;
}
}
@Override
public void stop() throws ServiceStopException {
try {
if (connectionPool != null)
connectionPool.close();
} catch (Exception e) {
logger.error("Error stopping database service", e);
throw new ServiceStopException(e);
} finally {
connectionPool = null;
connectionFactory = null;
poolableConnectionFactory = null;
dataSource = null;
}
}
public interface Query<R> {
R query(Connection conn) throws SQLException;
}
public static abstract class InsertUpdateQuery<T> implements Query<Integer> {
private final Iterator<T> iterator;
public InsertUpdateQuery(T... objects) {
this(new ArrayIterator<T>(objects));
}
public InsertUpdateQuery(Iterator<T> iterator) {
this.iterator = iterator;
}
@Override
public Integer query(Connection conn) throws SQLException {
int rows = 0;
while (iterator.hasNext()) {
final T object = iterator.next();
final PreparedStatement st = conn.prepareStatement(query());
this.parametize(st, object);
rows += st.executeUpdate();
final Mapper<T> mapper = keyMapper(object);
if (mapper == null)
continue;
final ResultSet rs = st.getGeneratedKeys();
while (rs.next()) {
mapper.map(rs);
}
}
return rows;
}
protected abstract String query();
protected abstract void parametize(PreparedStatement st, T object)
throws SQLException;
protected Mapper<T> keyMapper(T object) {
return null;
}
}
public static abstract class SelectListQuery<T> implements Query<List<T>> {
@Override
public List<T> query(Connection conn) throws SQLException {
final PreparedStatement st = conn.prepareStatement(query());
parametize(st);
st.execute();
final List<T> list = CollectionFactory.newList(null);
final ResultSet rs = st.getResultSet();
while (rs.next()) {
list.add(mapper().map(rs));
}
return list;
}
protected abstract String query();
protected void parametize(PreparedStatement st) throws SQLException {
}
protected abstract Mapper<T> mapper();
}
public static abstract class SelectSingleQuery<T> implements Query<T> {
@Override
public T query(Connection conn) throws SQLException {
final PreparedStatement st = conn.prepareStatement(query());
parametize(st);
st.execute();
final ResultSet rs = st.getResultSet();
while (rs.next()) {
return mapper().map(rs);
}
return null;
}
protected abstract String query();
protected abstract void parametize(PreparedStatement st)
throws SQLException;
protected abstract Mapper<T> mapper();
}
public interface Mapper<T> {
T map(ResultSet rs) throws SQLException;
}
}

View File

@@ -0,0 +1,14 @@
package com.l2jserver.service.logging;
import org.apache.log4j.BasicConfigurator;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStopException;
public class Log4JLoggingService extends AbstractService implements
LoggingService {
@Override
public void stop() throws ServiceStopException {
BasicConfigurator.configure();
}
}

View File

@@ -0,0 +1,13 @@
package com.l2jserver.service.logging;
import org.slf4j.Logger;
import com.l2jserver.service.Service;
/**
* The logging service is used to get instances of the {@link Logger} class.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface LoggingService extends Service {
}

View File

@@ -0,0 +1,31 @@
package com.l2jserver.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayIterator<T> implements Iterator<T> {
private final T[] objects;
private int i = 0;
public ArrayIterator(T... objects) {
this.objects = objects;
}
@Override
public boolean hasNext() {
return i < objects.length;
}
@Override
public T next() {
try {
return objects[i++];
} catch (ArrayIndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
}
}

View File

@@ -0,0 +1,92 @@
package com.l2jserver.util.factory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Factory class to create {@link Collection} instances.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CollectionFactory {
/**
* Creates a new list of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created list
*/
public static final <T> List<T> newList(Class<T> type) {
return new ArrayList<T>();
}
/**
* Creates a new set of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created set
*/
public static final <T> Set<T> newSet(Class<T> type) {
return new HashSet<T>();
}
/**
* Creates a new concurrent queue of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created queue
*/
public static final <T> Queue<T> newConcurrentQueue(Class<T> type) {
return new ConcurrentLinkedQueue<T>();
}
/**
* Creates a new map.
*
* @param <K>
* the key type
* @param <V>
* the value type
* @param key
* the key type class
* @param value
* the value type class
* @return the new map
*/
public static final <K, V> Map<K, V> newMap(Class<K> key, Class<V> value) {
return new HashMap<K, V>();
}
/**
* Creates a new weak map.
*
* @param <K>
* the key type
* @param <V>
* the value type
* @param key
* the key type class
* @param value
* the value type class
* @return the new map
*/
public static final <K, V> Map<K, V> newWeakMap(Class<K> key, Class<V> value) {
return new WeakHashMap<K, V>();
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer;
/**
* An transformer can transform an {@link Object} into an {@link String} and the
* {@link String} back to an equivalent object.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface Transformer<T> {
/**
* Transform the object in a string
*
* @param value
* the object
* @return the string of the object
*/
String transform(T value);
/**
* Untransforms the string back to an object
*
* @param value
* the string
* @return the object
*/
T untransform(String value);
}

View File

@@ -0,0 +1,42 @@
package com.l2jserver.util.transformer;
import java.io.File;
import java.net.InetSocketAddress;
import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer;
import com.l2jserver.util.transformer.impl.ClassTransformer;
import com.l2jserver.util.transformer.impl.DoubleTransformer;
import com.l2jserver.util.transformer.impl.FileTransformer;
import com.l2jserver.util.transformer.impl.FloatTransformer;
import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer;
import com.l2jserver.util.transformer.impl.IntegerTransformer;
import com.l2jserver.util.transformer.impl.LongTransformer;
import com.l2jserver.util.transformer.impl.ShortTransformer;
public class TransformerFactory {
public static final Transformer<?> getTransfromer(Class<?> type) {
if (type == Byte.class || type == Byte.TYPE) {
return ByteTransformer.SHARED_INSTANCE;
} else if (type == Short.class || type == Short.TYPE) {
return ShortTransformer.SHARED_INSTANCE;
} else if (type == Integer.class || type == Integer.TYPE) {
return IntegerTransformer.SHARED_INSTANCE;
} else if (type == Long.class || type == Long.TYPE) {
return LongTransformer.SHARED_INSTANCE;
} else if (type == Float.class || type == Float.TYPE) {
return FloatTransformer.SHARED_INSTANCE;
} else if (type == Double.class || type == Double.TYPE) {
return DoubleTransformer.SHARED_INSTANCE;
} else if (type == Boolean.class || type == Boolean.TYPE) {
return BooleanTransformer.SHARED_INSTANCE;
} else if(type == InetSocketAddress.class) {
return InetSocketAddressTransformer.SHARED_INSTANCE;
} else if(type == File.class) {
return FileTransformer.SHARED_INSTANCE;
}else if(type == Class.class) {
return ClassTransformer.SHARED_INSTANCE;
}
return null;
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Boolean} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class BooleanTransformer implements Transformer<Boolean> {
public static final BooleanTransformer SHARED_INSTANCE = new BooleanTransformer();
@Override
public String transform(Boolean value) {
return (value ? "true" : "false");
}
@Override
public Boolean untransform(String value) {
return Boolean.parseBoolean(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ByteTransformer implements Transformer<Byte> {
public static final ByteTransformer SHARED_INSTANCE = new ByteTransformer();
@Override
public String transform(Byte value) {
return Double.toString(value);
}
@Override
public Byte untransform(String value) {
return Byte.decode(value);
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Class} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ClassTransformer implements Transformer<Class<?>> {
public static final ClassTransformer SHARED_INSTANCE = new ClassTransformer();
@Override
public String transform(Class<?> value) {
return value.getName();
}
@Override
public Class<?> untransform(String value) {
try {
return Class.forName(value);
} catch (ClassNotFoundException e) {
return null;
}
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DoubleTransformer implements Transformer<Double> {
public static final DoubleTransformer SHARED_INSTANCE = new DoubleTransformer();
@Override
public String transform(Double value) {
return Double.toString(value);
}
@Override
public Double untransform(String value) {
return Double.parseDouble(value);
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer.impl;
import java.io.File;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class FileTransformer implements Transformer<File> {
public static final FileTransformer SHARED_INSTANCE = new FileTransformer();
private final File root = new File("./");
@Override
public String transform(File value) {
return value.getAbsolutePath();
}
@Override
public File untransform(String value) {
return new File(root, value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class FloatTransformer implements Transformer<Float> {
public static final FloatTransformer SHARED_INSTANCE = new FloatTransformer();
@Override
public String transform(Float value) {
return Double.toString(value);
}
@Override
public Float untransform(String value) {
return Float.parseFloat(value);
}
}

View File

@@ -0,0 +1,29 @@
package com.l2jserver.util.transformer.impl;
import java.net.InetSocketAddress;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class InetSocketAddressTransformer implements
Transformer<InetSocketAddress> {
public static final InetSocketAddressTransformer SHARED_INSTANCE = new InetSocketAddressTransformer();
@Override
public String transform(InetSocketAddress value) {
return value.getHostName() + ":" + value.getPort();
}
@Override
public InetSocketAddress untransform(String value) {
final String[] pieces = value.split(":");
if (pieces.length != 2)
return null;
return new InetSocketAddress(pieces[0], Integer.parseInt(pieces[1]));
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class IntegerTransformer implements Transformer<Integer> {
public static final IntegerTransformer SHARED_INSTANCE = new IntegerTransformer();
@Override
public String transform(Integer value) {
return Integer.toString(value);
}
@Override
public Integer untransform(String value) {
return Integer.decode(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Long} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class LongTransformer implements Transformer<Long> {
public static final LongTransformer SHARED_INSTANCE = new LongTransformer();
@Override
public String transform(Long value) {
return Long.toString(value);
}
@Override
public Long untransform(String value) {
return Long.decode(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ShortTransformer implements Transformer<Short> {
public static final ShortTransformer SHARED_INSTANCE = new ShortTransformer();
@Override
public String transform(Short value) {
return Short.toString(value);
}
@Override
public Short untransform(String value) {
return Short.decode(value);
}
}

View File

@@ -12,7 +12,6 @@ import com.l2jserver.model.id.ID;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.id.object.factory.CharacterIDFactory;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.database.DatabaseService;
@@ -21,8 +20,7 @@ import com.l2jserver.service.game.template.TemplateService;
public class IDFactoryTest {
private final Injector injector = Guice.createInjector(new ServiceModule(),
new BasicServiceModule(), new DAOModuleMySQL5(),
new IDFactoryModule());
new DAOModuleMySQL5(), new IDFactoryModule());
private final CharacterIDFactory charIdFactory = injector
.getInstance(CharacterIDFactory.class);

View File

@@ -13,7 +13,6 @@ import com.l2jserver.model.id.factory.IDFactoryModule;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.id.object.factory.CharacterIDFactory;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.database.DatabaseService;
@@ -22,8 +21,7 @@ import com.l2jserver.service.game.template.TemplateService;
public class CharacterFriendListTest {
private final Injector injector = Guice.createInjector(new ServiceModule(),
new BasicServiceModule(), new DAOModuleMySQL5(),
new IDFactoryModule());
new DAOModuleMySQL5(), new IDFactoryModule());
private final CharacterIDFactory charIdFactory = injector
.getInstance(CharacterIDFactory.class);

View File

@@ -11,15 +11,13 @@ import com.google.inject.Scopes;
import com.l2jserver.db.dao.DAOModuleMySQL5;
import com.l2jserver.model.id.factory.IDFactoryModule;
import com.l2jserver.model.id.template.factory.ItemTemplateIDFactory;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.game.scripting.ScriptingService;
import com.l2jserver.service.game.scripting.ScriptingServiceImpl;
public class StaticTemplateServiceTest {
private final Injector injector = Guice.createInjector(
new BasicServiceModule(), new IDFactoryModule(),
new DAOModuleMySQL5(), new AbstractModule() {
new IDFactoryModule(), new DAOModuleMySQL5(), new AbstractModule() {
@Override
protected void configure() {
bind(ScriptingService.class).to(ScriptingServiceImpl.class)

View File

@@ -23,7 +23,6 @@ import com.l2jserver.model.world.item.ItemListener;
import com.l2jserver.model.world.player.PlayerEvent;
import com.l2jserver.model.world.player.PlayerListener;
import com.l2jserver.model.world.player.PlayerSpawnEvent;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.game.world.WorldService;
@@ -40,9 +39,9 @@ public class WorldEventDispatcherImplTest {
@Before
public void tearUp() throws ServiceStartException {
Injector injector = Guice.createInjector(new BasicServiceModule(),
new ServiceModule(), new DAOModuleMySQL5(),
new IDFactoryModule(), new AbstractModule() {
Injector injector = Guice.createInjector(new ServiceModule(),
new DAOModuleMySQL5(), new IDFactoryModule(),
new AbstractModule() {
@Override
protected void configure() {
bind(WorldService.class).to(WorldServiceImpl.class).in(

View File

@@ -13,7 +13,6 @@ import com.l2jserver.model.world.Item;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.model.world.filter.impl.InstanceFilter;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.WorldServiceImpl;
@@ -25,17 +24,15 @@ public class WorldServiceImplTest {
@Before
public void tearUp() throws ServiceStartException {
Injector injector = Guice.createInjector(new BasicServiceModule(),
new AbstractModule() {
@Override
protected void configure() {
bind(WorldService.class).to(WorldServiceImpl.class).in(
Scopes.SINGLETON);
bind(WorldEventDispatcher.class).to(
WorldEventDispatcherImpl.class).in(
Scopes.SINGLETON);
}
});
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(WorldService.class).to(WorldServiceImpl.class).in(
Scopes.SINGLETON);
bind(WorldEventDispatcher.class).to(
WorldEventDispatcherImpl.class).in(Scopes.SINGLETON);
}
});
world = injector.getInstance(WorldService.class);
Assert.assertNotNull(world);