Search in sources :

Example 16 with IPluginManager

use of org.pentaho.platform.api.engine.IPluginManager in project pentaho-platform by pentaho.

the class ActionUtilTest method resolveClassTestHappyPath.

@Test
public void resolveClassTestHappyPath() throws Exception {
    // TODO: rewrite this test to read bean from spring rather than mocking it
    String beanId = "ktr.backgroundAction";
    Class<?> clazz = MyTestAction.class;
    IPluginManager pluginManager = mock(IPluginManager.class);
    PowerMockito.mockStatic(PentahoSystem.class);
    BDDMockito.given(PentahoSystem.get(IPluginManager.class)).willReturn(pluginManager);
    Mockito.doReturn(clazz).when(pluginManager).loadClass(anyString());
    Class<?> aClass = ActionUtil.resolveActionClass(MyTestAction.class.getName(), beanId);
    assertEquals(MyTestAction.class, aClass);
}
Also used : IPluginManager(org.pentaho.platform.api.engine.IPluginManager) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with IPluginManager

use of org.pentaho.platform.api.engine.IPluginManager in project pentaho-platform by pentaho.

the class ChartBeansSystemListener method initPlugins.

/**
 * This methods looks in the chartbeans configuration file, and retrieves the prescribed list of chart plugins
 * (renderers). For each defined renderer, the method then looks to the PluginManager to see if that renderer has been
 * overridden. If it has, then the class defined in the PluginManager will be loaded IN PLACE OF the configuration
 * file's class.
 *
 * @return list of available chart "plugin" (renderer) instances.
 * @throws Exception
 *           if no chart plugins (renderers) are found.
 */
@SuppressWarnings("unchecked")
private List<IChartPlugin> initPlugins() throws Exception {
    ArrayList<IChartPlugin> plugins = new ArrayList<IChartPlugin>();
    HashMap<String, Object> pluginMap = new HashMap<String, Object>();
    // $NON-NLS-1$
    List<Element> nodes = PentahoSystem.getSystemSettings().getSystemSettings(configFile, "bean");
    if (nodes == null || nodes.size() == 0) {
        // $NON-NLS-1$
        String msg = Messages.getInstance().getString("ChartBeansSystemListener.ERROR_0001_CONFIG_MISSING");
        Logger.warn(ChartBeansSystemListener.class.getName(), msg);
        throw new ChartSystemInitializationException(msg);
    }
    Element node;
    String id;
    Object plugin;
    IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class);
    for (int i = 0; i < nodes.size(); i++) {
        node = nodes.get(i);
        // $NON-NLS-1$
        id = node.attribute("id").getText();
        // $NON-NLS-1$
        pluginMap.put(id, node.attribute("class").getText());
        // Now let's see if there is a plugin overriding this engine...
        if ((null != pluginManager) && (pluginManager.isBeanRegistered(id))) {
            try {
                plugin = pluginManager.getBean(id);
                pluginMap.put(id, plugin);
            } catch (PluginBeanException e) {
                Logger.warn(ChartBeansSystemListener.class.getName(), Messages.getInstance().getString("ChartBeansSystemListener.ERROR_0002_PLUGINMANAGER_BEAN_MISSING", // $NON-NLS-1$
                id), e);
            }
        }
    }
    for (Object clazz : pluginMap.values()) {
        try {
            if (clazz instanceof String) {
                plugins.add((IChartPlugin) Class.forName(clazz.toString()).newInstance());
            } else {
                plugins.add((IChartPlugin) clazz);
            }
        } catch (Exception ex) {
            Logger.warn(ChartBeansSystemListener.class.getName(), Messages.getInstance().getString("ChartBeansSystemListener.ERROR_0003_CLASS_CREATION_PROBLEM") + clazz, // $NON-NLS-1$
            ex);
        }
    }
    return plugins;
}
Also used : IChartPlugin(org.pentaho.chart.plugin.IChartPlugin) HashMap(java.util.HashMap) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) IPluginManager(org.pentaho.platform.api.engine.IPluginManager)

Example 18 with IPluginManager

use of org.pentaho.platform.api.engine.IPluginManager in project pentaho-platform by pentaho.

the class ActionUtil method resolveActionClass.

/**
 * Returns the {@link Class} that corresponds to the provides {@code actionClassName} and {@code beanId}.
 *
 * @param actionClassName the name of the class being resolved
 * @param beanId          the beanId of the class being resolved
 * @param retryBeanInstantiationIfFailed re-trigger bean instantiation attempt, should it fail for some reason
 * @return the {@link Class} that corresponds to the provides {@code actionClassName} and {@code beanId}
 * @throws PluginBeanException when the plugin required to resolve the bean class from the {@code beanId} cannot be
 *                             created
 * @throws Exception           when the required parameters are not provided
 */
