Skip to content

get Delegator without calling `main`

Samuel Trégouët requested to merge 134-start-ofbiz-without-main into trunk

Use case for this patch is to run a groovy script like this one :

import static org.apache.ofbiz.entity.DelegatorFactoryImpl.getDelegator

import org.apache.ofbiz.base.container.ComponentContainer
import org.apache.ofbiz.base.container.Container
import org.apache.ofbiz.service.ServiceContainer
import org.apache.ofbiz.base.start.Start
import org.apache.ofbiz.entity.Delegator
import org.apache.ofbiz.entity.util.EntityQuery
import org.apache.ofbiz.base.start.Config
import org.apache.ofbiz.base.start.StartupCommand

StartupCommand command = new StartupCommand.Builder("start").build()

Start.getInstance().setConfig(new Config([command]))


Container containerObj = new ComponentContainer()
containerObj.init([command], "component-container", null)
containerObj = new ServiceContainer()
containerObj.init([command], "service-container", null)


Delegator d = getDelegator("default")


println("will make call on database")
def result = EntityQuery.use(d).from("Product").queryList()
println(result)

and then run it with command like:

java  -cp "/usr/share/groovy/lib/groovy-2.4.17.jar:$HOME/src/Communautaire/build/libs/ofbiz.jar" org.codehaus.groovy.tools.GroovyStarter --main groovy.ui.GroovyMain   -Dofbiz.home=$HOME/src/Communautaire my-script.groovy

related to #134

Edited by Mathieu Lirzin

Merge request reports