PortletConfig 对象在可以根据Portlet 部署描述符的配置获取Portlet 的基本信息。比如Portlet 的title,short title 以及Key 值。该信息配置在portlet.xml 中。例如:
<portlet>
...
<init-param>
<name>Stock</name>
<value>/portlet_action/stock/edit</value>
</init-param>
...
</portlet>
初始时可调用getInitParameterNames() 与getInitParameter() 两个方法获得。
Liferay 中对PortletConfig 接口的实现如下:
public class PortletConfigImpl implements PortletConfig {
public PortletConfigImpl(String portletName, PortletContext portletCtx,
Map params, String resourceBundle,
PortletInfo portletInfo) {
_rootPortletId = PortletImpl.getRootPortletId(portletName);
_portletId = portletName;
_portletName = _rootPortletId;
int pos = _portletName.indexOf(PortletImpl.WAR_SEPARATOR);
//如果Portlet 名包涵了后缀 "_WAR_" 则返回去掉后缀之后的Portlet 名。
if (pos != -1) {
_portletName = _portletName.substring(0, pos);
_warFile = true;
}
//将初始化的信息传入该类的实例。
_portletCtx = portletCtx;
_params = params;
_resourceBundle = resourceBundle;
_portletInfo = portletInfo;
_bundlePool = CollectionFactory.getHashMap();
}
public String getPortletId() {
return _portletId;
}
public String getPortletName() {
return _portletName;
}
public boolean isWARFile() {
return _warFile;
}
public PortletContext getPortletContext() {
return _portletCtx;
}
//该方法用于获取Portlet 显式信息。
public ResourceBundle getResourceBundle(Locale locale) {
/* 若Portlet 信息是写在Portlet 配置文件中的例如:
<portlet>
...
<portlet-info>
<title>Stock Quote Portlet</title>
<short-title>Stock</short-title>
<keywords>finance,stock market</keywords>
</portlet-info>
...
</portlet>
此时将取出Portlet 对应的title,short-title 以及keywords。
*/
if (_resourceBundle == null) {
String poolId = _portletId;
ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
if (bundle == null) {
StringBuffer sb = new StringBuffer();
try {
sb.append(WebKeys.JAVAX_PORTLET_TITLE);
sb.append("=");
sb.append(_portletInfo.getTitle());
sb.append("/n");
sb.append(WebKeys.JAVAX_PORTLET_SHORT_TITLE);
sb.append("=");
sb.append(_portletInfo.getShortTitle());
sb.append("/n");
sb.append(WebKeys.JAVAX_PORTLET_KEYWORDS);
sb.append("=");
sb.append(_portletInfo.getKeywords());
sb.append("/n");
bundle = new PropertyResourceBundle(
new ByteArrayInputStream(sb.toString().getBytes()));
}
catch (Exception e) {
e.printStackTrace();
}
//将获取的Portlet 信息资源放入资源池中。
_bundlePool.put(poolId, bundle);
}
return bundle;
}
/* 如果该信息是写在资源文件中,则在配置时需要给出资源文件的位置,如
<portlet>
...
<resource-bundle>com.foo.myApp.QuotePortlet</resource-bundle>
...
</portlet>
如果配置时配置了该参数,则获取Portlet 的信息资源文件时,将首先从该路径查询资源文件。若没照到该资源文件或者该资源文件内没有定义相关的信息,则再次查找portlet.xml 配置文件中的配置。若还没有定义则返回空字符串。
*/
else {
String poolId = _portletId + "." + locale.toString();
ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
if (bundle == null) {
//首先查询资源文件。
if (!_warFile &&
_resourceBundle.equals(
StrutsResourceBundle.class.getName())) {
String companyId =
(String)_portletCtx.getAttribute(WebKeys.COMPANY_ID);
bundle = StrutsResourceBundle.getBundle(
_portletName, companyId, locale);
}
//从已配置的信息中查找该信息
else {
PortletContextWrapper pcw =
PortletContextPool.get(_rootPortletId);
bundle = pcw.getResourceBundle(locale);
}
bundle = new PortletResourceBundle(bundle, _portletInfo);
_bundlePool.put(poolId, bundle);
}
return bundle;
}
}
//获取Portlet 的基本信息
public String getInitParameter(String name) {
if (name == null) {
throw new IllegalArgumentException();
}
return (String)_params.get(name);
}
public Enumeration getInitParameterNames() {
return Collections.enumeration(_params.keySet());
}
private String _rootPortletId;
private String _portletId;
private String _portletName;
private boolean _warFile;
private PortletContext _portletCtx;
private Map _params;
private String _resourceBundle;
private PortletInfo _portletInfo;
private Map _bundlePool;
}
<portlet>
...
<init-param>
<name>Stock</name>
<value>/portlet_action/stock/edit</value>
</init-param>
...
</portlet>
初始时可调用getInitParameterNames() 与getInitParameter() 两个方法获得。
Liferay 中对PortletConfig 接口的实现如下:
public class PortletConfigImpl implements PortletConfig {
public PortletConfigImpl(String portletName, PortletContext portletCtx,
Map params, String resourceBundle,
PortletInfo portletInfo) {
_rootPortletId = PortletImpl.getRootPortletId(portletName);
_portletId = portletName;
_portletName = _rootPortletId;
int pos = _portletName.indexOf(PortletImpl.WAR_SEPARATOR);
//如果Portlet 名包涵了后缀 "_WAR_" 则返回去掉后缀之后的Portlet 名。
if (pos != -1) {
_portletName = _portletName.substring(0, pos);
_warFile = true;
}
//将初始化的信息传入该类的实例。
_portletCtx = portletCtx;
_params = params;
_resourceBundle = resourceBundle;
_portletInfo = portletInfo;
_bundlePool = CollectionFactory.getHashMap();
}
public String getPortletId() {
return _portletId;
}
public String getPortletName() {
return _portletName;
}
public boolean isWARFile() {
return _warFile;
}
public PortletContext getPortletContext() {
return _portletCtx;
}
//该方法用于获取Portlet 显式信息。
public ResourceBundle getResourceBundle(Locale locale) {
/* 若Portlet 信息是写在Portlet 配置文件中的例如:
<portlet>
...
<portlet-info>
<title>Stock Quote Portlet</title>
<short-title>Stock</short-title>
<keywords>finance,stock market</keywords>
</portlet-info>
...
</portlet>
此时将取出Portlet 对应的title,short-title 以及keywords。
*/
if (_resourceBundle == null) {
String poolId = _portletId;
ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
if (bundle == null) {
StringBuffer sb = new StringBuffer();
try {
sb.append(WebKeys.JAVAX_PORTLET_TITLE);
sb.append("=");
sb.append(_portletInfo.getTitle());
sb.append("/n");
sb.append(WebKeys.JAVAX_PORTLET_SHORT_TITLE);
sb.append("=");
sb.append(_portletInfo.getShortTitle());
sb.append("/n");
sb.append(WebKeys.JAVAX_PORTLET_KEYWORDS);
sb.append("=");
sb.append(_portletInfo.getKeywords());
sb.append("/n");
bundle = new PropertyResourceBundle(
new ByteArrayInputStream(sb.toString().getBytes()));
}
catch (Exception e) {
e.printStackTrace();
}
//将获取的Portlet 信息资源放入资源池中。
_bundlePool.put(poolId, bundle);
}
return bundle;
}
/* 如果该信息是写在资源文件中,则在配置时需要给出资源文件的位置,如
<portlet>
...
<resource-bundle>com.foo.myApp.QuotePortlet</resource-bundle>
...
</portlet>
如果配置时配置了该参数,则获取Portlet 的信息资源文件时,将首先从该路径查询资源文件。若没照到该资源文件或者该资源文件内没有定义相关的信息,则再次查找portlet.xml 配置文件中的配置。若还没有定义则返回空字符串。
*/
else {
String poolId = _portletId + "." + locale.toString();
ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
if (bundle == null) {
//首先查询资源文件。
if (!_warFile &&
_resourceBundle.equals(
StrutsResourceBundle.class.getName())) {
String companyId =
(String)_portletCtx.getAttribute(WebKeys.COMPANY_ID);
bundle = StrutsResourceBundle.getBundle(
_portletName, companyId, locale);
}
//从已配置的信息中查找该信息
else {
PortletContextWrapper pcw =
PortletContextPool.get(_rootPortletId);
bundle = pcw.getResourceBundle(locale);
}
bundle = new PortletResourceBundle(bundle, _portletInfo);
_bundlePool.put(poolId, bundle);
}
return bundle;
}
}
//获取Portlet 的基本信息
public String getInitParameter(String name) {
if (name == null) {
throw new IllegalArgumentException();
}
return (String)_params.get(name);
}
public Enumeration getInitParameterNames() {
return Collections.enumeration(_params.keySet());
}
private String _rootPortletId;
private String _portletId;
private String _portletName;
private boolean _warFile;
private PortletContext _portletCtx;
private Map _params;
private String _resourceBundle;
private PortletInfo _portletInfo;
private Map _bundlePool;
}