Java tutorial
/** * Copyright (C) 2009 Guenther Niess. All rights reserved. * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package org.frogx.service.core; import java.io.File; import java.lang.reflect.Constructor; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.frogx.service.api.MUGManager; import org.frogx.service.api.MUGMatch; import org.frogx.service.api.MUGRoom; import org.frogx.service.api.MultiUserGame; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Laiho */ public class DefaultMultiUserGame implements MultiUserGame { private static final Logger log = LoggerFactory.getLogger(DefaultMultiUserGame.class); private String matchClassName; private ClassLoader matchClassloader; /** * @uml.property name="mugManager" * @uml.associationEnd */ private MUGManager mugManager; private Map<String, MUGMatch> matches = new ConcurrentHashMap<String, MUGMatch>(); /** * @uml.property name="name" */ private String name; /** * @uml.property name="namespace" */ private String namespace; /** * @uml.property name="description" */ private String description; /** * @uml.property name="category" */ private String category; public DefaultMultiUserGame(File pluginDirectory, MUGManager mugManager) throws DocumentException { this.mugManager = mugManager; readGameDescriptor(new File(pluginDirectory, "frogx.xml")); if (matchClassloader == null) { matchClassloader = DefaultMultiUserGame.class.getClassLoader(); } if (matchClassloader == null) { matchClassloader = ClassLoader.getSystemClassLoader(); } } /** * Read the provided game descriptor and update the plug-in attributes. * * @param gameDescriptor The file which describes the game descriptor. * @throws DocumentException if the file can't be read. */ @SuppressWarnings("unchecked") public void readGameDescriptor(File gameDescriptor) throws DocumentException { SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); Document document = reader.read(gameDescriptor); Element root = document.getRootElement(); Element type = root.element("type"); if (type == null || "match".equals(type.getText().toLowerCase())) { for (Iterator i = root.elementIterator(); i.hasNext();) { Element child = (Element) i.next(); if ("match".equals(child.getName())) { matchClassName = child.getText(); } else if ("namespace".equals(child.getName())) { namespace = child.getText(); } else if ("name".equals(child.getName())) { name = child.getText(); } else if ("description".equals(child.getName())) { description = child.getText(); } else if ("category".equals(child.getName())) { category = child.getText(); } } } else { throw new IllegalArgumentException("The descriptor don't provide a match."); } // verify if (matchClassName == null || matchClassName.trim().length() == 0) { throw new IllegalArgumentException("The descriptor don't provide a match class."); } if (namespace == null || namespace.trim().length() == 0) { throw new IllegalArgumentException("The descriptor don't provide a namespace."); } if (description == null || description.trim().length() == 0) { description = name; } } /** * @param classloader * @uml.property name="matchClassloader" */ public void setMatchClassloader(ClassLoader classloader) { if (classloader == null) { throw new IllegalArgumentException("No classloader is set."); } this.matchClassloader = classloader; } /** * Create a match which implements the game logic within a * game room. * * @param room The MUGRoom which offers the match. * @return The created MUGMatch. */ @SuppressWarnings("unchecked") public MUGMatch createMatch(MUGRoom room) { log.debug("Create a " + description + " match: " + room.getJID().toBareJID()); MUGMatch match = null; try { Class matchClass = matchClassloader.loadClass(matchClassName); Class[] parameterTypes = new Class[2]; parameterTypes[0] = MUGRoom.class; parameterTypes[1] = MUGManager.class; Constructor consttructor = matchClass.getConstructor(parameterTypes); Object[] parameter = new Object[2]; parameter[0] = room; parameter[1] = mugManager; match = (MUGMatch) consttructor.newInstance(parameter); } catch (Exception e) { log.error("Can't create a " + description + " match.", e); } if (match != null) matches.put(room.getJID().toBareJID(), match); return match; } /** * Destroy the match in the specified game room. * * @param room The MUGRoom which hosts the match. */ public void destroyMatch(MUGRoom room) { log.debug("Delete the " + description + " match: " + room.getJID().toBareJID()); MUGMatch match = matches.get(room.getJID().toBareJID()); if (match != null) { match.destroy(); matches.remove(room.getJID().toBareJID()); } } /** * Get the name of the game. * @return the name of the game. * @uml.property name="name" */ public String getName() { return name; } /** * Get the xml namespace of the game. * @return the xml namespace of the game. * @uml.property name="namespace" */ public String getNamespace() { return namespace; } /** * Get a short human readable description of the game. * @return the description of the game. * @uml.property name="description" */ public String getDescription() { return description; } /** * Get the category of the game e.g. board, cards, etc. * @return the category of the game. * @uml.property name="category" */ public String getCategory() { return category; } }