Search in sources :

Example 1 with Instance

use of org.omegat.filters2.Instance in project omegat by omegat-org.

the class RealProject method loadSourceFiles.

/**
 * Load source files for project.
 *
 * @param projectRoot
 *            project root dir
 */
private void loadSourceFiles() throws Exception {
    long st = System.currentTimeMillis();
    FilterMaster fm = Core.getFilterMaster();
    File root = new File(config.getSourceRoot());
    List<String> srcPathList = FileUtil.buildRelativeFilesList(root, Collections.emptyList(), config.getSourceRootExcludes()).stream().sorted(StreamUtil.comparatorByList(getSourceFilesOrder())).collect(Collectors.toList());
    for (String filepath : srcPathList) {
        Core.getMainWindow().showStatusMessageRB("CT_LOAD_FILE_MX", filepath);
        LoadFilesCallback loadFilesCallback = new LoadFilesCallback(existSource, existKeys, transMemories);
        FileInfo fi = new FileInfo();
        fi.filePath = filepath;
        loadFilesCallback.setCurrentFile(fi);
        IFilter filter = fm.loadFile(config.getSourceRoot() + filepath, new FilterContext(config), loadFilesCallback);
        loadFilesCallback.fileFinished();
        if (filter != null && !fi.entries.isEmpty()) {
            // Don't store the instance, because every file gets an instance and
            fi.filterClass = filter.getClass();
            // then we consume a lot of memory for all instances.
            // See also IFilter "TODO: each filter should be stateless"
            fi.filterFileFormatName = filter.getFileFormatName();
            try {
                fi.fileEncoding = filter.getInEncodingLastParsedFile();
            } catch (Error e) {
                // In case a filter doesn't have getInEncodingLastParsedFile() (e.g., Okapi plugin)
                fi.fileEncoding = "";
            }
            projectFilesList.add(fi);
        }
    }
    findNonUniqueSegments();
    Core.getMainWindow().showStatusMessageRB("CT_LOAD_SRC_COMPLETE");
    long en = System.currentTimeMillis();
    Log.log("Load project source files: " + (en - st) + "ms");
}
Also used : IFilter(org.omegat.filters2.IFilter) FilterMaster(org.omegat.filters2.master.FilterMaster) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext)

Example 2 with Instance

use of org.omegat.filters2.Instance in project omegat by omegat-org.

the class FilterMaster method cloneFilter.

/**
 * Clone one filter's config for editing.
 *
 * @param f
 *            one filter's config
 * @return new config instance
 */
public static Filter cloneFilter(Filter filter) {
    Filter f = new Filter();
    f.setClassName(filter.getClassName());
    f.setEnabled(filter.isEnabled());
    for (Files ff : filter.getFiles()) {
        f.getFiles().add(cloneFiles(ff));
    }
    for (Option o : filter.getOption()) {
        Option fo = new Option();
        fo.setName(o.getName());
        fo.setValue(o.getValue());
        f.getOption().add(fo);
    }
    return f;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) Option(gen.core.filters.Filter.Option) Files(gen.core.filters.Files)

Example 3 with Instance

use of org.omegat.filters2.Instance in project omegat by omegat-org.

the class OpenXMLFilter method processFile.

/**
 * Processes a single OpenXML file, which is actually a ZIP file consisting of many XML files, some of
 * which should be translated.
 */
