Search in sources :

Example 1 with SystemSettingsParameterProvider

use of org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider in project pentaho-platform by pentaho.

the class PojoComponent method executeAction.

@SuppressWarnings({ "unchecked" })
@Override
protected boolean executeAction() throws Throwable {
    Set<?> inputNames = getInputNames();
    Element defnNode = (Element) getComponentDefinition();
    // if( pojo instanceof IConfiguredPojo ) {
    if (getMethods.containsKey("CONFIGSETTINGSPATHS") && configureMethod != null) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        Method method = getMethods.get("CONFIGSETTINGSPATHS");
        Set<String> settingsPaths = (Set<String>) method.invoke(pojo, new Object[] {});
        Iterator<String> keys = settingsPaths.iterator();
        Map<String, String> settings = new HashMap<String, String>();
        SystemSettingsParameterProvider params = new SystemSettingsParameterProvider();
        while (keys.hasNext()) {
            String path = keys.next();
            String value = params.getStringParameter(path, null);
            if (value != null) {
                settings.put(path, value);
            }
        }
        configureMethod.invoke(pojo, new Object[] { settings });
    }
    // set the PentahoSession
    if (sessionMethod != null) {
        callMethods(Arrays.asList(new Method[] { sessionMethod }), getSession());
    }
    // set the logger
    if (loggerMethod != null) {
        callMethods(Arrays.asList(new Method[] { loggerMethod }), getLogger());
    }
    Map<String, Object> inputMap = new HashMap<String, Object>();
    // look at the component settings
    // $NON-NLS-1$
    List<?> nodes = defnNode.selectNodes("*");
    for (int idx = 0; idx < nodes.size(); idx++) {
        Element node = (Element) nodes.get(idx);
        // inputs may typically contain a dash in them, such as
        // something like "report-definition" and we should expect
        // a setter as setReportDefinition, so we will remove the
        // dashes and everything should proceed as expected
        // $NON-NLS-1$ //$NON-NLS-2$
        String name = node.getName().replace("-", "").toUpperCase();
        if (!name.equals("CLASS") && !name.equals("OUTPUTSTREAM")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String value = node.getText();
            List<Method> method = setMethods.get(name);
            if (method != null) {
                callMethodWithString(method, value);
            } else if (runtimeInputsMethod != null) {
                inputMap.put(name, value);
            } else {
                // Supress error (For string/value replacement)
                // $NON-NLS-1$
                getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
            }
        }
    }
    Iterator<?> it = null;
    // now process all of the resources and see if we can call them as setters
    Set<?> resourceNames = getResourceNames();
    Map<String, IActionSequenceResource> resourceMap = new HashMap<String, IActionSequenceResource>();
    if (resourceNames != null && resourceNames.size() > 0) {
        it = resourceNames.iterator();
        while (it.hasNext()) {
            String name = (String) it.next();
            IActionSequenceResource resource = getResource(name);
            // $NON-NLS-1$ //$NON-NLS-2$
            name = name.replace("-", "");
            resourceMap.put(name, resource);
            List<Method> methods = setMethods.get(name.toUpperCase());
            if (methods != null) {
                for (Method method : methods) {
                    Class<?>[] paramTypes = method.getParameterTypes();
                    if (paramTypes.length == 1) {
                        Object value = null;
                        if (paramTypes[0] == InputStream.class) {
                            value = resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
                        } else if (paramTypes[0] == IActionSequenceResource.class) {
                            value = resource;
                        } else if (paramTypes[0] == String.class) {
                            value = getRuntimeContext().getResourceAsString(resource);
                        } else if (paramTypes[0] == Document.class) {
                            value = getRuntimeContext().getResourceAsDocument(resource);
                        }
                        callMethod(method, value);
                    }
                }
            // CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
            } else {
            // BISERVER-2715 we should ignore this as the resource might be meant for another component
            }
        }
    }
    // now process all of the inputs, overriding the component settings
    it = inputNames.iterator();
    while (it.hasNext()) {
        String name = (String) it.next();
        Object value = getInputValue(name);
        // now that we have the value, we can fix the name
        // $NON-NLS-1$ //$NON-NLS-2$
        name = name.replace("-", "");
        List<Method> methods = setMethods.get(name.toUpperCase());
        if (methods != null) {
            callMethods(methods, value);
        } else if (runtimeInputsMethod != null) {
            inputMap.put(name, value);
        } else {
            // Supress error (For string/value replacement)
            // $NON-NLS-1$
            getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
        }
    }
    if (resourceMap.size() > 0 && resourcesMethod != null) {
        // call the resources setter
        resourcesMethod.invoke(pojo, new Object[] { resourceMap });
    }
    if (inputMap.size() > 0 && runtimeInputsMethod != null) {
        // call the generic input setter
        runtimeInputsMethod.invoke(pojo, new Object[] { inputMap });
    }
    if (// $NON-NLS-1$ //$NON-NLS-2$
    getOutputNames().contains("outputstream") && setMethods.containsKey("OUTPUTSTREAM") && getMethods.containsKey("MIMETYPE")) {
        // $NON-NLS-1$
        // get the mime-type
        // Get the first method to match
        // $NON-NLS-1$
        Method method = getMethods.get("MIMETYPE");
        String mimeType = (String) method.invoke(pojo, new Object[] {});
        // $NON-NLS-1$
        String mappedOutputName = "outputstream";
        if ((getActionDefinition() != null) && (getActionDefinition().getOutput("outputstream") != null)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            mappedOutputName = getActionDefinition().getOutput("outputstream").getPublicName();
        }
        // this marks the HttpOutputHandler as contentDone=true, causing the MessageFormatter to not print an error
        IContentItem contentItem = getOutputContentItem(mappedOutputName, mimeType);
        if (!(contentItem instanceof SimpleContentItem)) {
            // SimpleContentItem can't handle being added to outputs because it
            // doesn't have a getInputStream(), and the path used to return
            // null.
            // $NON-NLS-1$
            setOutputValue("outputstream", contentItem);
        }
        // set the output stream
        OutputStream out = contentItem.getOutputStream(getActionName());
        // $NON-NLS-1$
        method = setMethods.get("OUTPUTSTREAM").get(0);
        method.invoke(pojo, new Object[] { out });
    }
    if (validateMethod != null) {
        Object obj = validateMethod.invoke(pojo, (Object[]) null);
        if (obj instanceof Boolean) {
            Boolean ok = (Boolean) obj;
            if (!ok) {
                return false;
            }
        }
    }
    // now execute the pojo
    Boolean result = Boolean.FALSE;
    if (executeMethod != null) {
        result = (Boolean) executeMethod.invoke(pojo, new Object[] {});
    } else {
        // we can only assume we are ok so far
        result = Boolean.TRUE;
    }
    // now handle outputs
    Set<?> outputNames = getOutputNames();
    // first get the runtime outputs
    Map<String, Object> outputMap = new HashMap<String, Object>();
    if (runtimeOutputsMethod != null) {
        outputMap = (Map<String, Object>) runtimeOutputsMethod.invoke(pojo, new Object[] {});
    }
    it = outputNames.iterator();
    while (it.hasNext()) {
        String name = (String) it.next();
        // CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
        if (name.equals("outputstream")) {
        // $NON-NLS-1$
        // we should be done
        } else {
            IActionParameter param = getOutputItem(name);
            Method method = getMethods.get(name.toUpperCase());
            if (method != null) {
                Object value = method.invoke(pojo, new Object[] {});
                param.setValue(value);
            } else {
                Object value = outputMap.get(name);
                if (value != null) {
                    param.setValue(value);
                } else {
                    throw new NoSuchMethodException(name);
                }
            }
        }
    }
    return result.booleanValue();
}
Also used : Set(java.util.Set) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) HashMap(java.util.HashMap) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) Method(java.lang.reflect.Method) IContentItem(org.pentaho.platform.api.repository.IContentItem) SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 2 with SystemSettingsParameterProvider

