Compass---CompassConfiguration

CompassConfiguration配置

为了创建Compass实例,首先要配置CompassConfiguration

有两种配置方法

1 程序配置

一个Compass实例能使用CompassConfiguration类程序化配置,两个主要的配置方面是添加映射定义和设置不同的参数。CompassConfiguration提供了很多接口去添加映射定义(xml映射文件,后缀.cmp.xml,或注释类),还有通用元数据定义(后缀为.cmd.xml的xml配置文件)。

addFile(String) Loads the mapping file (cpm or cmd) according to the  specified file path string.
addFile(File) Loads the mapping file (cpm or cmd) according to the specified file object reference.
addClass(Class) Loads the mapping file (cpm) according to the specified  class. test.Author.class will map to
test/Author.cpm.xml within the class path. Can also add annotated classes if using Compass annotations support.
addURL(URL) Loads the mapping file (cpm or cmd) according to the specified URL.
addResource(String) Loads the mapping file (cpm or cmd) according to the specified resource path from the class path.
addInputStream(InputStream) Loads the mapping file (cpm or cmd) according to the specified input stream.
addDirectory(String) Loads all the files named *.cpm.xml or *.cmd.xml from within the specified directory.
addJar(File) Loads all the files named *.cpm.xml or *.cmd.xml from within the specified Jar file.
addMapping(ResourceMapping) Programmatically add resource mapping (domain model that represents different mappings such as XSEM, OSEM,
and RSEM).
addScan(String basePackage, String pattern)Scans for all the mappings that exist wihtin the base
backage recursively. An optioal ant style pattern can be provided as well. The mappings detected are all the xml
based mappings. Annotation based mappings will be detected automatically if either ASM or Javassist exists
within the classpath.
addMappingResolver(MappingResolver) Uses a class that implements the MappingResolver to get an
InputStream for xml mapping definitions.

 下面列出一个最小化的程序控制的例子:

CompassConfiguration conf = new CompassConfiguration()
.setSetting(CompassEnvironment.CONNECTION, "my/index/dir")
.addResource(DublinCore.cmd.xml)
.addClass(Author.class);

 2 XML/JSON配置

所有Compass的操作配置都可以定义在一个xml配置文件里,以compass.cfg.xml为默认名称。在这个文件里你可以定义环境的配置和映射文件所在位置。

configure() Loads a configuration file called compass.cfg.xml from the root of the class path.默认从类路径下寻找compass.cfg.xml
configure(String) Loads a configuration file from the specified path 从指定位置下寻找

(1) 基于schema配置

<compass-core-config xmlns="http://www.compass-project.org/schema/core-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.compass-project.org/schema/core-config
http://www.compass-project.org/schema/compass-core-config-2.2.xsd">
<compass name="default">
<connection>
<file path="target/test-index"/>
</connection>
<mappings>
<class name="test.Author" />
</mappings>
</compass>
</compass-core-config>

 还有比较复杂的基于jdbc,以及事务,分析器(such as converters, highlighters, analyzers, and so on)的配置

(2)基于json的配置

{
compass : {
engine : {
connection : "test-index"
},
event : {
preCreate : {
event1 : {
type : "test.MyEvent1"
},
event2 : {
type : "test.MyEvent2"
}
}
}
}
}

 (3) 基于dtd配置

<!DOCTYPE compass-core-configuration PUBLIC
"-//Compass/Compass Core Configuration DTD 2.2//EN"
"http://www.compass-project.org/dtd/compass-core-configuration-2.2.dtd">
<compass-core-configuration>
<compass>
<setting name="compass.engine.connection">my/index/dir</setting>
<meta-data resource="vocabulary/DublinCore.cmd.xml" />
<mapping resource="test/Author.cpm.xml" />
</compass>
</compass-core-configuration>

3  Compass获取

 CompassConfiguration配置之后,你就可以创建一个Compass实例了,Compass将会在不同的应用线程之间共享。

下面的简单代码说明如何获取compass实例

Compass compass = cfg.buildCompass();

注: 在同一个应用中可能会有多个Compass 实例,每一个都有不同的配置。

4 重建Compass

Compass允许使用从Compass取得的CompassConfiguration实例动态添加或删除映射,一旦所以改变在配置对象上完成,可以调用Compass的rebuild()方法

Compass compass = cfg.buildCompass();
// ...
compass.getConfig().removeMappingByClass(A.class);
compass.getConifg().addClass(B.class);
compass.rebuild();

 旧的Compass实例在短时间内将关闭。关闭时间可以用配置参数compass.rebuild.sleepBeforeClose控制。

5 配置回调事件

Compass允许配置事件(events),当在Compass使用指定的操作时触发的事件,比如保存操作。

配置事件监听器可以由设置(settings)完成。比如说,配置一个保存前事件监听器,下面的配置参数将会用到,

compass.event.preSave.mylistener.type ,它的值可以是监听器的真实类名

猜你喜欢

转载自crazycat03.iteye.com/blog/571027
今日推荐