myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r1150097 - in /myfaces/extensions/cdi/trunk/core: api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/ impl/src/main/java/org/apache/myfaces/extensions/...
Date Sat, 23 Jul 2011 12:42:19 GMT
Author: gpetracek
Date: Sat Jul 23 12:42:17 2011
New Revision: 1150097

URL: http://svn.apache.org/viewvc?rev=1150097&view=rev
Log:
EXTCDI-198 and EXTCDI-209 optional simple service provider

Added:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProvider.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProviderContext.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/EditableServiceProviderContext.java
Modified:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ServiceProviderContext.java
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/CodiStartupBroadcaster.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProvider.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProviderContext.java
    myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/serviceloader/ServiceProviderTest.java

Modified: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ServiceProviderContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ServiceProviderContext.java?rev=1150097&r1=1150096&r2=1150097&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ServiceProviderContext.java
(original)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ServiceProviderContext.java
Sat Jul 23 12:42:17 2011
@@ -19,20 +19,16 @@
 package org.apache.myfaces.extensions.cdi.core.api.provider;
 
 import javax.enterprise.inject.Typed;
-import java.util.List;
 
 /**
  * @author Gerhard Petracek
  */
-//TODO
 @Typed()
 public abstract class ServiceProviderContext<T>
 {
+    /**
+     * Allows to provide a custom {@link ClassLoader}
+     * @return the class-loader which should be used for the first try
+     */
     public abstract ClassLoader getClassLoader();
-
-    public abstract T postConstruct(T instance);
-
-    public abstract boolean filterService(Class<T> serviceClass);
-
-    public abstract void preInstallServices(List<Class<?>> foundServiceClasses);
 }

Modified: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/CodiStartupBroadcaster.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/CodiStartupBroadcaster.java?rev=1150097&r1=1150096&r2=1150097&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/CodiStartupBroadcaster.java
(original)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/startup/CodiStartupBroadcaster.java
Sat Jul 23 12:42:17 2011
@@ -83,26 +83,6 @@ public abstract class CodiStartupBroadca
                     {
                         return classLoader;
                     }
-
-                    @Override
-                    public StartupEventBroadcaster postConstruct(StartupEventBroadcaster
instance)
-                    {
-                        //do nothing
-                        return instance;
-                    }
-
-                    @Override
-                    public boolean filterService(Class<StartupEventBroadcaster> serviceClass)
-                    {
-                        //do nothing
-                        return false;
-                    }
-
-                    @Override
-                    public void preInstallServices(List<Class<?>> foundServiceClasses)
-                    {
-                        //do nothing
-                    }
                 });
 
         List<Class<? extends StartupEventBroadcaster>> filter = broadcasterFilter.get(classLoader);

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProvider.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProvider.java?rev=1150097&r1=1150096&r2=1150097&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProvider.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProvider.java
Sat Jul 23 12:42:17 2011
@@ -18,31 +18,25 @@
  */
 package org.apache.myfaces.extensions.cdi.core.impl.provider;
 
-import org.apache.myfaces.extensions.cdi.core.api.UnhandledException;
 import org.apache.myfaces.extensions.cdi.core.api.provider.ServiceProviderContext;
 import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.provider.spi.EditableServiceProviderContext;
 
 import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Typed;
 import javax.enterprise.inject.spi.AfterDeploymentValidation;
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.lang.reflect.Constructor;
-import java.net.URL;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import org.apache.myfaces.extensions.cdi.core.api.provider.ServiceProvider;
 
 /**
  * @author Gerhard Petracek
  */
