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

Service and transformer improvements

This commit is contained in:
2011-12-12 13:49:44 -02:00
parent 03e6ad6e1a
commit 798e34fcfc
15 changed files with 112 additions and 49 deletions

View File

@@ -91,7 +91,7 @@ public abstract class AbstractService implements Service {
}
@Override
public Class<? extends Service>[] getDependencies() {
public final Class<? extends Service>[] getDependencies() {
final Depends deps = this.getClass().getAnnotation(Depends.class);
if (deps == null)
return null;

View File

@@ -16,6 +16,7 @@
*/
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;
@@ -45,6 +46,7 @@ public interface Configuration {
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
@Documented
public @interface ConfigurationPropertyGetter {
/**
* @return the default value to be used
@@ -61,6 +63,7 @@ public interface Configuration {
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
@Documented
public @interface ConfigurationPropertySetter {
}
}

View File

@@ -73,7 +73,7 @@ public class ProxyConfigurationService extends AbstractService implements
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(value = ElementType.METHOD)
public @interface ConfigurationPropertiesKey {
public @interface ConfigurationPropertyKey {
String value();
}
@@ -156,8 +156,8 @@ public class ProxyConfigurationService extends AbstractService implements
if (args == null || args.length == 0) {
final ConfigurationPropertyGetter getter = method
.getAnnotation(ConfigurationPropertyGetter.class);
final ConfigurationPropertiesKey propertiesKey = method
.getAnnotation(ConfigurationPropertiesKey.class);
final ConfigurationPropertyKey propertiesKey = method
.getAnnotation(ConfigurationPropertyKey.class);
if (getter == null)
return null;
if (propertiesKey == null)
@@ -166,8 +166,8 @@ public class ProxyConfigurationService extends AbstractService implements
} else if (args.length == 1) {
final ConfigurationPropertySetter setter = method
.getAnnotation(ConfigurationPropertySetter.class);
final ConfigurationPropertiesKey propertiesKey = method
.getAnnotation(ConfigurationPropertiesKey.class);
final ConfigurationPropertyKey propertiesKey = method
.getAnnotation(ConfigurationPropertyKey.class);
if (setter == null)
return null;
if (propertiesKey == null)
@@ -189,7 +189,7 @@ public class ProxyConfigurationService extends AbstractService implements
* @return the untransformed property
*/
private Object get(ConfigurationPropertyGetter getter,
ConfigurationPropertiesKey propertiesKey, Class<?> type) {
ConfigurationPropertyKey propertiesKey, Class<?> type) {
if (cache.containsKey(propertiesKey.value()))
return cache.get(propertiesKey.value());
Object o = untransform(
@@ -208,7 +208,7 @@ public class ProxyConfigurationService extends AbstractService implements
* @param type
* the transformed type
*/
private void set(ConfigurationPropertiesKey setter, Object value,
private void set(ConfigurationPropertyKey setter, Object value,
Class<?> type) {
if (value != null) {
properties.setProperty(setter.value(),

View File

@@ -23,7 +23,7 @@ import com.l2jserver.service.Service;
import com.l2jserver.service.ServiceConfiguration;
import com.l2jserver.service.configuration.Configuration;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationName;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertiesKey;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertyKey;
import com.l2jserver.service.configuration.XMLConfigurationService.ConfigurationXPath;
/**
@@ -45,7 +45,7 @@ public interface VFSService extends Service {
* @return the VFS root {@link URI}
*/
@ConfigurationPropertyGetter(defaultValue = "")
@ConfigurationPropertiesKey("vfs.root")
@ConfigurationPropertyKey("vfs.root")
@ConfigurationXPath("/configuration/services/vfs/root")
Path getRoot();
@@ -54,7 +54,7 @@ public interface VFSService extends Service {
* the new VFS root {@link URI}
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("vfs.root")
@ConfigurationPropertyKey("vfs.root")
@ConfigurationXPath("/configuration/services/vfs/root")
void setRoot(Path root);
}

View File

@@ -47,7 +47,7 @@ import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.cache.Cache;
import com.l2jserver.service.cache.CacheService;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertiesKey;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertyKey;
import com.l2jserver.service.configuration.XMLConfigurationService.ConfigurationXPath;
import com.l2jserver.service.core.threading.ScheduledAsyncFuture;
import com.l2jserver.service.core.threading.ThreadService;
@@ -139,7 +139,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the jdbc url
*/
@ConfigurationPropertyGetter(defaultValue = "jdbc:mysql://localhost/l2jserver2")
@ConfigurationPropertiesKey("jdbc.url")
@ConfigurationPropertyKey("jdbc.url")
@ConfigurationXPath("/configuration/services/database/jdbc/url")
String getJdbcUrl();
@@ -148,7 +148,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the new jdbc url
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.url")
@ConfigurationPropertyKey("jdbc.url")
@ConfigurationXPath("/configuration/services/database/jdbc/url")
void setJdbcUrl(String jdbcUrl);
@@ -156,7 +156,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the jdbc driver class
*/
@ConfigurationPropertyGetter(defaultValue = "com.jdbc.jdbc.Driver")
@ConfigurationPropertiesKey("jdbc.driver")
@ConfigurationPropertyKey("jdbc.driver")
@ConfigurationXPath("/configuration/services/database/jdbc/driver")
String getDriver();
@@ -165,7 +165,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the new jdbc driver
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.driver")
@ConfigurationPropertyKey("jdbc.driver")
@ConfigurationXPath("/configuration/services/database/jdbc/driver")
void setDriver(Class<?> driver);
@@ -173,7 +173,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the jdbc database username
*/
@ConfigurationPropertyGetter(defaultValue = "l2j")
@ConfigurationPropertiesKey("jdbc.username")
@ConfigurationPropertyKey("jdbc.username")
@ConfigurationXPath("/configuration/services/database/jdbc/username")
String getUsername();
@@ -182,7 +182,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the jdbc database username
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.username")
@ConfigurationPropertyKey("jdbc.username")
@ConfigurationXPath("/configuration/services/database/jdbc/username")
void setUsername(String username);
@@ -190,7 +190,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the jdbc database password
*/
@ConfigurationPropertyGetter(defaultValue = "changeme")
@ConfigurationPropertiesKey("jdbc.password")
@ConfigurationPropertyKey("jdbc.password")
@ConfigurationXPath("/configuration/services/database/jdbc/password")
String getPassword();
@@ -199,7 +199,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the jdbc database password
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.password")
@ConfigurationPropertyKey("jdbc.password")
@ConfigurationXPath("/configuration/services/database/jdbc/password")
void setPassword(String password);
@@ -207,7 +207,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the maximum number of active connections
*/
@ConfigurationPropertyGetter(defaultValue = "20")
@ConfigurationPropertiesKey("jdbc.active.max")
@ConfigurationPropertyKey("jdbc.active.max")
@ConfigurationXPath("/configuration/services/database/connections/active-maximum")
int getMaxActiveConnections();
@@ -216,7 +216,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the maximum number of active connections
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.active.max")
@ConfigurationPropertyKey("jdbc.active.max")
@ConfigurationXPath("/configuration/services/database/connections/active-maximum")
void setMaxActiveConnections(int password);
@@ -224,7 +224,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the maximum number of idle connections
*/
@ConfigurationPropertyGetter(defaultValue = "20")
@ConfigurationPropertiesKey("jdbc.idle.max")
@ConfigurationPropertyKey("jdbc.idle.max")
@ConfigurationXPath("/configuration/services/database/connections/idle-maximum")
int getMaxIdleConnections();
@@ -233,7 +233,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the maximum number of idle connections
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.idle.max")
@ConfigurationPropertyKey("jdbc.idle.max")
@ConfigurationXPath("/configuration/services/database/connections/idle-maximum")
void setMaxIdleConnections(int password);
@@ -241,7 +241,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* @return the minimum number of idle connections
*/
@ConfigurationPropertyGetter(defaultValue = "5")
@ConfigurationPropertiesKey("jdbc.idle.min")
@ConfigurationPropertyKey("jdbc.idle.min")
@ConfigurationXPath("/configuration/services/database/connections/idle-minimum")
int getMinIdleConnections();
@@ -250,7 +250,7 @@ public abstract class AbstractJDBCDatabaseService extends AbstractService
* the minimum number of idle connections
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.idle.min")
@ConfigurationPropertyKey("jdbc.idle.min")
@ConfigurationXPath("/configuration/services/database/connections/idle-minimum")
void setMinIdleConnections(int password);
}

View File

@@ -37,7 +37,7 @@ import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.cache.Cache;
import com.l2jserver.service.cache.CacheService;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertiesKey;
import com.l2jserver.service.configuration.ProxyConfigurationService.ConfigurationPropertyKey;
import com.l2jserver.service.configuration.XMLConfigurationService.ConfigurationXPath;
import com.l2jserver.service.core.threading.ScheduledAsyncFuture;
import com.l2jserver.service.core.threading.ThreadService;
@@ -117,7 +117,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* @return the orientdb url
*/
@ConfigurationPropertyGetter(defaultValue = "file:data/database")
@ConfigurationPropertiesKey("orientdb.url")
@ConfigurationPropertyKey("orientdb.url")
@ConfigurationXPath("/configuration/services/database/orientdb/url")
String getUrl();
@@ -126,7 +126,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* the new orientdb url
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("orientdb.url")
@ConfigurationPropertyKey("orientdb.url")
@ConfigurationXPath("/configuration/services/database/orientdb/url")
void setUrl(String url);
@@ -134,7 +134,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* @return the orientdb database username
*/
@ConfigurationPropertyGetter(defaultValue = "l2j")
@ConfigurationPropertiesKey("orientdb.username")
@ConfigurationPropertyKey("orientdb.username")
@ConfigurationXPath("/configuration/services/database/orientdb/username")
String getUsername();
@@ -143,7 +143,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* the orientdb database username
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("orientdb.username")
@ConfigurationPropertyKey("orientdb.username")
@ConfigurationXPath("/configuration/services/database/orientdb/username")
void setUsername(String username);
@@ -151,7 +151,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* @return the orientdb database password
*/
@ConfigurationPropertyGetter(defaultValue = "changeme")
@ConfigurationPropertiesKey("orientdb.password")
@ConfigurationPropertyKey("orientdb.password")
@ConfigurationXPath("/configuration/services/database/orientdb/password")
String getPassword();
@@ -160,7 +160,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
* the jdbc database password
*/
@ConfigurationPropertySetter
@ConfigurationPropertiesKey("jdbc.password")
@ConfigurationPropertyKey("jdbc.password")
@ConfigurationXPath("/configuration/services/database/jdbc/password")
void setPassword(String password);
}

View File

@@ -0,0 +1,54 @@
/*
* This file is part of l2jserver2 <l2jserver2.com>.
*
* l2jserver2 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.
*
* l2jserver2 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 l2jserver2. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.util.transformer;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class TransformException extends RuntimeException {
private static final long serialVersionUID = 1L;
public TransformException() {
}
/**
* @param message
* the message
*/
public TransformException(String message) {
super(message);
}
/**
* @param cause
* the cause
*/
public TransformException(Throwable cause) {
super(cause);
}
/**
* @param message
* the message
* @param cause
* the cause
*/
public TransformException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -32,8 +32,10 @@ public interface Transformer<T> {
* @param value
* the object
* @return the string of the object
* @throws TransformException
* if any error occur while transforming
*/
String transform(T value);
String transform(T value) throws TransformException;
/**
* Untransforms the string back to an object
@@ -41,6 +43,8 @@ public interface Transformer<T> {
* @param value
* the string
* @return the object
* @throws TransformException
* if any error occur while transforming
*/
T untransform(String value);
T untransform(String value) throws TransformException;
}

View File

@@ -16,6 +16,7 @@
*/
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.TransformException;
import com.l2jserver.util.transformer.Transformer;
/**
@@ -39,7 +40,7 @@ public class ClassTransformer implements Transformer<Class<?>> {
try {
return Class.forName(value);
} catch (ClassNotFoundException e) {
return null;
throw new TransformException(e);
}
}

View File

@@ -18,6 +18,7 @@ package com.l2jserver.util.transformer.impl;
import java.net.InetSocketAddress;
import com.l2jserver.util.transformer.TransformException;
import com.l2jserver.util.transformer.Transformer;
/**
@@ -41,7 +42,7 @@ public class InetSocketAddressTransformer implements
public InetSocketAddress untransform(String value) {
final String[] pieces = value.split(":");
if (pieces.length != 2)
return null;
throw new TransformException("InetSocketAddress must have format ip:port");
return new InetSocketAddress(pieces[0], Integer.parseInt(pieces[1]));
}

View File

@@ -20,6 +20,7 @@ import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import com.l2jserver.util.transformer.TransformException;
import com.l2jserver.util.transformer.Transformer;
/**
@@ -43,7 +44,7 @@ public class URLTransformer implements Transformer<URL> {
try {
return new URL(value);
} catch (MalformedURLException e) {
return null;
throw new TransformException(e);
}
}