mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-10 09:22:49 +00:00
Added Java 7 support and Java 7 VFSServive using native APIs
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
|
||||
@@ -131,7 +131,7 @@ public class SoftCacheService extends AbstractService implements CacheService {
|
||||
*
|
||||
* @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> {
|
||||
/**
|
||||
* This class is a {@link SoftReference} with additional responsibility
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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 <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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 <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
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.
|
||||
* <p>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Path>() {
|
||||
@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);
|
||||
|
||||
Reference in New Issue
Block a user