Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Philippe Tran
Can I get PortletContext in a PortletLayoutListener method ?
October 12, 2012 1:16 AM
Answer

Philippe Tran

Rank: Junior Member

Posts: 34

Join Date: January 22, 2009

Recent Posts

Hi,

I'm trying to get a reference to the PortletContext (or ServletContext) in a PortletLayoutListener method - thus I have no access to a Request.
I have tried the PortletBagPool.get(portletId) method, but it returns null...

Does anybody has an idea ?

(LR 6.1)

Thanks,
Philippe
Siby Mathew
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 9, 2012 6:00 AM
Answer

Siby Mathew

Rank: Expert

Posts: 261

Join Date: March 4, 2011

Recent Posts

Hi Philippe,
Can you tell me why you need the PortletContext object. Maybe we can see other ways to achieve the same end result.
Thanks,
Siby
Philippe Tran
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 9, 2012 8:17 AM
Answer

Philippe Tran

Rank: Junior Member

Posts: 34

Join Date: January 22, 2009

Recent Posts

Hi Siby,

I'd like to read some configuration stuff inside of portlet's WEB-INF dir. That's why.

Philippe
Siby Mathew
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 10, 2012 2:50 AM
Answer

Siby Mathew

Rank: Expert

Posts: 261

Join Date: March 4, 2011

Recent Posts

Philippe Tran:
Hi Siby,

I'd like to read some configuration stuff inside of portlet's WEB-INF dir. That's why.

Philippe



Hi Philippe,
As per DeployUtil in portal-impl, the following code returns the webapp folder location of the server :

 1    public static String getAutoDeployDestDir() throws Exception {
 2        String destDir = PrefsPropsUtil.getString(
 3            PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
 4
 5        if (Validator.isNull(destDir)) {
 6            destDir = getAutoDeployServerDestDir();
 7        }
 8
 9        return destDir;
10    }
11
12    public static String getAutoDeployServerDestDir() throws Exception {
13        String destDir = null;
14
15        String serverId = GetterUtil.getString(ServerDetector.getServerId());
16
17        if (serverId.equals(ServerDetector.TOMCAT_ID)) {
18            destDir = PrefsPropsUtil.getString(
19                PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
20                PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
21        }
22        else {
23            destDir = PrefsPropsUtil.getString(
24                "auto.deploy." + serverId + ".dest.dir");
25        }
26
27        if (Validator.isNull(destDir)) {
28            destDir = PrefsPropsUtil.getString(
29                PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
30                PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
31        }
32
33        destDir = StringUtil.replace(
34            destDir, CharPool.BACK_SLASH, CharPool.SLASH);
35
36        return destDir;
37    }


You can use this code to refer your portlet. Hope this helps !

Thanks,
Siby
Philippe Tran
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 10, 2012 6:36 AM
Answer

Philippe Tran

Rank: Junior Member

Posts: 34

Join Date: January 22, 2009

Recent Posts

Thanks Siby for this great idea,
investigatiing...

Philippe
Philippe Tran
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 10, 2012 6:43 AM
Answer

Philippe Tran

Rank: Junior Member

Posts: 34

Join Date: January 22, 2009

Recent Posts

It just works, using getAutoDeployDestDir() with:

public static String getAutoDeployDestDir() throws Exception
{
final String PropsValues_AUTO_DEPLOY_DEST_DIR = PropsUtil.get(PropsKeys.AUTO_DEPLOY_DEST_DIR);
String destDir = PrefsPropsUtil_getString(PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues_AUTO_DEPLOY_DEST_DIR);

if (Validator.isNull(destDir)) {
destDir = getAutoDeployServerDestDir();
}
return destDir;
}

public static String getAutoDeployServerDestDir() throws Exception
{
final String PropsValues_AUTO_DEPLOY_TOMCAT_DEST_DIR = PropsUtil.get(PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR);
final String PropsValues_AUTO_DEPLOY_DEFAULT_DEST_DIR = PropsUtil.get(PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR);
String destDir = null;

String serverId = GetterUtil.getString(ServerDetector.getServerId());

if (serverId.equals(ServerDetector.TOMCAT_ID)) {
destDir = PrefsPropsUtil_getString(
PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR, PropsValues_AUTO_DEPLOY_TOMCAT_DEST_DIR);
}
else {
destDir = PrefsPropsUtil.getString("auto.deploy." + serverId + ".dest.dir");
}

if (Validator.isNull(destDir)) {
destDir = PrefsPropsUtil_getString(
PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR, PropsValues_AUTO_DEPLOY_DEFAULT_DEST_DIR);
}
destDir = StringUtil.replace(destDir, CharPool.BACK_SLASH, CharPool.SLASH);
return destDir;
}

public static String PrefsPropsUtil_getString(String name, String defaultValue)
{
String result;
try {
result = PrefsPropsUtil.getString(name);
}
catch (Exception e) {
result = null;
}
if (result != null)
return result;

return defaultValue;
}


Thanks again, Siby.
Philippe
Siby Mathew
RE: Can I get PortletContext in a PortletLayoutListener method ?
October 10, 2012 8:19 AM
Answer

Siby Mathew

Rank: Expert

Posts: 261

Join Date: March 4, 2011

Recent Posts

That's Great ! Please mark the answer and update to SOLVED