Implemented ebeans

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-04-01 16:04:43 +01:00
parent 41bcf14b27
commit 0373e53844
6 changed files with 82 additions and 1 deletions

View File

@ -34,5 +34,10 @@
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.avaje</groupId>
<artifactId>ebean</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
</project>

View File

@ -31,7 +31,7 @@ public final class Bukkit {
*/
public static void setServer(Server server) {
if (Bukkit.server != null) {
throw new UnsupportedOperationException("Cannot redelcare singleton Server");
throw new UnsupportedOperationException("Cannot redefine singleton Server");
}
Bukkit.server = server;

View File

@ -1,6 +1,7 @@
package org.bukkit;
import com.avaje.ebean.config.ServerConfig;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.logging.Logger;
@ -186,4 +187,11 @@ public interface Server {
* @throws CommandException Thrown when the executor for the given command fails with an unhandled exception
*/
public boolean dispatchCommand(CommandSender sender, String commandLine);
/**
* Populates a given {@link ServerConfig} with values attributes to this server
*
* @param config ServerConfig to populate
*/
public void configureDbConfig(ServerConfig config);
}

View File

@ -1,6 +1,7 @@
package org.bukkit.plugin;
import com.avaje.ebean.EbeanServer;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.command.CommandExecutor;
@ -80,4 +81,11 @@ public interface Plugin extends CommandExecutor {
* @param canNag is this plugin still naggable?
*/
public void setNaggable(boolean canNag);
/**
* Gets the {@link EbeanServer} tied to this plugin
*
* @return Ebean server instance
*/
public EbeanServer getDatabase();
}

View File

@ -23,6 +23,7 @@ public final class PluginDescriptionFile {
private String description = null;
private ArrayList<String> authors = new ArrayList<String>();
private String website = null;
private boolean database = false;
@SuppressWarnings("unchecked")
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
@ -120,6 +121,14 @@ public final class PluginDescriptionFile {
return website;
}
public boolean isDatabaseEnabled() {
return database;
}
public void setDatabaseEnabled(boolean database) {
this.database = database;
}
private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
try {
name = map.get("name").toString();
@ -164,6 +173,14 @@ public final class PluginDescriptionFile {
}
}
if (map.containsKey("database")) {
try {
database = (Boolean)map.get("database");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "database is of wrong type");
}
}
if (map.containsKey("website")) {
try {
website = (String)map.get("website");
@ -204,6 +221,7 @@ public final class PluginDescriptionFile {
map.put("name", name);
map.put("main", main);
map.put("version", version);
map.put("database", database);
if (commands != null) map.put("command", commands);
if (depend != null) map.put("depend", depend);

View File

@ -1,7 +1,13 @@
package org.bukkit.plugin.java;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -25,6 +31,7 @@ public abstract class JavaPlugin implements Plugin {
private ClassLoader classLoader = null;
private Configuration config = null;
private boolean naggable = true;
private EbeanServer ebean = null;
public JavaPlugin() {
}
@ -147,9 +154,41 @@ public abstract class JavaPlugin implements Plugin {
this.classLoader = classLoader;
this.config = new Configuration(new File(dataFolder, "config.yml"));
this.config.load();
if (description.isDatabaseEnabled()) {
ServerConfig db = new ServerConfig();
db.setDefaultServer(false);
db.setRegister(false);
db.setClasses(getDatabaseClasses());
db.setName(description.getName());
server.configureDbConfig(db);
DataSourceConfig ds = db.getDataSourceConfig();
ds.setUrl(replaceDatabaseString(ds.getUrl()));
getDataFolder().mkdirs();
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
ebean = EbeanServerFactory.create(db);
Thread.currentThread().setContextClassLoader(previous);
}
}
}
/**
* Provides a list of all classes that should be persisted in the database
*
* @return List of Classes that are Ebeans
*/
public List<Class<?>> getDatabaseClasses() {
return new ArrayList<Class<?>>();
}
private String replaceDatabaseString(String input) {
return input.replaceAll("\\{DIR\\}", getDataFolder().getPath().replaceAll("\\\\", "/"));
}
/**
* Gets the initialization status of this plugin
*
@ -199,4 +238,7 @@ public abstract class JavaPlugin implements Plugin {
this.naggable = canNag;;
}
public EbeanServer getDatabase() {
return ebean;
}
}