-public class DefaultServiceProvider<T> extends ServiceProvider<T>
+@Typed()
+@SuppressWarnings({"unchecked"})
+public class DefaultServiceProvider<T> extends SimpleServiceProvider<T>
 {
-    protected List<Class<?>> foundServiceClasses = new ArrayList<Class<?>>();
     private static Map<ClassLoader, Boolean> deploymentFinished = new ConcurrentHashMap<ClassLoader,
Boolean>();
     private static Map<Class<?>, List<Class<?>>> serviceCache = new
ConcurrentHashMap<Class<?>, List<Class<?>>>();
 
@@ -64,193 +58,63 @@ public class DefaultServiceProvider<T> e
     {
         List<Class<?>> result = serviceCache.get(this.serviceType);
 
-        if(result == null)
+        if(result != null)
         {
-            result = resolveServiceImplementations();
-        }
-
-        if(result == null)
-        {
-            return Collections.emptyList();
-        }
-
-        List<T> foundServices = new ArrayList<T>();
-
-        for(Class<?> serviceClass : result)
-        {
-            foundServices.add(createInstance(serviceClass));
-        }
-
-        return foundServices;
-    }
-
-    private List<Class<?>> resolveServiceImplementations()
-    {
-        for (URL configFile : getConfigFileList())
-        {
-            loadConfiguredServices(configFile);
-        }
-
-        this.serviceProviderContext.preInstallServices(this.foundServiceClasses);
-
-        serviceCache.put(this.serviceType, this.foundServiceClasses);
-        return this.foundServiceClasses;
-    }
-
-    protected void activateInjectionSupport(@Observes AfterDeploymentValidation afterDeploymentValidation)
-    {
-        deploymentFinished.put(ClassUtils.getClassLoader(null), Boolean.TRUE);
-    }
+            List<T> foundServices = new ArrayList<T>();
 
-    private List<URL> getConfigFileList()
-    {
-        List<URL> serviceFiles = new ArrayList<URL>();
-
-        try
-        {
-            Enumeration<URL> serviceFileEnumerator =
-                    this.serviceProviderContext.getClassLoader().getResources(getConfigFileLocation());
-
-            while (serviceFileEnumerator.hasMoreElements())
+            for(Class<?> serviceClass : result)
             {
-                serviceFiles.add(serviceFileEnumerator.nextElement());
+                foundServices.add(createInstance(serviceClass));
             }
+
+            return foundServices;
         }
-        catch (Exception e)
-        {
-            throw new UnhandledException(
-                    "Failed to load " + this.serviceType.getName() + " configured in " +
getConfigFileLocation(), e);
-        }
-        return serviceFiles;
-    }
 
-    private String getConfigFileLocation()
-    {
-        return SERVICE_CONFIG + this.serviceType.getName();
+        return super.loadServiceImplementations();
     }
 
-    private void loadConfiguredServices(URL serviceFile)
+    @Override
+    protected List<Class<?>> resolveServiceImplementations()
     {
-        InputStream inputStream = null;
+        super.resolveServiceImplementations();
 
-        try
-        {
-            String serviceClassName;
-            inputStream = serviceFile.openStream();
-            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,
FILE_ENCODING));
-
-            while ((serviceClassName = bufferedReader.readLine()) != null)
-            {
-                serviceClassName = extractConfiguredServiceClassName(serviceClassName);
-                if (!"".equals(serviceClassName))
-                {
-                    loadService(serviceClassName);
-                }
-            }
-        }
-        catch (Exception e)
+        if(this.serviceProviderContext instanceof EditableServiceProviderContext)
         {
-            throw new UnhandledException("Failed to process service-config: " + serviceFile,
e);
+            ((EditableServiceProviderContext)this.serviceProviderContext)
+                    .preInstallServices(this.foundServiceClasses);
         }
-        finally
-        {
-            if (inputStream != null)
-            {
-                try
-                {
-                    inputStream.close();
-                }
-                catch (Exception e)
-                {
-                    throw new UnhandledException("Failed to close " + serviceFile, e);
-                }
-            }
-        }
-    }
-
-    private String extractConfiguredServiceClassName(String currentConfigLine)
-    {
-        int startOfComment = currentConfigLine.indexOf('#');
 
-        if (startOfComment > -1)
-        {
-            currentConfigLine = currentConfigLine.substring(0, startOfComment);
-        }
-        return currentConfigLine.trim();
+        serviceCache.put(this.serviceType, this.foundServiceClasses);
+        return this.foundServiceClasses;
     }
 
