1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +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

@@ -28,8 +28,6 @@ import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
import com.l2jserver.model.id.template.ItemTemplateID;
import com.l2jserver.model.id.template.NPCTemplateID;
import com.l2jserver.model.world.Actor.ActorSex;
@@ -240,7 +238,6 @@ public class NPCTemplate extends ActorTemplate<NPC> {
@XmlAttribute(name = "id")
protected String id = null;
@XmlValue
@XmlCDATA
protected String html = null;
}

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);
}

View File

@@ -18,6 +18,8 @@ package com.l2jserver.util.transformer;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer;
@@ -29,6 +31,8 @@ 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.ShortTransformer;
import com.l2jserver.util.transformer.impl.URITransformer;
import com.l2jserver.util.transformer.impl.URLTransformer;
/**
* The {@link TransformerFactory} return the transformer instance for any given
@@ -65,6 +69,10 @@ public class TransformerFactory {
return FileTransformer.SHARED_INSTANCE;
} else if (type == Class.class) {
return ClassTransformer.SHARED_INSTANCE;
} else if (type == URI.class) {
return URITransformer.SHARED_INSTANCE;
} else if (type == URL.class) {
return URLTransformer.SHARED_INSTANCE;
}
return null;
}

View File

@@ -0,0 +1,46 @@
/*
* 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.net.URI;
import java.net.URISyntaxException;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link URI} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class URITransformer implements Transformer<URI> {
public static final URITransformer SHARED_INSTANCE = new URITransformer();
@Override
public String transform(URI value) {
return value.toString();
}
@Override
public URI untransform(String value) {
try {
return new URI(value);
} catch (URISyntaxException e) {
return null;
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link URI} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class URLTransformer implements Transformer<URL> {
public static final URLTransformer SHARED_INSTANCE = new URLTransformer();
@Override
public String transform(URL value) {
return value.toString();
}
@Override
public URL untransform(String value) {
try {
return new URL(value);
} catch (MalformedURLException e) {
return null;
}
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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);
}
}