() {
+ /**
+ * The Iterator that will return the ID objects
+ */
+ private final Iterator iterator = AbstractDAO.this.selectIDs()
+ .iterator();
+ /**
+ * The last used ID (will be used to remove the last element)
+ */
+ private I lastID;
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public T next() {
+ lastID = iterator.next();
+ if (lastID == null)
+ return null;
+ return select(lastID);
+ }
+
+ @Override
+ public void remove() {
+ AbstractDAO.this.delete(select(lastID));
+ }
+ };
+ }
+
/**
* @return the database service
*/
diff --git a/src/main/java/com/l2jserver/service/database/DataAccessObject.java b/src/main/java/com/l2jserver/service/database/DataAccessObject.java
index dee8cf012..b49ce3afa 100644
--- a/src/main/java/com/l2jserver/service/database/DataAccessObject.java
+++ b/src/main/java/com/l2jserver/service/database/DataAccessObject.java
@@ -16,23 +16,32 @@
*/
package com.l2jserver.service.database;
+import java.util.Iterator;
+import java.util.List;
+
import com.l2jserver.model.Model;
import com.l2jserver.model.id.ID;
import com.l2jserver.service.cache.IgnoreCaching;
/**
- * The DAO interface
+ * The Data Access Object interface used used to retrieve, save and remove
+ * objects from the database. The underlying storage engine can be an plain text
+ * file, SQL Database or an serialized version of the object. This layer will
+ * abstract the translation of the data and ease the transition from one engine
+ * to another.
*
- * TODO make DAO an {@link Iterable}. So if we want to select all objects we can
- * do like this:
- * for(final Object o : daoInstance) {
- * ...
- * }
- *
+ * Every DAO is also an {@link Iterable}. If you wish you can iterate through
+ * all objects in the database very abstractly. But please note that the default
+ * {@link Iterator} implementation in {@link AbstractDAO} will load all the
+ * {@link ID} objects and for every call {@link Iterator#next()}, a new database
+ * query will be made requesting the given object. In a large dataset, this
+ * could be a huge performance issue. DAO implementations are encouraged to
+ * override the iterator implementation with a more specific implementation.
*
* @author Rogiel
*/
-public interface DataAccessObject, I extends ID>> {
+public interface DataAccessObject, I extends ID>> extends
+ Iterable {
/**
* Load the instance represented by id from the database
*
@@ -41,6 +50,14 @@ public interface DataAccessObject, I extends ID>> {
*/
O select(I id);
+ /**
+ * Loads an List of all {@link ID}s in the database
+ *
+ * @return the list containing all {@link ID} objects
+ */
+ @IgnoreCaching
+ List selectIDs();
+
/**
* Save the instance to the database. If a new database entry was created
* returns true.
diff --git a/src/main/java/com/l2jserver/service/database/MySQLDatabaseService.java b/src/main/java/com/l2jserver/service/database/MySQLDatabaseService.java
index e5559b3d5..587720b4c 100644
--- a/src/main/java/com/l2jserver/service/database/MySQLDatabaseService.java
+++ b/src/main/java/com/l2jserver/service/database/MySQLDatabaseService.java
@@ -41,9 +41,10 @@ import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
-import com.l2jserver.model.id.ObjectID;
+import com.l2jserver.model.Model;
+import com.l2jserver.model.Model.ObjectState;
+import com.l2jserver.model.id.ID;
import com.l2jserver.model.id.object.allocator.IDAllocator;
-import com.l2jserver.model.world.WorldObject;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.ServiceStartException;
@@ -268,6 +269,11 @@ public class MySQLDatabaseService extends AbstractService implements
final PreparedStatement st = conn.prepareStatement(query());
this.parametize(st, object);
rows += st.executeUpdate();
+
+ // update object state
+ if (object instanceof Model)
+ ((Model>) object).setObjectState(ObjectState.STORED);
+
final Mapper mapper = keyMapper(object);
if (mapper == null)
continue;
@@ -330,8 +336,11 @@ public class MySQLDatabaseService extends AbstractService implements
final ResultSet rs = st.getResultSet();
while (rs.next()) {
final T obj = mapper().map(rs);
- if (obj != null)
- list.add(obj);
+ if (obj == null)
+ continue;
+ if (obj instanceof Model)
+ ((Model>) obj).setObjectState(ObjectState.STORED);
+ list.add(obj);
}
return list;
}
@@ -385,7 +394,10 @@ public class MySQLDatabaseService extends AbstractService implements
st.execute();
final ResultSet rs = st.getResultSet();
while (rs.next()) {
- return mapper().map(rs);
+ final T object = mapper().map(rs);
+ if (object instanceof Model)
+ ((Model>) object).setObjectState(ObjectState.STORED);
+ return object;
}
return null;
}
@@ -456,27 +468,30 @@ public class MySQLDatabaseService extends AbstractService implements
* @param
* the id type
*/
- public abstract static class CachedMapper>
+ public abstract static class CachedMapper, I extends ID>>
implements Mapper {
/**
* The database service instance
*/
private final MySQLDatabaseService database;
+ private final Mapper idMapper;
+
/**
* Creates a new instance
*
* @param database
* the database service
*/
- public CachedMapper(MySQLDatabaseService database) {
+ public CachedMapper(MySQLDatabaseService database, Mapper idMapper) {
this.database = database;
+ this.idMapper = idMapper;
}
@Override
@SuppressWarnings("unchecked")
public final T map(ResultSet rs) throws SQLException {
- final I id = createID(rs);
+ final I id = idMapper.map(rs);
Preconditions.checkNotNull(id, "id");
if (database.hasCachedObject(id))
@@ -488,16 +503,6 @@ public class MySQLDatabaseService extends AbstractService implements
return object;
}
- /**
- * Creates an ID for an object
- *
- * @param rs
- * the jdbc result set
- * @return the id
- * @throws SQLException
- */
- protected abstract I createID(ResultSet rs) throws SQLException;
-
/**
* Maps an uncached object. Once mapping is complete, it will be added
* to the cache.
diff --git a/src/main/java/com/l2jserver/service/game/scripting/ScriptClassLoader.java b/src/main/java/com/l2jserver/service/game/scripting/ScriptClassLoader.java
index d14cdbd29..30a7aa91e 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/ScriptClassLoader.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/ScriptClassLoader.java
@@ -31,7 +31,6 @@ import java.util.Set;
* @author Rogiel
*/
public abstract class ScriptClassLoader extends URLClassLoader {
-
/**
* Just for compatibility with {@link URLClassLoader}
*
diff --git a/src/main/java/com/l2jserver/service/game/scripting/ScriptCompiler.java b/src/main/java/com/l2jserver/service/game/scripting/ScriptCompiler.java
index 4bfda891b..ff48d49ad 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/ScriptCompiler.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/ScriptCompiler.java
@@ -43,7 +43,7 @@ public interface ScriptCompiler {
* @param files
* list of jar files
*/
- void setLibraires(Iterable files);
+ void setLibraries(Iterable files);
/**
* Compiles single class that is represented as string
@@ -51,10 +51,10 @@ public interface ScriptCompiler {
* @param className
* class name
* @param sourceCode
- * class sourse code
+ * class source code
* @return {@link CompilationResult}
*/
- CompilationResult compile(String className, String sourceCode);
+ CompilationResult compile(String className, byte[] sourceCode);
/**
* Compiles classes that are represented as strings
@@ -67,7 +67,7 @@ public interface ScriptCompiler {
* @throws IllegalArgumentException
* if number of class names != number of sources
*/
- CompilationResult compile(String[] className, String[] sourceCode)
+ CompilationResult compile(String[] className, byte[][] sourceCode)
throws IllegalArgumentException;
/**
@@ -80,7 +80,7 @@ public interface ScriptCompiler {
CompilationResult compile(Iterable compilationUnits);
/**
- * Returns array of supported file types. This files will be threated as
+ * Returns array of supported file types. This files will be treated as
* source files.
*
* @return array of supported file types.
diff --git a/src/main/java/com/l2jserver/service/game/scripting/PreCompiledScriptingService.java b/src/main/java/com/l2jserver/service/game/scripting/ScriptingServiceImpl.java
similarity index 96%
rename from src/main/java/com/l2jserver/service/game/scripting/PreCompiledScriptingService.java
rename to src/main/java/com/l2jserver/service/game/scripting/ScriptingServiceImpl.java
index 60188fc9f..8b709b752 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/PreCompiledScriptingService.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/ScriptingServiceImpl.java
@@ -48,13 +48,13 @@ import com.l2jserver.util.factory.CollectionFactory;
* @author Rogiel
*/
@Depends(LoggingService.class)
-public class PreCompiledScriptingService extends AbstractService implements
+public class ScriptingServiceImpl extends AbstractService implements
ScriptingService {
/**
* Logger for script context
*/
private static final Logger log = LoggerFactory
- .getLogger(PreCompiledScriptingService.class);
+ .getLogger(ScriptingServiceImpl.class);
private final Injector injector;
@@ -64,7 +64,7 @@ public class PreCompiledScriptingService extends AbstractService implements
private final Set contexts = CollectionFactory.newSet();
@Inject
- public PreCompiledScriptingService(Injector injector) {
+ public ScriptingServiceImpl(Injector injector) {
this.injector = injector;
}
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/BinaryClass.java b/src/main/java/com/l2jserver/service/game/scripting/impl/BinaryClass.java
similarity index 88%
rename from src/main/java/com/l2jserver/service/game/scripting/impl/javacc/BinaryClass.java
rename to src/main/java/com/l2jserver/service/game/scripting/impl/BinaryClass.java
index 7bc0d90e2..d882e893b 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/BinaryClass.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/BinaryClass.java
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see .
*/
-package com.l2jserver.service.game.scripting.impl.javacc;
+package com.l2jserver.service.game.scripting.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -58,10 +58,25 @@ public class BinaryClass implements JavaFileObject {
* @param name
* class name
*/
- protected BinaryClass(String name) {
+ public BinaryClass(String name) {
this.name = name;
}
+ /**
+ * Constructor that accepts class name as parameter
+ *
+ * @param name
+ * class name
+ */
+ public BinaryClass(String name, byte[] data) {
+ this.name = name;
+ try {
+ this.baos.write(data);
+ } catch (IOException e) {
+ throw new RuntimeException("Cannor write class data", e);
+ }
+ }
+
/**
* Throws {@link UnsupportedOperationException}
*
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ErrorListener.java b/src/main/java/com/l2jserver/service/game/scripting/impl/ErrorListener.java
similarity index 91%
rename from src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ErrorListener.java
rename to src/main/java/com/l2jserver/service/game/scripting/impl/ErrorListener.java
index b837ca064..85a4ea507 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ErrorListener.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/ErrorListener.java
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see .
*/
-package com.l2jserver.service.game.scripting.impl.javacc;
+package com.l2jserver.service.game.scripting.impl;
import java.util.Locale;
@@ -26,7 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * This class is simple compiler error listener that forwards errors to log4j
+ * This class is simple compiler error listener that forwards errors to slf4j
* logger
*
* @author Rogiel
@@ -40,7 +40,7 @@ public class ErrorListener implements DiagnosticListener {
.getLogger(ErrorListener.class);
/**
- * Reports compilation errors to log4j
+ * Reports compilation errors to slf4j
*
* @param diagnostic
* compiler errors
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromString.java b/src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromByteArray.java
similarity index 67%
rename from src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromString.java
rename to src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromByteArray.java
index b7fbcce6f..033316a60 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromString.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromByteArray.java
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see .
*/
-package com.l2jserver.service.game.scripting.impl.javacc;
+package com.l2jserver.service.game.scripting.impl;
import java.net.URI;
@@ -26,11 +26,11 @@ import javax.tools.SimpleJavaFileObject;
*
* @author Rogiel
*/
-public class JavaSourceFromString extends SimpleJavaFileObject {
+public class JavaSourceFromByteArray extends SimpleJavaFileObject {
/**
* Source code of the class
*/
- private final String code;
+ private final byte[] code;
/**
* Creates new object that contains sources of java class
@@ -40,13 +40,28 @@ public class JavaSourceFromString extends SimpleJavaFileObject {
* @param code
* source code of class
*/
- public JavaSourceFromString(String className, String code) {
+ public JavaSourceFromByteArray(String className, byte[] code) {
super(URI.create("string:///" + className.replace('.', '/')
+ JavaFileObject.Kind.SOURCE.extension),
JavaFileObject.Kind.SOURCE);
this.code = code;
}
+ /**
+ * Creates new object that contains sources of java class
+ *
+ * @param className
+ * class name of class
+ * @param code
+ * source code of class
+ */
+ public JavaSourceFromByteArray(String className, byte[] code,
+ JavaFileObject.Kind kind) {
+ super(URI.create("string:///" + className.replace('.', '/')
+ + JavaFileObject.Kind.SOURCE.extension), kind);
+ this.code = code;
+ }
+
/**
* Returns class source code
*
@@ -56,6 +71,6 @@ public class JavaSourceFromString extends SimpleJavaFileObject {
*/
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
- return code;
+ return new String(code);
}
}
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromFile.java b/src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromFile.java
similarity index 93%
rename from src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromFile.java
rename to src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromFile.java
index e6e962e39..01103e410 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/JavaSourceFromFile.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/JavaSourceFromFile.java
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see .
*/
-package com.l2jserver.service.game.scripting.impl.javacc;
+package com.l2jserver.service.game.scripting.impl;
import java.io.File;
import java.io.IOException;
@@ -30,7 +30,6 @@ import org.apache.commons.io.FileUtils;
* @author Rogiel
*/
public class JavaSourceFromFile extends SimpleJavaFileObject {
-
/**
* Construct a JavaFileObject of the given kind and with the given File.
*
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/ScriptContextImpl.java b/src/main/java/com/l2jserver/service/game/scripting/impl/ScriptContextImpl.java
index 24053f3df..74fa2b238 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/ScriptContextImpl.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/ScriptContextImpl.java
@@ -143,7 +143,7 @@ public class ScriptContextImpl implements ScriptContext {
.getCompilationResult().getClassLoader());
}
- scriptCompiler.setLibraires(libraries);
+ scriptCompiler.setLibraries(libraries);
compilationResult = scriptCompiler.compile(files);
getClassListener().postLoad(compilationResult.getCompiledClasses());
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptClassLoader.java b/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptClassLoader.java
new file mode 100644
index 000000000..13df5b5cd
--- /dev/null
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptClassLoader.java
@@ -0,0 +1,137 @@
+/*
+ * This file is part of l2jserver .
+ *
+ * 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 .
+ */
+package com.l2jserver.service.game.scripting.impl.compiled;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.io.FileUtils;
+
+import com.l2jserver.service.game.scripting.ScriptClassLoader;
+import com.l2jserver.util.factory.CollectionFactory;
+
+/**
+ * This classloader is used to load script classes.
+ *
+ * Due to JavaCompiler limitations we have to keep list of available classes
+ * here.
+ *
+ * @author Rogiel
+ */
+public class PrecompiledScriptClassLoader extends ScriptClassLoader {
+ /**
+ * Map of all loaded classes
+ */
+ private final Map> classes = CollectionFactory.newMap();
+
+ /**
+ * Creates new ScriptClassLoader with given ClassFileManger
+ */
+ PrecompiledScriptClassLoader(final File root) {
+ super(new URL[] {});
+ }
+
+ /**
+ * Creates new ScriptClassLoader with given ClassFileManger and another
+ * classLoader as parent
+ *
+ * @param parent
+ * parent classLoader
+ */
+ PrecompiledScriptClassLoader(ClassLoader parent) {
+ super(new URL[] {}, parent);
+ }
+
+ /**
+ * AddsLibrary jar
+ *
+ * @param file
+ * jar file to add
+ * @throws IOException
+ */
+ @Override
+ public void addLibrary(File file) throws IOException {
+ }
+
+ /**
+ * Loads class from library, parent or compiled
+ *
+ * @param name
+ * class to load
+ * @param file
+ * the class file
+ * @return loaded class
+ * @throws ClassNotFoundException
+ * if class not found
+ * @throws IOException
+ */
+ public Class> loadClass(File file) throws ClassNotFoundException,
+ IOException {
+ byte[] b = FileUtils.readFileToByteArray(file);
+ Class> c = super.defineClass(null, b, 0, b.length);
+ classes.put(c.getName(), c);
+ return c;
+ }
+
+ @Override
+ public synchronized Class> loadClass(String name, boolean resolve)
+ throws ClassNotFoundException {
+ final File file = new File("target/templates");
+ final File classFile = new File(file, name.replaceAll("\\.", "/")
+ + ".class");
+ System.out.println(name + " -> " + classFile);
+ if (!classFile.exists()) {
+ System.out.println("super");
+ return super.loadClass(name, resolve);
+ } else if (classes.containsKey(name)) {
+ return classes.get(name);
+ } else {
+ try {
+ byte[] b = FileUtils.readFileToByteArray(classFile);
+ System.out.println("Defining...");
+ Class> c = super.defineClass(null, b, 0, b.length);
+ classes.put(c.getName(), c);
+ System.out.println(c.getName());
+
+ return c;
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Set getLibraryClasses() {
+ return Collections.emptySet();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Set getCompiledClasses() {
+ return Collections.unmodifiableSet(classes.keySet());
+ }
+}
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompiler.java b/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompiler.java
new file mode 100644
index 000000000..a705058ed
--- /dev/null
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompiler.java
@@ -0,0 +1,93 @@
+/*
+ * This file is part of l2jserver .
+ *
+ * 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 .
+ */
+package com.l2jserver.service.game.scripting.impl.compiled;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import com.l2jserver.service.game.scripting.CompilationResult;
+import com.l2jserver.service.game.scripting.ScriptClassLoader;
+import com.l2jserver.service.game.scripting.ScriptCompiler;
+import com.l2jserver.util.factory.CollectionFactory;
+
+/**
+ * Wrapper for JavaCompiler api
+ *
+ * @author Rogiel
+ */
+public class PrecompiledScriptCompiler implements ScriptCompiler {
+ /**
+ * Parent classloader that has to be used for this compiler
+ */
+ protected ScriptClassLoader parentClassLoader;
+
+ public PrecompiledScriptCompiler() {
+ }
+
+ @Override
+ public void setParentClassLoader(ScriptClassLoader classLoader) {
+ parentClassLoader = classLoader;
+ }
+
+ @Override
+ public void setLibraries(Iterable files) {
+ // do nothing
+ }
+
+ @Override
+ public CompilationResult compile(String className, byte[] sourceCode) {
+ throw new UnsupportedOperationException(
+ "This compiler cannot compile from source");
+ }
+
+ @Override
+ public CompilationResult compile(String[] className, byte[][] sourceCode)
+ throws IllegalArgumentException {
+ throw new UnsupportedOperationException(
+ "This compiler cannot compile from source");
+ }
+
+ @Override
+ public CompilationResult compile(Iterable compilationUnits) {
+ final PrecompiledScriptClassLoader cl = new PrecompiledScriptClassLoader(
+ this.getClass().getClassLoader());
+ final List> classes = CollectionFactory.newList();
+ for (final File file : compilationUnits) {
+ try {
+ classes.add(cl.loadClass(file));
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return new CompilationResult(classes.toArray(new Class>[classes
+ .size()]), cl);
+ }
+
+ /**
+ * Only class files are supported by this compiler
+ *
+ * @return "class";
+ */
+ @Override
+ public String[] getSupportedFileTypes() {
+ return new String[] { "class" };
+ }
+}
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ClassFileManager.java b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ClassFileManager.java
index eb3f69d3c..571b31e01 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ClassFileManager.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ClassFileManager.java
@@ -34,6 +34,7 @@ import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;
import com.l2jserver.service.game.scripting.ScriptClassLoader;
+import com.l2jserver.service.game.scripting.impl.BinaryClass;
import com.l2jserver.util.factory.CollectionFactory;
/**
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptClassLoaderImpl.java b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptClassLoaderImpl.java
index baaee9090..93c382706 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptClassLoaderImpl.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptClassLoaderImpl.java
@@ -35,6 +35,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.l2jserver.service.game.scripting.ScriptClassLoader;
+import com.l2jserver.service.game.scripting.impl.BinaryClass;
import com.l2jserver.util.ClassUtils;
import com.l2jserver.util.factory.CollectionFactory;
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptCompilerImpl.java b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptCompilerImpl.java
index 30092bf07..21c3629e3 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptCompilerImpl.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/ScriptCompilerImpl.java
@@ -33,6 +33,9 @@ import org.slf4j.LoggerFactory;
import com.l2jserver.service.game.scripting.CompilationResult;
import com.l2jserver.service.game.scripting.ScriptClassLoader;
import com.l2jserver.service.game.scripting.ScriptCompiler;
+import com.l2jserver.service.game.scripting.impl.ErrorListener;
+import com.l2jserver.service.game.scripting.impl.JavaSourceFromByteArray;
+import com.l2jserver.service.game.scripting.impl.JavaSourceFromFile;
import com.l2jserver.util.factory.CollectionFactory;
/**
@@ -41,7 +44,6 @@ import com.l2jserver.util.factory.CollectionFactory;
* @author Rogiel
*/
public class ScriptCompilerImpl implements ScriptCompiler {
-
/**
* Logger for this class
*/
@@ -99,7 +101,7 @@ public class ScriptCompilerImpl implements ScriptCompiler {
* list of jar files
*/
@Override
- public void setLibraires(Iterable files) {
+ public void setLibraries(Iterable files) {
libraries = files;
}
@@ -115,8 +117,8 @@ public class ScriptCompilerImpl implements ScriptCompiler {
* if compilation failed with errros
*/
@Override
- public CompilationResult compile(String className, String sourceCode) {
- return compile(new String[] { className }, new String[] { sourceCode });
+ public CompilationResult compile(String className, byte[] sourceCode) {
+ return compile(new String[] { className }, new byte[][] { sourceCode });
}
/**
@@ -134,7 +136,7 @@ public class ScriptCompilerImpl implements ScriptCompiler {
* if compilation failed with errros
*/
@Override
- public CompilationResult compile(String[] classNames, String[] sourceCode)
+ public CompilationResult compile(String[] classNames, byte[][] sourceCode)
throws IllegalArgumentException {
if (classNames.length != sourceCode.length) {
@@ -145,7 +147,7 @@ public class ScriptCompilerImpl implements ScriptCompiler {
List compilationUnits = CollectionFactory.newList();
for (int i = 0; i < classNames.length; i++) {
- JavaFileObject compilationUnit = new JavaSourceFromString(
+ JavaFileObject compilationUnit = new JavaSourceFromByteArray(
classNames[i], sourceCode[i]);
compilationUnits.add(compilationUnit);
}
@@ -212,7 +214,7 @@ public class ScriptCompilerImpl implements ScriptCompiler {
}
/**
- * Reolves list of classes by their names
+ * Resolves list of classes by their names
*
* @param classNames
* names of the classes
diff --git a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/VirtualClassURLConnection.java b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/VirtualClassURLConnection.java
index 3b1787a3a..2b50fe402 100644
--- a/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/VirtualClassURLConnection.java
+++ b/src/main/java/com/l2jserver/service/game/scripting/impl/javacc/VirtualClassURLConnection.java
@@ -22,6 +22,8 @@ import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
+import com.l2jserver.service.game.scripting.impl.BinaryClass;
+
/**
* This class represents URL Connection that is used to "connect" to scripts
* binary data that was loaded by specified {@link ScriptCompilerImpl}.
diff --git a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java
index 182a3e03a..b5530f788 100644
--- a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java
+++ b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java
@@ -82,7 +82,8 @@ public class CachedWorldIDService extends AbstractService implements
@Inject
public CachedWorldIDService(CacheService cacheService,
- IDAllocator allocator, CharacterDAO characterDao, ItemDAO itemDao, NPCDAO npcDao) {
+ IDAllocator allocator, CharacterDAO characterDao, ItemDAO itemDao,
+ NPCDAO npcDao) {
this.cacheService = cacheService;
this.allocator = allocator;
this.characterDao = characterDao;
@@ -103,9 +104,9 @@ public class CachedWorldIDService extends AbstractService implements
@Override
public void load() {
- load(characterDao.listIDs());
- load(itemDao.listIDs());
- //load(npcDao.listIDs());
+ load(characterDao.selectIDs());
+ load(itemDao.selectIDs());
+ load(npcDao.selectIDs());
loaded = true;
}
diff --git a/src/main/java/com/l2jserver/util/calculator/AbstractFunction.java b/src/main/java/com/l2jserver/util/calculator/AbstractFunction.java
new file mode 100644
index 000000000..72d66dcfa
--- /dev/null
+++ b/src/main/java/com/l2jserver/util/calculator/AbstractFunction.java
@@ -0,0 +1,36 @@
+/*
+ * This file is part of l2jserver .
+ *
+ * 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 .
+ */
+package com.l2jserver.util.calculator;
+
+/**
+ * @author Rogiel
+ *
+ */
+public abstract class AbstractFunction implements
+ Function {
+ private final int order;
+
+ public AbstractFunction(int order) {
+ this.order = order;
+ }
+
+ @Override
+ public int order() {
+ return order;
+ }
+
+}
diff --git a/src/main/java/com/l2jserver/util/calculator/Calculator.java b/src/main/java/com/l2jserver/util/calculator/Calculator.java
index 357cad207..e8b6b5296 100644
--- a/src/main/java/com/l2jserver/util/calculator/Calculator.java
+++ b/src/main/java/com/l2jserver/util/calculator/Calculator.java
@@ -16,8 +16,8 @@
*/
package com.l2jserver.util.calculator;
-import java.util.Arrays;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import com.l2jserver.util.factory.CollectionFactory;
@@ -28,18 +28,19 @@ import com.l2jserver.util.factory.CollectionFactory;
*
* @author Rogiel
*/
-public class Calculator implements Function {
+public class Calculator extends
+ AbstractFunction {
/**
* List of operations in this calculator
*/
- private final List functions = CollectionFactory
- .newList();
+ private final List> functions = CollectionFactory.newList();
/**
* Creates a new empty calculator. Functions can be add using
* {@link #add(int, Function)}.
*/
public Calculator() {
+ super(0x00);
}
/**
@@ -49,9 +50,10 @@ public class Calculator implements Function {
* @param functions
* the calculator functions
*/
- public Calculator(Function... functions) {
- for (int i = 0; i < functions.length; i++) {
- this.functions.add(new FunctionContainer(i, functions[i]));
+ public Calculator(Function... functions) {
+ super(0x00);
+ for (final Function func : functions) {
+ this.functions.add(func);
}
}
@@ -66,14 +68,14 @@ public class Calculator implements Function {
* @param function
* the operation
*/
- public void add(int order, Function function) {
- functions.add(new FunctionContainer(order, function));
- Collections.sort(functions);
+ public void add(Function function) {
+ functions.add(function);
+ Collections.sort(functions, FunctionOrderComparator.SHARED_INSTANCE);
}
/**
- * Imports all functions in the given calculator. This is useful to
- * preserve right calculation ordering but changes to original
+ * Imports all functions from the given calculator. This is useful
+ * to preserve right calculation ordering but changes to original
* calculator will no reflect in this one.
*
* This method will heuristically search for nested calculators.
@@ -81,101 +83,48 @@ public class Calculator implements Function {
* @param calculator
* the calculator
*/
- public void importFunctions(Calculator calculator) {
- for (final FunctionContainer container : calculator.functions) {
- if (container.function instanceof Calculator) {
- importFunctions((Calculator) container.function);
+ public void importFunctions(Calculator calculator) {
+ for (final Function function : calculator.functions) {
+ if (function instanceof Calculator) {
+ importFunctions((Calculator) function);
} else {
- functions.add(container);
+ functions.add(function);
}
}
}
/**
- * Computes the result and output it. Input value is 0.
+ * Removes all imported functions from the given calculator.
+ *
+ * This method will heuristically search for nested calculators.
*
- * @return the computed value
- * @see #calculate(Double)
+ * @param calculator
+ * the calculator
*/
- public double calculate() {
- return calculate(0.00);
+ public void removeFunctions(Calculator calculator) {
+ for (final Function function : calculator.functions) {
+ if (function instanceof Calculator) {
+ removeFunctions((Calculator) function);
+ } else {
+ functions.remove(function);
+ }
+ }
}
@Override
- public Double calculate(Double input) {
- double result = input;
- for (final FunctionContainer container : functions) {
- result = container.function.calculate(result);
+ public void calculate(T ctx) {
+ for (final Function function : functions) {
+ function.calculate(ctx);
}
- return result;
}
- /**
- * -- Internal use only --
Container used to sort calculator
- * functions. This class implements {@link Comparable} and can be used to
- * sort lists using {@link Collections#sort(List)} or
- * {@link Arrays#sort(Object[])}.
- *
- * @author Rogiel
- */
- private static class FunctionContainer implements
- Comparable {
- /**
- * The execution order
- */
- protected final int order;
- /**
- * The function object
- */
- protected final Function function;
-
- /**
- * Creates a new instance
- *
- * @param order
- * the execution order
- * @param function
- * the function
- */
- public FunctionContainer(int order, Function function) {
- this.order = order;
- this.function = function;
- }
+ public static class FunctionOrderComparator implements
+ Comparator> {
+ public static final FunctionOrderComparator SHARED_INSTANCE = new FunctionOrderComparator();
@Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result
- + ((function == null) ? 0 : function.hashCode());
- result = prime * result + order;
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- FunctionContainer other = (FunctionContainer) obj;
- if (function == null) {
- if (other.function != null)
- return false;
- } else if (!function.equals(other.function))
- return false;
- if (order != other.order)
- return false;
- return true;
- }
-
- @Override
- public int compareTo(FunctionContainer o) {
- if (this.equals(o))
- return 0;
- return this.order - o.order;
+ public int compare(Function> func1, Function> func2) {
+ return (func1.order() - func2.order());
}
}
}
\ No newline at end of file
diff --git a/src/main/java/com/l2jserver/util/calculator/CalculatorContext.java b/src/main/java/com/l2jserver/util/calculator/CalculatorContext.java
new file mode 100644
index 000000000..47b11ce1f
--- /dev/null
+++ b/src/main/java/com/l2jserver/util/calculator/CalculatorContext.java
@@ -0,0 +1,25 @@
+/*
+ * This file is part of l2jserver .
+ *
+ * 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 .
+ */
+package com.l2jserver.util.calculator;
+
+/**
+ * @author Rogiel
+ *
+ */
+public class CalculatorContext {
+ public double result;
+}
diff --git a/src/main/java/com/l2jserver/util/calculator/DivisionFunction.java b/src/main/java/com/l2jserver/util/calculator/DivisionFunction.java
index cf0ebea85..5a0351b32 100644
--- a/src/main/java/com/l2jserver/util/calculator/DivisionFunction.java
+++ b/src/main/java/com/l2jserver/util/calculator/DivisionFunction.java
@@ -22,18 +22,19 @@ package com.l2jserver.util.calculator;
*
* @author Rogiel
*/
-public class DivisionFunction implements Function {
+public class DivisionFunction extends AbstractFunction {
/**
* The value
*/
private final double value;
- public DivisionFunction(double value) {
+ public DivisionFunction(int order, double value) {
+ super(order);
this.value = value;
}
@Override
- public Double calculate(Double value) {
- return value / this.value;
+ public void calculate(CalculatorContext ctx) {
+ ctx.result /= this.value;
}
}
diff --git a/src/main/java/com/l2jserver/util/calculator/Function.java b/src/main/java/com/l2jserver/util/calculator/Function.java
index e0ed0fce5..442a56397 100644
--- a/src/main/java/com/l2jserver/util/calculator/Function.java
+++ b/src/main/java/com/l2jserver/util/calculator/Function.java
@@ -21,7 +21,7 @@ package com.l2jserver.util.calculator;
*
* @author Rogiel
*/
-public interface Function {
+public interface Function {
/**
* Performs the operation in the calculation process.
*
@@ -33,5 +33,10 @@ public interface Function