[jboss-user] [JBoss Portal Users] - Re: How to obtain the current portlet instance?

ebephil do-not-reply at jboss.com
Mon Sep 14 10:53:17 EDT 2009


It took more two afternoons, but I found a solution.

The trick is to use the PortalObjectContainer to obtain the current PortalObject from the current PortalNode.

Anyway, here's my code:


  | 	private Instance getCurrentInstance() {
  | 		PortalNodeImpl node = Navigation.getCurrentNode();
  | 		if (node.getType() != PortalNode.TYPE_WINDOW) {
  | 			return null;
  | 		}
  | 		PortalObjectId id = node.getObjectId();
  | 		Window window = (Window) getPortalObjectContainer().getObject(id);
  | 		PortletContent pc = (PortletContent) window.getContent();
  | 		return getInstanceContainer().getDefinition(pc.getInstanceRef());
  | 	}
  | 
  | 	@Override
  | 	public boolean writePref(String prefKey, String newPrefValue) {
  | 		Instance currentInstance = getCurrentInstance();
  | 
  | 		if (null == currentInstance) {
  | 			logger.error("<-- writePref() Current Instance is null, stopping.");
  | 			return false;
  | 		}
  | 
  | 		try {
  | 			List<PropertyChange> changeList = new ArrayList<PropertyChange>();
  | 			PropertyChange change = PropertyChange.newUpdate(prefKey,
  | 					newPrefValue);
  | 			changeList.add(change);
  | 
  | 			PropertyChange[] changeArray = changeList
  | 					.toArray(new PropertyChange[changeList.size()]);
  | 
  | 			logger.info("--> save()", " setting properties");
  | 			currentInstance.setProperties(changeArray);
  | 
  | 		} catch (PortletInvokerException e) {
  | 			logger.error(e.toString());
  | 			return false;
  | 		} catch (Exception e) {
  | 			logger.error(e.toString());
  | 			return false;
  | 		} catch (Throwable t) {
  | 			logger.error(t.toString());
  | 			return false;
  | 		}
  | 
  | 		return true;
  | 	}
  | 

You need to inject the InstanceContainer and PortalObjectContainer via the jboss-portlet.xml:


  | <portlet-app>
  | ...
  | 	<service>
  | 		<service-name>PortalObjectContainer</service-name>
  | 		<service-class>org.jboss.portal.core.model.portal.PortalObjectContainer</service-class>
  | 		<service-ref>portal:container=PortalObject</service-ref>
  | 	</service>
  | 	<service>
  | 		<service-name>InstanceContainer</service-name>
  | 		<service-class>org.jboss.portal.core.model.instance.InstanceContainer</service-class>
  | 		<service-ref>:container=Instance</service-ref>
  | 	</service>
  | ...
  | </portlet-app>
  | 

Then you can access them in your beans, I do it by injecting them via faces-config.xml:

  | <faces-config>
  | ...
  | 	<managed-bean>
  | 		<managed-bean-name>portletPreferencesHelper</managed-bean-name>
  | 		<managed-bean-class>package.foo.PortletPreferencesHelperJBossImpl</managed-bean-class>
  | 		<managed-bean-scope>session</managed-bean-scope>
  | 		<managed-property>
  | 			<property-name>instanceContainer</property-name>
  | 			<value>#{applicationScope.InstanceContainer}</value>
  | 		</managed-property>
  | 		<managed-property>
  | 			<property-name>portalObjectContainer</property-name>
  | 			<value>#{applicationScope.PortalObjectContainer}</value>
  | 		</managed-property>
  | 	</managed-bean>
  | ...
  | </faces-config>
  | 

And now I finally have InstancePreferences  capsuled in a Bean. Btw: Reading the PortletPreferences works the usual way. As I never write user-level PortletPreferences, the Portal always falls back to the InstancePreferences.

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4255118#4255118

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4255118



More information about the jboss-user mailing list