Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add database and jndi configuration stores.
Browse files Browse the repository at this point in the history
These will be integrated under the ApplicationConfiguration class in a future revision to pull configuration elements from different sources.
  • Loading branch information
Carlos Munoz committed Jun 25, 2013
1 parent f8f8155 commit d379eb2
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 1 deletion.
52 changes: 52 additions & 0 deletions zanata-war/src/main/java/org/zanata/config/ConfigurationStore.java
@@ -0,0 +1,52 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.config;

import java.util.Collection;

/**
* Configuration store interface. Configuration stores are a collection of key-indexed values.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public interface ConfigurationStore<K, V>
{
/**
* Returns a configuration value based on a key.
*
* @param key The key
* @return The value stored for key. May return null if there is no such key.
*/
V getConfigValue(K key);

/**
* Indicates if a key is contained in the store.
*
* @param key The key
* @return True, if the key is contained in the store (no guarantees on the value). False otherwise.
*/
boolean containsKey(K key);

/**
* Resets the store by clearing out all values.
*/
void reset();
}
156 changes: 156 additions & 0 deletions zanata-war/src/main/java/org/zanata/config/DatabaseBackedConfig.java
@@ -0,0 +1,156 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.config;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.dao.ApplicationConfigurationDAO;
import org.zanata.model.HApplicationConfiguration;

/**
* Configuration store implementation that is backed by database tables.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("databaseBackedConfig")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class DatabaseBackedConfig implements ConfigurationStore<String, String>
{

private static final String[] allConfigKeys = new String[]
{
HApplicationConfiguration.KEY_ADMIN_EMAIL,
HApplicationConfiguration.KEY_DOMAIN,
HApplicationConfiguration.KEY_EMAIL_FROM_ADDRESS,
HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS,
HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL,
HApplicationConfiguration.KEY_HELP_CONTENT,
HApplicationConfiguration.KEY_HOME_CONTENT,
HApplicationConfiguration.KEY_HOST,
HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL,
HApplicationConfiguration.KEY_REGISTER
};

@In
private ApplicationConfigurationDAO applicationConfigurationDAO;

private Map<String, String> configurationValues;

/**
* Resets the store by clearing out all values. This means that values will need to be
* reloaded as they are requested.
*/
@Override
@Create
public void reset()
{
configurationValues = new HashMap<String, String>( allConfigKeys.length );
}

@Override
public String getConfigValue(String key)
{
if( !configurationValues.containsKey(key) )
{
HApplicationConfiguration configRecord = applicationConfigurationDAO.findByKey(key);
configurationValues.put(key, configRecord.getValue());
}
return configurationValues.get(key);
}

@Override
public boolean containsKey(String key)
{
for( String k : allConfigKeys )
{
if( k.equals(key) )
{
return true;
}
}
return false;
}

/**
* ==================================================================================================================
* Specific property accessor methods for configuration values
* ==================================================================================================================
*/
public String getAdminEmailAddress()
{
return getConfigValue(HApplicationConfiguration.KEY_ADMIN_EMAIL);
}

public String getDomain()
{
return getConfigValue(HApplicationConfiguration.KEY_DOMAIN);
}

public String getFromEmailAddress()
{
return getConfigValue(HApplicationConfiguration.KEY_EMAIL_FROM_ADDRESS);
}

public String getShouldLogEvents()
{
return getConfigValue(HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS);
}

public String getEmailLogLevel()
{
return getConfigValue(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL);
}

public String getHelpContent()
{
return getConfigValue(HApplicationConfiguration.KEY_HELP_CONTENT);
}

public String getHomeContent()
{
return getConfigValue(HApplicationConfiguration.KEY_HOME_CONTENT);
}

public String getServerHost()
{
return getConfigValue(HApplicationConfiguration.KEY_HOST);
}

public String getLogEventsDestinationEmailAddress()
{
return getConfigValue(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL);
}

