Search in sources :

Example 21 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class ComponentScriptService method getInstance.

/**
 * Find a component instance that implements that passed type. If the component has a singleton lifecycle then this
 * method always return the same instance.
 *
 * @param <T> the component role type
 * @param roleType the class (aka role) that the component implements
 * @return the component instance or null if not found
 * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility)
 */
public <T> T getInstance(Type roleType) {
    T result = null;
    ComponentManager cm = getContextComponentManager();
    if (cm != null) {
        try {
            result = cm.getInstance(roleType);
        } catch (ComponentLookupException e) {
            setError(e);
        }
    }
    return result;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 22 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class ComponentScriptService method getInstance.

/**
 * Find a component instance that implements that passed interface class. If the component has a singleton lifecycle
 * then this method always return the same instance.
 *
 * @param <T> the component role type
 * @param roleType the class (aka role) that the component implements
 * @param roleHint the hint that differentiates a component implementation from another one (each component is
 *            registered with a hint; the "default" hint being the default)
 * @return the component instance or null if not found
 * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility)
 */
public <T> T getInstance(Type roleType, String roleHint) {
    T result = null;
    ComponentManager cm = getContextComponentManager();
    if (cm != null) {
        try {
            result = cm.getInstance(roleType, roleHint);
        } catch (ComponentLookupException e) {
            setError(e);
        }
    }
    return result;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 23 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class DefaultChartGenerator method generate.

@Override
public byte[] generate(ChartModel model, Map<String, String> parameters) throws ChartGeneratorException {
    setDefaultParams(parameters);
    String type = parameters.get(TYPE_PARAM);
    String title = parameters.get(TITLE_PARAM);
    PlotGenerator generator;
    try {
        generator = contextComponentManager.get().getInstance(PlotGenerator.class, type);
    } catch (ComponentLookupException e) {
        throw new ChartGeneratorException(String.format("No such chart type : [%s].", type), e);
    }
    Plot plot = generator.generate(model, parameters);
    JFreeChart jfchart = new JFreeChart(title, plot);
    // Run customizations
    for (ChartCustomizer customizer : this.customizerProvider.get()) {
        customizer.customize(jfchart, parameters);
    }
    int width = Integer.parseInt(parameters.get(WIDTH_PARAM));
    int height = Integer.parseInt(parameters.get(HEIGHT_PARAM));
    try {
        return ChartUtilities.encodeAsPNG(jfchart.createBufferedImage(width, height));
    } catch (IOException ex) {
        throw new ChartGeneratorException("Error while png encoding the chart image.");
    }
}
Also used : ChartGeneratorException(org.xwiki.chart.ChartGeneratorException) ChartCustomizer(org.xwiki.chart.ChartCustomizer) PlotGenerator(org.xwiki.chart.internal.plot.PlotGenerator) Plot(org.jfree.chart.plot.Plot) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException) JFreeChart(org.jfree.chart.JFreeChart)

Example 24 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method saveFilterPreferences.

@Override
public void saveFilterPreferences(Set<NotificationFilterPreference> filterPreferences) {
    Map<String, Set<NotificationFilterPreference>> preferencesMapping = new HashMap<>();
    for (NotificationFilterPreference filterPreference : filterPreferences) {
        // Try to get the corresponding provider, if no provider can be found, discard the save of the preference
        String providerHint = filterPreference.getProviderHint();
        if (componentManager.hasComponent(NotificationFilterPreferenceProvider.class, providerHint)) {
            if (!preferencesMapping.containsKey(providerHint)) {
                preferencesMapping.put(providerHint, new HashSet<>());
            }
            preferencesMapping.get(providerHint).add(filterPreference);
        }
    }
    // Once we have created the mapping, save all the preferences using their correct providers
    for (String providerHint : preferencesMapping.keySet()) {
        try {
            NotificationFilterPreferenceProvider provider = componentManager.getInstance(NotificationFilterPreferenceProvider.class, providerHint);
            provider.saveFilterPreferences(preferencesMapping.get(providerHint));
        } catch (ComponentLookupException e) {
            logger.error("Unable to retrieve the notification filter preference provider for hint [{}]: [{}]", providerHint, e);
        } catch (NotificationException e) {
            logger.warn("Unable save the filter preferences [{}] against the provider [{}]: [{}]", preferencesMapping.get(providerHint), providerHint, ExceptionUtils.getRootCauseMessage(e));
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) NotificationFilterPreferenceProvider(org.xwiki.notifications.filters.NotificationFilterPreferenceProvider) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationException(org.xwiki.notifications.NotificationException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 25 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class ModelScriptServiceTest method createDocumentReferenceWhenInvalidHint.

@Test
public void createDocumentReferenceWhenInvalidHint() throws Exception {
    when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "invalid")).thenThrow(new ComponentLookupException("error"));
    // Make sure backward compatibility is preserved.
    when(this.componentManager.getInstance(DocumentReferenceResolver.class, "invalid")).thenThrow(new ComponentLookupException("error"));
    Assert.assertNull(this.service.createDocumentReference("wiki", "space", "page", "invalid"));
}
Also used : ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Test(org.junit.Test)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)104 ComponentManager (org.xwiki.component.manager.ComponentManager)24 Test (org.junit.Test)15 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 InitializationException (org.xwiki.component.phase.InitializationException)8 ArrayList (java.util.ArrayList)7 XWikiException (com.xpn.xwiki.XWikiException)5 HashMap (java.util.HashMap)5 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)5 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)5 FilterException (org.xwiki.filter.FilterException)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Type (java.lang.reflect.Type)4 HashSet (java.util.HashSet)4 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 WikiReference (org.xwiki.model.reference.WikiReference)4