-    private void loadService(String serviceClassName)
+    protected boolean isServiceSupported(Class<T> serviceClass)
     {
-        Class<T> serviceClass = (Class<T>) loadClass(serviceClassName);
-
-        if (serviceClass != null && !this.foundServiceClasses.contains(serviceClass)
&&
-                !this.serviceProviderContext.filterService(serviceClass))
-        {
-            this.foundServiceClasses.add(serviceClass);
-        }
-        else if(serviceClass == null)
+        //noinspection SimplifiableIfStatement
+        if(this.serviceProviderContext instanceof EditableServiceProviderContext)
         {
-            throw new IllegalStateException(serviceClassName + " couldn't be loaded. " +
-                    "Please ensure that this class is in the classpath or remove the entry
from "
-                    + getConfigFileLocation() + ". Or mark it as optional.");
+            return !((EditableServiceProviderContext)this.serviceProviderContext)
+                    .filterService(serviceClass);
         }
+        return true;
     }
 
-    private Class<? extends T> loadClass(String serviceClassName)
+    @Override
+    protected T createInstance(Class<?> serviceClass)
     {
-        Class<?> targetClass = ClassUtils.tryToLoadClassForName(serviceClassName);
+        T instance = super.createInstance(serviceClass);
 
-        if(targetClass == null)
+        if(this.serviceProviderContext instanceof EditableServiceProviderContext)
         {
-            targetClass = loadClassForName(serviceClassName, this.serviceProviderContext.getClassLoader());
-
-            if(targetClass == null)
-            {
-                targetClass = loadClassForName(serviceClassName, ClassUtils.getClassLoader(null));
-
-                if(targetClass == null)
-                {
-                    return null;
-                }
-            }
+            return ((EditableServiceProviderContext<T>)this.serviceProviderContext).postConstruct(instance);
         }
 
-        return targetClass.asSubclass(this.serviceType);
+        return instance;
     }
 
-    private static Class<?> loadClassForName(String serviceClassName, ClassLoader classLoader)
-    {
-        if(classLoader == null)
-        {
-            return null;
-        }
-
-        try
-        {
-            return classLoader.loadClass(serviceClassName);
-        }
-        catch (Exception e)
-        {
-            return loadClassForName(serviceClassName, classLoader.getParent());
-        }
-    }
-
-    private T createInstance(Class<?> serviceClass)
+    protected void activateInjectionSupport(@Observes AfterDeploymentValidation afterDeploymentValidation)
     {
-        try
-        {
-            Constructor<?> constructor = serviceClass.getDeclaredConstructor();
-            constructor.setAccessible(true);
-            T instance = (T)constructor.newInstance();
-
-            this.serviceProviderContext.postConstruct(instance);
-
-            return instance;
-        }
-        catch (Exception e)
-        {
-            return null;
-        }
+        deploymentFinished.put(ClassUtils.getClassLoader(null), Boolean.TRUE);
     }
 
     //deactivated by default - register this class as cdi extension to activate it
@@ -264,13 +128,4 @@ public class DefaultServiceProvider<T> e
         foundServiceClasses.clear();
         serviceCache.clear();
     }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString()
-    {
-        return "Config file: " + getConfigFileLocation();
-    }
 }

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProviderContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProviderContext.java?rev=1150097&r1=1150096&r2=1150097&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProviderContext.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/DefaultServiceProviderContext.java
Sat Jul 23 12:42:17 2011
@@ -20,6 +20,7 @@ package org.apache.myfaces.extensions.cd
 
 import org.apache.myfaces.extensions.cdi.core.api.tools.InvocationOrderComparator;
 import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.provider.spi.EditableServiceProviderContext;
 import org.apache.myfaces.extensions.cdi.core.impl.util.ActivationUtils;
 import org.apache.myfaces.extensions.cdi.core.impl.util.CodiUtils;
 
@@ -33,7 +34,8 @@ import javax.enterprise.inject.Typed;
  * @author Gerhard Petracek
  */
 @Typed
