测试ofbiz3 的entity. 前期工作:
1:请先用jbx建立ofbiz3的应用 2:在D:\work\ofbiz\base\config中建一个ofbiz-test.xml文件,内容为:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ofbiz-containers PUBLIC "-//OFBiz//DTD Containers Config//EN" "http://www.ofbiz.org/dtds/ofbiz-containers.dtd"> <ofbiz-containers> <!-- load the ofbiz components (always first) --> <container name="component-container" class="org.ofbiz.base.container.ComponentContainer"/>
<!-- load the cached classloader container (always second) --> <container name="classloader-container" class="org.ofbiz.base.container.ClassLoaderContainer"/>
<!-- load JNDI/JOTM --> <container name="jotm-container" class="org.ofbiz.entity.transaction.JotmContainer"/>
<!-- RMI Service Dispatcher --> <container name="rmi-dispatcher" class="org.ofbiz.service.rmi.RmiServiceContainer"> <property name="lookup-name" value="RMIDispatcher"/> <property name="delegator-name" value="default"/> <property name="client-factory" value="org.ofbiz.service.rmi.socket.CompressionClientSocketFactory"/> <property name="server-factory" value="org.ofbiz.service.rmi.socket.CompressionServerSocketFactory"/> </container>
</ofbiz-containers>
3:copy D:\work\ofbiz\base\src\start\org\ofbiz\base\start\Start.properties 到src目录
4:修改Start.properties文件: ofbiz.container.config=base/config/ofbiz-test.xml
编写java文件:
package test;
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.ServerSocket; import java.net.ConnectException; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Properties; import org.ofbiz.base.start.*; import org.ofbiz.entity.*; import org.ofbiz.base.util.*;
public class TestEntity {
public static final String CONFIG_FILE = "start.properties"; public static final String SHUTDOWN_COMMAND = "SHUTDOWN"; public static final String STATUS_COMMAND = "STATUS";
private Classpath classPath = new Classpath(System.getProperty( "java.class.path"));
private List loaders = null; private Start.Config config = null;
public TestEntity(String configFile) throws IOException { if (configFile == null) { configFile = CONFIG_FILE; }
this.config = new Start.Config(configFile); this.loaders = new ArrayList(); }
private void loadLibs(String path) throws Exception { File libDir = new File(path); if (libDir.exists()) { File files[] = libDir.listFiles(); for (int i = 0; i < files.length; i++) { String fileName = files[i].getName(); if (files[i].isDirectory() && !"CVS".equals(fileName)) { loadLibs(files[i].getCanonicalPath()); } else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { classPath.addComponent(files[i]); } } } }
private void startServer() throws Exception { // load the lib directory loadLibs(config.baseLib);
// load the ofbiz-base.jar classPath.addComponent(config.baseJar);
// load the config directory classPath.addComponent(config.baseConfig);
// set the classpath/classloader System.setProperty("java.class.path", classPath.toString()); ClassLoader classloader = classPath.getClassLoader(); Thread.currentThread().setContextClassLoader(classloader);
// stat the log directory boolean createdDir = false; File logDir = new File(config.logDir); if (!logDir.exists()) { logDir.mkdir(); createdDir = true; }
// start the loaders Iterator li = config.loaders.iterator(); while (li.hasNext()) { String loaderClassName = (String) li.next(); try { Class loaderClass = classloader.loadClass(loaderClassName); StartupLoader loader = (StartupLoader) loaderClass.newInstance(); loader.load(config); loaders.add(loader); } catch (Exception e) { e.printStackTrace(); System.exit(99); } } }
public void testEntity() throws Exception { System.out.println("Entered testFindByPrimaryKey"); //Instantiate the delegator. GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
// Find book by primary key try { GenericValue party = delegator.findByPrimaryKey("PartyType", UtilMisc.toMap("partyTypeId", "PERSON")); System.out.println(party.getString("description") ); System.out.println(party.getString("hasTable") ); } catch (GenericEntityException ex1) { } return;
}
public void start() throws Exception { startServer(); }
public static void main(String[] args) throws Exception { TestEntity start = new TestEntity(args.length == 2 ? args[1] : null);
start.start(); start.testEntity();
}
} 编译,运行:
结果为:
Entered testFindByPrimaryKey
Person
Y
mail:[email protected]

|