use of org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider in project pentaho-platform by pentaho.

the class SettingsParameterProviderTest method testSystemSettingsParameterProvider3.

@Test
public void testSystemSettingsParameterProvider3() {
    // pull a string from pentaho.xml
    SystemSettingsParameterProvider provider = new SystemSettingsParameterProvider();
    String value = provider.getStringParameter("bogus.xml{pentaho-system/log-file}", null);
    Assert.assertEquals("Expected null result", null, value);
}
Also used : SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) Test(org.junit.Test)

Example 3 with SystemSettingsParameterProvider

use of org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider in project pentaho-platform by pentaho.

the class SettingsParameterProviderTest method testSystemSettingsParameterProvider2.

@Test
public void testSystemSettingsParameterProvider2() throws PlatformInitializationException {
    // pull a string from pentaho.xml
    SystemSettingsParameterProvider provider = new SystemSettingsParameterProvider();
    String value = provider.getStringParameter("pentaho.xml{pentaho-system/log-file}", null);
    Assert.assertEquals("Could not get setting from pentaho.xml", "server.log", value);
    value = (String) provider.getParameter("pentaho.xml{pentaho-system/log-file}");
    Assert.assertEquals("Could not get setting from pentaho.xml", "server.log", value);
    Assert.assertFalse(provider.getParameterNames().hasNext());
}
Also used : SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) Test(org.junit.Test)

Example 4 with SystemSettingsParameterProvider

use of org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider in project pentaho-platform by pentaho.

the class SettingsParameterProviderTest method testSystemSettingsParameterProvider4.

@Test
public void testSystemSettingsParameterProvider4() {
    // pull a string from pentaho.xml
    SystemSettingsParameterProvider provider = new SystemSettingsParameterProvider();
    String value = provider.getStringParameter("pentaho.xml{bogus}", null);
    Assert.assertEquals("Expected null result", null, value);
}
Also used : SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) Test(org.junit.Test)

Example 5 with SystemSettingsParameterProvider

use of org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider in project pentaho-platform by pentaho.

the class SystemParameterProviderTest method testSystemParameter.

public void testSystemParameter() {
    startTest();
    SystemSettingsParameterProvider provider = new SystemSettingsParameterProvider();
    assertEquals("Output is not correct", "server.log", provider.getStringParameter("pentaho.xml{pentaho-system/log-file}", null));
    finishTest();
}
Also used : SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider)

Aggregations

SystemSettingsParameterProvider (org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider)5 Test (org.junit.Test)3 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)1 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)1 IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)1 IContentItem (org.pentaho.platform.api.repository.IContentItem)1 SimpleContentItem (org.pentaho.platform.engine.core.output.SimpleContentItem)1