List of usage examples for java.util Properties setProperty
public synchronized Object setProperty(String key, String value)
From source file:com.bcmcgroup.flare.client.ClientUtil.java
/** * Set the value of a property for a specific property name. * * @param property the property name with its value to be set in the config.properties * @param value the value to be set in the config.properties * *//*from w ww. j ava2 s. c o m*/ public static void setProperty(String property, String value) { Properties properties = new Properties(); InputStream inputStream = null; OutputStream outputStream = null; File file = new File("config.properties"); try { inputStream = new FileInputStream(file); properties.load(inputStream); properties.setProperty(property, value); outputStream = new FileOutputStream(file); properties.store(outputStream, ""); } catch (IOException e) { logger.error("IOException when attempting to set a configuration property. "); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { logger.error("IOException when attempting to set a configuration property. "); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { logger.error("IOException when attempting to set a configuration property. "); } } } }
From source file:de.tudarmstadt.ukp.lmf.hibernate.HibernateConnect.java
/** * This method creates and returns Hibernate Properties. * * @param jdbc_url/*w ww .j av a 2s. c o m*/ * Host_to_the_database/database_name * @param jdbc_driver_class * driver used to connect * @param db_vendor * database vendor * @param user * user name * @param password * password * @param showSQL * set to true in order to print all SQL-queries to the console * * @return hibernate properties based on the consumed parameters * * @see Properties */ public static Properties getProperties(String jdbc_url, String jdbc_driver_class, String db_vendor, String user, String password, boolean showSQL) { Properties p = new Properties(); /* * <property name="driverClassName" value="org.h2.Driver"/> <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/> </bean> <bean id="jpaAdaptor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false" /> <!-- Let Hibernate generate the DDL for the schema --> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" /> */ // Database connection settings common for mysql and h2 p.setProperty("hibernate.connection.driver_class", jdbc_driver_class); p.setProperty("hibernate.connection.characterEncoding", "UTF-8"); p.setProperty("hibernate.connection.useUnicode", "true"); p.setProperty("hibernate.connection.charSet", "UTF-8"); p.setProperty("hibernate.connection.username", user); p.setProperty("hibernate.connection.password", password); // connection url if (!jdbc_url.startsWith("jdbc:")) { if (db_vendor.equals("mysql")) { p.setProperty("hibernate.connection.url", "jdbc:" + db_vendor + "://" + jdbc_url + "?characterEncoding=UTF-8&useUnicode=true"); } else if (db_vendor.equals("h2")) { p.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); } } else { p.setProperty("hibernate.connection.url", jdbc_url); } // JDBC connection pool (use the built-in) --> // p.setProperty("hibernate.connection.pool_size","1"); //Using c3p0 instead now for better connection handling p.setProperty("hibernate.c3p0.min_size", "1"); p.setProperty("hibernate.c3p0.max_size", "1"); p.setProperty("hibernate.c3p0.timeout", "0"); p.setProperty("hibernate.c3p0.max_statements", "0"); p.setProperty("hibernate.c3p0.idle_test_period", "5"); // SQL dialect if (db_vendor.equals("mysql")) { p.setProperty("hibernate.dialect", UBYMySQLDialect.class.getName()); } else if (db_vendor.equals("h2")) { p.setProperty("hibernate.dialect", UBYH2Dialect.class.getName()); } else { p.setProperty("hibernate.dialect", db_vendor); } // Enable Hibernate's automatic session context management p.setProperty("hibernate.current_session_context_class", "thread"); // Disable the second-level cache p.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); //p.setProperty("hibernate.cache.provider_class","org.hibernate.connection.C3P0ConnectionProvider"); p.setProperty("hibernate.order_inserts", "true"); p.setProperty("hibernate.order_updates", "true"); //p.setProperty("hibernate.cache.provider_class","org.hibernate.cache.OSCacheProvider"); p.setProperty("hibernate.jdbc.batch_size", "100"); p.setProperty("hibernate.cache.use_second_level_cache", "false"); p.setProperty("hibernate.cache.use_query_cache", "false"); // Echo all executed SQL to stdout if (showSQL) { p.setProperty("hibernate.show_sql", "true"); } else { p.setProperty("hibernate.show_sql", "false"); } // Do only update schema on changes e.g. validate | update | create | create-drop // p.setProperty("hibernate.hbm2ddl.auto","update"); // JEK see http://stackoverflow.com/questions/3179765/how-to-turn-off-hbm2ddl p.setProperty("hibernate.hbm2ddl.auto", "validate"); // if (db_vendor.equals("mysql")) { // p.setProperty("hibernate.hbm2ddl.auto","validate"); // } else if (db_vendor.equals("h2")) { // p.setProperty("hibernate.hbm2ddl.auto","update"); // } // p.setProperty("hibernate.hbm2ddl.auto","none"); return p; }
From source file:es.csic.iiia.planes.generator.Cli.java
/** * Parse the provided list of arguments according to the program's options. * * @param in_args list of input arguments. * @return a configuration object set according to the input options. *//*from w w w.j ava 2 s.c o m*/ private static Configuration parseOptions(String[] in_args) { CommandLineParser parser = new PosixParser(); CommandLine line = null; Properties settings = loadDefaultSettings(); try { line = parser.parse(options, in_args); } catch (ParseException ex) { Logger.getLogger(Cli.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); showHelp(); } if (line.hasOption('h')) { showHelp(); } if (line.hasOption('d')) { dumpSettings(); } if (line.hasOption('s')) { String fname = line.getOptionValue('s'); try { settings.load(new FileReader(fname)); } catch (IOException ex) { throw new IllegalArgumentException("Unable to load the settings file \"" + fname + "\""); } } // Apply overrides settings.setProperty("quiet", String.valueOf(line.hasOption('q'))); Properties overrides = line.getOptionProperties("o"); settings.putAll(overrides); String[] args = line.getArgs(); if (args.length < 1) { showHelp(); } settings.setProperty("problem", args[0]); Configuration c = new Configuration(settings); if (line.hasOption('t')) { System.exit(0); } return c; }
From source file:atg.tools.dynunit.nucleus.NucleusUtils.java
/** * Creates an Initial.properties file//from w w w . ja v a 2 s. c o m * * @param rootConfigPath * The root of the config path entry. * @param initialServices * initial services list * * @return the create initial services properties file. * * @throws IOException * if an error occurs */ public static File createInitial(File rootConfigPath, Iterable<String> initialServices) throws IOException { Properties prop = new Properties(); prop.setProperty("initialServices", StringUtils.join(initialServices, ',')); return ComponentUtil.newComponent(rootConfigPath, "Initial", InitialService.class, prop); }
From source file:com.egt.core.util.VelocityEngineer.java
private static Properties getProperties(String propsFilename) throws Exception { Bitacora.trace(VelocityEngineer.class, "getProperties", propsFilename); Properties p = new Properties(); try (FileInputStream inStream = new FileInputStream(propsFilename)) { p.load(inStream);//w ww.j ava 2s. c o m } String comma = System.getProperties().getProperty("path.separator"); String slash = System.getProperties().getProperty("file.separator"); String VFRLP = "$" + EAC.VELOCITY_FILE_RESOURCE_LOADER_PATH; String vfrlp = EA.getString(EAC.VELOCITY_FILE_RESOURCE_LOADER_PATH); vfrlp = vfrlp.replace(comma, ", "); vfrlp = vfrlp.replace(slash, "/"); String key; String value; for (Enumeration e = p.propertyNames(); e.hasMoreElements();) { key = (String) e.nextElement(); value = p.getProperty(key); if (StringUtils.isNotBlank(value) && value.contains(VFRLP)) { value = value.replace(VFRLP, vfrlp); p.setProperty(key, value); } Bitacora.trace(key + "=" + value); } return p; }
From source file:com.glaf.core.config.DBConfiguration.java
public static Properties toProperties(ConnectionDefinition conn) { if (conn != null) { Properties props = new Properties(); if (conn.getProperties() != null) { props.putAll(conn.getProperties()); }/* w w w . ja va 2 s.co m*/ if (conn.getSubject() != null) { props.setProperty(SUBJECT, conn.getSubject()); } if (conn.getDatasource() != null) { props.setProperty(JDBC_DATASOURCE, conn.getDatasource()); } if (conn.getName() != null) { props.setProperty(JDBC_NAME, conn.getName()); } props.setProperty(JDBC_DRIVER, conn.getDriver()); props.setProperty(JDBC_URL, conn.getUrl()); props.setProperty(JDBC_USER, conn.getUser()); if (conn.getPassword() != null) { props.setProperty(JDBC_PASSWORD, conn.getPassword()); } if (conn.getProvider() != null) { props.setProperty(JDBC_PROVIDER, conn.getProvider()); } String type = getDatabaseType(conn.getUrl()); props.setProperty(JDBC_TYPE, type); if (conn.getHost() != null) { props.setProperty(HOST, conn.getHost()); } props.setProperty(PORT, String.valueOf(conn.getPort())); if (conn.getDatabase() != null) { props.setProperty(DATABASE, conn.getDatabase()); } return props; } return null; }
From source file:com.mcleodmoores.mvn.natives.defaults.Defaults.java
private static void setSingle(final Properties properties, final String prefix, final String key, final String value) { if (value != null) { properties.setProperty(prefix + "." + key, value); }/*from w w w . j ava 2 s.c o m*/ }
From source file:io.starter.datamodel.Sys.java
/** * /*from w w w.j av a 2 s . c om*/ * @param params * @throws Exception * * */ public static void sendEmail(String url, final User from, String toEmail, Map params, SqlSession session, CountDownLatch latch) throws Exception { final CountDownLatch ltc = latch; if (from == null) { throw new ServletException("Sys.sendEmail cannot have a null FROM User."); } String fromEmail = from.getEmail(); final String fro = fromEmail; final String to = toEmail; final Map p = params; final String u = url; final SqlSession sr = session; // TODO: check message sender/recipient validity if (!checkEmailValidity(fromEmail)) { throw new RuntimeException( fromEmail + " is not a valid email address. SENDING MESSAGE TO " + toEmail + " FAILED"); } // TODO: check mail server if it's a valid message if (!checkEmailValidity(toEmail)) { throw new RuntimeException( toEmail + " is not a valid email address. SENDING MESSAGE TO " + toEmail + " FAILED"); } // message.setSubject("Testing Email From Starter.io"); // Send the actual HTML message, as big as you like // fetch the content final String content = getText(u); new Thread(new Runnable() { @Override public void run() { // Sender's email ID needs to be mentioned // String sendAccount = "$EMAIL_USER_NAME$"; // final String username = "$EMAIL_USER_NAME$", password = // "hum0rm3"; // Assuming you are sending email from localhost // String host = "gator3083.hostgator.com"; String sendAccount = "$EMAIL_USER_NAME$"; final String username = "$EMAIL_USER_NAME$", password = "$EMAIL_USER_PASS$"; // Assuming you are sending email from localhost String host = SystemConstants.EMAIL_SERVER; // Get system properties Properties props = System.getProperties(); // Setup mail server props.setProperty("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.port", "587"); // Get the default Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(sendAccount)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(sendAccount)); // Set Subject: header field Object o = p.get("MESSAGE_SUBJECT"); message.setSubject(o.toString()); String ctx = new String(content.toString()); // TODO: String Rep on the params ctx = stringRep(ctx, p); message.setContent(ctx, "text/html; charset=utf-8"); // message.setContent(new Multipart()); // Send message Transport.send(message); Logger.log("Sending message to:" + to + " SUCCESS"); } catch (MessagingException mex) { // mex.printStackTrace(); Logger.log("Sending message to:" + to + " FAILED"); Logger.error("Sys.sendEmail() failed to send message. Messaging Exception: " + mex.toString()); } // log the data Syslog logEntry = new Syslog(); logEntry.setDescription("OK [ email sent from: " + fro + " to: " + to + "]"); logEntry.setUserId(from.getId()); logEntry.setEventType(SystemConstants.LOG_EVENT_TYPE_SEND_EMAIL); logEntry.setSourceId(from.getId()); // unknown logEntry.setSourceType(SystemConstants.TARGET_TYPE_USER); logEntry.setTimestamp(new java.util.Date(System.currentTimeMillis())); SqlSessionFactory sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession sesh = sqlSessionFactory.openSession(true); sesh.insert("io.starter.dao.SyslogMapper.insert", logEntry); sesh.commit(); if (ltc != null) ltc.countDown(); // release the latch } }).start(); }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * <p>Loads a properties file from a zip file and updates 2 properties in that properties file.</p> * <p>The properties file in the zip is not written back to the zip file with the updates.</p> * @param inZip The zip file to load the properties file from. * @param cadmiumPropertiesEntry The entry of a properties file in the zip to load. * @param repoUri The value to set the "com.meltmedia.cadmium.git.uri" property with. * @param branch The value to set the "com.meltmedia.cadmium.branch" property with. * @param configRepoUri The value to set the "com.meltmedia.cadmium.config.git.uri" property with. * @param configBranch The value to set the "com.meltmedia.cadmium.config.branch" property with. * @return The updated properties object that was loaded from the zip file. * @throws IOException//from w w w. j a v a 2 s . c o m */ public static Properties updateProperties(ZipFile inZip, ZipEntry cadmiumPropertiesEntry, String repoUri, String branch, String configRepoUri, String configBranch) throws IOException { Properties cadmiumProps = new Properties(); cadmiumProps.load(inZip.getInputStream(cadmiumPropertiesEntry)); if (org.apache.commons.lang3.StringUtils.isNotBlank(repoUri)) { cadmiumProps.setProperty("com.meltmedia.cadmium.git.uri", repoUri); } if (branch != null) { cadmiumProps.setProperty("com.meltmedia.cadmium.branch", branch); } if (org.apache.commons.lang3.StringUtils.isNotBlank(configRepoUri) && !org.apache.commons.lang3.StringUtils .equals(configRepoUri, cadmiumProps.getProperty("com.meltmedia.cadmium.git.uri"))) { cadmiumProps.setProperty("com.meltmedia.cadmium.config.git.uri", configRepoUri); } else if (org.apache.commons.lang3.StringUtils.equals(configRepoUri, cadmiumProps.getProperty("com.meltmedia.cadmium.git.uri"))) { cadmiumProps.remove("com.meltmedia.cadmium.config.git.uri"); } if (configBranch != null) { cadmiumProps.setProperty("com.meltmedia.cadmium.config.branch", configBranch); } return cadmiumProps; }
From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException { // can't process non-existent examples, or out of context snippets if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) { // validate checkstyle structure and contents try {/* w w w.j a v a 2s.c o m*/ final Properties properties = new Properties(); properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath()); final PropertiesExpander expander = new PropertiesExpander(properties); final Configuration config = ConfigurationLoader .loadConfiguration(new InputSource(new StringReader(code)), expander, false); final Checker checker = new Checker(); try { final ClassLoader moduleClassLoader = Checker.class.getClassLoader(); checker.setModuleClassLoader(moduleClassLoader); checker.configure(config); } finally { checker.destroy(); } } catch (CheckstyleException ex) { throw new CheckstyleException( fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex); } } }