List of usage examples for org.apache.commons.io FileUtils writeLines
public static void writeLines(File file, Collection lines) throws IOException
toString()
value of each item in a collection to the specified File
line by line. From source file:org.wisdom.maven.utils.ApplicationSecretGenerator.java
/** * Checks whether the application configuration file as the application secret. * If not generates one./*from w ww .j av a 2 s . c om*/ * * @param project the Maven Project * @param log the logger * @throws java.io.IOException if the application file cannot be read, or rewritten */ public static void ensureOrGenerateSecret(MavenProject project, Log log) throws IOException { File conf = new File(project.getBasedir(), "src/main/configuration/application.conf"); if (conf.isFile()) { List<String> lines = FileUtils.readLines(conf); boolean changed = false; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); Matcher matcher = OLD_SECRET_LINE_PATTERN.matcher(line); if (matcher.matches()) { if (matcher.group(1).length() == 0) { lines.set(i, "application.secret=\"" + generate() + "\""); changed = true; } } else { matcher = SECRET_LINE_PATTERN.matcher(line); if (matcher.matches()) { if (matcher.group(2).trim().length() == 0) { lines.set(i, " secret = \"" + generate() + "\""); changed = true; } } } } if (changed) { FileUtils.writeLines(conf, lines); log.info("Application Secret generated - the configuration file was updated."); } } }
From source file:org.wso2.appcloud.core.docker.DockerClient.java
/** * Create a docker file according to given details. This will get docker template file and replace the parameters * with the given customized values in the dockerFilePropertyMap * @param dockerFilePath//w w w . j ava 2 s.co m * @param runtimeId - application runtime id * @param dockerTemplateFilePath * @param dockerFileCategory - app creation method eg : svn, url, default * @param dockerFilePropertyMap * @param customDockerFileProperties * @throws IOException * @throws AppCloudException */ public void createDockerFile(String dockerFilePath, String runtimeId, String dockerTemplateFilePath, String dockerFileCategory, Map<String, String> dockerFilePropertyMap, Map<String, String> customDockerFileProperties) throws AppCloudException { customDockerFileProperties.keySet().removeAll(dockerFilePropertyMap.keySet()); dockerFilePropertyMap.putAll(customDockerFileProperties); // Get docker template file // A sample docker file can be found at // https://github.com/wso2/app-cloud/blob/master/modules/resources/dockerfiles/wso2as/default/Dockerfile.wso2as.6.0.0-m1 String dockerFileTemplatePath = DockerUtil.getDockerFileTemplatePath(runtimeId, dockerTemplateFilePath, dockerFileCategory); List<String> dockerFileConfigs = new ArrayList<>(); try { for (String line : FileUtils.readLines(new File(dockerFileTemplatePath))) { StringTokenizer stringTokenizer = new StringTokenizer(line); //Search if line contains keyword to replace with the value while (stringTokenizer.hasMoreElements()) { String element = stringTokenizer.nextElement().toString().trim(); if (dockerFilePropertyMap.containsKey(element)) { if (log.isDebugEnabled()) { log.debug("Dockerfile placeholder : " + element); } String value = dockerFilePropertyMap.get(element); line = line.replace(element, value); } } dockerFileConfigs.add(line); } } catch (IOException e) { String msg = "Error occurred while reading docker template file " + dockerFileTemplatePath; throw new AppCloudException(msg, e); } try { FileUtils.writeLines(new File(dockerFilePath), dockerFileConfigs); } catch (IOException e) { String msg = "Error occurred while writing to docker file " + dockerFilePath; throw new AppCloudException(msg, e); } }
From source file:org.wso2.appcloud.core.docker.DockerOpClient.java
public void createDockerFile(String runtimeId, String artifactName, String dockerFilePath, String dockerTemplateFilePath) throws IOException, AppCloudException { String dockerFileTemplatePath = DockerUtil.getDockerFileTemplatePath(runtimeId, dockerTemplateFilePath, "default"); String artifactNameWithoutExtension = artifactName.substring(0, artifactName.lastIndexOf(".")); List<String> dockerFileConfigs = new ArrayList<String>(); for (String line : FileUtils.readLines(new File(dockerFileTemplatePath))) { if (line.contains("ARTIFACT_NAME")) { dockerFileConfigs.add(line.replace("ARTIFACT_NAME", artifactName)); } else if (line.contains("ARTIFACT_DIR")) { dockerFileConfigs.add(line.replace("ARTIFACT_DIR", artifactNameWithoutExtension)); } else {/*w ww. jav a 2s . c o m*/ dockerFileConfigs.add(line); } } FileUtils.writeLines(new File(dockerFilePath), dockerFileConfigs); }
From source file:org.wso2.appcloud.core.docker.DockerOpClient.java
public void createDockerFileForGitHub(String runtimeId, String gitRepoUrl, String gitRepoBranch, String dockerFilePath, String projectRoot, String dockerGitHubTemplateFilePath) throws IOException, AppCloudException { String dockerFileTemplatePath = DockerUtil.getDockerFileTemplatePath(runtimeId, dockerGitHubTemplateFilePath, "github"); List<String> dockerFileConfigs = new ArrayList<String>(); for (String line : FileUtils.readLines(new File(dockerFileTemplatePath))) { if (line.contains("GIT_REPO_URL")) { line = line.replace("GIT_REPO_URL", gitRepoUrl); }//w w w .ja v a 2 s . com if (line.contains("GIT_REPO_BRANCH")) { line = line.replace("GIT_REPO_BRANCH", gitRepoBranch); } if (line.contains("PROJECT_ROOT")) { line = line.replace("PROJECT_ROOT", projectRoot); } dockerFileConfigs.add(line); } FileUtils.writeLines(new File(dockerFilePath), dockerFileConfigs); }
From source file:pl.asie.modalyze.mcp.MCPDataManager.java
private void loadMappings(String version) throws IOException { File mappingClient = new File(MCP_DIR, version + "-client.map"); File mappingServer = new File(MCP_DIR, version + "-server.map"); if (mappingClient.exists() && mappingServer.exists()) { MAPPINGS.put(version + "-client", new HashSet<>(FileUtils.readLines(mappingClient, "UTF-8"))); MAPPINGS.put(version + "-server", new HashSet<>(FileUtils.readLines(mappingServer, "UTF-8"))); } else {/*from w w w . j a va2 s .c om*/ File mcpFile = new File(MCP_DIR, MCP_VERSION_MAP.get(version).mcpFile); if (mcpFile.exists()) { ZipFile zipFile = new ZipFile(mcpFile); ZipEntry joinedSrgEntry = zipFile.getEntry("conf/joined.srg"); if (joinedSrgEntry != null) { loadJoinedSrgMapping(version, zipFile, joinedSrgEntry); } else { ZipEntry clientSrgEntry = zipFile.getEntry("conf/client.srg"); ZipEntry serverSrgEntry = zipFile.getEntry("conf/server.srg"); if (clientSrgEntry != null && serverSrgEntry != null) { loadSrgMapping(version + "-client", zipFile, clientSrgEntry); loadSrgMapping(version + "-server", zipFile, serverSrgEntry); } else { ZipEntry csvFields = zipFile.getEntry("conf/fields.csv"); ZipEntry csvMethods = zipFile.getEntry("conf/methods.csv"); if (csvFields != null && csvMethods != null) { loadCsvMapping(version, zipFile, csvFields, csvMethods); } else { System.err.println("MCP file for Minecraft " + version + " (" + mcpFile.toString() + ") stored in an unknown format!"); MAPPINGS.put(version + "-client", Collections.EMPTY_SET); MAPPINGS.put(version + "-server", Collections.EMPTY_SET); } } } if (MAPPINGS.get(version + "-client") != null) { FileUtils.writeLines(mappingClient, MAPPINGS.get(version + "-client")); } if (MAPPINGS.get(version + "-server") != null) { FileUtils.writeLines(mappingServer, MAPPINGS.get(version + "-server")); } } else { System.err .println("MCP file for Minecraft " + version + " (" + mcpFile.toString() + ") not found!"); MAPPINGS.put(version + "-client", Collections.EMPTY_SET); MAPPINGS.put(version + "-server", Collections.EMPTY_SET); } } }
From source file:poe.trade.assist.UniquesListSearchGenerator.java
/**imgurl, reqLvl, base, mod * @param args//from w ww. j a v a 2 s . c om * @throws Exception */ public static void main(String[] args) throws Exception { List<String> outputLines = new LinkedList<>(); outputLines.add( "Name Art Req.Level Base Mods TaslismanSC TalismanHC Standard Hardcore poewiki"); for (String list : lists) { HttpResponse<String> response = Unirest.get("http://pathofexile.gamepedia.com/" + list) .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0") .asString(); Document doc = Jsoup.parse(response.getBody()); Elements elems = doc.select("table.wikitable.sortable"); for (Element table : elems) { Elements rows = table.select("tr"); int ctr = 0; boolean hasRequiredLevel = false; for (Element row : rows) { if (ctr == 0) { // first row is headers hasRequiredLevel = !row.select("abbr[title=\"Required Level\"]").isEmpty(); ctr++; continue; } String name = row.child(0).child(0).attr("title"); System.out.println("Now processing: " + name); String imgurl = "=IMAGE(\"" + row.select("img").attr("src") + "\", 3)"; String base = row.child(1).child(0).attr("title"); String reqLvl = hasRequiredLevel ? row.child(2).text() : "0"; reqLvl = reqLvl.equalsIgnoreCase("n/a") ? "0" : reqLvl; String mod = "=\""; Elements mods = row.select("span.itemboxstatsgroup.text-mod"); if (!mods.isEmpty()) { if (mods.size() > 2) throw new Exception("mods.size() is > 2. " + name + " - " + mods.toString()); boolean hasImplicit = mods.size() > 1; String imp = hasImplicit ? mods.get(0).text() : ""; int expIdx = hasImplicit ? 1 : 0; String lineSeparator = "\"&CHAR(10)&\""; String exp = mods.get(expIdx).textNodes().stream().map(n -> n.text().trim()) .filter(s -> !s.isEmpty()).collect(Collectors.joining(lineSeparator)); String additionalExp = mods.get(expIdx).children().stream().filter(e -> e.hasText()) .map(e -> e.text().trim()).collect(Collectors.joining(lineSeparator)); if (additionalExp != null && !additionalExp.isEmpty()) exp += lineSeparator + additionalExp; mod += imp; if (hasImplicit) mod += (lineSeparator + "--------------" + lineSeparator); mod += exp; } mod += "\""; String standard = "Standard"; String hardcore = "Hardcore"; String tempsc = "Talisman"; String temphc = "Talisman+Hardcore"; String nameenc = URLEncoder.encode(name, "UTF-8"); String sc = hyperlink(getSearchURL(standard, nameenc)); String hc = hyperlink(getSearchURL(hardcore, nameenc)); String tsc = hyperlink(getSearchURL(tempsc, nameenc)); String thc = hyperlink(getSearchURL(temphc, nameenc)); String poewikiurl = hyperlink("http://pathofexile.gamepedia.com/" + (name.replace(' ', '_'))); String s = format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", name, imgurl, reqLvl, base, mod, tsc, thc, sc, hc, poewikiurl); outputLines.add(s); Thread.sleep(1000); } } } FileUtils.writeLines(new File("uniqueslist.txt"), outputLines); }
From source file:proguard.AwbProguardConfiguration.java
/** * ?proguardconfig//from ww w . ja v a 2 s .co m * * @param outConfigFile */ public void printConfigFile(File outConfigFile) throws IOException { List<String> configs = Lists.newArrayList(); //awblib??proguardpredex for (AwbTransform awbTransform : awbTransforms) { List<File> inputLibraries = Lists.newArrayList(); String name = awbTransform.getAwbBundle().getName(); File obuscateDir = new File(awbObfuscatedDir, awbTransform.getAwbBundle().getName()); obuscateDir.mkdirs(); //configs.add(); if (null != awbTransform.getInputDir() && awbTransform.getInputDir().exists()) { configs.add(INJARS_OPTION + " " + awbTransform.getInputDir().getAbsolutePath()); File obsJar = new File(obuscateDir, "inputdir_" + OBUSCATED_JAR); inputLibraries.add(obsJar); configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath()); } Set<String> classNames = new HashSet<>(); for (File inputLibrary : awbTransform.getInputLibraries()) { configs.add(INJARS_OPTION + " " + inputLibrary.getAbsolutePath()); String fileName = inputLibrary.getName(); if (classNames.contains(fileName)) { fileName = "a" + classNames.size() + "_" + fileName; } classNames.add(fileName); File obsJar = new File(obuscateDir, fileName); inputLibraries.add(obsJar); configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath()); } // configs.add(); awbTransform.setInputFiles(inputLibraries); awbTransform.setInputDir(null); awbTransform.getInputLibraries().clear(); appVariantOutputContext.getAwbTransformMap().put(name, awbTransform); } FileUtils.writeLines(outConfigFile, configs); }
From source file:pt.ua.tm.neji.cli.Main.java
private static void generateServer(ContextConfiguration contextConfig, String modelsFolderPath, String dictionariesFolderPath, boolean includeAnnotationsWithoutIDs) throws IOException { // Zip file/*from w ww . java2 s . c o m*/ final File zipFile = new File(pt.ua.tm.neji.core.Constants.TARGET_PATH, "neji-server.zip"); if (zipFile.exists()) { FileDeleteStrategy.FORCE.delete(zipFile); } File tmpRoot = new File(TMP_DIR, "neji-server"); if (tmpRoot.exists()) { FileDeleteStrategy.FORCE.delete(tmpRoot); } tmpRoot.mkdirs(); tmpRoot.deleteOnExit(); // Server jar file File serverFile = new File(pt.ua.tm.neji.core.Constants.TARGET_PATH, "neji-2.0.0-server.jar"); if (!serverFile.exists()) { throw new IOException( "The neji-server jar file must be packaged first! " + "Please run 'mvn clean install' first."); } // bat, sh and readme files File batFile = new File(tmpRoot, "neji-server.bat"); File shFile = new File(tmpRoot, "neji-server.sh"); File readmeFile = new File(tmpRoot, "README.txt"); File loginRealmFile = new File(tmpRoot, "loginRealm.properties"); // Web resources folders File webResourcesFolder = new File(tmpRoot, "resources"); webResourcesFolder.mkdirs(); File dictionariesResourcesFolder = new File(tmpRoot, "resources" + File.separator + "dictionaries"); dictionariesResourcesFolder.mkdirs(); File modelsResourcesFolder = new File(tmpRoot, "resources" + File.separator + "models"); modelsResourcesFolder.mkdirs(); // Tools folder File toolsFolder = new File(pt.ua.tm.neji.core.Constants.GDEP_DIR); // Models folder if (modelsFolderPath != null) { FileUtils.copyDirectory(new File(modelsFolderPath), modelsResourcesFolder); } // Dictionaries folder if (dictionariesFolderPath != null) { FileUtils.copyDirectory(new File(dictionariesFolderPath), dictionariesResourcesFolder); } // Write bat file FileUtils.write(batFile, "@echo off\n" + "\n" + "set cp=neji-server.jar\n" + "set memory=6144m\n" + "set encoding=UTF-8\n" + "set class=pt.ua.tm.neji.web.cli.WebMain\n" + "\n" + ":run\n" + "\n" + "java -Xmx%memory% -ea -Dfile.encoding=%encoding% -classpath %cp% %class% %*\n" + "\n" + ":eof", "UTF-8"); // Write sh file FileUtils.write(shFile, "#!/bin/bash\n" + "cp=neji-server.jar:$CLASSPATH\n" + "MEMORY=6G\n" + "JAVA_COMMAND=\"java -Xmx$MEMORY -Dfile.encoding=UTF-8 -classpath $cp\"\n" + "CLASS=pt.ua.tm.neji.web.cli.WebMain\n" + "\n" + "$JAVA_COMMAND $CLASS $*", "UTF-8"); // Write readme file FileUtils.write(readmeFile, SERVER_README); // Write login realm file FileUtils.write(loginRealmFile, "jdbcdriver = org.sqlite.JDBC\n" + "url = jdbc:sqlite:neji.db\n" + "usertable = users\n" + "usertablekey = id\n" + "usertableuserfield = username\n" + "usertablepasswordfield = pwd\n" + "roletable = roles\n" + "roletablekey = id\n" + "roletablerolefield = role\n" + "userroletable = user_roles\n" + "userroletableuserkey = user_id\n" + "userroletablerolekey = role_id\n" + "cachetime = 300"); try { String dbName = "neji.db"; FileDeleteStrategy.FORCE.delete(new File(dbName)); DatabaseHandler db = new DefaultDatabaseHandler(dbName, true); Map<String, String> groups = new HashMap<>(); // Dictionaries List<String> dicts; List<String> dictFiles = new ArrayList<>(); List<String> dictionaries = new ArrayList<>(); File dictPriorityFile = new File(dictionariesResourcesFolder, "_priority"); if (!dictPriorityFile.exists()) { dictPriorityFile.createNewFile(); } dicts = DictionariesLoader.loadPriority(new FileInputStream(dictPriorityFile)); for (String dict : dicts) { File dictionaryFile = new File( dictionariesResourcesFolder.getAbsolutePath() + File.separator + dict); List<String> dictionaryLines = IOUtils.readLines(new FileInputStream(dictionaryFile), "UTF-8"); Dictionary d = VariantMatcherLoader.loadDictionaryFromLines(dictionaryLines); String dictGroup = d.getGroup(); String dictName = FilenameUtils.getBaseName(dictionaryFile.getName()); dictName = dictName.replaceAll("[^a-zA-Z0-9._-]", ""); pt.ua.tm.neji.web.manage.Dictionary serverDictionary = new pt.ua.tm.neji.web.manage.Dictionary( dictName, dict, new ArrayList<String>(), new ArrayList<String>(), dictGroup); dictionaries.add(dictName); dictFiles.add(FilenameUtils.getName(dictionaryFile.getName())); groups.put(dictGroup, dictGroup); db.addDictionary(serverDictionary); } // Models List<String> models = new ArrayList<>(); File modelPriorityFile = new File(modelsResourcesFolder, "_priority"); if (!modelPriorityFile.exists()) { modelPriorityFile.createNewFile(); } List<String> modelsPriority = MLModelsLoader.loadPriority(new FileInputStream(modelPriorityFile)); for (String p : modelsPriority) { String propertiesFilePath = modelsResourcesFolder.getAbsolutePath() + File.separator + p; File propertiesFile = new File(propertiesFilePath); String modelName = propertiesFile.getParentFile().getName(); MLModel ml = new MLModel(modelName, propertiesFile); String modelGroup = ml.getSemanticGroup(); String modelFile = FilenameUtils.getName(ml.getModelFile()); models.add(modelName); groups.put(modelGroup, modelGroup); // Get model normalization dictionaries List<String> normDicts = new ArrayList<>(); if (ml.getNormalizationDictionariesFolder() != null) { String normalizationFolder = ml.getNormalizationDictionariesFolder(); File normalizationPriorityFile = new File(normalizationFolder, "_priority"); List<String> normalizationDictsPath = FileUtils.readLines(normalizationPriorityFile); List<String> normPriorityLines = new ArrayList<>(); List<String> dictPriorityLines = FileUtils.readLines(dictPriorityFile); for (String normDictPath : normalizationDictsPath) { String dictFileName = FilenameUtils.getName(normDictPath); if (!dictFiles.contains(dictFileName)) { File dictionaryFile = new File(normalizationFolder, dictFileName); List<String> dictionaryLines = IOUtils.readLines(new FileInputStream(dictionaryFile), "UTF-8"); Dictionary d = VariantMatcherLoader.loadDictionaryFromLines(dictionaryLines); String dictGroup = d.getGroup(); String dictName = FilenameUtils.getBaseName(dictionaryFile.getName()); pt.ua.tm.neji.web.manage.Dictionary serverDictionary = new pt.ua.tm.neji.web.manage.Dictionary( dictName, dictFileName, new ArrayList<String>(), new ArrayList<String>(), dictGroup); normDicts.add(dictName); dictFiles.add(dictFileName); db.addDictionary(serverDictionary); FileUtils.copyFileToDirectory(dictionaryFile, dictionariesResourcesFolder); FileUtils.deleteQuietly(dictionaryFile); } normPriorityLines.add("../../../dictionaries/" + dictFileName); dictPriorityLines.add(dictFileName); } FileUtils.writeLines(normalizationPriorityFile, normPriorityLines); FileUtils.writeLines(dictPriorityFile, dictPriorityLines); File normDir = new File(normalizationFolder); if (!normDir.getName().equals("normalization")) { File newNormDir = new File(normDir.getParent(), "normalization" + File.separator); normDir.renameTo(newNormDir); List<String> lines = FileUtils.readLines(propertiesFile); int i = 0; for (String line : lines) { if (line.startsWith("dictionaries=")) { break; } i++; } if (i < lines.size()) { lines.remove(i); } lines.add("dictionaries=normalization" + File.separator); FileUtils.writeLines(propertiesFile, lines); } } modelName = modelName.replaceAll("[^a-zA-Z0-9._-]", ""); Model model = new Model(modelName, modelFile, normDicts, new ArrayList<String>(), modelGroup); db.addModel(model); } // Default Service if ((!dicts.isEmpty()) || (!models.isEmpty())) { String parsingLevel = models.isEmpty() ? "Tokenization" : "Chunking"; boolean noIds = !models.isEmpty(); Service defaultService = new Service("default", null, parsingLevel, noIds, dictionaries, models, groups, null); db.addService(defaultService); } } catch (NejiException ex) { ex.printStackTrace(); logger.error("Zip file could not be generated."); return; } // Configuration folder File confFolder = new File("conf"); // Create the zip with all files to run the server InputStream zipFileStream = new ZipGenerator(tmpRoot, // root folder new Pair<>("neji-server.jar", serverFile), // server executable jar new Pair<>("resources" + File.separator + "tools" + File.separator + toolsFolder.getName(), toolsFolder), // selected tools // selected dictionaries new Pair<>("conf", confFolder), // configuration folder new Pair<>("neji.db", new File("neji.db"))).generate(); FileUtils.copyInputStreamToFile(zipFileStream, zipFile); }
From source file:pt.ua.tm.neji.web.server.Server.java
/** * Edit a model./*from w ww . j a va 2 s .co m*/ * * @param model model * @throws NejiException */ public void editModel(Model model) throws NejiException { // Validate the model validateModel(model, null, null, false); // Update model context try { Model oldModel = db.getModel(model.getId()); List<String> addedDictionaries = ListUtils.subtract(model.getDictionaries(), oldModel.getDictionaries()); List<String> removedDictionaries = ListUtils.subtract(oldModel.getDictionaries(), model.getDictionaries()); MLModel ml = context.getModel(model.getName()); // Remove removed dictionaries from model context for (String dictionaryName : removedDictionaries) { String dictionaryFile = db.getDictionaryFile(dictionaryName); ml.removeNormalizationDictionary("../../../dictionaries/" + dictionaryFile); } // Add added dictionaries to model context for (String dictionaryName : addedDictionaries) { String dictionaryFile = db.getDictionaryFile(dictionaryName); List<String> lines = FileUtils.readLines(new File(DICTIONARIES_PATH + dictionaryFile)); ml.addNormalizationDictionary("../../../dictionaries/" + dictionaryFile, lines); } } catch (IOException | NejiException ex) { throw new NejiException("There was a problem editing the model.\n" + "Please try again later.", ex); } // Update normalization info try { // Get normalization dictionaries List<String> lines = new ArrayList<>(); for (String dictionaryName : model.getDictionaries()) { String dictionaryFile = db.getDictionaryFile(dictionaryName); lines.add("../../../dictionaries/" + dictionaryFile); } // Write normalization _priority file File normalizationPriorityFile = new File(MODELS_PATH + model.getName() + File.separator + "normalization" + File.separator + "_priority"); FileUtils.writeLines(normalizationPriorityFile, lines); } catch (IOException ex) { throw new NejiException("There was a problem editing the model.\n" + "Please try again later.", ex); } // Edit model data in database try { db.editModel(model); } catch (NejiException ex) { throw new NejiException("There was a problem editing the model.\n" + "Please try again later.", ex); } }
From source file:se.cambio.cds.gdl.parser.GDLOutputTest.java
private void write(List<String> lines, String file) throws Exception { FileUtils.writeLines(new File(file), lines); }