Querying data about your environment

You can use the AdminPortletModel interface to find out more information about your portal installation, things like what all Web applications are installed on your portal, the PortletDefintions means all the portlets installed on your portal. I built this sample portlet to demonstrate that


package com.wpcertification.spi;

import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.portal.ModelException;
import com.ibm.portal.model.PortletModelHome;
import com.ibm.portal.portletmodel.PortletDefinition;
import com.ibm.portal.portletmodel.WebApplication;
import com.ibm.portal.portletmodel.admin.AdminPortletModel;
import com.ibm.portal.portletmodel.admin.PortletDefinitionList;
import com.ibm.portal.portletmodel.admin.WebApplicationList;

public class PortletDeflistPortlet extends GenericPortlet{

PortletModelHome portletModelHome;

public void init() throws PortletException {
System.out.println("Entering PortalPOCPortlet.init()");
try {
InitialContext context = new InitialContext();
portletModelHome =(PortletModelHome) context.lookup("portal:service/model/PortletModel");
System.out.println("PortletModelHome " + portletModelHome);
} catch (NamingException e) {
e.printStackTrace(System.out);
}
System.out.println("Entering PortalPOCPortlet.init()");
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
response.getWriter().println("**************** Portlet Web Applications ************
");
printPortletApplicationList(request,response);
response.getWriter().println("**************** Portlet Definitions ************
");

printPortletDefinitionList(request,response);
response.getWriter().println("**********************************************************************
");
}

public void printPortletApplicationList(RenderRequest request, RenderResponse response){
System.out.println("Entering PortalPOCPortlet.getObjectIdOfPortlet()");
try {
AdminPortletModel adminModel = portletModelHome.getPortletModelProvider().getAdminPortletModel((HttpServletRequest)request, (HttpServletResponse)response);
WebApplicationList webApplicationList = adminModel.getWebApplicationList();

Iterator webAppIt = webApplicationList.iterator();
while(webAppIt.hasNext()){
WebApplication webApplication = (WebApplication)webAppIt.next();
response.getWriter().println(webApplication.getObjectID() +" " +webApplication.getContextRoot() +"
");
}
} catch (ModelException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Exiting PortalPOCPortlet.getObjectIdOfPortlet()");
}

public void printPortletDefinitionList(RenderRequest request, RenderResponse response){
System.out.println("Entering PortalPOCPortlet.getObjectIdOfPortlet()");
try {
AdminPortletModel adminModel = portletModelHome.getPortletModelProvider().getAdminPortletModel((HttpServletRequest)request, (HttpServletResponse)response);
PortletDefinitionList portletDefinitionList = adminModel.getPortletDefinitionList();

Iterator portletDefinitionIt = portletDefinitionList.iterator();
while(portletDefinitionIt.hasNext()){
PortletDefinition portletDefinition = portletDefinitionIt.next();
response.getWriter().println(portletDefinition.getObjectID().toString() +" " + portletDefinition.getObjectID().getUniqueName() +" " + portletDefinition.getTitle(new Locale("en","US"))+" " + portletDefinition.getDescription(new Locale("en","US")) +"
");
}
} catch (ModelException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Exiting PortalPOCPortlet.getObjectIdOfPortlet()");
}

}

No comments: