Search in sources :

Example 1 with JetBean

use of org.talend.designer.codegen.config.JetBean in project tdi-studio-se by Talend.

the class CodeGenerator method generateTypedComponentCode.

/**
     * Generate Code Part for a given Component.
     * 
     * @param type the internal component template
     * @param argument the bean
     * @param part part of code to generate
     * @param subProcess
     * @return the genrated code
     * @throws CodeGeneratorException if an error occurs during Code Generation
     */
private StringBuffer generateTypedComponentCode(EInternalTemplate type, Object argument, ECodePart part, String incomingName, NodesSubTree subProcess) throws CodeGeneratorException {
    CodeGeneratorArgument codeGenArgument = new CodeGeneratorArgument();
    codeGenArgument.setNode(argument);
    if (subProcess != null) {
        codeGenArgument.setAllMainSubTreeConnections(subProcess.getAllMainSubTreeConnections());
        codeGenArgument.setSubTree(subProcess);
    }
    codeGenArgument.setCodePart(part);
    codeGenArgument.setStatistics(statistics);
    codeGenArgument.setTrace(trace);
    codeGenArgument.setInterpreterPath(interpreterPath);
    codeGenArgument.setLibPath(libPath);
    codeGenArgument.setRuntimeFilePath(runtimeFilePath);
    codeGenArgument.setCurrentProjectName(currentProjectName);
    codeGenArgument.setContextName(contextName);
    codeGenArgument.setJobName(jobName);
    codeGenArgument.setJobVersion(jobVersion);
    codeGenArgument.setCheckingSyntax(checkingSyntax);
    codeGenArgument.setIncomingName(incomingName);
    codeGenArgument.setIsRunInMultiThread(getRunInMultiThread());
    codeGenArgument.setPauseTime(CorePlugin.getDefault().getRunProcessService().getPauseTime());
    JetBean jetBean = initializeJetBean(codeGenArgument);
    StringBuffer content = new StringBuffer();
    for (TemplateUtil template : CodeGeneratorInternalTemplatesFactoryProvider.getInstance().getTemplatesFromType(type)) {
        jetBean.setJetPluginRepository(template.getJetPluginRepository());
        jetBean.setTemplateRelativeUri(template.getTemplateRelativeUri());
        content.append(instantiateJetProxy(jetBean));
    }
    return content;
}
Also used : JetBean(org.talend.designer.codegen.config.JetBean) TemplateUtil(org.talend.designer.codegen.config.TemplateUtil) CodeGeneratorArgument(org.talend.designer.codegen.config.CodeGeneratorArgument)

Example 2 with JetBean

use of org.talend.designer.codegen.config.JetBean in project tdi-studio-se by Talend.

the class CodeGenerator method generateComponentCode.

/**
     * Generate Part Code for a given Component.
     * 
     * @param node the component
     * @param part the component's part
     * @return the generated code
     * @throws CodeGeneratorException if an error occurs during Code Generation
     */
