本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593860
建立时间:2006年5月29日




[Java Open Source]Integrate Compass, Appfuse, DisplayTag Tutorial Part 2
软件技术

lhwork 发表于 2007/1/23 9:00:41

Quickly you will find that, every time you run "test-web".   All data in db earsed, reload test-data and index data.   It is good news that we can have a clear index data for test.  But not at all ^^   Sometimes in our test, we need to create some test data at runtime.  We appreciate to index them too.   On the other hand, application admin will be happy that they can reindex by hand.  So, let us create index and delete index functions and test them ^^   All Codes could be download here   I remove lib, extra, docs folder to reduce size.   Step 1. Create test to delete index and create index.   1. Create Action named "CompassAction.java" under src\web, and add below codes   ------ start here ------  package org.myapp.webapp.action; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.compass.core.Compass;import org.compass.gps.CompassGps; /** *  * * @struts.action name="compassForm" path="/compass" scope="request" *  validate="false" parameter="method" input="mainMenu" *  * @struts.action-forward name="list" path="/WEB-INF/pages/compassList.jsp" *  */public final class CompassAction extends BaseAction {        public ActionForward createIndex(ActionMapping mapping, ActionForm form,              HttpServletRequest request,              HttpServletResponse response)  throws Exception {    System.out.println("Enter createIndex method");    CompassGps compassGps = (CompassGps) getBean("compassGps");    boolean isRun = compassGps.isRunning();    System.out.println("isRun : "+ isRun);    if(!isRun){   compassGps.start();  }    compassGps.index();   return mapping.findForward("list"); }        public ActionForward deleteIndex(ActionMapping mapping, ActionForm form,              HttpServletRequest request,              HttpServletResponse response)  throws Exception {    System.out.println("Enter deleteIndex method");    Compass compass = (Compass) getBean("compass");    CompassGps compassGps = (CompassGps) getBean("compassGps");    boolean isRun = compassGps.isRunning();    System.out.println("isRun : "+ isRun);    if(isRun){   compassGps.stop();  }    compass.getSearchEngineIndexManager().deleteIndex();    return mapping.findForward("list"); }        public ActionForward unspecified(ActionMapping mapping, ActionForm form,                                     HttpServletRequest request,                                     HttpServletResponse response)    throws Exception {                return createIndex(mapping, form, request, response);    }     }   ------ end here------  Well, what we done? ^^   In createIndex, we get compassGps, check if engine running, then index it.   In deleteIndex, compass, check if engine stop, then delete index.  Note:Form and jsp is empty, since we will not use them at this time.  2. Create file named "CompassIndexActionTest.java" under test\web, and add below codes;   ------ start here ------  package org.myapp.webapp.action; public class CompassIndexActionTest extends BaseStrutsTestCase {     public CompassIndexActionTest(String name) {        super(name);    }     protected void setUp() throws Exception {        super.setUp();        getMockRequest().setUserRole("admin");    }        public void testDeleteIndex() throws Exception {        setRequestPathInfo("/compass");        addRequestParameter("method", "deleteIndex");        actionPerform();         verifyForward("list");        verifyNoActionErrors();    }        public void testCreateIndex() throws Exception {        setRequestPathInfo("/compass");        addRequestParameter("method", "createIndex");        actionPerform();         verifyForward("list");        verifyNoActionErrors();    }    }   ------ end here------  Run "ant test-web -Dtestcase=CompassIndexActionTest"  If all ok, you should get a beatiful "BUILD SUCCESSFUL" ^^  Check you compass index folder to see if newer index file generated.  In my case, the location is C:\compass\myapp\index\user  you should have _2.cfs                             deletable                             segments          Launch  luke to see the index results.  Step2. Add ant task to create and delete index   Now admin is happy ^^, just give her a jsp that she can reindex data by hand.  What about us?  Let's create ant tasks for create and delete index  1. In test\web, create package "util" under "webapp" packgae   Create file named "DeleteLuceneIndex.java", and add below codes  ------ start here ------  package org.myapp.webapp.util; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.myapp.Constants;import org.compass.core.Compass;import org.compass.gps.CompassGps;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.util.ClassUtils; public class DeleteLuceneIndex {     protected final static Log log = LogFactory.getLog(DeleteLuceneIndex.class);        public static void main(String[] args) throws Exception{        // hibernate & spring stuff        String pkg = ClassUtils.classPackageAsResourcePath(Constants.class);        String[] paths = {"classpath*:/" + pkg + "/dao/applicationContext-*.xml",              "classpath*:/" + pkg + "/service/applicationContext-service.xml",                          "classpath*:META-INF/applicationContext-*.xml"};                ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);                Compass compass = (Compass) ctx.getBean("compass");    CompassGps compassGps = (CompassGps) ctx.getBean("compassGps");    boolean isRun = compassGps.isRunning();    System.out.println("isRun : "+ isRun);    if(isRun){   compassGps.stop();  }    System.out.println("Delete Index");    compass.getSearchEngineIndexManager().deleteIndex();    System.out.println("Delete Finish");    }}  ------ end here------  2. Create file named "PopulateLuceneIndex.java", and add below codes    ------ start here ------  package org.myapp.webapp.util; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.myapp.Constants;import org.compass.core.Compass;import org.compass.gps.CompassGps;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.util.ClassUtils; public class DeleteLuceneIndex {     protected final static Log log = LogFactory.getLog(DeleteLuceneIndex.class);        public static void main(String[] args) throws Exception{        // hibernate & spring stuff        String pkg = ClassUtils.classPackageAsResourcePath(Constants.class);        String[] paths = {"classpath*:/" + pkg + "/dao/applicationContext-*.xml",              "classpath*:/" + pkg + "/service/applicationContext-service.xml",                          "classpath*:META-INF/applicationContext-*.xml"};                ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);                Compass compass = (Compass) ctx.getBean("compass");    CompassGps compassGps = (CompassGps) ctx.getBean("compassGps");    boolean isRun = compassGps.isRunning();    System.out.println("isRun : "+ isRun);    if(isRun){   compassGps.stop();  }    System.out.println("Delete Index");    compass.getSearchEngineIndexManager().deleteIndex();    System.out.println("Delete Finish");    }}     ------ end here------  3. Edit build.xml, add tasks below     ------ start here ------     <!-- =================================================================== -->    <!--            Convenience target to populate a lucene index              -->    <!-- =================================================================== -->    <target name="create-lucene-index" description="Create the lucene index" depends="compile-web,delete-lucene-index,db-load">     <java classname="org.myapp.webapp.util.PopulateLuceneIndex">      <classpath>                <path refid="web.test.classpath"/>                <pathelement location="${build.dir}/web/classes"/>                <pathelement location="${test.dir}/web/classes"/>                <pathelement path="${java.class.path}"/>                                    <pathelement path="src/web"/>            </classpath>          </java>    </target>    <!-- =================================================================== -->    <!--            Convenience target to Delete a lucene index              -->    <!-- =================================================================== -->    <target name="delete-lucene-index" description="Delete the lucene index" depends="compile-web">        <java classname="org.myapp.webapp.util.DeleteLuceneIndex">      <classpath>                <path refid="web.test.classpath"/>                <pathelement location="${build.dir}/web/classes"/>                <pathelement location="${test.dir}/web/classes"/>                <pathelement path="${java.class.path}"/>                                    <pathelement path="src/web"/>            </classpath>          </java>    </target>    ------ end here------  4. run "ant create-lucene-index" or "ant delete-lucene-index"     Well, now we have ant tasks for delete and create index ^^    Note:     Thomas Gaudin's tutorail present another option to create and delete index     Frankly, use <delete> in ant would be more simpler ^^    But since we have resourceLocations in applicationContext-service.xml     I choose to create classes to do it.     Thanks Thomas Gaudin's tutorail, the original codes are referenced there.  Summary     In this tutorail, we create actions and ant tasks that help us create and delete index.    In next part, we will use annotation to simplify compass cpm file generation.


阅读全文(3951) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.898 second(s), page refreshed 144769232 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号