I've had similar scenarios on quite a few occassions. I always handle this by creating a singleton class (IndexHandler) on startup, that you load with an array or hashmap of your in-memory entities.
You can then create a few java-actions that call methods of this singleton (add entity, getSize, setArray, Search, etc).
Here is a very simple example that doesn't handle search in the java, but merely returns the searcheable list (I tend to try and keep as much of my logic in Mendix as possible, for easy maintenance by other devs):
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.logging.ILogNode;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
public class IndexHandler {
private IContext context;
private static IndexHandler indexHandler;
private static boolean indexLoaded = false;
private ArrayList<IMendixObject> searchIndexList;
private static final ILogNode oLogNode = Core.getLogger("SearchIndex");
private GregorianCalendar lastChanged;
public static IndexHandler getInstance(IContext context) throws Exception {
if(IndexHandler.indexHandler == null)
{
IndexHandler.indexHandler = new IndexHandler(context);
}
return IndexHandler.indexHandler;
}
private IndexHandler(IContext context) {
this.context = context;
try {
searchIndexList = getSearchTable();
oLogNode.info("IndexHandler singleton loaded");
lastChanged = new GregorianCalendar();
setIndexLoaded(true);
} catch (Exception ex) {
oLogNode.error("Error loading and setting index file: " + ex);
}
}
public boolean isIndexLoaded() {
return indexLoaded;
}
public int countIndex () {
return searchIndexList.size();
}
private void setIndexLoaded(boolean indexLoaded) {
IndexHandler.indexLoaded = indexLoaded;
}
public ArrayList<IMendixObject> getSearchIndex () {
return this.searchIndexList;
}
public void addToIndex (IMendixObject searchIndex) {
lastChanged = new GregorianCalendar();
searchIndexList.add(searchIndex);
}
public void removeFromIndex (IMendixObject searchIndex) {
lastChanged = new GregorianCalendar();
searchIndexList.remove(searchIndex);
}
public GregorianCalendar getLastChanged() {
return lastChanged;
}
public void forceReIndex () throws CoreException {
lastChanged = new GregorianCalendar();
searchIndexList = getSearchTable();
}
private ArrayList<IMendixObject> getSearchTable() throws CoreException
{
Map<String,Object> parameters = new HashMap<String, Object>();
ArrayList<IMendixObject> outputList = Core.execute(context, "MyApp.SUB_Zoektabel_Build", parameters);
return outputList;
}
}