public String generateComponentCode(NodesSubTree subProcess, INode node, ECodePart part, String incomingName, ETypeGen typeGen) throws CodeGeneratorException {
    CodeGeneratorArgument argument = new CodeGeneratorArgument();
    argument.setNode(node);
    argument.setAllMainSubTreeConnections(subProcess.getAllMainSubTreeConnections());
    argument.setCodePart(part);
    argument.setStatistics(statistics);
    argument.setTrace(trace);
    argument.setInterpreterPath(interpreterPath);
    argument.setLibPath(libPath);
    argument.setRuntimeFilePath(runtimeFilePath);
    argument.setCurrentProjectName(currentProjectName);
    argument.setContextName(contextName);
    argument.setJobName(jobName);
    argument.setJobVersion(jobVersion);
    argument.setCheckingSyntax(checkingSyntax);
    argument.setIncomingName(incomingName);
    argument.setIsRunInMultiThread(getRunInMultiThread());
    argument.setPauseTime(CorePlugin.getDefault().getRunProcessService().getPauseTime());
    JetBean jetBean = initializeJetBean(argument);
    StringBuffer content = new StringBuffer();
    try {
        if (typeGen == ETypeGen.ETL) {
            content.append(generateTypedComponentCode(EInternalTemplate.PART_HEADER, node, part, incomingName, subProcess));
        }
        IComponentFileNaming componentFileNaming = ComponentsFactoryProvider.getFileNamingInstance();
        IComponent component = node.getComponent();
        String templateURI = component.getPathSource() + TemplateUtil.DIR_SEP + node.getComponent().getName() + TemplateUtil.DIR_SEP + componentFileNaming.getJetFileName(node.getComponent(), language.getExtension(), part);
        // Need rewrite templateURI for generic component since create a new JetBean .
        if (EComponentType.GENERIC.equals(component.getComponentType())) {
            templateURI = TemplateUtil.JET_STUB_DIRECTORY + TemplateUtil.DIR_SEP + TemplateUtil.RESOURCES_DIRECTORY_GENERIC + TemplateUtil.DIR_SEP + "component_" + //$NON-NLS-1$
            part.getName() + TemplateUtil.EXT_SEP + language.getExtension() + TemplateUtil.TEMPLATE_EXT;
        }
        jetBean.setTemplateRelativeUri(templateURI);
        JetProxy proxy = new JetProxy(jetBean);
        content.append(proxy.generate());
        if (jetBean.getGenerationError() != null) {
            throw new CodeGeneratorException(jetBean.getGenerationError());
        }
        if (typeGen == ETypeGen.ETL) {
            content.append(generateTypedComponentCode(EInternalTemplate.PART_FOOTER, node, part, incomingName, subProcess));
        }
    } catch (JETException jetException) {
        log.error(jetException.getMessage(), jetException);
        throw new CodeGeneratorException(//$NON-NLS-1$
        jetException.toString() + " in " + argument.getJobName() + " job", //$NON-NLS-1$
        jetException);
    } catch (CoreException coreException) {
        log.error(coreException.getMessage(), coreException);
        throw new CodeGeneratorException(coreException);
    }
    return content.toString();
}
Also used : JetBean(org.talend.designer.codegen.config.JetBean) IComponentFileNaming(org.talend.core.model.components.IComponentFileNaming) CoreException(org.eclipse.core.runtime.CoreException) CodeGeneratorArgument(org.talend.designer.codegen.config.CodeGeneratorArgument) IComponent(org.talend.core.model.components.IComponent) CodeGeneratorException(org.talend.designer.codegen.exception.CodeGeneratorException) JetProxy(org.talend.designer.codegen.proxy.JetProxy) JETException(org.eclipse.emf.codegen.jet.JETException)

Example 3 with JetBean

use of org.talend.designer.codegen.config.JetBean in project tdi-studio-se by Talend.

the class CodeGenerator method generateContextCode.

/**
     * Parse Process, and generate Code for Context Variables.
     * 
     * @param designerContext the context to generate code from
     * @return the generated code
     * @throws CodeGeneratorException if an error occurs during Code Generation
     */
@Override
public String generateContextCode(IContext designerContext) throws CodeGeneratorException {
    if (process != null) {
        if (designerContext == null) {
            designerContext = process.getContextManager().getDefaultContext();
        }
        List<IContextParameter> listParameters = designerContext.getContextParameterList();
        if (listParameters != null) {
            List<IContextParameter> listParametersCopy = new ArrayList<IContextParameter>(listParameters.size());
            CodeGeneratorArgument codeGenArgument = new CodeGeneratorArgument();
            // encrypt the password
            for (IContextParameter iContextParameter : listParameters) {
                if (PasswordEncryptUtil.isPasswordType(iContextParameter.getType())) {
                    IContextParameter icp = iContextParameter.clone();
                    String pwd = icp.getValue();
                    if (pwd != null && !pwd.isEmpty()) {
                        try {
                            icp.setValue(PasswordEncryptUtil.encryptPasswordHex(pwd));
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                        }
                    }
                    listParametersCopy.add(icp);
                } else {
                    listParametersCopy.add(iContextParameter);
                }
            }
            codeGenArgument.setNode(listParametersCopy);
            codeGenArgument.setContextName(designerContext.getName());
            codeGenArgument.setCurrentProjectName(currentProjectName);
            codeGenArgument.setJobName(jobName);
            codeGenArgument.setJobVersion(jobVersion);
            codeGenArgument.setIsRunInMultiThread(getRunInMultiThread());
            codeGenArgument.setPauseTime(CorePlugin.getDefault().getRunProcessService().getPauseTime());
            JetBean jetBean = initializeJetBean(codeGenArgument);
            StringBuffer content = new StringBuffer();
            for (TemplateUtil template : CodeGeneratorInternalTemplatesFactoryProvider.getInstance().getTemplatesFromType(EInternalTemplate.CONTEXT)) {
                jetBean.setJetPluginRepository(template.getJetPluginRepository());
                jetBean.setTemplateRelativeUri(template.getTemplateRelativeUri());
                content.append(instantiateJetProxy(jetBean));
            }
            return content.toString();
        }
    }
    //$NON-NLS-1$
    return "";
}
Also used : JetBean(org.talend.designer.codegen.config.JetBean) TemplateUtil(org.talend.designer.codegen.config.TemplateUtil) CodeGeneratorArgument(org.talend.designer.codegen.config.CodeGeneratorArgument) ArrayList(java.util.ArrayList) IContextParameter(org.talend.core.model.process.IContextParameter) CoreException(org.eclipse.core.runtime.CoreException) JETException(org.eclipse.emf.codegen.jet.JETException) CodeGeneratorException(org.talend.designer.codegen.exception.CodeGeneratorException)