@Override
public void processFile(File inFile, File outFile, FilterContext fc) throws IOException, TranslationException {
    // Define the documents to read
    defineDOCUMENTSOptions(processOptions);
    ZipOutputStream zipout = null;
    try (ZipFile zipfile = new ZipFile(inFile)) {
        if (outFile != null) {
            zipout = new ZipOutputStream(new FileOutputStream(outFile));
        }
        Enumeration<? extends ZipEntry> unsortedZipcontents = zipfile.entries();
        List<? extends ZipEntry> filelist = Collections.list(unsortedZipcontents);
        // Sort filenames, because zipfile.entries give a random order
        // We use a simplified natural sort, to have slide1, slide2 ...
        // slide10
        // instead of slide1, slide10, slide 2
        // We also order files arbitrarily, to have, for instance
        // documents.xml before comments.xml
        Collections.sort(filelist, this::compareZipEntries);
        for (ZipEntry zipentry : filelist) {
            String shortname = removePath(zipentry.getName());
            if (translatable.matcher(shortname).matches()) {
                File tmpin = tmp();
                FileUtils.copyInputStreamToFile(zipfile.getInputStream(zipentry), tmpin);
                File tmpout = null;
                if (zipout != null) {
                    tmpout = tmp();
                }
                try {
                    createXMLFilter().processFile(tmpin, tmpout, fc);
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
                    throw new TranslationException(e.getLocalizedMessage() + "\n" + OStrings.getString("OpenXML_ERROR_IN_FILE") + inFile, e);
                }
                if (zipout != null) {
                    ZipEntry outEntry = new ZipEntry(zipentry.getName());
                    zipout.putNextEntry(outEntry);
                    FileUtils.copyFile(tmpout, zipout);
                    zipout.closeEntry();
                }
                if (!tmpin.delete()) {
                    tmpin.deleteOnExit();
                }
                if (tmpout != null && !tmpout.delete()) {
                    tmpout.deleteOnExit();
                }
            } else {
                if (zipout != null) {
                    ZipEntry outEntry = new ZipEntry(zipentry.getName());
                    zipout.putNextEntry(outEntry);
                    try (InputStream is = zipfile.getInputStream(zipentry)) {
                        IOUtils.copy(is, zipout);
                    }
                    zipout.closeEntry();
                }
            }
        }
    } finally {
        if (zipout != null) {
            zipout.close();
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) TranslationException(org.omegat.filters2.TranslationException) ZipFile(java.util.zip.ZipFile) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 4 with Instance

use of org.omegat.filters2.Instance in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method mapDiskSize.

/**
 *     Maps the disk_size property of a ComputeCapability to an EC2 Instance.
 *
 *     @param computeCapability Capability containing the disk_size property
 *     @param cfnModule Module containing the Instance
 *     @param nodeName name of the Instance
 */
public void mapDiskSize(ComputeCapability computeCapability, CloudFormationModule cfnModule, String nodeName) {
    // If disk_size is not set, default to 8000 Mb
    Integer diskSizeInMb = computeCapability.getDiskSizeInMb().orElse(8000);
    // Convert disk_size to Gb
    Integer diskSizeInGb = diskSizeInMb / 1000;
    logger.debug("Check diskSize: '{}' Gb", diskSizeInGb);
    if (diskSizeInGb < 8) {
        logger.warn("Disk size of '{}' smaller than the minimum value required by EC2 Instances. Setting the disk size of '{}' to the minimum allowed value of 8 Gb.", nodeName, nodeName);
        diskSizeInGb = 8;
    }
    // Add BlockDeviceMapping if needed
    if (diskSizeInGb > 8) {
        logger.debug("Disk size of '{}' bigger than the default value of EC2 Instances. Adding a BlockDeviceMapping to '{}'.", nodeName, nodeName);
        Instance computeAsInstance = (Instance) cfnModule.getResource(nodeName);
        computeAsInstance.blockDeviceMappings(new EC2BlockDeviceMapping().deviceName("/dev/sda1").ebs(new EC2EBSBlockDevice().volumeSize(diskSizeInGb.toString())));
    }
}
Also used : EC2BlockDeviceMapping(com.scaleset.cfbuilder.ec2.instance.EC2BlockDeviceMapping) EC2EBSBlockDevice(com.scaleset.cfbuilder.ec2.instance.ec2blockdevicemapping.EC2EBSBlockDevice) Instance(com.scaleset.cfbuilder.ec2.Instance)

Example 5 with Instance

use of org.omegat.filters2.Instance in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

@Override
public void visit(Compute node) {
    try {
        if (cfnModule.checkComputeToEc2(node)) {
            logger.debug("Compute '{}' will be transformed to EC2", node.getEntityName());
            String nodeName = toAlphanumerical(node.getEntityName());
            // default security group the EC2 Instance
            SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, nodeName + SECURITY_GROUP).groupDescription("Enables ports for " + nodeName + ".");
            // open endpoint port
            node.getEndpoint().getPort().ifPresent(port -> webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, port.port));
            // check what image id should be taken
            CapabilityMapper capabilityMapper = createCapabilityMapper();
            OsCapability computeOs = node.getOs();
            String imageId = capabilityMapper.mapOsCapabilityToImageId(computeOs);
            ComputeCapability computeCompute = node.getHost();
            String instanceType = capabilityMapper.mapComputeCapabilityToInstanceType(computeCompute, CapabilityMapper.EC2_DISTINCTION);
            // create CFN init and store it
            CFNInit init = new CFNInit(CONFIG_SETS);
            cfnModule.putCFNInit(nodeName, init);
            cfnModule.resource(Instance.class, nodeName).securityGroupIds(webServerSecurityGroup).imageId(imageId).instanceType(instanceType);
            capabilityMapper.mapDiskSize(computeCompute, cfnModule, nodeName);
            // Add Reference to keyName if KeyPair needed and open Port 22 (Allows SSH access)
            if (cfnModule.hasKeyPair()) {
                Instance instance = (Instance) cfnModule.getResource(nodeName);
                instance.keyName(cfnModule.getKeyNameVar());
                webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, 22);
            }
        } else {
            logger.debug("Compute '{}' will not be transformed to EC2", node.getEntityName());
        }
    } catch (SdkClientException se) {
        logger.error("SDKClient failed, no valid credentials or no internet connection");
        throw new TransformationFailureException("Failed", se);
    } catch (Exception e) {
        logger.error("Error while creating EC2Instance resource.");
        throw new TransformationFailureException("Failed at Compute node " + node.getEntityName(), e);
    }
}
Also used : Apache(org.opentosca.toscana.model.node.Apache) EndpointCapability(org.opentosca.toscana.model.capability.EndpointCapability) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) CONFIG_CONFIGURE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CONFIGURE) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) SECURITY_GROUP(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.SECURITY_GROUP) ArrayList(java.util.ArrayList) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) CFNPackage(com.scaleset.cfbuilder.ec2.metadata.CFNPackage) MysqlDatabase(org.opentosca.toscana.model.node.MysqlDatabase) CFNCommand(com.scaleset.cfbuilder.ec2.metadata.CFNCommand) CONFIG_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CREATE) CONFIG_SETS(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_SETS) MysqlDbms(org.opentosca.toscana.model.node.MysqlDbms) APACHE_RESTART_COMMAND(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler.APACHE_RESTART_COMMAND) WebApplication(org.opentosca.toscana.model.node.WebApplication) FILEPATH_NODEJS_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.FILEPATH_NODEJS_CREATE) APACHE_ENV_IMPORT(org.opentosca.toscana.plugins.cloudformation.handler.EnvironmentHandler.APACHE_ENV_IMPORT) Artifact(org.opentosca.toscana.model.artifact.Artifact) Compute(org.opentosca.toscana.model.node.Compute) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability) Database(org.opentosca.toscana.model.node.Database) OperationHandler(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) Dbms(org.opentosca.toscana.model.node.Dbms) Nodejs(org.opentosca.toscana.model.node.Nodejs) Instance(com.scaleset.cfbuilder.ec2.Instance) Set(java.util.Set) OsCapability(org.opentosca.toscana.model.capability.OsCapability) StrictNodeVisitor(org.opentosca.toscana.model.visitor.StrictNodeVisitor) List(java.util.List) SdkClientException(com.amazonaws.SdkClientException) CloudFormationLifecycle.toAlphanumerical(org.opentosca.toscana.plugins.cloudformation.CloudFormationLifecycle.toAlphanumerical) TransformationContext(org.opentosca.toscana.core.transformation.TransformationContext) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) CONFIG_START(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_START) CloudFormationModule(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule) SdkClientException(com.amazonaws.SdkClientException) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Instance(com.scaleset.cfbuilder.ec2.Instance) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) OsCapability(org.opentosca.toscana.model.capability.OsCapability) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability)