-public class DefaultServiceProviderContext<T> extends ServiceProviderContext<T>
+public class DefaultServiceProviderContext<T>
+        extends ServiceProviderContext<T> implements EditableServiceProviderContext<T>
 {
     protected boolean deploymentFinished = false;
 
@@ -49,7 +51,6 @@ public class DefaultServiceProviderConte
     /**
      * {@inheritDoc}
      */
-    @Override
     public T postConstruct(T instance)
     {
         if(this.deploymentFinished)
@@ -63,7 +64,6 @@ public class DefaultServiceProviderConte
     /**
      * {@inheritDoc}
      */
-    @Override
     public boolean filterService(Class<T> serviceClass)
     {
         return !ActivationUtils.isActivated(serviceClass, SystemPropertyExpressionInterpreter.class);
@@ -73,7 +73,6 @@ public class DefaultServiceProviderConte
      * {@inheritDoc}
      */
     //TODO remove all usages of InvocationOrderComparator which aren't needed any more
-    @Override
     public void preInstallServices(List<Class<?>> foundServiceClasses)
     {
         if(foundServiceClasses != null && !foundServiceClasses.isEmpty())

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProvider.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProvider.java?rev=1150097&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProvider.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProvider.java
Sat Jul 23 12:42:17 2011
@@ -0,0 +1,243 @@
+/*
+ * 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.myfaces.extensions.cdi.core.impl.provider;
+
+import org.apache.myfaces.extensions.cdi.core.api.UnhandledException;
+import org.apache.myfaces.extensions.cdi.core.api.provider.ServiceProvider;
+import org.apache.myfaces.extensions.cdi.core.api.provider.ServiceProviderContext;
+import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+
+import javax.enterprise.inject.Typed;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Constructor;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+public class SimpleServiceProvider<T> extends ServiceProvider<T>
+{
+    protected List<Class<?>> foundServiceClasses = new ArrayList<Class<?>>();
+
+    protected SimpleServiceProvider(Class<T> serviceType, ServiceProviderContext serviceProviderContext)
+    {
+        super(serviceType, serviceProviderContext);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected List<T> loadServiceImplementations()
+    {
+        List<Class<?>> result = resolveServiceImplementations();
+
+        if(result == null)
+        {
+            return Collections.emptyList();
+        }
+
+        List<T> foundServices = new ArrayList<T>();
+
+        for(Class<?> serviceClass : result)
+        {
+            foundServices.add(createInstance(serviceClass));
+        }
+
+        return foundServices;
+    }
+
+    protected List<Class<?>> resolveServiceImplementations()
+    {
+        for (URL configFile : getConfigFileList())
+        {
+            loadConfiguredServices(configFile);
+        }
+
+        return this.foundServiceClasses;
+    }
+
+    protected List<URL> getConfigFileList()
+    {
+        List<URL> serviceFiles = new ArrayList<URL>();
+
+        try
+        {
+            Enumeration<URL> serviceFileEnumerator =
+                    this.serviceProviderContext.getClassLoader().getResources(getConfigFileLocation());
+
+            while (serviceFileEnumerator.hasMoreElements())
+            {
+                serviceFiles.add(serviceFileEnumerator.nextElement());
+            }
+        }
+        catch (Exception e)
+        {
+            throw new UnhandledException(
+                    "Failed to load " + this.serviceType.getName() + " configured in " +
getConfigFileLocation(), e);
+        }
+        return serviceFiles;
+    }
+
+    protected String getConfigFileLocation()
+    {
+        return SERVICE_CONFIG + this.serviceType.getName();
+    }
+
+    protected void loadConfiguredServices(URL serviceFile)
+    {
+        InputStream inputStream = null;
+
+        try
+        {
+            String serviceClassName;
+            inputStream = serviceFile.openStream();
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,
FILE_ENCODING));
+
+            while ((serviceClassName = bufferedReader.readLine()) != null)
+            {
+                serviceClassName = extractConfiguredServiceClassName(serviceClassName);
+                if (!"".equals(serviceClassName))
+                {
+                    loadService(serviceClassName);
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            throw new UnhandledException("Failed to process service-config: " + serviceFile,
e);
+        }
+        finally
+        {
+            if (inputStream != null)
+            {
+                try
+                {
+                    inputStream.close();
+                }
+                catch (Exception e)
+                {
+                    throw new UnhandledException("Failed to close " + serviceFile, e);
+                }
+            }
+        }
+    }
+
+    protected String extractConfiguredServiceClassName(String currentConfigLine)
+    {
+        int startOfComment = currentConfigLine.indexOf('#');
+
+        if (startOfComment > -1)
+        {
+            currentConfigLine = currentConfigLine.substring(0, startOfComment);
+        }
+        return currentConfigLine.trim();
+    }
+
+    protected void loadService(String serviceClassName)
+    {
+        Class<T> serviceClass = (Class<T>) loadClass(serviceClassName);
+
+        if (serviceClass != null &&
+                !this.foundServiceClasses.contains(serviceClass) &&
+                isServiceSupported(serviceClass))
+        {
+            this.foundServiceClasses.add(serviceClass);
+        }
+        else if(serviceClass == null)
+        {
+            throw new IllegalStateException(serviceClassName + " couldn't be loaded. " +
+                    "Please ensure that this class is in the classpath or remove the entry
from "
+                    + getConfigFileLocation() + ". Or mark it as optional.");
+        }
+    }
+
+    protected boolean isServiceSupported(Class<T> serviceClass)
+    {
+        return true; //TODO
+    }
+
+    protected Class<? extends T> loadClass(String serviceClassName)
+    {
+        Class<?> targetClass = ClassUtils.tryToLoadClassForName(serviceClassName);
+
+        if(targetClass == null)
+        {
+            targetClass = loadClassForName(serviceClassName, this.serviceProviderContext.getClassLoader());
+
+            if(targetClass == null)
+            {
+                targetClass = loadClassForName(serviceClassName, ClassUtils.getClassLoader(null));
+
+                if(targetClass == null)
+                {
+                    return null;
+                }
+            }
+        }
+
+        return targetClass.asSubclass(this.serviceType);
+    }
+
+    protected static Class<?> loadClassForName(String serviceClassName, ClassLoader
classLoader)
+    {
+        if(classLoader == null)
+        {
+            return null;
+        }
+
+        try
+        {
+            return classLoader.loadClass(serviceClassName);
+        }
+        catch (Exception e)
+        {
+            return loadClassForName(serviceClassName, classLoader.getParent());
+        }
+    }
+
+    protected T createInstance(Class<?> serviceClass)
+    {
+        try
+        {
+            Constructor<?> constructor = serviceClass.getDeclaredConstructor();
+            constructor.setAccessible(true);
+            return (T)constructor.newInstance();
+        }
+        catch (Exception e)
+        {
+            return null;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString()
+    {
+        return "Config file: " + getConfigFileLocation();
+    }
+}

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProviderContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProviderContext.java?rev=1150097&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProviderContext.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/SimpleServiceProviderContext.java
Sat Jul 23 12:42:17 2011
@@ -0,0 +1,39 @@
+/*
+ * 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.myfaces.extensions.cdi.core.impl.provider;
+
+import org.apache.myfaces.extensions.cdi.core.api.provider.ServiceProviderContext;
+import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+
+import javax.enterprise.inject.Typed;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+public class SimpleServiceProviderContext<T> extends ServiceProviderContext<T>
+{
+    /**
+     * {@inheritDoc}
+     */
+    public ClassLoader getClassLoader()
+    {
+        return ClassUtils.getClassLoader(null);
+    }
+}

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/EditableServiceProviderContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/EditableServiceProviderContext.java?rev=1150097&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/EditableServiceProviderContext.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/provider/spi/EditableServiceProviderContext.java
Sat Jul 23 12:42:17 2011
@@ -0,0 +1,36 @@
+/*
+ * 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.myfaces.extensions.cdi.core.impl.provider.spi;
+
+import javax.enterprise.inject.Typed;
+import java.util.List;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+//TODO
+public interface EditableServiceProviderContext<T>
+{
+    T postConstruct(T instance);
+
+    boolean filterService(Class<T> serviceClass);
+
+    void preInstallServices(List<Class<?>> foundServiceClasses);
+}

Modified: myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/serviceloader/ServiceProviderTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/serviceloader/ServiceProviderTest.java?rev=1150097&r1=1150096&r2=1150097&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/serviceloader/ServiceProviderTest.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/serviceloader/ServiceProviderTest.java
Sat Jul 23 12:42:17 2011
@@ -28,6 +28,8 @@ import org.apache.myfaces.extensions.cdi
 import org.apache.myfaces.extensions.cdi.core.impl.config.PropertyFileResolver;
 import org.apache.myfaces.extensions.cdi.core.impl.config.ServiceLoaderResolver;
 import org.apache.myfaces.extensions.cdi.core.impl.config.SystemPropertyResolver;
+import org.apache.myfaces.extensions.cdi.core.impl.provider.SimpleServiceProvider;
+import org.apache.myfaces.extensions.cdi.core.impl.provider.SimpleServiceProviderContext;
 import org.apache.myfaces.extensions.cdi.core.test.impl.config.TestConfiguredValueResolver;
 import org.apache.myfaces.extensions.cdi.core.test.impl.config.TestInterface;
 import org.testng.Assert;
@@ -40,7 +42,8 @@ import java.util.List;
 public class ServiceProviderTest
 {
     @Test
-    public void testExtensions()
+    @SuppressWarnings({"unchecked"})
+    public void testExtensionsWithDefaultServiceProvider()
     {
         List<Extension> extensionList = ServiceProvider.loadServices(Extension.class);
         Assert.assertEquals(extensionList.size(), 3);
@@ -62,6 +65,59 @@ public class ServiceProviderTest
     }
 
     @Test
+    @SuppressWarnings({"unchecked"})
+    public void testExtensionsWithDefaultServiceProviderWithSimple()
+    {
+        List<Extension> extensionList =
+                ServiceProvider.loadServices(Extension.class, new SimpleServiceProviderContext<Extension>());
+        Assert.assertEquals(extensionList.size(), 3);
+
+        Iterator<Extension> iterator = extensionList.iterator();
+
+        Extension extension;
+        while (iterator.hasNext())
+        {
+            extension = iterator.next();
+            Assert.assertTrue(extension instanceof ActivationExtension ||
+                extension instanceof BeanManagerProvider ||
+                extension instanceof CodiDeactivatorExtension);
+
+            iterator.remove();
+        }
+
+        Assert.assertEquals(extensionList.size(), 0);
+    }
+
+    @Test
+    @SuppressWarnings({"unchecked"})
+    public void testExtensionsWithSimpleServiceProvider()
+    {
+        List<Extension> extensionList = new SimpleServiceProvider(Extension.class,
new SimpleServiceProviderContext<Extension>()) {
+            @Override
+            public List<Extension> loadServiceImplementations()
+            {
+                return super.loadServiceImplementations();
+            }
+        }.loadServiceImplementations();
+        Assert.assertEquals(extensionList.size(), 3);
+
+        Iterator<Extension> iterator = extensionList.iterator();
+
+        Extension extension;
+        while (iterator.hasNext())
+        {
+            extension = iterator.next();
+            Assert.assertTrue(extension instanceof ActivationExtension ||
+                extension instanceof BeanManagerProvider ||
+                extension instanceof CodiDeactivatorExtension);
+
+            iterator.remove();
+        }
+
+        Assert.assertEquals(extensionList.size(), 0);
+    }
+
+    @Test
     public void testDeactivatedImplementationConditionalExtensions()
     {
         System.setProperty("env", "test");
@@ -101,6 +157,10 @@ public class ServiceProviderTest
         Assert.assertNotNull(ClassUtils.tryToLoadClassForName(
                 "org.apache.myfaces.extensions.cdi.core.impl.provider.DefaultServiceProvider"));
         Assert.assertNotNull(ClassUtils.tryToLoadClassForName(
+                "org.apache.myfaces.extensions.cdi.core.impl.provider.SimpleServiceProvider"));
+        Assert.assertNotNull(ClassUtils.tryToLoadClassForName(
                 "org.apache.myfaces.extensions.cdi.core.impl.provider.DefaultServiceProviderContext"));
+        Assert.assertNotNull(ClassUtils.tryToLoadClassForName(
+                "org.apache.myfaces.extensions.cdi.core.impl.provider.SimpleServiceProviderContext"));
     }
 }



Mime
View raw message