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");
}
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;
}
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();
}
}
}
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())));
}
}
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);
}
}
Aggregations