Aggregations

IFilter (org.omegat.filters2.IFilter)4 Instance (com.scaleset.cfbuilder.ec2.Instance)3 Files (gen.core.filters.Files)3 Filter (gen.core.filters.Filter)3 AbstractFilter (org.omegat.filters2.AbstractFilter)3 CFNInit (com.scaleset.cfbuilder.ec2.metadata.CFNInit)2 File (java.io.File)2 SdkClientException (com.amazonaws.SdkClientException)1 Authentication (com.scaleset.cfbuilder.cloudformation.Authentication)1 Resource (com.scaleset.cfbuilder.core.Resource)1 SecurityGroup (com.scaleset.cfbuilder.ec2.SecurityGroup)1 UserData (com.scaleset.cfbuilder.ec2.UserData)1 EC2BlockDeviceMapping (com.scaleset.cfbuilder.ec2.instance.EC2BlockDeviceMapping)1 EC2EBSBlockDevice (com.scaleset.cfbuilder.ec2.instance.ec2blockdevicemapping.EC2EBSBlockDevice)1 CFNCommand (com.scaleset.cfbuilder.ec2.metadata.CFNCommand)1 CFNPackage (com.scaleset.cfbuilder.ec2.metadata.CFNPackage)1 Role (com.scaleset.cfbuilder.iam.Role)1 DBInstance (com.scaleset.cfbuilder.rds.DBInstance)1 Option (gen.core.filters.Filter.Option)1 Filters (gen.core.filters.Filters)1