Example 4 with JetBean

use of org.talend.designer.codegen.config.JetBean in project tdi-studio-se by Talend.

the class CodeGeneratorEmittersPoolFactory method initGenericComponent.

/**
     * 
     * Initialization of the generic components.
     * 
     * @param codeLanguage
     * @param jetBeans
     * @param codePart
     * @param component
     */
private static void initGenericComponent(ECodeLanguage codeLanguage, List<JetBean> jetBeans, ECodePart codePart, IComponent component) {
    if (component.getAvailableCodeParts().contains(codePart)) {
        String templateURI = TemplateUtil.JET_STUB_DIRECTORY + TemplateUtil.DIR_SEP + TemplateUtil.RESOURCES_DIRECTORY_GENERIC + TemplateUtil.DIR_SEP + "component_" + //$NON-NLS-1$
        codePart.getName() + TemplateUtil.EXT_SEP + codeLanguage.getExtension() + TemplateUtil.TEMPLATE_EXT;
        //$NON-NLS-1$
        String componentsPath = "org.talend.designer.codegen";
        // TODO
        JetBean jetBean = new //$NON-NLS-1$
        JetBean(//$NON-NLS-1$
        componentsPath, //$NON-NLS-1$
        templateURI, //$NON-NLS-1$
        "component", //$NON-NLS-1$
        component.getVersion(), "", //$NON-NLS-1$
        codePart.getName());
        //$NON-NLS-1$ //$NON-NLS-2$
        jetBean.addClassPath("EMF_ECORE", "org.eclipse.emf.ecore");
        //$NON-NLS-1$ //$NON-NLS-2$
        jetBean.addClassPath("EMF_COMMON", "org.eclipse.emf.common");
        //$NON-NLS-1$ //$NON-NLS-2$
        jetBean.addClassPath("CORERUNTIME_LIBRARIES", "org.talend.core.runtime");
        //$NON-NLS-1$ //$NON-NLS-2$
        jetBean.addClassPath("MANAGEMENT_LIBRARIES", "org.talend.metadata.managment");
        //$NON-NLS-1$
        jetBean.addClassPath("CORE_LIBRARIES", CorePlugin.PLUGIN_ID);
        //$NON-NLS-1$
        jetBean.addClassPath("CODEGEN_LIBRARIES", CodeGeneratorActivator.PLUGIN_ID);
        //$NON-NLS-1$
        jetBean.addClassPath("COMMON_LIBRARIES", CommonsPlugin.PLUGIN_ID);
        //$NON-NLS-1$ //$NON-NLS-2$
        jetBean.addClassPath("AVRO", "org.apache.servicemix.bundles.avro");
        for (String pluginDependency : component.getPluginDependencies()) {
            //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            jetBean.addClassPath(pluginDependency.toUpperCase().replaceAll("\\.", "_") + "_LIBRARIES", pluginDependency);
        }
        String familyName = component.getOriginalFamilyName();
        // TODO
        //$NON-NLS-1$
        familyName = "generic/component";
        if (familyName.contains("|")) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            familyName = component.getOriginalFamilyName().substring(0, component.getOriginalFamilyName().indexOf("|"));
        }
        jetBean.setFamily(StringUtils.removeSpecialCharsForPackage(familyName.toLowerCase()));
        if (component.getPluginExtension() != null) {
            //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            jetBean.addClassPath(//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            "EXTERNAL_COMPONENT_" + component.getPluginExtension().toUpperCase().replaceAll("\\.", "_"), component.getPluginExtension());
            jetBean.setClassLoader(ExternalNodesFactory.getInstance(component.getPluginExtension()).getClass().getClassLoader());
        } else {
            jetBean.setClassLoader(new CodeGeneratorEmittersPoolFactory().getClass().getClassLoader());
        }
        jetBeans.add(jetBean);
    }
}
Also used : JetBean(org.talend.designer.codegen.config.JetBean) LightJetBean(org.talend.designer.codegen.config.LightJetBean)

