public static Configuration instance() { if (config == null) { try { // Default values build into war file, this is last prio used if no of the other sources // override this boolean allowexternal = Boolean.getBoolean( new PropertiesConfiguration( ExtraConfiguration.class.getResource("/" + PROPERTY_FILENAME)) .getString(CONFIGALLOWEXTERNAL, "false")); config = new CompositeConfiguration(); PropertiesConfiguration pc; // Only add these config sources if we allow external configuration if (allowexternal) { // Override with system properties, this is prio 1 if it exists (java -Dscep.test=foo) config.addConfiguration(new SystemConfiguration()); log.info("Added system properties to configuration source (java -Dfoo.prop=bar)."); // Override with file in "application server home directory"/conf, this is prio 2 File f1 = new File("conf/" + PROPERTY_FILENAME); pc = new PropertiesConfiguration(f1); pc.setReloadingStrategy(new FileChangedReloadingStrategy()); config.addConfiguration(pc); log.info("Added file to configuration source: " + f1.getAbsolutePath()); // Override with file in "/etc/ejbca/conf/extra, this is prio 3 File f2 = new File("/etc/ejbca/conf/extra/" + PROPERTY_FILENAME); pc = new PropertiesConfiguration(f2); pc.setReloadingStrategy(new FileChangedReloadingStrategy()); config.addConfiguration(pc); log.info("Added file to configuration source: " + f2.getAbsolutePath()); } // Default values build into war file, this is last prio used if no of the other sources // override this URL url = ExtraConfiguration.class.getResource("/" + PROPERTY_FILENAME); pc = new PropertiesConfiguration(url); config.addConfiguration(pc); log.info("Added url to configuration source: " + url); log.info("Allow external re-configuration: " + allowexternal); // Test log.debug("Using keystore path (1): " + config.getString(SCEPKEYSTOREPATH + ".1")); // log.debug("Using keystore pwd (1): "+config.getString(SCEPKEYSTOREPWD+".1")); // log.debug("Using authPwd: "+config.getString(SCEPAUTHPWD)); log.debug("Using certificate profile: " + config.getString(SCEPCERTPROFILEKEY)); log.debug("Using entity profile: " + config.getString(SCEPENTITYPROFILEKEY)); log.debug("Using default CA: " + config.getString(SCEPDEFAULTCA)); log.debug("Create or edit user: "******"Mapping for CN=Scep CA,O=EJBCA Sample,C=SE: " + config.getString("CN=Scep CA,O=EJBCA Sample,C=SE")); } catch (ConfigurationException e) { log.error("Error intializing ExtRA Configuration: ", e); } } return config; }
public boolean connectToMailBox() { try { Properties props = new Properties(); props.setProperty("mail.imap.ssl.enable", "true"); // required for Gmail props.setProperty("mail.imap.auth.mechanisms", "XOAUTH2"); props.setProperty("mail.store.protocol", config.getString("imap.protocol")); props.setProperty("mail.imaps.fetchsize", "" + fetchSize); props.setProperty("mail.imaps.timeout", "" + rTimeout); props.setProperty("mail.imaps.writetimeout", "" + rTimeout); props.setProperty("mail.imaps.connectiontimeout", "" + cTimeout); props.setProperty("mail.imaps.connectionpooltimeout", "" + cTimeout); Session session = Session.getInstance(props); mailbox = session.getStore(config.getString("imap.protocol")); mailbox.connect( config.getString("imap.host"), config.getString("imap.user"), config.getString("imap.access_token")); LOG.info("Connected to mailbox"); return true; } catch (MessagingException e) { LOG.error("Connection failed", e); return false; } }
@Before public void setup() throws Exception, IOException, URISyntaxException { URL sourceUrl = this.getClass().getResource("/"); sourceFolder = new File(sourceUrl.getFile()); if (!sourceFolder.exists()) { throw new Exception("Cannot find sample data structure!"); } destinationFolder = folder.getRoot(); templateFolder = new File(sourceFolder, templateDir); if (!templateFolder.exists()) { throw new Exception("Cannot find template folder!"); } config = ConfigUtil.load(new File(this.getClass().getResource("/").getFile())); Iterator<String> keys = config.getKeys(); while (keys.hasNext()) { String key = keys.next(); if (key.startsWith("template") && key.endsWith(".file")) { String old = (String) config.getProperty(key); config.setProperty(key, old.substring(0, old.length() - 4) + "." + templateExtension); } } Assert.assertEquals(".html", config.getString(ConfigUtil.Keys.OUTPUT_EXTENSION)); db = DBUtil.createDataStore("memory", "documents" + System.currentTimeMillis()); }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); try { config.addConfiguration(new PropertiesConfiguration("storage.properties")); } catch (ConfigurationException e) { // TODO Auto-generated catch block logger.error(e.getMessage()); } String rootFolder = config.getString("bp.root.folder"); String altpartyid = args[0]; SqlSession sqlSession = RazorServer.openSession(); try { if (StringUtils.isBlank(altpartyid)) { // altpartyid = "231051"; //NextPax partner GA altpartyid = "179795"; } Partner partner = sqlSession.getMapper(PartnerMapper.class).exists(altpartyid); if (partner == null) { throw new ServiceException(Error.party_id, altpartyid); } A_Handler handler = new A_Handler(partner); handler.downloadImages(rootFolder); } catch (Exception e) { logger.error(e.getMessage()); } }
private Writer createWriter(File file) throws IOException { if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } return new OutputStreamWriter( new FileOutputStream(file), config.getString(ConfigUtil.Keys.RENDER_ENCODING)); }
/** * Render the supplied content to a file. * * @param content The content to renderDocument * @throws Exception */ public void render(Map<String, Object> content) throws Exception { String docType = (String) content.get("type"); String outputFilename = destination.getPath() + File.separatorChar + (String) content.get("uri"); outputFilename = outputFilename.substring(0, outputFilename.lastIndexOf(".")); // delete existing versions if they exist in case status has changed either way File draftFile = new File( outputFilename + config.getString(Keys.DRAFT_SUFFIX) + FileUtil.findExtension(config, docType)); if (draftFile.exists()) { draftFile.delete(); } File publishedFile = new File(outputFilename + FileUtil.findExtension(config, docType)); if (publishedFile.exists()) { publishedFile.delete(); } if (content.get("status").equals("draft")) { outputFilename = outputFilename + config.getString(Keys.DRAFT_SUFFIX); } File outputFile = new File(outputFilename + FileUtil.findExtension(config, docType)); StringBuilder sb = new StringBuilder(); sb.append("Rendering [").append(outputFile).append("]... "); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", content); model.put("renderer", renderingEngine); try { Writer out = createWriter(outputFile); renderingEngine.renderDocument(model, findTemplateName(docType), out); out.close(); sb.append("done!"); LOGGER.info(sb.toString()); } catch (Exception e) { sb.append("failed!"); LOGGER.error(sb.toString(), e); throw new Exception("Failed to render file. Cause: " + e.getMessage()); } }
/** * Get a file pathname. Any backslashes found are converted to forward slashes. N.B. We know this * is a file pathname. Backslashes can cause problems with Java code expecting forward slashes. So * it *should* be safe to convert any backslashes to forward slashes. If any client code needs to * fetch properties with backslashes (eg to pass to native JNI code), then either convert the * result back to forward slashes, or just call getString instead. * * @see gda.configuration.properties.PropertiesConfig#getPath(java.lang.String, java.lang.String) */ @Override public String getPath(String name, String defaultValue) { String value = config.getString(name, defaultValue); if (value != null) { value = value.replace('\\', '/'); } return value; }
public boolean getBoolean(String key, boolean defaultValue) { try { String value = config.getString(key); if (value == null) { return defaultValue; } return Boolean.valueOf(value.trim()); } catch (RuntimeException e) { return defaultValue; } }
public int getInt(String key, int defaultValue) { try { String value = config.getString(key); if (value == null) { return defaultValue; } return Integer.parseInt(value.trim()); } catch (NumberFormatException e) { return defaultValue; } }
/** * 判断是否超出策略阀值 * * @param policyType 1:系统功耗,2:CPU功耗,3:内存功耗,4:进风口温度 * @param policyLimt 阀值 * @return * @author zhangjh 新增日期:2012-9-5 * @since ipmi_task */ private boolean overPolicyLimit(SerInfo ser, int policyType, int policyLimt) { int current = 0; if (policyType == 4) { CompositeConfiguration conf = Config.getConfig(); GobblerServer gobblerServer = new GobblerServer( conf.getString("gobbler.host"), conf.getString("gobbler.user"), conf.getString("gobbler.passwd")); current = ipmiSensor.getTempSensorReading(ser, gobblerServer, Parameter.InletTemp); } else { PowerReading p = ipmiPower.getPlatformPowerReading(ser, policyType2Domain(policyType)); current = p.getCurrent(); } if (current >= policyLimt) { logger.info( "4.[" + ser.getHost() + "]的当前" + policyType2Name(policyType) + "为<" + current + ">,阀值为<" + policyLimt + ">,已超过阀值."); return true; } else { logger.info( "4.[" + ser.getHost() + "]的当前" + policyType2Name(policyType) + "为<" + current + ">,阀值为<" + policyLimt + ">,未超过阀值,退出."); return false; } }
@Override public int render( Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException { if (config.getBoolean(ConfigurationKeys.RENDER_INDEX)) { try { renderer.renderIndex(config.getString(ConfigurationKeys.INDEX_FILE), db); return 1; } catch (Exception e) { throw new RenderingException(e); } } else { return 0; } }
@Test public void initFailDestinationContainsContent() throws IOException { Init init = new Init(config); File initPath = folder.newFolder("init"); File contentFolder = new File( initPath.getPath() + File.separatorChar + config.getString(ConfigurationKeys.CONTENT_FOLDER)); contentFolder.mkdir(); try { init.run(initPath, rootPath, "freemarker"); fail("Shouldn't be able to initialise folder with content folder within it!"); } catch (Exception e) { e.printStackTrace(); } File testFile = new File(initPath, "testfile.txt"); assertThat(testFile).doesNotExist(); }
/** * Render tag files using the supplied content. * * @param tags The content to renderDocument * @param tagPath The output path * @throws Exception */ public void renderTags(Set<String> tags, String tagPath) throws Exception { final List<String> errors = new LinkedList<String>(); for (String tag : tags) { Map<String, Object> model = new HashMap<String, Object>(); model.put("renderer", renderingEngine); model.put("tag", tag); Map<String, Object> map = buildSimpleModel("tag"); map.put("rootpath", "../"); model.put("content", map); File outputFile = new File( destination.getPath() + File.separator + tagPath + File.separator + tag + config.getString(Keys.OUTPUT_EXTENSION)); StringBuilder sb = new StringBuilder(); sb.append("Rendering tags [").append(outputFile).append("]... "); try { Writer out = createWriter(outputFile); renderingEngine.renderDocument(model, findTemplateName("tag"), out); out.close(); sb.append("done!"); LOGGER.info(sb.toString()); } catch (Exception e) { sb.append("failed!"); LOGGER.error(sb.toString(), e); errors.add(e.getMessage()); } } if (!errors.isEmpty()) { StringBuilder sb = new StringBuilder(); sb.append("Failed to render tags. Cause(s):"); for (String error : errors) { sb.append("\n" + error); } throw new Exception(sb.toString()); } }
@Before public void setup() throws Exception { currentLocale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); ModelExtractorsDocumentTypeListener listener = new ModelExtractorsDocumentTypeListener(); DocumentTypes.addListener(listener); URL sourceUrl = this.getClass().getResource("/"); sourceFolder = new File(sourceUrl.getFile()); if (!sourceFolder.exists()) { throw new Exception("Cannot find sample data structure!"); } destinationFolder = folder.getRoot(); templateFolder = new File(sourceFolder, templateDir); if (!templateFolder.exists()) { throw new Exception("Cannot find template folder!"); } config = ConfigUtil.load(new File(this.getClass().getResource("/").getFile())); Iterator<String> keys = config.getKeys(); while (keys.hasNext()) { String key = keys.next(); if (key.startsWith("template") && key.endsWith(".file")) { String old = (String) config.getProperty(key); config.setProperty(key, old.substring(0, old.length() - 4) + "." + templateExtension); } } Assert.assertEquals(".html", config.getString(ConfigUtil.Keys.OUTPUT_EXTENSION)); db = DBUtil.createDataStore("memory", "documents" + System.currentTimeMillis()); crawler = new Crawler(db, sourceFolder, config); crawler.crawl(new File(sourceFolder.getPath() + File.separator + "content")); parser = new Parser(config, sourceFolder.getPath()); renderer = new Renderer(db, destinationFolder, templateFolder, config); setupExpectedOutputStrings(); }
private String findTemplateName(String docType) { return config.getString("template." + docType + ".file"); }
/** @return */ public String getString(String s) { return config.getString(s); }
@Override public String getString(String name, String defaultValue) { return config.getString(name, defaultValue); }