国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Solr4.8.0源碼分析(1)之Solr的Servlet

2019-11-14 20:52:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
Solr4.8.0源碼分析(1)之Solr的Servlet 2014-08-13 22:41 by 追風(fēng)的藍(lán)寶, ... 閱讀, ... 評(píng)論, 收藏, 編輯

Solr是作為一個(gè)Servlet運(yùn)行在Tomcat里面的,可以查看Solr的web.xml

1.web.xml配置

由web.xml可以看出,基本上所有Solr的操作都是在SolrDispatchFilter中實(shí)現(xiàn)的。當(dāng)輸入http://localhost:8080/solr/前綴的URL就會(huì)觸發(fā)SolrDispatchFilter.

 1   <filter> 2     <filter-name>SolrRequestFilter</filter-name> 3     <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class> 4     <init-param> 5       <param-name>path-PRefix</param-name> 6       <param-value>/xxx</param-value> 7     </init-param> 8   </filter> 9 10   <filter-mapping>11     <filter-name>SolrRequestFilter</filter-name>12     <url-pattern>/*</url-pattern>13   </filter-mapping>14   <servlet>15     <servlet-name>RedirectOldAdminUI</servlet-name>16     <servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>17     <init-param>18       <param-name>destination</param-name>19       <param-value>${context}/#/</param-value>20     </init-param>21   </servlet>22   <servlet-mapping>23     <servlet-name>RedirectOldAdminUI</servlet-name>24     <url-pattern>/admin/</url-pattern>25   </servlet-mapping>

2. SolrDispatchFilter的實(shí)現(xiàn)

SolrDispatchFilter繼承了Filter,實(shí)現(xiàn)主要分為三個(gè)接口:init,dofilter,destory。其中init和destory分別在tomcat的啟動(dòng)和關(guān)閉時(shí)候進(jìn)行。

 1  /** 2   *初始化,當(dāng)tomcat啟動(dòng)時(shí)候開(kāi)始初始化,其中主要調(diào)用createCoreContainer來(lái)實(shí)現(xiàn)Solr的初始化 3   */ 4 public void init(FilterConfig config) throws ServletException 5   { 6     log.info("SolrDispatchFilter.init()"); 7  8     try { 9       // web.xml configuration10       this.pathPrefix = config.getInitParameter( "path-prefix" );11 12       this.cores = createCoreContainer();13       log.info("user.dir=" + System.getProperty("user.dir"));14     }15     catch( Throwable t ) {16       // catch this so our filter still works17       log.error( "Could not start Solr. Check solr/home property and the logs");18       SolrCore.log( t );19       if (t instanceof Error) {20         throw (Error) t;21       }22     }23 24     log.info("SolrDispatchFilter.init() done");25   }26   /**27    * filter接口的具體實(shí)現(xiàn),這里主要實(shí)現(xiàn)了主要的Solr功能28    */29   @Override30   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {31     doFilter(request, response, chain, false);32   }33 34   /**35    * 關(guān)閉Solr36    */37   @Override38   public void destroy() {39     if (cores != null) {40       cores.shutdown();41       cores = null;42     }    43   }44 }

3.Servlet的實(shí)現(xiàn)

通過(guò)查看web.xml以及源碼可以看到,雖然Solr繼承并實(shí)現(xiàn)了Servlet接口,但是Solr的主要操作卻是主要集中在dofilter里面。以RedictServlet為例,它主要實(shí)現(xiàn)了http的重定向功能。從web.xml配置中可以看到,當(dāng)url為solr/admin時(shí)候就會(huì)重定向?yàn)閟olr/#/

 1 /** 2  * A Simple redirection servlet to help us deprecate old UI elements 3  */ 4 public class RedirectServlet extends BaseSolrServlet { 5    6   static final String CONTEXT_KEY = "${context}"; 7    8   String destination; 9   int code = HttpServletResponse.SC_MOVED_PERMANENTLY;10   11   @Override12   public void init(ServletConfig config) throws ServletException {13     super.init(config);14     15     destination = config.getInitParameter("destination");16     if(destination==null) {17       throw new ServletException("RedirectServlet missing destination configuration");18     }19     if( "false".equals(config.getInitParameter("permanent") )) {20       code = HttpServletResponse.SC_MOVED_TEMPORARILY;21     }22         // 獲取重定向的url 解析init-param 獲取destination值23     // Replace the context key24     if(destination.startsWith(CONTEXT_KEY)) {25       destination = config.getServletContext().getContextPath()26           +destination.substring(CONTEXT_KEY.length());27     }28   }29   30   @Override31   public void doGet(HttpServletRequest req, HttpServletResponse res)32           throws ServletException,IOException {33       34     res.setStatus(code);35     res.setHeader("Location", destination);36   }37 38 }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 涪陵区| 巴中市| 金堂县| 托克托县| 汶川县| 故城县| 同心县| 涿鹿县| 永清县| 辽源市| 大同市| 高青县| 双牌县| 灌阳县| 萨嘎县| 嘉兴市| 沁源县| 南城县| 宜兰市| 当阳市| 蓬溪县| 平山县| 瑞金市| 扶绥县| 宁陕县| 永登县| 商洛市| 洞口县| 大埔县| 益阳市| 辽阳市| 东乡县| 西和县| 全南县| 从江县| 吴堡县| 绵阳市| 阳信县| 三门峡市| 仁化县| 三门峡市|