static Class<?> resolveActionClass(final String actionClassName, final String beanId, final boolean retryBeanInstantiationIfFailed) throws PluginBeanException, ActionInvocationException {
    Class<?> clazz = null;
    if (StringUtils.isEmpty(beanId) && StringUtils.isEmpty(actionClassName)) {
        throw new ActionInvocationException(Messages.getInstance().getErrorString("ActionUtil.ERROR_0001_REQUIRED_PARAM_MISSING", INVOKER_ACTIONCLASS, INVOKER_ACTIONID));
    }
    final long retryCount = (retryBeanInstantiationIfFailed ? RETRY_COUNT : 1);
    // millis
    final long retrySleepCount = (retryBeanInstantiationIfFailed ? RETRY_SLEEP_AMOUNT : 1);
    for (int i = 0; i < retryCount; i++) {
        try {
            if (!StringUtils.isEmpty(beanId)) {
                IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class);
                clazz = pluginManager.loadClass(beanId);
                if (clazz != null) {
                    return clazz;
                }
            }
            // we will execute this only if the beanId is not provided, or if the beanId cannot be resolved
            if (!StringUtils.isEmpty(actionClassName)) {
                clazz = Class.forName(actionClassName);
                return clazz;
            }
        } catch (Throwable t) {
            try {
                Thread.sleep(retrySleepCount);
            } catch (InterruptedException ie) {
                logger.info(ie.getMessage(), ie);
            }
        }
    }
    // which can typically happen at system startup
    throw new ActionInvocationException(Messages.getInstance().getErrorString("ActionUtil.ERROR_0002_FAILED_TO_CREATE_ACTION", StringUtils.isEmpty(beanId) ? actionClassName : beanId));
}
Also used : IPluginManager(org.pentaho.platform.api.engine.IPluginManager) ActionInvocationException(org.pentaho.platform.api.action.ActionInvocationException)

Example 19 with IPluginManager

use of org.pentaho.platform.api.engine.IPluginManager in project pentaho-platform by pentaho.

the class PluginManagerNotConfiguredIT method testBadConfig1.

public void testBadConfig1() {
    startTest();
    // $NON-NLS-1$
    IPentahoSession session = new StandaloneSession("test user");
    IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class, session);
    assertNull(pluginManager);
    finishTest();
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IPluginManager(org.pentaho.platform.api.engine.IPluginManager)

Example 20 with IPluginManager

use of org.pentaho.platform.api.engine.IPluginManager in project pentaho-platform by pentaho.

the class RepositoryResource method doExecuteDefault.

/**
 * Takes a pathId to a file and generates a URI that represents the URL to call to generate content from that file.
 *
 * <p><b>Example Request:</b><br />
 *    GET pentaho/api/repos/public:steel%20wheels:Invoice%20(report).prpt/default
 * </p>
 *
 * @param pathId @param pathId
 *
 * @return URI that represents a forwarding URL to execute to generate content from the file {pathId}.
 *
 * <p><b>Example Response:</b></p>
 *  <pre function="syntax.xml">
 *    This response does not contain data.
 *  </pre>
 */
@GET
@Path("{pathId : .+}/default")
@Produces({ WILDCARD })
@StatusCodes({ @ResponseCode(code = 303, condition = "Successfully get the resource."), @ResponseCode(code = 404, condition = "Failed to find the resource.") })
public Response doExecuteDefault(@PathParam("pathId") String pathId) throws FileNotFoundException, MalformedURLException, URISyntaxException {
    String perspective = null;
    StringBuffer buffer = null;
    String url = null;
    String path = FileResource.idToPath(pathId);
    String extension = path.substring(path.lastIndexOf('.') + 1);
    IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class, PentahoSessionHolder.getSession());
    IContentInfo info = pluginManager.getContentTypeInfo(extension);
    for (IPluginOperation operation : info.getOperations()) {
        if (operation.getId().equalsIgnoreCase("RUN")) {
            // $NON-NLS-1$
            perspective = operation.getPerspective();
            break;
        }
    }
    if (perspective == null) {
        perspective = GENERATED_CONTENT_PERSPECTIVE;
    }
    buffer = httpServletRequest.getRequestURL();
    String queryString = httpServletRequest.getQueryString();
    url = // $NON-NLS-1$
    buffer.substring(0, buffer.lastIndexOf("/") + 1) + perspective + ((queryString != null && queryString.length() > 0) ? "?" + httpServletRequest.getQueryString() : "");
    return Response.seeOther((new URL(url)).toURI()).build();
}
Also used : IContentInfo(org.pentaho.platform.api.engine.IContentInfo) IPluginOperation(org.pentaho.platform.api.engine.IPluginOperation) IPluginManager(org.pentaho.platform.api.engine.IPluginManager) URL(java.net.URL) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes)

Aggregations

IPluginManager (org.pentaho.platform.api.engine.IPluginManager)24 ArrayList (java.util.ArrayList)7 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)5 IOException (java.io.IOException)4 Facet (org.codehaus.enunciate.Facet)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)2 IContentInfo (org.pentaho.platform.api.engine.IContentInfo)2 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)2 IPluginOperation (org.pentaho.platform.api.engine.IPluginOperation)2 IPluginResourceLoader (org.pentaho.platform.api.engine.IPluginResourceLoader)2 PluginBeanException (org.pentaho.platform.api.engine.PluginBeanException)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1