From 91b770a923553f18a748c050125744bb2f2b9dbc Mon Sep 17 00:00:00 2001 From: Rogiel Date: Sun, 11 Sep 2011 21:47:20 -0300 Subject: [PATCH] Added Java 7 support and Java 7 VFSServive using native APIs --- .classpath | 2 +- config/vfs.properties | 1 + dist/config/template.properties | 1 + dist/config/vfs.properties | 1 + pom.xml | 7 --- .../com/l2jserver/service/ServiceModule.java | 4 +- .../service/cache/AbstractReferenceCache.java | 4 +- .../service/cache/SoftCacheService.java | 2 +- .../core/vfs/Java7VFSConfiguration.java | 25 ++++++++ ...SServiceImpl.java => Java7VFSService.java} | 63 ++++++++++--------- .../service/core/vfs/VFSConfiguration.java | 45 +++++++++++++ .../service/core/vfs/VFSService.java | 18 +----- .../game/template/XMLTemplateService.java | 51 ++++++++++----- .../util/transformer/TransformerFactory.java | 4 ++ .../transformer/impl/PathTransformer.java | 43 +++++++++++++ .../util/transformer/impl/URITransformer.java | 8 +-- .../util/vfs/ExtensionFileSelector.java | 60 ------------------ .../model/template/NPCTemplateConverter.java | 1 + .../template/SkillTemplateConverter.java | 1 + 19 files changed, 196 insertions(+), 145 deletions(-) create mode 100644 config/vfs.properties create mode 100644 dist/config/template.properties create mode 100644 dist/config/vfs.properties create mode 100644 src/main/java/com/l2jserver/service/core/vfs/Java7VFSConfiguration.java rename src/main/java/com/l2jserver/service/core/vfs/{VFSServiceImpl.java => Java7VFSService.java} (57%) create mode 100644 src/main/java/com/l2jserver/service/core/vfs/VFSConfiguration.java create mode 100644 src/main/java/com/l2jserver/util/transformer/impl/PathTransformer.java delete mode 100644 src/main/java/com/l2jserver/util/vfs/ExtensionFileSelector.java diff --git a/.classpath b/.classpath index 291a9b671..fa98c230d 100644 --- a/.classpath +++ b/.classpath @@ -8,7 +8,7 @@ - + diff --git a/config/vfs.properties b/config/vfs.properties new file mode 100644 index 000000000..2f6df2fab --- /dev/null +++ b/config/vfs.properties @@ -0,0 +1 @@ +vfs.root = \ No newline at end of file diff --git a/dist/config/template.properties b/dist/config/template.properties new file mode 100644 index 000000000..b431db32e --- /dev/null +++ b/dist/config/template.properties @@ -0,0 +1 @@ +template.directory = data/templates \ No newline at end of file diff --git a/dist/config/vfs.properties b/dist/config/vfs.properties new file mode 100644 index 000000000..83e9ecc58 --- /dev/null +++ b/dist/config/vfs.properties @@ -0,0 +1 @@ +vfs.root = file:/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index e55d6d1ed..22dea8573 100644 --- a/pom.xml +++ b/pom.xml @@ -143,13 +143,6 @@ runtime - - commons-vfs - commons-vfs - 20050307052300 - jar - runtime - diff --git a/src/main/java/com/l2jserver/service/ServiceModule.java b/src/main/java/com/l2jserver/service/ServiceModule.java index a447e66e0..9e7c8f43a 100644 --- a/src/main/java/com/l2jserver/service/ServiceModule.java +++ b/src/main/java/com/l2jserver/service/ServiceModule.java @@ -28,7 +28,7 @@ import com.l2jserver.service.core.LoggingService; import com.l2jserver.service.core.threading.ThreadService; import com.l2jserver.service.core.threading.ThreadServiceImpl; 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.JDBCDatabaseService; import com.l2jserver.service.game.AttackService; @@ -77,7 +77,7 @@ public class ServiceModule extends AbstractModule { bind(ServiceManager.class).in(Scopes.SINGLETON); bind(LoggingService.class).to(Log4JLoggingService.class).in( 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( Scopes.SINGLETON); bind(ConfigurationService.class).to(ProxyConfigurationService.class) diff --git a/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java b/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java index b4e4af4b7..6c45b060b 100644 --- a/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java +++ b/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java @@ -24,12 +24,10 @@ import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.l2jserver.service.cache.SoftCacheService.SoftCache; 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 Rogiel * diff --git a/src/main/java/com/l2jserver/service/cache/SoftCacheService.java b/src/main/java/com/l2jserver/service/cache/SoftCacheService.java index e6ed86414..db322bc68 100644 --- a/src/main/java/com/l2jserver/service/cache/SoftCacheService.java +++ b/src/main/java/com/l2jserver/service/cache/SoftCacheService.java @@ -131,7 +131,7 @@ public class SoftCacheService extends AbstractService implements CacheService { * * @author Rogiel */ - public class SoftCache extends AbstractReferenceCache implements + private class SoftCache extends AbstractReferenceCache implements Cache { /** * This class is a {@link SoftReference} with additional responsibility diff --git a/src/main/java/com/l2jserver/service/core/vfs/Java7VFSConfiguration.java b/src/main/java/com/l2jserver/service/core/vfs/Java7VFSConfiguration.java new file mode 100644 index 000000000..82345af0a --- /dev/null +++ b/src/main/java/com/l2jserver/service/core/vfs/Java7VFSConfiguration.java @@ -0,0 +1,25 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.core.vfs; + +/** + * Configuration interface for {@link Java7VFSService}. + * + * @author Rogiel + */ +public interface Java7VFSConfiguration extends VFSConfiguration { +} diff --git a/src/main/java/com/l2jserver/service/core/vfs/VFSServiceImpl.java b/src/main/java/com/l2jserver/service/core/vfs/Java7VFSService.java similarity index 57% rename from src/main/java/com/l2jserver/service/core/vfs/VFSServiceImpl.java rename to src/main/java/com/l2jserver/service/core/vfs/Java7VFSService.java index 6035ef667..b844f1ac7 100644 --- a/src/main/java/com/l2jserver/service/core/vfs/VFSServiceImpl.java +++ b/src/main/java/com/l2jserver/service/core/vfs/Java7VFSService.java @@ -16,73 +16,74 @@ */ package com.l2jserver.service.core.vfs; -import java.io.File; +import java.io.IOException; 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.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; /** - * Default implementation for VFS system + * Implementation of {@link VFSService} using default Java7 APIs. * * @author Rogiel */ -public class VFSServiceImpl extends AbstractService implements VFSService { +public class Java7VFSService extends AbstractService implements VFSService { /** * The logger */ 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 protected void doStart() throws ServiceStartException { - manager = new StandardFileSystemManager(); try { - manager.init(); - manager.setBaseFile(new File("./")); - } catch (FileSystemException e) { - manager = null; + fs = FileSystems.getFileSystem(new URI("file:///")); + } catch (URISyntaxException e) { throw new ServiceStartException(e); } + root = config.getRoot().toAbsolutePath(); + log.debug("Root path is {}", root); } @Override - public FileObject resolve(URI uri) { - return resolve(uri.toString()); - } - - @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; - } + public Path resolve(String path) { + log.debug("Resolving file {}", path); + return root.resolve(path); } @Override protected void doStop() throws ServiceStopException { try { - manager.getBaseFile().close(); - } catch (FileSystemException e) { + fs.close(); + } catch (IOException e) { throw new ServiceStopException(e); } finally { - manager = null; + fs = null; } } } diff --git a/src/main/java/com/l2jserver/service/core/vfs/VFSConfiguration.java b/src/main/java/com/l2jserver/service/core/vfs/VFSConfiguration.java new file mode 100644 index 000000000..a22f0fabc --- /dev/null +++ b/src/main/java/com/l2jserver/service/core/vfs/VFSConfiguration.java @@ -0,0 +1,45 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +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 Rogiel + * @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); +} diff --git a/src/main/java/com/l2jserver/service/core/vfs/VFSService.java b/src/main/java/com/l2jserver/service/core/vfs/VFSService.java index 5cd28558e..86ba08379 100644 --- a/src/main/java/com/l2jserver/service/core/vfs/VFSService.java +++ b/src/main/java/com/l2jserver/service/core/vfs/VFSService.java @@ -16,9 +16,7 @@ */ package com.l2jserver.service.core.vfs; -import java.net.URI; - -import org.apache.commons.vfs.FileObject; +import java.nio.file.Path; import com.l2jserver.service.Service; @@ -29,18 +27,6 @@ import com.l2jserver.service.Service; * @author Rogiel */ public interface VFSService extends Service { - /** - * Resolves an file. If the file cannot be resolved, null will be returned. - *

- * 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. *

@@ -51,5 +37,5 @@ public interface VFSService extends Service { * the file uri as an string * @return the resolved file. Will return null if could not resolve. */ - FileObject resolve(String uri); + Path resolve(String name); } diff --git a/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java b/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java index 55974e83d..b634fd13a 100644 --- a/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java +++ b/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java @@ -18,6 +18,11 @@ package com.l2jserver.service.game.template; import java.io.IOException; 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 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.XmlType; -import org.apache.commons.vfs.FileObject; import org.slf4j.Logger; 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.SkillTemplateIDAdapter; import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; -import com.l2jserver.util.vfs.ExtensionFileSelector; @Depends({ LoggingService.class, VFSService.class, CacheService.class, ConfigurationService.class }) @@ -121,21 +124,35 @@ public class XMLTemplateService extends AbstractService implements unmarshaller.setAdapter(TeleportationTemplateIDAdapter.class, teleportationIdTemplateAdapter); - final FileObject root = vfsService.resolve(config - .getTemplateDirectory()); + final Path templatePath = vfsService.resolve(config + .getTemplateDirectory().toString()); - log.info("Scanning {} for XML templates", root); + log.info("Scanning {} for XML templates", templatePath); - FileObject[] files = root.findFiles(ExtensionFileSelector - .ext("xml")); + Files.walkFileTree(templatePath, new SimpleFileVisitor() { + @Override + public FileVisitResult preVisitDirectory(Path dir, + BasicFileAttributes attrs) throws IOException { + return FileVisitResult.CONTINUE; + } - log.info("Located {} XML template files", files.length); - for (final FileObject file : files) { - loadTemplate(file); - } - final FileObject teleportsXml = root.getParent().resolveFile( + @Override + public FileVisitResult visitFile(Path file, + BasicFileAttributes attrs) throws IOException { + if (!file.toString().endsWith(".xml")) + return FileVisitResult.CONTINUE; + try { + loadTemplate(file); + return FileVisitResult.CONTINUE; + } catch (JAXBException e) { + throw new IOException(e); + } + } + }); + + final Path teleportsXmlPath = templatePath.getParent().resolve( "teleports.xml"); - final InputStream in = teleportsXml.getContent().getInputStream(); + final InputStream in = Files.newInputStream(teleportsXmlPath); try { TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller .unmarshal(in); @@ -159,10 +176,10 @@ public class XMLTemplateService extends AbstractService implements return (T) templates.get(id); } - public void loadTemplate(FileObject file) throws JAXBException, IOException { - Preconditions.checkNotNull(file, "file"); - log.debug("Loading template {}", file); - final InputStream in = file.getContent().getInputStream(); + public void loadTemplate(Path path) throws JAXBException, IOException { + Preconditions.checkNotNull(path, "path"); + log.debug("Loading template {}", path); + final InputStream in = Files.newInputStream(path); try { final Template template = (Template) unmarshaller .unmarshal(in); diff --git a/src/main/java/com/l2jserver/util/transformer/TransformerFactory.java b/src/main/java/com/l2jserver/util/transformer/TransformerFactory.java index b0a143c4f..620d17738 100644 --- a/src/main/java/com/l2jserver/util/transformer/TransformerFactory.java +++ b/src/main/java/com/l2jserver/util/transformer/TransformerFactory.java @@ -20,6 +20,7 @@ import java.io.File; import java.net.InetSocketAddress; import java.net.URI; import java.net.URL; +import java.nio.file.Path; import com.l2jserver.util.transformer.impl.BooleanTransformer; 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.IntegerTransformer; 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.URITransformer; import com.l2jserver.util.transformer.impl.URLTransformer; @@ -73,6 +75,8 @@ public class TransformerFactory { return URITransformer.SHARED_INSTANCE; } else if (type == URL.class) { return URLTransformer.SHARED_INSTANCE; + } else if (type == Path.class) { + return PathTransformer.SHARED_INSTANCE; } return null; } diff --git a/src/main/java/com/l2jserver/util/transformer/impl/PathTransformer.java b/src/main/java/com/l2jserver/util/transformer/impl/PathTransformer.java new file mode 100644 index 000000000..d6a4603ea --- /dev/null +++ b/src/main/java/com/l2jserver/util/transformer/impl/PathTransformer.java @@ -0,0 +1,43 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +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 Rogiel + */ +public class PathTransformer implements Transformer { + 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); + } +} diff --git a/src/main/java/com/l2jserver/util/transformer/impl/URITransformer.java b/src/main/java/com/l2jserver/util/transformer/impl/URITransformer.java index 166c83f34..41cb79d15 100644 --- a/src/main/java/com/l2jserver/util/transformer/impl/URITransformer.java +++ b/src/main/java/com/l2jserver/util/transformer/impl/URITransformer.java @@ -17,7 +17,6 @@ package com.l2jserver.util.transformer.impl; import java.net.URI; -import java.net.URISyntaxException; import com.l2jserver.util.transformer.Transformer; @@ -36,11 +35,6 @@ public class URITransformer implements Transformer { @Override public URI untransform(String value) { - try { - return new URI(value); - } catch (URISyntaxException e) { - return null; - } + return URI.create(value); } - } diff --git a/src/main/java/com/l2jserver/util/vfs/ExtensionFileSelector.java b/src/main/java/com/l2jserver/util/vfs/ExtensionFileSelector.java deleted file mode 100644 index ad6122908..000000000 --- a/src/main/java/com/l2jserver/util/vfs/ExtensionFileSelector.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -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 FILES that has an given extension. - * - * @author Rogiel - */ -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); - } -} diff --git a/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java b/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java index cc615ad85..4e2128641 100644 --- a/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java +++ b/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java @@ -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.XMLSerializer; +@SuppressWarnings("restriction") public class NPCTemplateConverter { private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy"; private static final String JDBC_USERNAME = "l2j"; diff --git a/src/tool/java/com/l2jserver/model/template/SkillTemplateConverter.java b/src/tool/java/com/l2jserver/model/template/SkillTemplateConverter.java index a0806e165..33fdb6070 100644 --- a/src/tool/java/com/l2jserver/model/template/SkillTemplateConverter.java +++ b/src/tool/java/com/l2jserver/model/template/SkillTemplateConverter.java @@ -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.XMLSerializer; +@SuppressWarnings("restriction") public class SkillTemplateConverter { private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy"; private static final String JDBC_USERNAME = "l2j";