Author: struberg
Date: Tue Aug 17 07:54:39 2010
New Revision: 986225
URL: http://svn.apache.org/viewvc?rev=986225&view=rev
Log:
OWB-441 introduce new openwebbeans.properties mechanism
Added:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/PropertyLoaderTest.java
openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/
openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/propertyloadertest.properties
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansAnnotatedTypeUtil.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java?rev=986225&r1=986224&r2=986225&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
Tue Aug 17 07:54:39 2010
@@ -852,8 +852,7 @@ public final class DefinitionUtil
{
Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
- boolean useOwbSpecificInjection =
OpenWebBeansConfiguration.getInstance().isOwbSpecificFieldInjection();
-
+
if (fields.length != 0)
{
for (Field field : fields)
@@ -868,23 +867,17 @@ public final class DefinitionUtil
}
}
- 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);
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java?rev=986225&r1=986224&r2=986225&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
Tue Aug 17 07:54:39 2010
@@ -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;
@@ -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";
@@ -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";
@@ -140,7 +129,7 @@ public class OpenWebBeansConfiguration
{
parseConfiguration();
- logger.debug("Overriden properties from System prpoerties");
+ logger.debug("Overriding properties from System properties");
//Look for System properties
loadFromSystemProperties();
@@ -223,72 +212,13 @@ public class OpenWebBeansConfiguration
*/
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
@@ -322,18 +252,6 @@ public class OpenWebBeansConfiguration
}
/**
- * 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
*/
Added:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java?rev=986225&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
(added)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
Tue Aug 17 07:54:39 2010
@@ -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;
+ }
+
+}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansAnnotatedTypeUtil.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansAnnotatedTypeUtil.java?rev=986225&r1=986224&r2=986225&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansAnnotatedTypeUtil.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansAnnotatedTypeUtil.java
Tue Aug 17 07:54:39 2010
@@ -65,7 +65,6 @@ import org.apache.webbeans.component.Web
import org.apache.webbeans.component.creation.AnnotatedTypeBeanCreatorImpl;
import org.apache.webbeans.config.DefinitionUtil;
import org.apache.webbeans.config.OWBLogConst;
-import org.apache.webbeans.config.OpenWebBeansConfiguration;
import org.apache.webbeans.container.InjectionResolver;
import org.apache.webbeans.decorator.DecoratorsManager;
import org.apache.webbeans.decorator.WebBeansDecoratorConfig;
@@ -341,17 +340,13 @@ public final class WebBeansAnnotatedType
public static <X> void defineInjectedFields(AbstractInjectionTargetBean<X>
bean,AnnotatedType<X> annotatedType)
{
Set<AnnotatedField<? super X>> annotatedFields =
annotatedType.getFields();
- boolean useOwbSpecificInjection =
OpenWebBeansConfiguration.getInstance().isOwbSpecificFieldInjection();
for(AnnotatedField<? super X> annotatedField: annotatedFields)
{
- if(!useOwbSpecificInjection)
+ if(!annotatedField.isAnnotationPresent(Inject.class))
{
- if(!annotatedField.isAnnotationPresent(Inject.class))
- {
- continue;
- }
+ continue;
}
-
+
if (annotatedField.isAnnotationPresent(Produces.class) ||
annotatedField.isAnnotationPresent(Delegate.class))
{
continue;
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/PropertyLoaderTest.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/PropertyLoaderTest.java?rev=986225&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/PropertyLoaderTest.java
(added)
+++
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/config/PropertyLoaderTest.java
Tue Aug 17 07:54:39 2010
@@ -0,0 +1,48 @@
+/*
+ * 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.newtests.config;
+
+import org.apache.webbeans.config.PropertyLoader;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Properties;
+
+public class PropertyLoaderTest
+{
+ private static final String PROPERTY_FILE =
"org/apache/webbeans/newtests/config/propertyloadertest.properties";
+
+ @Test
+ public void testPropertyLoader() throws Exception
+ {
+ Properties p = PropertyLoader.getProperties(PROPERTY_FILE);
+ Assert.assertNotNull(p);
+
+ String testValue = p.getProperty("testConfig");
+ Assert.assertNotNull(testValue);
+ Assert.assertEquals("testValue", testValue);
+ }
+
+ @Test
+ public void testNonExistentProperties()
+ {
+ Properties p = PropertyLoader.getProperties("notexisting.properties");
+ Assert.assertNull(p);
+ }
+}
Added:
openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/propertyloadertest.properties
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/propertyloadertest.properties?rev=986225&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/propertyloadertest.properties
(added)
+++
openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/config/propertyloadertest.properties
Tue Aug 17 07:54:39 2010
@@ -0,0 +1,25 @@
+# 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.
+#---------------------------------------------------------------
+#
+# test properties for the PropertyLoaderTest
+#
+#---------------------------------------------------------------
+
+configuration.ordinal=15
+
+testConfig=testValue