Author: chris.laprun(a)jboss.com
Date: 2011-04-22 11:18:24 -0400 (Fri, 22 Apr 2011)
New Revision: 6331
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/Utils.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/ServiceDescriptionHandler.java
Log:
- GTNWSRP-45: Improvements to locale management of portlet description generation code.
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/Utils.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/Utils.java 2011-04-22
15:11:17 UTC (rev 6330)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/Utils.java 2011-04-22
15:18:24 UTC (rev 6331)
@@ -172,27 +172,71 @@
public static org.oasis.wsrp.v2.LocalizedString
convertToWSRPLocalizedString(org.gatein.common.i18n.LocalizedString localizedString,
List<String> desiredLocales)
{
+ org.gatein.common.i18n.LocalizedString.Value match =
getPreferredOrBestMatchFor(localizedString, desiredLocales);
+ if (match != null)
+ {
+
+ Locale locale = match.getLocale();
+ String value = match.getString();
+ String language = WSRPUtils.toString(locale);
+ return WSRPTypeFactory.createLocalizedString(language, null, value);
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public static org.oasis.wsrp.v2.LocalizedString
convertToWSRPLocalizedString(org.gatein.common.i18n.LocalizedString localizedString,
Locale locale)
+ {
if (localizedString == null)
{
return null;
}
+ if (locale == null)
+ {
+ locale = Locale.getDefault();
+ }
+
+ String value = localizedString.getString(locale, true);
+ if (value != null)
+ {
+ return WSRPTypeFactory.createLocalizedString(WSRPUtils.toString(locale), null,
value);
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public static Locale
getPreferredOrBestLocaleFor(org.gatein.common.i18n.LocalizedString localizedString,
List<String> desiredLocales)
+ {
+ org.gatein.common.i18n.LocalizedString.Value match =
getPreferredOrBestMatchFor(localizedString, desiredLocales);
+ if (match != null)
+ {
+ return match.getLocale();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private static org.gatein.common.i18n.LocalizedString.Value
getPreferredOrBestMatchFor(org.gatein.common.i18n.LocalizedString localizedString,
List<String> desiredLocales)
+ {
+ if (localizedString == null)
+ {
+ return null;
+ }
+
if (desiredLocales == null || desiredLocales.isEmpty())
{
desiredLocales =
Collections.singletonList(WSRPUtils.toString(Locale.getDefault()));
}
// todo: rewrite getPreferredOrBestLocalizedMappingFor to take a List as argument
- org.gatein.common.i18n.LocalizedString.Value bestMapping =
- localizedString.getPreferredOrBestLocalizedMappingFor(desiredLocales.toArray(new
String[desiredLocales.size()]));
- if (bestMapping != null)
- {
- Locale locale = bestMapping.getLocale();
- String value = bestMapping.getString();
- String language = WSRPUtils.toString(locale);
- return WSRPTypeFactory.createLocalizedString(language, null, value);
- }
- return null;
+ return
localizedString.getPreferredOrBestLocalizedMappingFor(desiredLocales.toArray(new
String[desiredLocales.size()]));
}
public static void throwOperationFaultOnSessionOperation() throws OperationFailed
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java 2011-04-22
15:11:17 UTC (rev 6330)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java 2011-04-22
15:18:24 UTC (rev 6331)
@@ -173,7 +173,6 @@
UserContext userContext = getPortletPropertyDescription.getUserContext();
checkUserAuthorization(userContext);
- List<String> desiredLocales =
getPortletPropertyDescription.getDesiredLocales();
Portlet portlet = getPortletFrom(portletContext, registration);
PortletInfo info = portlet.getInfo();
PreferencesInfo prefsInfo = info.getPreferences();
@@ -194,6 +193,7 @@
{
//todo: check what we should use key
//todo: right now we only support String properties
+ List<String> desiredLocales =
getPortletPropertyDescription.getDesiredLocales();
PropertyDescription desc =
WSRPTypeFactory.createPropertyDescription(prefInfo.getKey(), WSRPConstants.XSD_STRING);
desc.setLabel(Utils.convertToWSRPLocalizedString(prefInfo.getDisplayName(),
desiredLocales));
desc.setHint(Utils.convertToWSRPLocalizedString(prefInfo.getDescription(),
desiredLocales));
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/ServiceDescriptionHandler.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/ServiceDescriptionHandler.java 2011-04-22
15:11:17 UTC (rev 6330)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/ServiceDescriptionHandler.java 2011-04-22
15:18:24 UTC (rev 6331)
@@ -135,7 +135,7 @@
boolean needsPortletDescriptions = !(registration == null &&
requirements.isRegistrationRequired()
&& requirements.isRegistrationRequiredForFullDescription());
- return serviceDescription.getServiceDescription(needsRegistrationProperties,
needsPortletDescriptions, gs.getPortletHandles());
+ return serviceDescription.getServiceDescription(needsRegistrationProperties,
needsPortletDescriptions, gs.getPortletHandles(), gs.getDesiredLocales());
}
finally
{
@@ -305,7 +305,7 @@
}
}
- private ServiceDescription getServiceDescription(boolean
needsRegistrationProperties, boolean needsPortletDescriptions, List<String>
portletHandles)
+ private ServiceDescription getServiceDescription(boolean
needsRegistrationProperties, boolean needsPortletDescriptions, List<String>
portletHandles, List<String> desiredLocales)
{
initIfNeeded();
@@ -313,11 +313,30 @@
ServiceDescription serviceDescription =
WSRPTypeFactory.createServiceDescription(false);
serviceDescription.setRequiresInitCookie(BEA_8_CONSUMER_FIX);
- serviceDescription.getLocales().addAll(producer.getSupportedLocales());
+ List<String> supportedLocales = producer.getSupportedLocales();
+ serviceDescription.getLocales().addAll(supportedLocales);
serviceDescription.getSupportedOptions().addAll(OPTIONS);
serviceDescription.setRegistrationPropertyDescription(registrationProperties);
serviceDescription.setRequiresRegistration(requireRegistrations);
+ // find the appropriate language to use
+ String language = null;
+ if (desiredLocales != null)
+ {
+ for (String desired : desiredLocales)
+ {
+ if (supportedLocales.contains(desired))
+ {
+ language = desired;
+ }
+ }
+ }
+ if (language == null)
+ {
+ language = WSRPUtils.toString(Locale.getDefault());
+ }
+
+
Collection<PortletDescription> portlets;
if (needsPortletDescriptions)
{
@@ -327,7 +346,7 @@
portlets = new
ArrayList<PortletDescription>(portletHandles.size());
for (String handle : portletHandles)
{
- PortletDescription description = portletDescriptions.get(handle);
+ PortletDescription description = getPortletDescription(handle);
if (description != null)
{
portlets.add(description);
@@ -356,14 +375,14 @@
}
}
- private void addEventInfo(EventInfo info, List<String> desiredLocales)
+ private void addEventInfo(EventInfo info, Locale locale)
{
QName name = info.getName();
if (!eventDescriptions.containsKey(name))
{
EventDescription desc = WSRPTypeFactory.createEventDescription(name);
- desc.setDescription(Utils.convertToWSRPLocalizedString(info.getDescription(),
desiredLocales));
- desc.setLabel(Utils.convertToWSRPLocalizedString(info.getDisplayName(),
desiredLocales));
+ desc.setDescription(Utils.convertToWSRPLocalizedString(info.getDescription(),
locale));
+ desc.setLabel(Utils.convertToWSRPLocalizedString(info.getDisplayName(),
locale));
Collection<QName> aliases = info.getAliases();
if (ParameterValidation.existsAndIsNotEmpty(aliases))
{
@@ -407,6 +426,77 @@
portletDescriptions.put(handle, desc);
}
+ /**
+ * TODO: Adapt to desired locales
+ *
+ * @param context
+ * @param desiredLocales
+ * @param registration
+ * @return
+ */
+ public PortletDescription getPortletDescription(PortletContext context,
List<String> desiredLocales, Registration registration)
+ {
+ initIfNeeded();
+
+ org.gatein.pc.api.PortletContext pcContext =
WSRPUtils.convertToPortalPortletContext(context);
+ if (producer.getRegistrationManager().getPolicy().allowAccessTo(pcContext,
registration, "getPortletDescription"))
+ {
+ PortletDescription description =
getPortletDescription(context.getPortletHandle());
+
+ if (description == null)
+ {
+ // check if we asked for the description of a clone
+ if (registration.knows(pcContext))
+ {
+ try
+ {
+ // retrieve initial context from portlet info and get description
from it
+ Portlet portlet = producer.getPortletWith(pcContext, registration);
+ PortletInfo info = portlet.getInfo();
+ org.gatein.pc.api.PortletContext original =
org.gatein.pc.api.PortletContext.createPortletContext(info.getApplicationName(),
info.getName());
+ return getPortletDescription(original.getId());
+ }
+ catch (Exception e)
+ {
+ log.debug("Couldn't retrieve portlet " + pcContext,
e);
+ return null;
+ }
+ }
+ }
+ return description;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private PortletDescription getPortletDescription(final String portletHandle)
+ {
+ return portletDescriptions.get(portletHandle);
+ }
+
+ public void removePortletDescription(org.gatein.pc.api.PortletContext pc)
+ {
+ String handle = WSRPUtils.convertToWSRPPortletContext(pc).getPortletHandle();
+
+ PortletDescription description = portletDescriptions.get(handle);
+ if (description != null)
+ {
+ // deal with events
+ for (QName event : description.getHandledEvents())
+ {
+ removeEvent(event);
+ }
+ for (QName event : description.getPublishedEvents())
+ {
+ removeEvent(event);
+ }
+
+ portletDescriptions.remove(handle);
+ }
+ }
+
private PortletDescription createPortletDescription(PortletInfo info,
List<String> locales, String handle)
{
if (log.isDebugEnabled())
@@ -432,22 +522,37 @@
MetaInfo metaInfo = info.getMeta();
+ Set<Locale> supportedLocales = info.getCapabilities().getAllLocales();
+ // find the best locale to use to generate the description
+ Locale localeMatch = null;
+ for (String languageTag : locales)
+ {
+ Locale locale = WSRPUtils.getLocale(languageTag);
+ if (supportedLocales.contains(locale))
+ {
+ localeMatch = locale;
+ }
+ }
+ if (localeMatch == null)
+ {
+ localeMatch = Locale.getDefault();
+ }
+
// description
-
desc.setDescription(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.DESCRIPTION),
locales));
+
desc.setDescription(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.DESCRIPTION),
localeMatch));
// short title
-
desc.setShortTitle(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.SHORT_TITLE),
locales));
+
desc.setShortTitle(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.SHORT_TITLE),
localeMatch));
// title
-
desc.setTitle(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.TITLE),
locales));
+
desc.setTitle(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.TITLE),
localeMatch));
// display name
-
desc.setDisplayName(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.DISPLAY_NAME),
locales));
+
desc.setDisplayName(Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.DISPLAY_NAME),
localeMatch));
// keywords
// metaInfo contains comma-separated keywords: we need to extract them into a
list
- org.oasis.wsrp.v2.LocalizedString concatenatedKeywords =
- Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.KEYWORDS),
locales);
+ org.oasis.wsrp.v2.LocalizedString concatenatedKeywords =
Utils.convertToWSRPLocalizedString(metaInfo.getMetaValue(MetaInfo.KEYWORDS),
localeMatch);
if (concatenatedKeywords != null)
{
String commaSeparatedKeywords = concatenatedKeywords.getValue();
@@ -474,7 +579,7 @@
for (Map.Entry<QName, ? extends EventInfo> entry :
producedEvents.entrySet())
{
publishedEvents.add(entry.getKey());
- addEventInfo(entry.getValue(), locales);
+ addEventInfo(entry.getValue(), localeMatch);
}
}
Map<QName, ? extends EventInfo> consumedEvents =
eventsInfo.getConsumedEvents();
@@ -484,7 +589,7 @@
for (Map.Entry<QName, ? extends EventInfo> entry :
consumedEvents.entrySet())
{
handledEvents.add(entry.getKey());
- addEventInfo(entry.getValue(), locales);
+ addEventInfo(entry.getValue(), localeMatch);
}
}
}
@@ -501,7 +606,7 @@
{
String id = parameterInfo.getId();
ParameterDescription paramDesc =
WSRPTypeFactory.createParameterDescription(id);
-
paramDesc.setDescription(Utils.convertToWSRPLocalizedString(parameterInfo.getDescription(),
locales));
+
paramDesc.setDescription(Utils.convertToWSRPLocalizedString(parameterInfo.getDescription(),
localeMatch));
paramDesc.setLabel(WSRPTypeFactory.createLocalizedString(id));
List<QName> names = paramDesc.getNames();
names.add(parameterInfo.getName());
@@ -541,70 +646,59 @@
return desc;
}
- /**
- * TODO: Adapt to desired locales
- *
- * @param context
- * @param desiredLocales
- * @param registration
- * @return
- */
- public PortletDescription getPortletDescription(PortletContext context,
List<String> desiredLocales, Registration registration)
+ /*private class PortletDescriptionInfo
{
- initIfNeeded();
+ private Map<String, PortletDescription> languageToDescription;
+ private String defaultLanguage;
- org.gatein.pc.api.PortletContext pcContext =
WSRPUtils.convertToPortalPortletContext(context);
- if (producer.getRegistrationManager().getPolicy().allowAccessTo(pcContext,
registration, "getPortletDescription"))
+ private PortletDescriptionInfo(String defaultLanguage, PortletDescription
description, Set<String> supportedLanguages)
{
- PortletDescription description =
portletDescriptions.get(context.getPortletHandle());
-
- if (description == null)
+ this.defaultLanguage = defaultLanguage;
+ languageToDescription = new HashMap<String,
PortletDescription>(supportedLanguages.size());
+ for (String supportedLanguage : supportedLanguages)
{
- // check if we asked for the description of a clone
- if (registration.knows(pcContext))
- {
- try
- {
- // retrieve initial context from portlet info and get description
from it
- Portlet portlet = producer.getPortletWith(pcContext, registration);
- PortletInfo info = portlet.getInfo();
- org.gatein.pc.api.PortletContext original =
org.gatein.pc.api.PortletContext.createPortletContext(info.getApplicationName(),
info.getName());
- return portletDescriptions.get(original.getId());
- }
- catch (Exception e)
- {
- log.debug("Couldn't retrieve portlet " + pcContext,
e);
- return null;
- }
- }
+ languageToDescription.put(supportedLanguage, null);
}
- return description;
+ languageToDescription.put(defaultLanguage, description);
}
- else
+
+ public Set<String> getSupportedLanguages()
{
- return null;
+ return languageToDescription.keySet();
}
- }
- public void removePortletDescription(org.gatein.pc.api.PortletContext pc)
- {
- String handle = WSRPUtils.convertToWSRPPortletContext(pc).getPortletHandle();
-
- PortletDescription description = portletDescriptions.get(handle);
- if (description != null)
+ public PortletDescription getBestDescriptionFor(List<String>
desiredLanguages, final PortletInfo info)
{
- // deal with events
- for (QName event : description.getHandledEvents())
+ Set<String> supportedLanguages = getSupportedLanguages();
+ String language = null;
+ for (String languageTag : desiredLanguages)
{
- removeEvent(event);
+ if (supportedLanguages.contains(languageTag))
+ {
+ language = languageTag;
+ }
}
- for (QName event : description.getPublishedEvents())
+
+ if (language == null)
{
- removeEvent(event);
+ return languageToDescription.get(defaultLanguage);
}
-
- portletDescriptions.remove(handle);
+ else
+ {
+ PortletDescription description = languageToDescription.get(language);
+ String handle =
languageToDescription.get(defaultLanguage).getPortletHandle();
+ if (description == null)
+ {
+ PortletDescription portletDescription = createPortletDescription(info,
desiredLanguages, handle);
+ languageToDescription.put(language, portletDescription);
+ return portletDescription;
+ }
+ else
+ {
+ return description;
+ }
+ }
}
- }
+ }*/
}
}