Example 5 with JetBean

use of org.talend.designer.codegen.config.JetBean in project tdi-studio-se by Talend.

the class CodeGeneratorEmittersPoolFactory method initializeUtilTemplate.

/**
     * initialization of available templates.
     * 
     * @param template
     * @param codeLanguage
     * @return
     */
private static JetBean initializeUtilTemplate(TemplateUtil template, ECodeLanguage codeLanguage) {
    JetBean jetBean = new JetBean(template.getJetPluginRepository(), template.getTemplateRelativeUri(), template.getResourceName(), template.getVersion(), codeLanguage.getName(), //$NON-NLS-1$
    "");
    //$NON-NLS-1$ //$NON-NLS-2$
    jetBean.addClassPath("CORERUNTIME_LIBRARIES", "org.talend.core.runtime");
    //$NON-NLS-1$ //$NON-NLS-2$
    jetBean.addClassPath("MANAGEMENT_LIBRARIES", "org.talend.metadata.managment");
    //$NON-NLS-1$
    jetBean.addClassPath("CORE_LIBRARIES", CorePlugin.PLUGIN_ID);
    //$NON-NLS-1$
    jetBean.addClassPath("CODEGEN_LIBRARIES", CodeGeneratorActivator.PLUGIN_ID);
    //$NON-NLS-1$
    jetBean.addClassPath("COMMON_LIBRARIES", CommonsPlugin.PLUGIN_ID);
    jetBean.setClassLoader(new CodeGeneratorEmittersPoolFactory().getClass().getClassLoader());
    //$NON-NLS-1$
    String sparkUtilsPluginName = "org.talend.designer.spark";
    if (PluginChecker.isPluginLoaded(sparkUtilsPluginName)) {
        //$NON-NLS-1$
        jetBean.addClassPath("SPARK_LIBRARIES", sparkUtilsPluginName);
        jetBean.setClassLoader(createDelegateClassLoader(jetBean.getClassLoader(), sparkUtilsPluginName, //$NON-NLS-1$
        "org.talend.designer.spark.SparkPlugin"));
    }
    return jetBean;
}
Also used : JetBean(org.talend.designer.codegen.config.JetBean) LightJetBean(org.talend.designer.codegen.config.LightJetBean)

Aggregations

JetBean (org.talend.designer.codegen.config.JetBean)16 LightJetBean (org.talend.designer.codegen.config.LightJetBean)8 CodeGeneratorArgument (org.talend.designer.codegen.config.CodeGeneratorArgument)7 JETException (org.eclipse.emf.codegen.jet.JETException)5 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)3 IComponentFileNaming (org.talend.core.model.components.IComponentFileNaming)3 IBrandingService (org.talend.core.ui.branding.IBrandingService)3 CodeGeneratorException (org.talend.designer.codegen.exception.CodeGeneratorException)3 HashMap (java.util.HashMap)2 BusinessException (org.talend.commons.exception.BusinessException)2 IContextParameter (org.talend.core.model.process.IContextParameter)2 INode (org.talend.core.model.process.INode)2 TalendJetEmitter (org.talend.designer.codegen.config.TalendJetEmitter)2 TemplateUtil (org.talend.designer.codegen.config.TemplateUtil)2 JetProxy (org.talend.designer.codegen.proxy.JetProxy)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1