public String getRegistrationUrl()
{
return getConfigValue(HApplicationConfiguration.KEY_REGISTER);
}
}
178 changes: 178 additions & 0 deletions zanata-war/src/main/java/org/zanata/config/JndiBackedConfig.java
@@ -0,0 +1,178 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.config;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

/**
* Configuration store implementation that is backed by JNDI properties.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("jndiBackedConfig")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class JndiBackedConfig implements ConfigurationStore<String, String>
{

private static final String KEY_AUTH_POLICY = "java:global/zanata/security/auth-policy-names/";
private static final String KEY_ADMIN_USERS = "java:global/zanata/security/admin-users";
private static final String KEY_DEFAULT_FROM_ADDRESS = "java:global/zanata/email/default-from-address";
private static final String KEY_EMAIL_HOST = "java:global/zanata/smtp/host";
private static final String KEY_EMAIL_PORT = "java:global/zanata/smtp/port";
private static final String KEY_EMAIL_USERNAME = "java:global/zanata/smtp/username";
private static final String KEY_EMAIL_PASSWORD = "java:global/zanata/smtp/password";
private static final String KEY_EMAIL_TLS = "java:global/zanata/smtp/tls";
private static final String KEY_EMAIL_SSL = "java:global/zanata/smtp/ssl";

private Map<String, String> configurationValues;

@Override
public String getConfigValue(String key)
{
if( !configurationValues.containsKey(key) )
{
try
{
Context ctx = new InitialContext();
configurationValues.put(key, (String) ctx.lookup(key));
}
catch (NamingException e)
{
throw new RuntimeException("Problem when fetching Jndi config key '" + key + "'", e);
}
}
return configurationValues.get(key);
}

@Override
public boolean containsKey(String key)
{
// Only one way to know if it's there... and that is to go look for it
getConfigValue(key);
return configurationValues.containsKey(key);
}

/**
* Resets the store by clearing out all values. This means that values will need to be
* reloaded as they are requested.
*/
@Override
@Create
public void reset()
{
configurationValues = new HashMap<String, String>();
}

/**
* Specific to Directory-based configuration, this method returns a set of sub-keys available for the given
* "parent" key. For example, if there are two Jndi properties called 'org/zanata/prop1' and 'org/zanata/prop2',
* and the following invocation is made: <code>getSubKeys("org/zanata")</code>, the return value would be an array
* containing "prop1" and "prop2".
*
* @param base The base context to look for sub keys.
* @return An array with all the available keys in the context.
*/
public Set<String> getSubKeys(String base)
{
try
{
Context ctx = new InitialContext();
NamingEnumeration<NameClassPair> pairs = ctx.list(base);
Set<String> results = new HashSet<String>();
while( pairs.hasMore() )
{
NameClassPair pair = pairs.next();
results.add(pair.getName());
}

return results;
}
catch (NamingException e)
{
throw new RuntimeException("Problem when fetching Jndi sub keys for '" + base + "'", e);
}
}

/**
* ==================================================================================================================
* Specific property accessor methods for configuration values
* ==================================================================================================================
*/
public Set<String> getEnabledAuthenticationPolicies()
{
return getSubKeys(KEY_AUTH_POLICY);
}

public String getAdminUsersList()
{
return getConfigValue(KEY_ADMIN_USERS);
}

public String getDefaultFromEmailAddress()
{
return getConfigValue(KEY_DEFAULT_FROM_ADDRESS);
}

public String getSmtpHostName()
{
return getConfigValue(KEY_EMAIL_HOST);
}

public String getSmtpPort()
{
return getConfigValue(KEY_EMAIL_PORT);
}

public String getSmtpUsername()
{
return getConfigValue(KEY_EMAIL_USERNAME);
}

public String getSmtpPassword()
{
return getConfigValue(KEY_EMAIL_PASSWORD);
}

public String getSmtpUsesTls()
{
return getConfigValue(KEY_EMAIL_TLS);
}

public String getStmpUsesSsl()
{
return getConfigValue(KEY_EMAIL_SSL);
}
}

0 comments on commit d379eb2

Please sign in to comment.