1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-10 09:22:49 +00:00

VFSService and pom.xml updated

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-26 19:06:37 -03:00
parent 566c4e2037
commit a162b9d6d5
17 changed files with 450 additions and 105 deletions

View File

@@ -25,6 +25,8 @@ import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.configuration.ProxyConfigurationService;
import com.l2jserver.service.core.Log4JLoggingService;
import com.l2jserver.service.core.LoggingService;
import com.l2jserver.service.core.vfs.VFSService;
import com.l2jserver.service.core.vfs.VFSServiceImpl;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.MySQLDatabaseService;
import com.l2jserver.service.game.character.CharacterService;
@@ -63,6 +65,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(ConfigurationService.class).to(ProxyConfigurationService.class)
.in(Scopes.SINGLETON);
bind(CacheService.class).to(EhCacheService.class).in(Scopes.SINGLETON);

View File

@@ -0,0 +1,55 @@
/*
* 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 org.apache.commons.vfs.FileObject;
import com.l2jserver.service.Service;
/**
* The VFS service provides access to files. With this service is possible to
* change the location of files.
*
* @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>
* Please note that event if the file DOES NOT exists a valid object will be
* returned.
*
* @param uri
* the file uri as an string
* @return the resolved file. Will return null if could not resolve.
*/
FileObject resolve(String uri);
}

View File

@@ -0,0 +1,79 @@
/*
* 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.io.File;
import java.net.URI;
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 com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
/**
* Default implementation for VFS system
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class VFSServiceImpl extends AbstractService implements VFSService {
/**
* The CommonsVFS {@link FileSystemManager}
*/
private DefaultFileSystemManager manager;
@Override
protected void doStart() throws ServiceStartException {
manager = new StandardFileSystemManager();
try {
manager.init();
manager.setBaseFile(new File("./"));
} catch (FileSystemException e) {
manager = null;
throw new ServiceStartException(e);
}
}
@Override
public FileObject resolve(URI uri) {
return resolve(uri.toString());
}
@Override
public FileObject resolve(String uri) {
try {
return manager.resolveFile(uri);
} catch (FileSystemException e) {
return null;
}
}
@Override
protected void doStop() throws ServiceStopException {
try {
manager.getBaseFile().close();
} catch (FileSystemException e) {
throw new ServiceStopException(e);
} finally {
manager = null;
}
}
}

View File

@@ -16,8 +16,8 @@
*/
package com.l2jserver.service.game.template;
import java.io.File;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@@ -30,7 +30,7 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.vfs.FileObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,17 +48,21 @@ import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.core.LoggingService;
import com.l2jserver.service.core.vfs.VFSService;
import com.l2jserver.util.factory.CollectionFactory;
import com.l2jserver.util.jaxb.CharacterTemplateIDAdapter;
import com.l2jserver.util.jaxb.ItemTemplateIDAdapter;
import com.l2jserver.util.jaxb.NPCTemplateIDAdapter;
import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter;
import com.l2jserver.util.vfs.ExtensionFileSelector;
@Depends({ LoggingService.class, ConfigurationService.class })
@Depends({ LoggingService.class, VFSService.class, ConfigurationService.class })
public class XMLTemplateService extends AbstractService implements
TemplateService {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final VFSService vfsService;
private final XMLTemplateServiceConfiguration config;
private final NPCTemplateIDAdapter npcTemplateIdAdapter;
private final ItemTemplateIDAdapter itemTemplateIdAdapter;
@@ -72,11 +76,13 @@ public class XMLTemplateService extends AbstractService implements
private Map<TemplateID, Template> templates = CollectionFactory.newMap();
@Inject
public XMLTemplateService(ConfigurationService configService,
public XMLTemplateService(final VFSService vfsService,
ConfigurationService configService,
NPCTemplateIDAdapter npcTemplateIdAdapter,
ItemTemplateIDAdapter itemTemplateIdAdapter,
CharacterTemplateIDAdapter charIdTemplateAdapter,
TeleportationTemplateIDAdapter teleportationIdTemplateAdapter) {
this.vfsService = vfsService;
this.config = configService.get(XMLTemplateServiceConfiguration.class);
this.npcTemplateIdAdapter = npcTemplateIdAdapter;
this.itemTemplateIdAdapter = itemTemplateIdAdapter;
@@ -91,7 +97,7 @@ public class XMLTemplateService extends AbstractService implements
context = JAXBContext.newInstance(CharacterTemplate.class,
NPCTemplate.class, ItemTemplate.class,
TeleportationTemplateContainer.class);
log.debug("Creating Unmarshaller instance");
unmarshaller = context.createUnmarshaller();
@@ -104,24 +110,33 @@ public class XMLTemplateService extends AbstractService implements
unmarshaller.setAdapter(TeleportationTemplateIDAdapter.class,
teleportationIdTemplateAdapter);
@SuppressWarnings("unchecked")
Collection<File> files = FileUtils
.listFiles(config.getTemplateDirectory(),
new String[] { "xml" }, true);
log.debug("Located {} XML template files", files.size());
for (final File file : files) {
log.debug("Loading template {}", file);
final FileObject root = vfsService.resolve(config
.getTemplateDirectory());
log.info("Scanning {} for XML templates", root);
FileObject[] files = root.findFiles(ExtensionFileSelector
.ext("xml"));
log.info("Located {} XML template files", files.length);
for (final FileObject file : files) {
loadTemplate(file);
}
TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller
.unmarshal(new File(config.getTemplateDirectory(),
"../teleports.xml"));
for (final TeleportationTemplate template : container.templates) {
templates.put(template.getID(), template);
final FileObject teleportsXml = root.getParent().resolveFile(
"teleports.xml");
final InputStream in = teleportsXml.getContent().getInputStream();
try {
TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller
.unmarshal(in);
for (final TeleportationTemplate template : container.templates) {
templates.put(template.getID(), template);
}
} finally {
in.close();
}
} catch (JAXBException e) {
e.printStackTrace();
System.exit(0);
throw new ServiceStartException(e);
} catch (IOException e) {
throw new ServiceStartException(e);
}
}
@@ -133,11 +148,18 @@ public class XMLTemplateService extends AbstractService implements
return (T) templates.get(id);
}
public void loadTemplate(File file) throws JAXBException {
public void loadTemplate(FileObject file) throws JAXBException, IOException {
Preconditions.checkNotNull(file, "file");
final Template<?> template = (Template<?>) unmarshaller.unmarshal(file);
if (template.getID() != null)
templates.put(template.getID(), template);
log.debug("Loading template {}", file);
final InputStream in = file.getContent().getInputStream();
try {
final Template<?> template = (Template<?>) unmarshaller
.unmarshal(in);
if (template.getID() != null)
templates.put(template.getID(), template);
} finally {
in.close();
}
}
public void removeTemplate(Template<?> template) {

View File

@@ -16,16 +16,16 @@
*/
package com.l2jserver.service.game.template;
import java.io.File;
import java.net.URI;
import com.l2jserver.service.configuration.Configuration;
import com.l2jserver.service.configuration.Configuration.ConfigurationName;
@ConfigurationName("template")
public interface XMLTemplateServiceConfiguration extends Configuration {
@ConfigurationPropertyGetter(name = "template.directory", defaultValue = "data/templates")
File getTemplateDirectory();
@ConfigurationPropertyGetter(name = "template.directory", defaultValue = "zip:data/templates.zip")
URI getTemplateDirectory();
@ConfigurationPropertySetter(name = "template.directory")
void setTemplateDirectory(File file);
void setTemplateDirectory(URI file);
}