天天看點

JetSpeed源碼分析

導讀:

  類功能分析:

  1. Request相關:

  1.1. Interface org.apache.jetspeed.request.RequestContext

  這個接口定義了許多可擷取的上下文元件:

  包括:

  1.HttpRequest/Response;

  2.ServletConfig;

  3.ProfileLocater

  4.Pipeline

  5.Page

  6.ContentDispachter

  7.PortalURL

  8.ActionWindow

  9.CapabilityMap

  這樣所有處理該RequestContext的元件都已包含在它自己裡面了;隻需在調用的過程中注入這些對象的執行個體即可。

  1.2. Class org.apache.jetspeed.container.ContainerRequest

  該類繼承自HttpServletRequestWrapper,持有PortletDefinition和PortletRequest;

  1.3. Class org.apache.jetspeed.request.PortalRequest

  該類繼承自HttpServletRequestWrapper,持有servletpath和contextpath和Httpsession;

  1.4. Interface org.apache.jetspeed.request.PortletRequest

  1.儲存Request參數和取得參數的鍵;

  2.擷取對應Portlet的模式;檢查是否支援特定的模式;

  3.擷取本地資訊和權限資訊;

  該類代表了一個 标準的通路portlet對象,功能如下:

  定義了一組常量用來從getAttribute()方法中擷取用戶端屬性;

  public static final String USER_INFO = "javax.portlet.userinfo";

  public static final String FORM_AUTH = "FORM";

  public Object getAttribute(String name);

  ...

  定義了兩個方法來擷取模式和窗體狀态:

  public PortletMode getPortletMode();

  public WindowState getWindowState();

  ...

  定義了一組方法來擷取目前PortletRequest的Session:

  public PortletMode getPortletMode();

  public WindowState getWindowState();

  ...

  1.5.

  Interface org.apache.jetspeed.request.ActionRequest

  該類繼承自PortletRequest,提供了Reader來讀取PortletRequest中的内容;

  1.6. Interface org.apache.jetspeed.request.RendereRequest

  該類繼承自PortletRequest,沒有新增任何方法;

  1.7. Class org.apache.jetspeed.engine.servlet.ServletRequestImpl

  該類繼承了HttpRequestWrapper,用來在PortletContainer内部傳遞Request請求;

  1.8. Class org.apache.jetspeed.container.invoker.LocalServletRequest

  該類封裝了HttpRequestWrapper來提供一組屬性值資訊;

  1.9.

  Interface org.apache.jetspeed.aggregator.ContentDispatcher

  該接口提供一個方法從Fragment解析為PortletContent:

  PortletContent getPortletContent(Fragment fragment);

  ...

  2. Response相關:

  2.1. public class ContainerResponse extends HttpServletResponseWrapper

  封裝了RenderResponse和ActionResponse,用來在容器内和被調用的Servlet通信;

  2.2. public class ContainerResponse extends HttpServletResponseWrapper

  包裝了HttpServletResponse,通過:

  _getHttpServletResonse()方法擷取其中的HttpServletResponse;

  encodeURL() 編碼URL字元串;

  sendRedirect() 進行轉發;

  2.3. class ServletResponseImpl extends HttpServletResponseWrapper implements PortletDispatcherIncludeAware

  對HttpServletResponseWrapper的又一次封裝,隻有包含include才使用它;

  2.4. interface PortletResponse

  定義了該接口來使Portlet容器和調用它的Client通信,定義了:

  public String encodeURL(String path); 用該方法來傳回一個包含資源/應用資料的URL字元串;

  2.5. interface RenderResponse

  Portlet使用它來完成對用戶端的相應;定義了:

  public PortletURL createRenderURL (); 方法來傳回一個标示Portlet的具體URL,該URL 可能包含狀态/模式等資料;

  public PortletURL createActionURL ();方法來傳回一個标示Portlet的具體URL,該URL 可能包含狀态/模式等資料;

  3. Servlet相關:

  3.1. class JetspeedServlet extends HttpServlet implements JetspeedEngineConstants, HttpSessionListener

  整個Portal引擎的入口,定義了如下的方法:

  public final void init( ServletConfig config )

  來完成啟動引擎和擷取相關資源的工作;

  public final void doGet( HttpServletRequest req, HttpServletResponse res )

  使用RequestContextComponent來建立用于和Portal通信的RequestContext;

  3.2. class JetspeedContainerServlet extends HttpServlet

  該類僅起到了路由請求的作用,并未提供任何上下文設定,定義了:

  public final void doGet(HttpServletRequest request, HttpServletResponse response)

  來完成根據request中設定的portletRequest/portletResponse的相關屬性來選擇交給目地Portlet的特定方法;

  4. Context相關:

  4.1. public interface PortalContext

  可以由該接口擷取支援的窗體狀态,portlet模式和啟動屬性等;

  4.2. public interface PortletContext

  可以由該接口擷取支援的MIME-TYPE,PortletRequestDispatcher等;

  4.3. public class PortletRequestContext

  該類使用ThreadLocal來儲存自己,并持有以下:

  private PortletDefinition pd;

  private Portlet portlet;

  

  private PortletRequest request

  private PortletResponse response;

  4.4. public class JetspeedPortletContext implements PortletContext, InternalPortletContext

  該類持有ServletContext和MutablePortletApplication,進而和ServletContext建立聯系,可以使用任何ServletContext中的上下文元件,如RequestDispatcher和MIME-TYPE;

  5. Invoker相關:

  5.1. public interface PortletInvoker

  5.2. class ServletPortletInvoker implements JetspeedPortletInvoker

  完成跨越Application的Portlet調用(redirect到另一個應用的portlet),通過調用另一個應用的JetSpeedContainerServlet來路由請求;

  它使用一個ServletContext來擷取另一個應用的ServletContext,如下:

  ServletContext appContext = jetspeedContext.getContext(portletApplicationName);

  再通過這個Context來擷取Portlet;

  PortletInstance portletInstance = portletFactory.getPortletInstance(appContext, portletDefinition);

  它定義了如下方法來完成Invoker:

  protected void invoke(PortletRequest portletRequest, PortletResponse portletResponse, Integer methodID)

  該方法将在擷取目地Portlet後在發送的servletRequest中設定如下資訊:

  PORTLET;PORTLET_CONFIG;PORTLET_REQUEST;PORTLET_RESPONSE;METHOD_ID;PORTLET_NAME;PORTAL_CONTEXT

  然後使用Dispatcher來轉發請求:

  dispatcher.include(servletRequest, servletResponse);

  6. URL相關:

  6.1. public interface PortletURL

  由RenderResponse建立(包含狀态/模式/安全資訊),使用PortletURL來代表Portlet自身;PortletURL可以包含一些不符合規範的應用資料,它們在portal中統一為正确的格式;它定義了:

  public String toString (); 使用該方法建立用于嵌入在HTML中的URL字元串;

  void setRequest(HttpServletRequest request); 使用該方法來注入用來生成URL的HttpServletRequest ;

  6.2. public interface PortalURL

  由Portal建立的在Portalneib使用的URL形式,定義了:

  NavigationalState getNavigationalState(); 來擷取該URL的導航狀态

  String createPortletURL(PortletWindow window, PortletMode mode, WindowState state, boolean secure);

  來建立對應該URL的 portletURL;

  6.3. public interface BasePortalURL

  定義了portal基本的URL資訊,如下:

  boolean isSecure();

  void setSecure(boolean secure);

  String getServerName();

  void setServerName(String serverName);

  int getServerPort();

  void setServerPort(int serverPort);

  String getServerScheme();

  void setServerScheme(String serverScheme);

  6.4. public abstract class AbstractPortalURL implements PortalURL

  使用一組: protected static String navStateParameter;

  protected NavigationalState navState;

  來儲存導航狀态,使用navStateParameter來将狀态儲存在PortalContext中;

  6.5. public class PortletURLImpl implements PortalURL

  6.6. public interface PortletURLFactory

  public PortletURL getPortletURL(PortletWindow portletWindow,

  javax.servlet.http.HttpServletRequest servletRequest,

  javax.servlet.http.HttpServletResponse servletResponse)

  使用該方法從WIndow來建立一個PortletURL

  

  6.7. public interface PortletURLProvider

  6.8. public class PortletURLProviderImpl implements PortletURLProvider

  它包裝了PortalURL和WIndow來完成建立PortletURL的任務;

  7. Pipeline相關:

  7.1. interface Pipeline

  管線是j2中所有閥的一個流程,Engine使用管線來完成所有在轉發給portlet之前的RequestContext,定義了以下方法:

  void addValve(Valve valve); 增加一個新的閥

  void invoke(RequestContext context) 調用下一個閥處理RequestContext,直到有一個閥終至掉這個流程;

  7.2. class JetspeedPipeline implements Pipeline

  j2中管線的實作,它的invoke方法中使用Invocation來表示一次Valve調用,也使用Invocation來計數管線的調用位置:

  class Invocation implements ValveContext

  {

  private final Valve[] valves;

  private int at = 0;

  public Invocation(Valve[] valves)

  {

  this.valves = valves;

  }

  

  public void invokeNext(RequestContext request) throws PipelineException

  {

  if (at

  {

  Valve next = valves[at];

  at++;

  // 将自己傳遞給Valve完成Pipeline位置計數

  next.invoke(request, this);

  }

  }

  }

  8. Pipeline相關:

  8.1. interface Valve

  j2中的一個閥用來完成特定的功能,定義了:

  public void invoke(RequestContext request, ValveContext context) 完成處理RequestContext 的功能;

  j2中建構了具有以下功能的Valve來完成基本的任務:

  頁面聚合;

  AJAX支援;

  性能;

  容器管理;

  頁面修飾;

  布局管理;

  本地化;

  登陸;

  監測;

  安全性;

  8.2. class ActionValveImpl extends AbstractValve implements ActionValve

  它持有一個PortletContainer和PortletWindowAccessor來通路特定的Portlet;

  它用patchResponseCommitted字段标示回應是否已經完成了;

  在它的Invoke方法中,先解析出actionWindow及相應的Request,然後調用:

  container.processPortletAction(

  actionWindow,

  requestForWindow,

  response);

  來使用PortletContainer處理該請求;

  最後标明回應已經完成;

  8.3. class ContainerValve extends AbstractValve

  在它的Invoke方法中,從RequetstContext中解析出PortalURL,從PortalURL中解析出PortletWindow,然後設定該RequetstContext的PortletWindow屬性,為調用AcitonValve做準備。

  PortalURL url = request.getPortalURL();

  PortletWindow actionWindow = url.getNavigationalState().getPortletWindowOfAction();

  // set the requested action window request.setActionWindow(actionWindow);

  8.4. class PortalURLValveImpl extends AbstractValve

  将根據RequestCOntext中encoder是否Desktop使用如下兩種方式解析Navigation,并設定RequestCOntext的Navigation:

  request.setPortalURL(navComponent.createDesktopURL(request.getRequest(), request.getCharacterEncoding()));

  request.setPortalURL(navComponent.createURL(request.getRequest(), request.getCharacterEncoding()));

  9. NavigationalState相關:

  9.1. interface NavigationalState

  它是一個嵌入在URL中的字元串用來表示目前PortalURL的狀态,可以從它擷取ActionWindow和PortletMode/WindowState;

  9.2. interface NavigationalStateComponent

  提供一組用于從HttpServletRequest構造PortalURL的方法;

本文轉自

http://soa.5d6d.com/redirect.php?fid=9&tid=3&goto=nextoldset