List of usage examples for org.apache.commons.configuration HierarchicalConfiguration getString
public String getString(String key, String defaultValue)
From source file:org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration.java
private static Optional<Duration> readGracePeriod(HierarchicalConfiguration config) { return Optional.ofNullable(config.getString(XmlKeys.GRACE_PERIOD, null)) .map(string -> TimeConverter.getMilliSeconds(string, TimeConverter.Unit.DAYS)) .map(Duration::ofMillis); }
From source file:org.apache.james.mailetcontainer.impl.JamesMailetContext.java
@Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { try {//from ww w.j ava2s .co m // Get postmaster String postMasterAddress = config.getString("postmaster", "postmaster").toLowerCase(Locale.US); // if there is no @domain part, then add the first one from the // list of supported domains that isn't localhost. If that // doesn't work, use the hostname, even if it is localhost. if (postMasterAddress.indexOf('@') < 0) { String domainName = null; // the domain to use // loop through candidate domains until we find one or exhaust // the // list for (String dom : domains.getDomains()) { String serverName = dom.toLowerCase(Locale.US); if (!("localhost".equals(serverName))) { domainName = serverName; // ok, not localhost, so // use it } } // if we found a suitable domain, use it. Otherwise fallback to // the // host name. postMasterAddress = postMasterAddress + "@" + (domainName != null ? domainName : domains.getDefaultDomain()); } try { this.postmaster = new MailAddress(postMasterAddress); if (!domains.containsDomain(postmaster.getDomain())) { String warnBuffer = "The specified postmaster address ( " + postmaster + " ) is not a local " + "address. This is not necessarily a problem, but it does mean that emails addressed to " + "the postmaster will be routed to another server. For some configurations this may " + "cause problems."; log.warn(warnBuffer); } } catch (AddressException e) { throw new ConfigurationException("Postmaster address " + postMasterAddress + "is invalid", e); } } catch (DomainListException e) { throw new ConfigurationException("Unable to access DomainList", e); } }
From source file:org.apache.james.mailetcontainer.impl.JamesMailetContextTest.java
@Test public void isLocalUserShouldReturnTrueWhenUsedWithLocalPartAndUserExistOnDefaultDomain() throws Exception { HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); when(configuration.getString(eq("defaultDomain"), any(String.class))).thenReturn(DOMAIN_COM); domainList.configure(configuration); domainList.addDomain(DOMAIN_COM);/*from w w w .j ava2s. c o m*/ usersRepository.addUser(USERMAIL, PASSWORD); assertThat(testee.isLocalUser(USERNAME)).isTrue(); }
From source file:org.apache.james.mailetcontainer.impl.JamesMailetContextTest.java
@Test public void isLocalUserShouldReturnFalseWhenUsedWithLocalPartAndUserDoNotExistOnDefaultDomain() throws Exception { HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); when(configuration.getString(eq("defaultDomain"), any(String.class))).thenReturn("any"); domainList.configure(configuration); domainList.addDomain(DOMAIN_COM);//from w w w . j a va 2s . c o m usersRepository.addUser(USERMAIL, PASSWORD); assertThat(testee.isLocalUser(USERNAME)).isFalse(); }
From source file:org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor.java
/** * @see/*from w w w . ja v a2s . co m*/ * org.apache.james.lifecycle.api.Configurable#configure(org.apache.commons.configuration.HierarchicalConfiguration) */ public void configure(HierarchicalConfiguration config) throws ConfigurationException { this.state = config.getString("[@state]", null); if (state == null) throw new ConfigurationException("Processor state attribute must be configured"); if (state.equals(Mail.GHOST)) throw new ConfigurationException( "Processor state of " + Mail.GHOST + " is reserved for internal use, choose a different one"); this.enableJmx = config.getBoolean("[@enableJmx]", true); this.config = config; }
From source file:org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor.java
/** * Load {@link CompositeMatcher} implementations and their child * {@link Matcher}'s//from ww w . j a v a 2s .c o m * * CompositeMatcher were added by JAMES-948 * * @param compMap * @param compMatcherConfs * @return compositeMatchers * @throws ConfigurationException * @throws MessagingException * @throws NotCompliantMBeanException */ private List<Matcher> loadCompositeMatchers(String state, Map<String, Matcher> compMap, List<HierarchicalConfiguration> compMatcherConfs) throws ConfigurationException, MessagingException { List<Matcher> matchers = new ArrayList<Matcher>(); for (HierarchicalConfiguration c : compMatcherConfs) { String compName = c.getString("[@name]", null); String matcherName = c.getString("[@match]", null); String invertedMatcherName = c.getString("[@notmatch]", null); Matcher matcher = null; if (matcherName != null && invertedMatcherName != null) { // if no matcher is configured throw an Exception throw new ConfigurationException("Please configure only match or nomatch per mailet"); } else if (matcherName != null) { matcher = matcherLoader.getMatcher(createMatcherConfig(matcherName)); if (matcher instanceof CompositeMatcher) { CompositeMatcher compMatcher = (CompositeMatcher) matcher; List<Matcher> childMatcher = loadCompositeMatchers(state, compMap, c.configurationsAt("matcher")); for (Matcher aChildMatcher : childMatcher) { compMatcher.add(aChildMatcher); } } } else if (invertedMatcherName != null) { Matcher m = matcherLoader.getMatcher(createMatcherConfig(invertedMatcherName)); if (m instanceof CompositeMatcher) { CompositeMatcher compMatcher = (CompositeMatcher) m; List<Matcher> childMatcher = loadCompositeMatchers(state, compMap, c.configurationsAt("matcher")); for (Matcher aChildMatcher : childMatcher) { compMatcher.add(aChildMatcher); } } matcher = new MatcherInverter(m); } if (matcher == null) throw new ConfigurationException("Unable to load matcher instance"); matchers.add(matcher); if (compName != null) { // check if there is already a composite Matcher with the name // registered in the processor if (compMap.containsKey(compName)) throw new ConfigurationException( "CompositeMatcher with name " + compName + " is already defined in processor " + state); compMap.put(compName, matcher); } } return matchers; }
From source file:org.apache.james.mailrepository.jcr.JCRMailRepository.java
/** * @see/*from w w w. j a va 2 s .c o m*/ * org.apache.james.mailrepository.lib.AbstractMailRepository * #doConfigure(org.apache.commons.configuration.HierarchicalConfiguration) */ @Override public void doConfigure(HierarchicalConfiguration config) throws ConfigurationException { this.workspace = config.getString("workspace", null); String username = config.getString("username", null); String password = config.getString("password", null); if (username != null && password != null) { this.creds = new SimpleCredentials(username, password.toCharArray()); } }
From source file:org.apache.james.protocols.lib.netty.AbstractConfigurableAsyncServer.java
/** * @see/* w w w . ja v a 2 s .com*/ * org.apache.james.lifecycle.api.Configurable * #configure(org.apache.commons.configuration.HierarchicalConfiguration) */ public final void configure(HierarchicalConfiguration config) throws ConfigurationException { enabled = config.getBoolean("[@enabled]", true); final Logger logger = getLogger(); if (!enabled) { logger.info(getServiceType() + " disabled by configuration"); return; } String listen[] = config.getString("bind", "0.0.0.0:" + getDefaultPort()).split(","); List<InetSocketAddress> bindAddresses = new ArrayList<InetSocketAddress>(); for (String aListen : listen) { String bind[] = aListen.split(":"); InetSocketAddress address; String ip = bind[0].trim(); int port = Integer.parseInt(bind[1].trim()); if (!ip.equals("0.0.0.0")) { try { ip = InetAddress.getByName(ip).getHostName(); } catch (UnknownHostException unhe) { throw new ConfigurationException( "Malformed bind parameter in configuration of service " + getServiceType(), unhe); } } address = new InetSocketAddress(ip, port); String infoBuffer = getServiceType() + " bound to: " + ip + ":" + port; logger.info(infoBuffer); bindAddresses.add(address); } setListenAddresses(bindAddresses.toArray(new InetSocketAddress[bindAddresses.size()])); jmxName = config.getString("jmxName", getDefaultJMXName()); int ioWorker = config.getInt("ioWorkerCount", DEFAULT_IO_WORKER_COUNT); setIoWorkerCount(ioWorker); maxExecutorThreads = config.getInt("maxExecutorCount", DEFAULT_MAX_EXECUTOR_COUNT); configureHelloName(config); setTimeout(config.getInt(TIMEOUT_NAME, DEFAULT_TIMEOUT)); StringBuilder infoBuffer = new StringBuilder(64).append(getServiceType()) .append(" handler connection timeout is: ").append(getTimeout()); logger.info(infoBuffer.toString()); setBacklog(config.getInt(BACKLOG_NAME, DEFAULT_BACKLOG)); infoBuffer = new StringBuilder(64).append(getServiceType()).append(" connection backlog is: ") .append(getBacklog()); logger.info(infoBuffer.toString()); String connectionLimitString = config.getString("connectionLimit", null); if (connectionLimitString != null) { try { connectionLimit = new Integer(connectionLimitString); } catch (NumberFormatException nfe) { logger.error("Connection limit value is not properly formatted.", nfe); } if (connectionLimit < 0) { logger.error("Connection limit value cannot be less than zero."); throw new ConfigurationException("Connection limit value cannot be less than zero."); } else if (connectionLimit > 0) { infoBuffer = new StringBuilder(128).append(getServiceType()).append(" will allow a maximum of ") .append(connectionLimitString).append(" connections."); logger.info(infoBuffer.toString()); } } String connectionLimitPerIP = config.getString("connectionLimitPerIP", null); if (connectionLimitPerIP != null) { try { connPerIP = Integer.parseInt(connectionLimitPerIP); } catch (NumberFormatException nfe) { logger.error("Connection limit per IP value is not properly formatted.", nfe); } if (connPerIP < 0) { logger.error("Connection limit per IP value cannot be less than zero."); throw new ConfigurationException("Connection limit value cannot be less than zero."); } else if (connPerIP > 0) { infoBuffer = new StringBuilder(128).append(getServiceType()).append(" will allow a maximum of ") .append(connPerIP).append(" per IP connections for ").append(getServiceType()); logger.info(infoBuffer.toString()); } } useStartTLS = config.getBoolean("tls.[@startTLS]", false); useSSL = config.getBoolean("tls.[@socketTLS]", false); if (useSSL && useStartTLS) throw new ConfigurationException("startTLS is only supported when using plain sockets"); if (useStartTLS || useSSL) { enabledCipherSuites = config.getStringArray("tls.supportedCipherSuites.cipherSuite"); keystore = config.getString("tls.keystore", null); if (keystore == null) { throw new ConfigurationException("keystore needs to get configured"); } secret = config.getString("tls.secret", ""); x509Algorithm = config.getString("tls.algorithm", defaultX509algorithm); } doConfigure(config); }
From source file:org.apache.james.protocols.lib.ProtocolHandlerChainImpl.java
public void init() throws Exception { List<org.apache.commons.configuration.HierarchicalConfiguration> children = handlerchainConfig .configurationsAt("handler"); // check if the coreHandlersPackage was specified in the config, if // not add the default if (handlerchainConfig.getString("[@coreHandlersPackage]") == null) handlerchainConfig.addProperty("[@coreHandlersPackage]", coreHandlersPackage); String coreHandlersPackage = handlerchainConfig.getString("[@coreHandlersPackage]"); if (handlerchainConfig.getString("[@jmxHandlersPackage]") == null) handlerchainConfig.addProperty("[@jmxHandlersPackage]", jmxHandlersPackage); String jmxHandlersPackage = handlerchainConfig.getString("[@jmxHandlersPackage]"); HandlersPackage handlersPackage = (HandlersPackage) loader.load(coreHandlersPackage, addHandler(coreHandlersPackage)); registerHandlersPackage(handlersPackage, null, children); if (handlerchainConfig.getBoolean("[@enableJmx]", true)) { DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); builder.addProperty("jmxName", jmxName); HandlersPackage jmxPackage = (HandlersPackage) loader.load(jmxHandlersPackage, addHandler(jmxHandlersPackage)); registerHandlersPackage(jmxPackage, builder, children); }/*from ww w . ja v a2 s . c om*/ for (HierarchicalConfiguration hConf : children) { String className = hConf.getString("[@class]", null); if (className != null) { handlers.add(loader.load(className, hConf)); } else { throw new ConfigurationException( "Missing @class attribute in configuration: " + ConfigurationUtils.toString(hConf)); } } wireExtensibleHandlers(); }
From source file:org.apache.james.rrt.jdbc.JDBCRecipientRewriteTable.java
protected void doConfigure(HierarchicalConfiguration conf) throws ConfigurationException { String destination = conf.getString("[@destinationURL]", null); if (destination == null) { throw new ConfigurationException("destinationURL must configured"); }//from w w w.j a v a 2 s. com // normalize the destination, to simplify processing. if (!destination.endsWith("/")) { destination += "/"; } // Parse the DestinationURL for the name of the datasource, // the table to use, and the (optional) repository Key. // Split on "/", starting after "db://" List<String> urlParams = new ArrayList<String>(); int start = 5; int end = destination.indexOf('/', start); while (end > -1) { urlParams.add(destination.substring(start, end)); start = end + 1; end = destination.indexOf('/', start); } // Build SqlParameters and get datasource name from URL parameters if (urlParams.size() == 0) { String exceptionBuffer = "Malformed destinationURL - Must be of the format '" + "db://<data-source>'. Was passed " + conf.getString("[@destinationURL]"); throw new ConfigurationException(exceptionBuffer); } if (urlParams.size() >= 2) { tableName = urlParams.get(1); } if (getLogger().isDebugEnabled()) { String logBuffer = "Parsed URL: table = '" + tableName + "'"; getLogger().debug(logBuffer); } sqlFileName = conf.getString("sqlFile"); }