1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-06 07:32:46 +00:00

Added Java 7 support and Java 7 VFSServive using native APIs

This commit is contained in:
2011-09-11 21:47:20 -03:00
parent d9f2c4b8c1
commit 91b770a923
19 changed files with 196 additions and 145 deletions

View File

@@ -8,7 +8,7 @@
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="src" output="target/tools-classes" path="src/tool/java"/> <classpathentry kind="src" output="target/tools-classes" path="src/tool/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

1
config/vfs.properties Normal file
View File

@@ -0,0 +1 @@
vfs.root =

1
dist/config/template.properties vendored Normal file
View File

@@ -0,0 +1 @@
template.directory = data/templates

1
dist/config/vfs.properties vendored Normal file
View File

@@ -0,0 +1 @@
vfs.root = file:/

View File

@@ -143,13 +143,6 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- vfs --> <!-- vfs -->
<dependency>
<groupId>commons-vfs</groupId>
<artifactId>commons-vfs</artifactId>
<version>20050307052300</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
</dependencies> </dependencies>
<issueManagement> <issueManagement>

View File

@@ -28,7 +28,7 @@ import com.l2jserver.service.core.LoggingService;
import com.l2jserver.service.core.threading.ThreadService; import com.l2jserver.service.core.threading.ThreadService;
import com.l2jserver.service.core.threading.ThreadServiceImpl; import com.l2jserver.service.core.threading.ThreadServiceImpl;
import com.l2jserver.service.core.vfs.VFSService; import com.l2jserver.service.core.vfs.VFSService;
import com.l2jserver.service.core.vfs.VFSServiceImpl; import com.l2jserver.service.core.vfs.Java7VFSService;
import com.l2jserver.service.database.DatabaseService; import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.JDBCDatabaseService; import com.l2jserver.service.database.JDBCDatabaseService;
import com.l2jserver.service.game.AttackService; import com.l2jserver.service.game.AttackService;
@@ -77,7 +77,7 @@ public class ServiceModule extends AbstractModule {
bind(ServiceManager.class).in(Scopes.SINGLETON); bind(ServiceManager.class).in(Scopes.SINGLETON);
bind(LoggingService.class).to(Log4JLoggingService.class).in( bind(LoggingService.class).to(Log4JLoggingService.class).in(
Scopes.SINGLETON); Scopes.SINGLETON);
bind(VFSService.class).to(VFSServiceImpl.class).in(Scopes.SINGLETON); bind(VFSService.class).to(Java7VFSService.class).in(Scopes.SINGLETON);
bind(ThreadService.class).to(ThreadServiceImpl.class).in( bind(ThreadService.class).to(ThreadServiceImpl.class).in(
Scopes.SINGLETON); Scopes.SINGLETON);
bind(ConfigurationService.class).to(ProxyConfigurationService.class) bind(ConfigurationService.class).to(ProxyConfigurationService.class)

View File

@@ -24,12 +24,10 @@ import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.l2jserver.service.cache.SoftCacheService.SoftCache;
import com.l2jserver.util.factory.CollectionFactory; import com.l2jserver.util.factory.CollectionFactory;
import com.sun.beans.WeakCache;
/** /**
* Base class for {@link WeakCache} and {@link SoftCache} * Base class for weak caches and soft caches
* *
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
* *

View File

@@ -131,7 +131,7 @@ public class SoftCacheService extends AbstractService implements CacheService {
* *
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
*/ */
public class SoftCache<K, V> extends AbstractReferenceCache<K, V> implements private class SoftCache<K, V> extends AbstractReferenceCache<K, V> implements
Cache<K, V> { Cache<K, V> {
/** /**
* This class is a {@link SoftReference} with additional responsibility * This class is a {@link SoftReference} with additional responsibility

View File

@@ -0,0 +1,25 @@
/*
* 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.core.vfs;
/**
* Configuration interface for {@link Java7VFSService}.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface Java7VFSConfiguration extends VFSConfiguration {
}

View File

@@ -16,73 +16,74 @@
*/ */
package com.l2jserver.service.core.vfs; package com.l2jserver.service.core.vfs;
import java.io.File; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import org.apache.commons.vfs.FileObject;
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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException; import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.configuration.ConfigurationService;
/** /**
* Default implementation for VFS system * Implementation of {@link VFSService} using default Java7 APIs.
* *
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
*/ */
public class VFSServiceImpl extends AbstractService implements VFSService { public class Java7VFSService extends AbstractService implements VFSService {
/** /**
* The logger * The logger
*/ */
private final Logger log = LoggerFactory.getLogger(this.getClass()); private final Logger log = LoggerFactory.getLogger(this.getClass());
private final Java7VFSConfiguration config;
/** /**
* The CommonsVFS {@link FileSystemManager} * The {@link FileSystem} implementation
*/ */
private DefaultFileSystemManager manager; private FileSystem fs;
/**
* The root {@link Path} of the server data
*/
private Path root;
@Inject
protected Java7VFSService(final ConfigurationService configService) {
this.config = configService.get(Java7VFSConfiguration.class);
}
@Override @Override
protected void doStart() throws ServiceStartException { protected void doStart() throws ServiceStartException {
manager = new StandardFileSystemManager();
try { try {
manager.init(); fs = FileSystems.getFileSystem(new URI("file:///"));
manager.setBaseFile(new File("./")); } catch (URISyntaxException e) {
} catch (FileSystemException e) {
manager = null;
throw new ServiceStartException(e); throw new ServiceStartException(e);
} }
root = config.getRoot().toAbsolutePath();
log.debug("Root path is {}", root);
} }
@Override @Override
public FileObject resolve(URI uri) { public Path resolve(String path) {
return resolve(uri.toString()); log.debug("Resolving file {}", path);
} return root.resolve(path);
@Override
public FileObject resolve(String uri) {
log.debug("Resolving file {}", uri);
try {
return manager.resolveFile(uri);
} catch (FileSystemException e) {
log.error("Error resolving file", e);
return null;
}
} }
@Override @Override
protected void doStop() throws ServiceStopException { protected void doStop() throws ServiceStopException {
try { try {
manager.getBaseFile().close(); fs.close();
} catch (FileSystemException e) { } catch (IOException e) {
throw new ServiceStopException(e); throw new ServiceStopException(e);
} finally { } finally {
manager = null; fs = null;
} }
} }
} }

View File

@@ -0,0 +1,45 @@
/*
* 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.core.vfs;
import java.net.URI;
import java.nio.file.Path;
import com.l2jserver.service.configuration.Configuration;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
/**
* VFS service configuration
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @see Configuration
*/
@ConfigurationName("vfs")
public interface VFSConfiguration extends Configuration {
/**
* @return the VFS root {@link URI}
*/
@ConfigurationPropertyGetter(name = "vfs.root", defaultValue = "")
Path getRoot();
/**
* @param root
* the new VFS root {@link URI}
*/
@ConfigurationPropertySetter(name = "vfs.root")
void setRoot(Path root);
}

View File

@@ -16,9 +16,7 @@
*/ */
package com.l2jserver.service.core.vfs; package com.l2jserver.service.core.vfs;
import java.net.URI; import java.nio.file.Path;
import org.apache.commons.vfs.FileObject;
import com.l2jserver.service.Service; import com.l2jserver.service.Service;
@@ -29,18 +27,6 @@ import com.l2jserver.service.Service;
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
*/ */
public interface VFSService extends Service { public interface VFSService extends Service {
/**
* Resolves an file. If the file cannot be resolved, null will be returned.
* <p>
* Please note that event if the file DOES NOT exists a valid object will be
* returned.
*
* @param uri
* the file uri
* @return the resolved file. Will return null if could not resolve.
*/
FileObject resolve(URI uri);
/** /**
* Resolves an file. If the file cannot be resolved, null will be returned. * Resolves an file. If the file cannot be resolved, null will be returned.
* <p> * <p>
@@ -51,5 +37,5 @@ public interface VFSService extends Service {
* the file uri as an string * the file uri as an string
* @return the resolved file. Will return null if could not resolve. * @return the resolved file. Will return null if could not resolve.
*/ */
FileObject resolve(String uri); Path resolve(String name);
} }

View File

@@ -18,6 +18,11 @@ package com.l2jserver.service.game.template;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List; import java.util.List;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
@@ -29,7 +34,6 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
import org.apache.commons.vfs.FileObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -56,7 +60,6 @@ import com.l2jserver.util.jaxb.ItemTemplateIDAdapter;
import com.l2jserver.util.jaxb.NPCTemplateIDAdapter; import com.l2jserver.util.jaxb.NPCTemplateIDAdapter;
import com.l2jserver.util.jaxb.SkillTemplateIDAdapter; import com.l2jserver.util.jaxb.SkillTemplateIDAdapter;
import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter;
import com.l2jserver.util.vfs.ExtensionFileSelector;
@Depends({ LoggingService.class, VFSService.class, CacheService.class, @Depends({ LoggingService.class, VFSService.class, CacheService.class,
ConfigurationService.class }) ConfigurationService.class })
@@ -121,21 +124,35 @@ public class XMLTemplateService extends AbstractService implements
unmarshaller.setAdapter(TeleportationTemplateIDAdapter.class, unmarshaller.setAdapter(TeleportationTemplateIDAdapter.class,
teleportationIdTemplateAdapter); teleportationIdTemplateAdapter);
final FileObject root = vfsService.resolve(config final Path templatePath = vfsService.resolve(config
.getTemplateDirectory()); .getTemplateDirectory().toString());
log.info("Scanning {} for XML templates", root); log.info("Scanning {} for XML templates", templatePath);
FileObject[] files = root.findFiles(ExtensionFileSelector Files.walkFileTree(templatePath, new SimpleFileVisitor<Path>() {
.ext("xml")); @Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
log.info("Located {} XML template files", files.length); @Override
for (final FileObject file : files) { public FileVisitResult visitFile(Path file,
loadTemplate(file); BasicFileAttributes attrs) throws IOException {
} if (!file.toString().endsWith(".xml"))
final FileObject teleportsXml = root.getParent().resolveFile( return FileVisitResult.CONTINUE;
try {
loadTemplate(file);
return FileVisitResult.CONTINUE;
} catch (JAXBException e) {
throw new IOException(e);
}
}
});
final Path teleportsXmlPath = templatePath.getParent().resolve(
"teleports.xml"); "teleports.xml");
final InputStream in = teleportsXml.getContent().getInputStream(); final InputStream in = Files.newInputStream(teleportsXmlPath);
try { try {
TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller
.unmarshal(in); .unmarshal(in);
@@ -159,10 +176,10 @@ public class XMLTemplateService extends AbstractService implements
return (T) templates.get(id); return (T) templates.get(id);
} }
public void loadTemplate(FileObject file) throws JAXBException, IOException { public void loadTemplate(Path path) throws JAXBException, IOException {
Preconditions.checkNotNull(file, "file"); Preconditions.checkNotNull(path, "path");
log.debug("Loading template {}", file); log.debug("Loading template {}", path);
final InputStream in = file.getContent().getInputStream(); final InputStream in = Files.newInputStream(path);
try { try {
final Template<?> template = (Template<?>) unmarshaller final Template<?> template = (Template<?>) unmarshaller
.unmarshal(in); .unmarshal(in);

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.file.Path;
import com.l2jserver.util.transformer.impl.BooleanTransformer; import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer; import com.l2jserver.util.transformer.impl.ByteTransformer;
@@ -30,6 +31,7 @@ import com.l2jserver.util.transformer.impl.FloatTransformer;
import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer; import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer;
import com.l2jserver.util.transformer.impl.IntegerTransformer; import com.l2jserver.util.transformer.impl.IntegerTransformer;
import com.l2jserver.util.transformer.impl.LongTransformer; import com.l2jserver.util.transformer.impl.LongTransformer;
import com.l2jserver.util.transformer.impl.PathTransformer;
import com.l2jserver.util.transformer.impl.ShortTransformer; import com.l2jserver.util.transformer.impl.ShortTransformer;
import com.l2jserver.util.transformer.impl.URITransformer; import com.l2jserver.util.transformer.impl.URITransformer;
import com.l2jserver.util.transformer.impl.URLTransformer; import com.l2jserver.util.transformer.impl.URLTransformer;
@@ -73,6 +75,8 @@ public class TransformerFactory {
return URITransformer.SHARED_INSTANCE; return URITransformer.SHARED_INSTANCE;
} else if (type == URL.class) { } else if (type == URL.class) {
return URLTransformer.SHARED_INSTANCE; return URLTransformer.SHARED_INSTANCE;
} else if (type == Path.class) {
return PathTransformer.SHARED_INSTANCE;
} }
return null; return null;
} }

View File

@@ -0,0 +1,43 @@
/*
* 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.util.transformer.impl;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Path} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class PathTransformer implements Transformer<Path> {
public static final PathTransformer SHARED_INSTANCE = new PathTransformer();
@Override
public String transform(Path value) {
if(value == null)
return "";
return value.toString();
}
@Override
public Path untransform(String value) {
return Paths.get(value);
}
}

View File

@@ -17,7 +17,6 @@
package com.l2jserver.util.transformer.impl; package com.l2jserver.util.transformer.impl;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import com.l2jserver.util.transformer.Transformer; import com.l2jserver.util.transformer.Transformer;
@@ -36,11 +35,6 @@ public class URITransformer implements Transformer<URI> {
@Override @Override
public URI untransform(String value) { public URI untransform(String value) {
try { return URI.create(value);
return new URI(value);
} catch (URISyntaxException e) {
return null;
}
} }
} }

View File

@@ -1,60 +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.util.vfs;
import java.util.Arrays;
import org.apache.commons.vfs.FileSelectInfo;
import org.apache.commons.vfs.FileSelector;
import org.apache.commons.vfs.FileType;
/**
* This selector will select all <tt>FILES</tt> that has an given extension.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ExtensionFileSelector implements FileSelector {
private final String[] extensions;
private final boolean descendents;
public ExtensionFileSelector(String... extensions) {
this(true, extensions);
}
public ExtensionFileSelector(boolean descendents, String... extensions) {
this.descendents = descendents;
this.extensions = extensions;
Arrays.sort(extensions);
}
@Override
public boolean includeFile(FileSelectInfo file) throws Exception {
if (file.getFile().getType() != FileType.FILE)
return false;
return (Arrays.binarySearch(extensions, file.getFile().getName()
.getExtension()) >= 0);
}
@Override
public boolean traverseDescendents(FileSelectInfo file) throws Exception {
return descendents;
}
public static final ExtensionFileSelector ext(String... extensions) {
return new ExtensionFileSelector(extensions);
}
}

View File

@@ -78,6 +78,7 @@ import com.l2jserver.util.geometry.Coordinate;
import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer; import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
@SuppressWarnings("restriction")
public class NPCTemplateConverter { public class NPCTemplateConverter {
private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy"; private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy";
private static final String JDBC_USERNAME = "l2j"; private static final String JDBC_USERNAME = "l2j";

View File

@@ -47,6 +47,7 @@ import com.l2jserver.util.factory.CollectionFactory;
import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer; import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
@SuppressWarnings("restriction")
public class SkillTemplateConverter { public class SkillTemplateConverter {
private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy"; private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy";
private static final String JDBC_USERNAME = "l2j"; private static final String JDBC_USERNAME = "l2j";