Skip to content

Commit

Permalink
OWB-441 introduce new openwebbeans.properties mechanism
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/openwebbeans/trunk@986225 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
struberg committed Aug 17, 2010
1 parent f385c20 commit eccc84c
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 106 deletions.
Expand Up @@ -852,8 +852,7 @@ public static <T> void defineInternalInjectedFields(AbstractInjectionTargetBean<
{

Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
boolean useOwbSpecificInjection = OpenWebBeansConfiguration.getInstance().isOwbSpecificFieldInjection();


if (fields.length != 0)
{
for (Field field : fields)
Expand All @@ -868,23 +867,17 @@ public static <T> void defineInternalInjectedFields(AbstractInjectionTargetBean<
}
}

if(!useOwbSpecificInjection)
if(!field.isAnnotationPresent(Inject.class))
{
if(!field.isAnnotationPresent(Inject.class))
{
continue;
}
continue;
}

Annotation[] anns = field.getDeclaredAnnotations();

// Injected fields can not be @Produces
if (AnnotationUtil.hasAnnotation(anns, Produces.class))
{
if(!useOwbSpecificInjection)
{
throw new WebBeansConfigurationException("Injection fields can not be annotated with @Produces");
}
throw new WebBeansConfigurationException("Injection fields can not be annotated with @Produces");
}

Annotation[] qualifierAnns = AnnotationUtil.getQualifierAnnotations(anns);
Expand Down
Expand Up @@ -18,9 +18,6 @@
*/
package org.apache.webbeans.config;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.webbeans.exception.WebBeansConfigurationException;
Expand All @@ -46,11 +43,7 @@ public class OpenWebBeansConfiguration
private static final WebBeansLogger logger = WebBeansLogger.getLogger(OpenWebBeansConfiguration.class);

/**Default configuration files*/
private final static String DEFAULT_CONFIG_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans-default.properties";
private final static String CONFIG_EE_COMMON_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans-ee-common.properties";
private final static String CONFIG_EE_WEB_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans-ee-web.properties";
private final static String CONFIG_JMS_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans-jms.properties";
private final static String CONFIG_JSF_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans-jsf.properties";
private final static String DEFAULT_CONFIG_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans.properties";

/**Application specified file*/
private final static String CONFIG_PROPERTIES_NAME = "META-INF/openwebbeans/openwebbeans.properties";
Expand All @@ -73,10 +66,6 @@ public class OpenWebBeansConfiguration
@Deprecated //Not use any more
public static final String USE_OWB_SPECIFIC_XML_CONFIGURATION = "org.apache.webbeans.useOwbSpecificXmlConfig";

/**Use OWB Specific Field Injection*/
@Deprecated //Not use anymore
public static final String USE_OWB_SPECIFIC_FIELD_INJECTION = "org.apache.webbeans.fieldInjection.useOwbSpecificInjection";

/**Use EJB Discovery or not*/
public static final String USE_EJB_DISCOVERY = "org.apache.webbeans.spi.deployer.useEjbMetaDataDiscoveryService";

Expand Down Expand Up @@ -140,7 +129,7 @@ public OpenWebBeansConfiguration()
{
parseConfiguration();

logger.debug("Overriden properties from System prpoerties");
logger.debug("Overriding properties from System properties");

//Look for System properties
loadFromSystemProperties();
Expand Down Expand Up @@ -223,72 +212,13 @@ private void setPropertyFromSystemProperty(String key, String value)
*/
public synchronized void parseConfiguration() throws WebBeansConfigurationException
{
Properties newConfigProperties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();

InputStream is = loader.getResourceAsStream(DEFAULT_CONFIG_PROPERTIES_NAME);
load(is, newConfigProperties);

is = loader.getResourceAsStream(CONFIG_JMS_PROPERTIES_NAME);
load(is, newConfigProperties);

is = loader.getResourceAsStream(CONFIG_JSF_PROPERTIES_NAME);
load(is, newConfigProperties);

is = loader.getResourceAsStream(CONFIG_EE_COMMON_PROPERTIES_NAME);
load(is, newConfigProperties);

is = loader.getResourceAsStream(CONFIG_EE_WEB_PROPERTIES_NAME);
load(is, newConfigProperties);

// and now overload those settings with the ones from the more specialized version (if available)

URL configUrl = loader.getResource(CONFIG_PROPERTIES_NAME);
if (configUrl == null)
{
logger.info(OWBLogConst.TEXT_CONFIG_NOT_FOUND, CONFIG_PROPERTIES_NAME);
}
else
{
logger.info(OWBLogConst.TEXT_CONFIG_FOUND, CONFIG_PROPERTIES_NAME, configUrl);

is = loader.getResourceAsStream(CONFIG_PROPERTIES_NAME);
load(is, newConfigProperties);
}
Properties newConfigProperties = PropertyLoader.getProperties(DEFAULT_CONFIG_PROPERTIES_NAME);

// set the new one as perfect fit.
configProperties = newConfigProperties;
}

private void load(InputStream is, Properties newConfigProperties)
{
try
{
if(is != null)
{
newConfigProperties.load(is);
}
}
catch (IOException ioEx)
{
throw new WebBeansConfigurationException(logger.getTokenString(OWBLogConst.EDCONF_FAIL), ioEx);
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch(Exception e)
{
// do nothing
}
}
}
}


/**
* Gets property.
* @param key
Expand Down Expand Up @@ -321,18 +251,6 @@ public synchronized void setProperty(String key, Object value)
configProperties.put(key, value);
}

/**
* Returns true if owb specific injection
* false otherwise.
* @return true if owb specific injection
*/
public boolean isOwbSpecificFieldInjection()
{
String value = getProperty(USE_OWB_SPECIFIC_FIELD_INJECTION);

return Boolean.valueOf(value);
}

/**
* Return true if use JSF2.
* @return true if use JSF2
Expand Down
@@ -0,0 +1,212 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.webbeans.config;

import org.apache.webbeans.logger.WebBeansLogger;
import org.apache.webbeans.util.WebBeansUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
* <p>Utility class to load configuration properties via a list of
* artibrary property files by a well defined order.</p>
* <p>User configurations should start with 'configuration.ordinal'
* greather than 100.</p>
*
*/
public class PropertyLoader
{
public final static int CONFIGURATION_ORDINAL_DEFAULT_VALUE = 100;

public final static String CONFIGURATION_ORDINAL_PROPERTY_NAME = "configuration.ordinal";


private static final WebBeansLogger logger = WebBeansLogger.getLogger(PropertyLoader.class);


/**
* <p>Look for all property files with the given name (e.g. 'myconfig.properties') in
* the classpath. Then load all properties files and sort them by their ascending
* configuration order and apply them in this order.</p>
*
* <p>The idea is to be able to 'override' properties by just providing
* a new properties file with the same name but a higher 'configuration.ordinal'
* than the old one.</p>
*
* <p>If a property file defines no 'configuration.ordinal' property than a default
* value of {@link #CONFIGURATION_ORDINAL_DEFAULT_VALUE} is assumed. Any sensitive
* default which is provided by the system parsing for the configuration should
* have a 'configuration.ordinal' value lower than 10. In most cases a value of 1</p>
*
* <p>If 2 property files have the same ordinal 'configuraiton.order' the outcome
* is not really defined. The Properties file which got found first will be
* processed first and thus get overwritten by the one found later.</p>
*
* @param propertyFileName the name of the properties file
* @return the final property values
*/
public static synchronized Properties getProperties(String propertyFileName)
{
try
{
List<Properties> allProperties = loadAllProperties(propertyFileName);
if (allProperties == null)
{
return null;
}

List<Properties> sortedProperties = sortProperties(allProperties);
Properties properties = mergeProperties(sortedProperties);
return properties;
}
catch (IOException e)
{
logger.error("Error while loading the propertyFile " + propertyFileName, e);
return null;
}
}

private static List<Properties> loadAllProperties(String propertyFileName)
throws IOException
{
ClassLoader cl = WebBeansUtil.getCurrentClassLoader();
Enumeration<URL> propertyUrls = cl.getResources(propertyFileName);
if (propertyUrls == null || !propertyUrls.hasMoreElements())
{
logger.info("could not find any property files with name " + propertyFileName);
return null;
}

List<Properties> properties = new ArrayList<Properties>();

while (propertyUrls.hasMoreElements())
{
URL propertyUrl = propertyUrls.nextElement();
InputStream is = null;
try
{
is = propertyUrl.openStream();
Properties prop = new Properties();
prop.load(is);
properties.add(prop);

// a bit debugging output
int ordinal = getConfigurationOrdinal(prop);
logger.info("loading properties with ordinal " + ordinal + " from file " + propertyUrl.getFile());
}
finally
{
if (is != null)
{
is.close();
}
}
}

return properties;
}

/**
* Implement a quick and dirty sorting mechanism for the given Properties.
* @param allProperties
* @return the Properties list sorted by it's 'configuration.ordinal' in ascending order.
*/
private static List<Properties> sortProperties(List<Properties> allProperties)
{
List<Properties> sortedProperties = new ArrayList<Properties>();
for (Properties p : allProperties)
{
int configOrder = getConfigurationOrdinal(p);

int i;
for (i = 0; i < sortedProperties.size(); i++)
{
int listConfigOrder = getConfigurationOrdinal(sortedProperties.get(i));
if (listConfigOrder > configOrder)
{
// only go as far as we found a higher priority Properties file
break;
}
}
sortedProperties.add(i, p);
}
return sortedProperties;
}

/**
* Determine the 'configuration.ordinal' of the given properties.
* {@link #CONFIGURATION_ORDINAL_DEFAULT_VALUE} if
* {@link #CONFIGURATION_ORDINAL_PROPERTY_NAME} is not set in the
* Properties file.
*
* @param p the Properties from the file.
* @return the ordinal number of the given Properties file.
*/
private static int getConfigurationOrdinal(Properties p)
{
int configOrder = CONFIGURATION_ORDINAL_DEFAULT_VALUE;

String configOrderString = p.getProperty(CONFIGURATION_ORDINAL_PROPERTY_NAME);
if (configOrderString != null && configOrderString.length() > 0)
{
try
{
configOrder = Integer.parseInt(configOrderString);
}
catch(NumberFormatException nfe)
{
logger.error(CONFIGURATION_ORDINAL_PROPERTY_NAME + " must be an integer value!");
throw nfe;
}
}

return configOrder;
}

/**
* Merge the given Properties in order of appearance.
* @param sortedProperties
* @return the merged Properties
*/
private static Properties mergeProperties(List<Properties> sortedProperties)
{
Properties mergedProperties = new Properties();
for (Properties p : sortedProperties)
{
for (Map.Entry entry : p.entrySet())
{
String key = (String) entry.getKey();
String value = (String) entry.getValue();

// simply overwrite the old properties with the new ones.
mergedProperties.setProperty(key, value);
}
}

return mergedProperties;
}

}

0 comments on commit eccc84c

Please sign in to comment.