Search in sources :

Example 1 with JavaJobExportReArchieveCreator

use of org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator in project tdi-studio-se by Talend.

the class BDJobReArchieveCreator method create.

public void create(File file, boolean isExport) {
    if (file == null || !file.exists() || (isExport && !file.isFile()) || fatherProcessItem == null) {
        return;
    }
    // check
    if (!isMRWithHDInsight() && !isFatJar()) {
        return;
    }
    Property property = processItem.getProperty();
    Property fatherProperty = fatherProcessItem.getProperty();
    String label = property.getLabel();
    String fatherLabel = fatherProperty.getLabel();
    String version = property.getVersion();
    JavaJobExportReArchieveCreator creator = new JavaJobExportReArchieveCreator(file.getAbsolutePath(), label);
    String jobJarName = JavaResourcesHelper.getJobJarName(property.getLabel(), property.getVersion()) + FileExtensions.JAR_FILE_SUFFIX;
    try {
        if (isExport) {
            // If we are in an export context, we first unzip the archive, then we modify the jar.
            // create temp folders.
            // clean temp folder
            creator.deleteTempFiles();
            //$NON-NLS-1$ //$NON-NLS-2$
            File zipTmpFolder = new File(creator.getTmpFolder(), "zip-" + label + "_" + version);
            //$NON-NLS-1$ //$NON-NLS-2$
            File jarTmpFolder = new File(creator.getTmpFolder(), "jar-" + label + "_" + version);
            zipTmpFolder.mkdirs();
            jarTmpFolder.mkdirs();
            // unzip the files.
            FilesUtils.unzip(file.getAbsolutePath(), zipTmpFolder.getAbsolutePath());
            // same the the job pom assembly for package.
            File originalJarFile = new File(zipTmpFolder, fatherLabel + '/' + jobJarName);
            modifyJar(originalJarFile, jarTmpFolder, creator, jobJarName, zipTmpFolder, property);
            ZipToFile.zipFile(zipTmpFolder.getAbsolutePath(), file.getAbsolutePath());
        } else {
            // If we are in a local context, we just have to modify the jar.
            // clean temp folder
            creator.deleteTempFiles();
            //$NON-NLS-1$ //$NON-NLS-2$
            File jarTmpFolder = new File(creator.getTmpFolder(), "jar-" + label + "_" + version);
            jarTmpFolder.mkdirs();
            if (GlobalServiceRegister.getDefault().isServiceRegistered(IRunProcessService.class)) {
                IRunProcessService service = (IRunProcessService) GlobalServiceRegister.getDefault().getService(IRunProcessService.class);
                ITalendProcessJavaProject talendProcessJavaProject = service.getTalendProcessJavaProject();
                File targetFolder = new File(talendProcessJavaProject.getTargetFolder().getLocationURI());
                // In a local mode, the routines/beans/udfs jars are not in the lib folder. We then copy them.
                // Copy routines and rename it to match the
                // "JavaUtils.ROUTINE_JAR_NAME + FileExtensions.JAR_FILE_SUFFIX" name
                Set<FilterInfo> codeJars = new HashSet<FilterInfo>(Arrays.asList(new FilterInfo(JavaUtils.ROUTINE_JAR_NAME, FileExtensions.JAR_FILE_SUFFIX)));
                for (File f : FileUtils.getAllFilesFromFolder(targetFolder, codeJars)) {
                    FilesUtils.copyFile(f, new File(talendProcessJavaProject.getLibFolder().getLocation().toPortableString() + "/" + JavaUtils.ROUTINE_JAR_NAME + //$NON-NLS-1$
                    FileExtensions.JAR_FILE_SUFFIX));
                }
                // Copy beans and rename it to match the "JavaUtils.BEANS_JAR_NAME + FileExtensions.JAR_FILE_SUFFIX"
                // name
                codeJars = new HashSet<FilterInfo>(Arrays.asList(new FilterInfo(JavaUtils.BEANS_JAR_NAME, FileExtensions.JAR_FILE_SUFFIX)));
                for (File f : FileUtils.getAllFilesFromFolder(targetFolder, codeJars)) {
                    FilesUtils.copyFile(f, new File(talendProcessJavaProject.getLibFolder().getLocation().toPortableString() + "/" + JavaUtils.BEANS_JAR_NAME + //$NON-NLS-1$
                    FileExtensions.JAR_FILE_SUFFIX));
                }
                // Copy udfs and rename it to match the
                // "JavaUtils.PIGUDFS_JAR_NAME + FileExtensions.JAR_FILE_SUFFIX"
                // name
                codeJars = new HashSet<FilterInfo>(Arrays.asList(new FilterInfo(JavaUtils.PIGUDFS_JAR_NAME, FileExtensions.JAR_FILE_SUFFIX)));
                for (File f : FileUtils.getAllFilesFromFolder(targetFolder, codeJars)) {
                    FilesUtils.copyFile(f, new File(talendProcessJavaProject.getLibFolder().getLocation().toPortableString() + "/" + JavaUtils.PIGUDFS_JAR_NAME + //$NON-NLS-1$
                    FileExtensions.JAR_FILE_SUFFIX));
                }
                modifyJar(new File(file, jobJarName), jarTmpFolder, creator, jobJarName, talendProcessJavaProject.getLibFolder().getParent().getLocation().toFile(), property);
            } else {
                //$NON-NLS-1$
                CommonExceptionHandler.log("Unable to update the job jar because the RunProcessService is not registered.");
            }
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        // when debug, keep the files first. because when build again. will do clean also.
        if (!CommonsPlugin.isDebugMode()) {
            creator.deleteTempFiles();
        }
    }
}
Also used : IRunProcessService(org.talend.designer.runprocess.IRunProcessService) JavaJobExportReArchieveCreator(org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator) Property(org.talend.core.model.properties.Property) ZipToFile(org.talend.repository.ui.utils.ZipToFile) File(java.io.File) FilterInfo(org.talend.utils.files.FilterInfo) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 2 with JavaJobExportReArchieveCreator

use of org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator in project tdi-studio-se by Talend.

the class BuildJobManager method buildJobs.

public boolean buildJobs(String destinationPath, List<? extends IRepositoryNode> nodes, List<String> topNames, String version, String context, Map<ExportChoice, Object> exportChoiceMap, JobExportType jobExportType, IProgressMonitor monitor) throws Exception {
    IProgressMonitor pMonitor = new NullProgressMonitor();
    if (monitor != null) {
        pMonitor = monitor;
    }
    final List<ProcessItem> processes = getProcesses(nodes);
    if (processes.size() == 1) {
        ProcessItem item = processes.get(0);
        buildJob(destinationPath, item, version, context, exportChoiceMap, jobExportType, pMonitor);
    } else {
        int scale = 1000;
        int steps = 3;
        pMonitor.beginTask(Messages.getString("JobScriptsExportWizardPage.newExportJobScript", jobExportType), //$NON-NLS-1$
        steps * scale * nodes.size());
        String topName = null;
        if (topNames != null && !topNames.isEmpty()) {
            topName = topNames.get(0);
        } else {
            topName = ProjectManager.getInstance().getCurrentProject().getLabel();
        }
        File desFile = new File(destinationPath);
        //$NON-NLS-1$ //$NON-NLS-2$
        File createTempFile = File.createTempFile("building_job", "");
        createTempFile.delete();
        File tempFolder = new File(desFile.getParent() + File.separator + createTempFile.getName());
        if (tempFolder.exists()) {
            tempFolder.delete();
        }
        File tempProFolder = new File(tempFolder, topName);
        tempProFolder.mkdirs();
        for (int i = 0; i < processes.size(); i++) {
            ProcessItem processItem = processes.get(i);
            //$NON-NLS-1$
            pMonitor.setTaskName(Messages.getString("BuildJobManager.building", processItem.getProperty().getLabel()));
            IBuildJobHandler buildJobHandler = BuildJobFactory.createBuildJobHandler(processItem, context, processItem.getProperty().getVersion(), exportChoiceMap, jobExportType);
            buildJobHandler.generateItemFiles(true, new SubProgressMonitor(pMonitor, scale));
            buildJobHandler.generateJobFiles(new SubProgressMonitor(pMonitor, scale));
            buildJobHandler.build(new SubProgressMonitor(pMonitor, scale));
            IFile jobTargetFile = buildJobHandler.getJobTargetFile();
            if (jobTargetFile != null && jobTargetFile.exists()) {
                // unzip to temp folder
                FilesUtils.unzip(jobTargetFile.getLocation().toPortableString(), tempProFolder.getAbsolutePath());
                String zipPath = jobTargetFile.getLocation().toPortableString();
                if (needClasspathJar(exportChoiceMap)) {
                    JavaJobExportReArchieveCreator creator = new JavaJobExportReArchieveCreator(zipPath, processItem.getProperty().getLabel());
                    creator.setTempFolder(tempFolder.getAbsolutePath());
                    creator.buildNewJar();
                }
            }
            pMonitor.worked(scale);
        }
        FilesUtils.zip(tempFolder.getAbsolutePath(), destinationPath);
        FilesUtils.deleteFile(tempFolder, true);
        pMonitor.done();
    }
    return true;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) ProcessItem(org.talend.core.model.properties.ProcessItem) IBuildJobHandler(org.talend.core.runtime.process.IBuildJobHandler) JavaJobExportReArchieveCreator(org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator) ZipToFile(org.talend.repository.ui.utils.ZipToFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 3 with JavaJobExportReArchieveCreator

use of org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator in project tdi-studio-se by Talend.

the class JobExportAction method reBuildJobZipFile.

/**
     * 
     * DOC aiming Comment method "reBuildJobZipFile".
     * 
     * @param processes
     */
protected void reBuildJobZipFile(List<ExportFileResource> processes) {
    JavaJobExportReArchieveCreator creator = null;
    String zipFile = getTempDestinationValue();
    String destinationZipFile = manager.getDestinationPath();
    String tmpFolder = ExportJobUtil.getTmpFolder();
    try {
        // unzip to tmpFolder
        ZipToFile.unZipFile(zipFile, tmpFolder);
        // build new jar
        for (ExportFileResource process : processes) {
            if (process != null) {
                String jobFolderName = process.getDirectoryName();
                //$NON-NLS-1$
                int pos = jobFolderName.indexOf("/");
                if (pos != -1) {
                    jobFolderName = jobFolderName.substring(pos + 1);
                }
                if (creator == null) {
                    creator = new JavaJobExportReArchieveCreator(zipFile, jobFolderName);
                } else {
                    creator.setJobFolerName(jobFolderName);
                }
                creator.buildNewJar();
            }
        }
        // Modified by Marvin Wang on Feb.1, 2012 for bug
        if (canCreateNewFile(destinationZipFile)) {
            // rezip the tmpFolder to zipFile
            ZipToFile.zipFile(tmpFolder, destinationZipFile);
        } else {
            MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(), "Can not create a file", "Can not create a file or have not the permission to create a file!");
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        ExportJobUtil.deleteTempFiles();
        JavaJobExportReArchieveCreator.deleteTempDestinationFiles();
        // delete the temp zip file
        new File(zipFile).delete();
    }
}
Also used : ExportFileResource(org.talend.repository.documentation.ExportFileResource) JavaJobExportReArchieveCreator(org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator) ZipToFile(org.talend.repository.ui.utils.ZipToFile) File(java.io.File) ProcessorException(org.talend.designer.runprocess.ProcessorException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 4 with JavaJobExportReArchieveCreator

use of org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator in project tmdm-studio-se by Talend.

the class DeployOnMDMExportWizardPage method reBuildJobZipFile.

/**
 * DOC aiming Comment method "reBuildJobZipFile".
 */
private void reBuildJobZipFile(ExportFileResource p) {
    JavaJobExportReArchieveCreator creator = null;
    String zipFile = getDestinationValue(p);
    String tmpFolder = ExportJobUtil.getTmpFolder();
    try {
        // unzip to tmpFolder
        ZipToFile.unZipFile(zipFile, tmpFolder);
        // remove jaxrpc.jar from lib , see 0021706
        // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        String javxRpc = tmpFolder + File.separator + "WEB-INF" + File.separator + "lib" + File.separator + "jaxrpc.jar";
        File rpcfile = new File(javxRpc);
        if (rpcfile.exists()) {
            rpcfile.delete();
        }
        if (p != null) {
            String jobFolderName = p.getDirectoryName();
            int pos = jobFolderName.indexOf(SEPERATOR);
            if (pos != -1) {
                jobFolderName = jobFolderName.substring(pos + 1);
            }
            if (creator == null) {
                creator = new JavaJobExportReArchieveCreator(zipFile, jobFolderName);
            } else {
                creator.setJobFolerName(jobFolderName);
            }
            creator.buildNewJar();
        }
        // rezip the tmpFolder to zipFile
        ZipToFile.zipFile(tmpFolder, zipFile);
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        ExportJobUtil.deleteTempFiles();
    }
}
Also used : JavaJobExportReArchieveCreator(org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator) ZipToFile(org.talend.repository.ui.utils.ZipToFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessorException(org.talend.designer.runprocess.ProcessorException) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 5 with JavaJobExportReArchieveCreator

use of org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator in project tdi-studio-se by Talend.

the class BuildJobManager method buildJob.

public void buildJob(String destinationPath, ProcessItem itemToExport, String version, String context, Map<ExportChoice, Object> exportChoiceMap, JobExportType jobExportType, boolean checkCompilationError, IProgressMonitor monitor) throws Exception {
    IProgressMonitor pMonitor = new NullProgressMonitor();
    if (monitor != null) {
        pMonitor = monitor;
    }
    final boolean oldMeasureActived = TimeMeasure.measureActive;
    if (!oldMeasureActived) {
        // not active before.
        TimeMeasure.display = TimeMeasure.displaySteps = TimeMeasure.measureActive = CommonsPlugin.isDebugMode();
    }
    final String timeMeasureId = "Build job for " + itemToExport.getProperty().getLabel() + ' ' + version;
    TimeMeasure.begin(timeMeasureId);
    try {
        final int scale = 1000;
        int total = 4;
        //$NON-NLS-1$
        pMonitor.beginTask(Messages.getString("JobScriptsExportWizardPage.newExportJobScript", jobExportType), scale * total);
        ProcessItem processItem = itemToExport;
        // get correct version
        if (!RelationshipItemBuilder.LATEST_VERSION.equals(version) && version != null && !"".equals(version) && !version.equals(processItem.getProperty().getVersion())) {
            processItem = ItemCacheManager.getProcessItem(processItem.getProperty().getId(), version);
        }
        final String label = processItem.getProperty().getLabel();
        final IBuildJobHandler buildJobHandler = BuildJobFactory.createBuildJobHandler(processItem, context, version, exportChoiceMap, jobExportType);
        ProcessUtils.setJarWithContext(ProcessUtils.needsToHaveContextInsideJar(processItem));
        final IWorkspaceRunnable op = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor wrMonitor) throws CoreException {
                try {
                    wrMonitor.beginTask(Messages.getString("JobScriptsExportWizardPage.newExportJobScript", jobExportType), scale * 3);
                    TimeMeasure.step(timeMeasureId, "prepare to build job");
                    buildJobHandler.generateItemFiles(true, new SubProgressMonitor(wrMonitor, scale));
                    wrMonitor.worked(scale);
                    TimeMeasure.step(timeMeasureId, "generateItemFiles");
                    buildJobHandler.generateJobFiles(new SubProgressMonitor(wrMonitor, scale));
                    wrMonitor.worked(scale);
                    TimeMeasure.step(timeMeasureId, "generateJobFiles");
                    buildJobHandler.build(new SubProgressMonitor(wrMonitor, scale));
                    TimeMeasure.step(timeMeasureId, "build and package");
                    wrMonitor.done();
                } catch (Exception e) {
                    throw new CoreException(new org.eclipse.core.runtime.Status(IStatus.ERROR, FrameworkUtil.getBundle(this.getClass()).getSymbolicName(), "Error", e));
                }
            }

            ;
        };
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        try {
            ISchedulingRule schedulingRule = workspace.getRoot();
            // the update the project files need to be done in the workspace runnable to avoid all
            // notification
            // of changes before the end of the modifications.
            workspace.run(op, schedulingRule, IWorkspace.AVOID_UPDATE, pMonitor);
        } catch (CoreException e) {
            Throwable cause = e.getCause();
            if (cause == null) {
                throw new PersistenceException(e);
            }
            throw new PersistenceException(cause);
        }
        ProcessUtils.setJarWithContext(false);
        IFile jobTargetFile = buildJobHandler.getJobTargetFile();
        if (jobTargetFile != null && jobTargetFile.exists()) {
            IPath jobZipLocation = jobTargetFile.getLocation();
            File jobZipFile = jobZipLocation.toFile();
            String jobZip = jobZipLocation.toString();
            if (needClasspathJar(exportChoiceMap)) {
                ExportJobUtil.deleteTempFiles();
                JavaJobExportReArchieveCreator creator = new JavaJobExportReArchieveCreator(jobZip, label);
                FilesUtils.unzip(jobZip, creator.getTmpFolder() + File.separator + label + "_" + version);
                creator.buildNewJar();
                ZipToFile.zipFile(creator.getTmpFolder(), jobZip);
                creator.deleteTempFiles();
                TimeMeasure.step(timeMeasureId, "Recreate job jar for classpath");
            }
            // TBD-2500
            Set<ProcessItem> processItems = new HashSet<ProcessItem>();
            processItems.add(processItem);
            // We get the father job childs.
            Set<JobInfo> infos = ProcessorUtilities.getChildrenJobInfo(processItem);
            Iterator<JobInfo> infoIterator = infos.iterator();
            while (infoIterator.hasNext()) {
                processItems.add(infoIterator.next().getProcessItem());
            }
            TimeMeasure.step(timeMeasureId, "getChildrenJobInfo");
            // We iterate over the job and its childs in order to re-archive them if needed.
            for (ProcessItem pi : processItems) {
                BDJobReArchieveCreator bdRecreator = new BDJobReArchieveCreator(pi, processItem);
                bdRecreator.create(jobZipFile);
            }
            TimeMeasure.step(timeMeasureId, "BDJobReArchieveCreator");
            File jobFileTarget = new File(destinationPath);
            if (jobFileTarget.isDirectory()) {
                jobFileTarget = new File(destinationPath, jobZipFile.getName());
            }
            FilesUtils.copyFile(jobZipFile, jobFileTarget);
            TimeMeasure.step(timeMeasureId, "Copy packaged file to target");
        } else if (jobTargetFile != null) {
            throw new Exception("Job was not built successfully, please check the logs for more details available on the workspace/.Java/lastGenerated.log");
        }
        if (checkCompilationError) {
            CorePlugin.getDefault().getRunProcessService().checkLastGenerationHasCompilationError(false);
        }
        pMonitor.worked(scale);
        pMonitor.done();
    } finally {
        TimeMeasure.end(timeMeasureId);
        // if active before, not disable and active still.
        if (!oldMeasureActived) {
            TimeMeasure.display = TimeMeasure.displaySteps = TimeMeasure.measureActive = false;
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) JobInfo(org.talend.core.model.process.JobInfo) JavaJobExportReArchieveCreator(org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator) HashSet(java.util.HashSet) IStatus(org.eclipse.core.runtime.IStatus) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IPath(org.eclipse.core.runtime.IPath) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) PersistenceException(org.talend.commons.exception.PersistenceException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProcessItem(org.talend.core.model.properties.ProcessItem) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IBuildJobHandler(org.talend.core.runtime.process.IBuildJobHandler) PersistenceException(org.talend.commons.exception.PersistenceException) ZipToFile(org.talend.repository.ui.utils.ZipToFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

File (java.io.File)5 ZipToFile (org.talend.repository.ui.utils.ZipToFile)5 JavaJobExportReArchieveCreator (org.talend.repository.ui.wizards.exportjob.JavaJobExportReArchieveCreator)5 PersistenceException (org.talend.commons.exception.PersistenceException)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 IFile (org.eclipse.core.resources.IFile)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 ProcessItem (org.talend.core.model.properties.ProcessItem)2 IBuildJobHandler (org.talend.core.runtime.process.IBuildJobHandler)2 ProcessorException (org.talend.designer.runprocess.ProcessorException)2 IWorkspace (org.eclipse.core.resources.IWorkspace)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IStatus (org.eclipse.core.runtime.IStatus)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)1