[weld-commits] Weld SVN: r6306 - in java-se/trunk/src: main/java/org/jboss/weld/environment/se/discovery and 2 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Tue May 25 13:52:02 EDT 2010


Author: pete.muir at jboss.org
Date: 2010-05-25 13:52:01 -0400 (Tue, 25 May 2010)
New Revision: 6306

Added:
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEDeployment.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLScanner.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/WeldSEResourceLoader.java
Removed:
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/AbstractScanner.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/NewSEWeldDeployment.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/handlers/
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/AbstractURLHandler.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLHandler.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/util/
Modified:
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldSEBeanRegistrant.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/Scanner.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEBeanDeploymentArchive.java
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/FileSystemURLHandler.java
   java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
Log:
expose Scanner directly for OSGi support

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -16,44 +16,44 @@
  */
 package org.jboss.weld.environment.se;
 
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+
 import javax.annotation.PostConstruct;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.UnsatisfiedResolutionException;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+
 import org.jboss.weld.bootstrap.api.Bootstrap;
 import org.jboss.weld.bootstrap.api.Environments;
-import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
-import org.jboss.weld.bootstrap.spi.Deployment;
 import org.jboss.weld.context.api.BeanStore;
 import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
-import org.jboss.weld.environment.se.beans.InstanceManager;
-import org.jboss.weld.environment.se.discovery.NewSEWeldDeployment;
-import org.jboss.weld.environment.se.discovery.WeldSEBeanDeploymentArchive;
-import org.jboss.weld.environment.se.discovery.URLScanner;
-import org.jboss.weld.environment.se.util.WeldManagerUtils;
-import org.jboss.weld.manager.api.WeldManager;
-import org.jboss.weld.resources.DefaultResourceLoader;
+import org.jboss.weld.environment.se.discovery.WeldSEDeployment;
+import org.jboss.weld.environment.se.discovery.url.URLScanner;
+import org.jboss.weld.environment.se.discovery.url.WeldSEResourceLoader;
 import org.jboss.weld.resources.spi.ResourceLoader;
 
 /**
- * An alternative means of booting WeldContainer form an arbitrary main method within an
- * SE application, <em>without</em> using the built-in ContainerInitialized event.
- * Typical usage of this API looks like this:
- * <code>
+ * An alternative means of booting WeldContainer form an arbitrary main method
+ * within an SE application, <em>without</em> using the built-in
+ * ContainerInitialized event. Typical usage of this API looks like this: <code>
  * WeldContainer weld = new Weld().initialize();
  * weld.instance().select(Foo.class).get();
  * weld.event().select(Bar.class).fire(new Bar());
  * weld.shutdown();
  * </code>
- *
+ * 
  * @author Peter Royle
  */
 public class Weld
 {
 
+   protected static final String[] RESOURCES = { "META-INF/beans.xml" };
+
    private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";
-   private WeldManager manager;
 
-   public Weld()
-   {
-   }
+   private ShutdownManager shutdownManager;
 
    /**
     * Boots Weld and creates and returns a WeldContainer instance, through which
@@ -64,81 +64,76 @@
    {
 
       BeanStore applicationBeanStore = new ConcurrentHashMapBeanStore();
-      Deployment deployment = createDeployment();
+      WeldSEDeployment deployment = createDeployment();
 
       Bootstrap bootstrap = null;
       try
       {
          bootstrap = (Bootstrap) deployment.getServices().get(ResourceLoader.class).classForName(BOOTSTRAP_IMPL_CLASS_NAME).newInstance();
-      } catch (InstantiationException ex)
+      }
+      catch (InstantiationException ex)
       {
          throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", ex);
-      } catch (IllegalAccessException ex)
+      }
+      catch (IllegalAccessException ex)
       {
          throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", ex);
       }
 
-      BeanDeploymentArchive discovery = discoverBeansAndResources(deployment);
-      // transfer discovered classes and resources to the deployment in a single BeanDeploymentArchive
-      deployment.getBeanDeploymentArchives().add(discovery);
-
+      
+      // Kick off the scan
+      deployment.getScanner().scan(deployment.getServices().get(ResourceLoader.class));
+      
+      // Set up the container
       bootstrap.startContainer(Environments.SE, deployment, applicationBeanStore);
-      final BeanDeploymentArchive mainBeanDepArch = deployment.getBeanDeploymentArchives().iterator().next();
-      this.manager = bootstrap.getManager(mainBeanDepArch);
+      
+      // Start the container
       bootstrap.startInitialization();
       bootstrap.deployBeans();
-      WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).setBootstrap(bootstrap);
+      getInstanceByType(bootstrap.getManager(deployment.loadBeanDeploymentArchive(ShutdownManager.class)), ShutdownManager.class).setBootstrap(bootstrap);
       bootstrap.validateBeans();
       bootstrap.endInitialization();
 
-      InstanceManager instanceManager = WeldManagerUtils.getInstanceByType(manager, InstanceManager.class);
+      // Set up the ShutdownManager for later
+      this.shutdownManager = getInstanceByType(bootstrap.getManager(deployment.loadBeanDeploymentArchive(ShutdownManager.class)), ShutdownManager.class);
 
-      return new WeldContainer(instanceManager, manager);
-
+      return getInstanceByType(bootstrap.getManager(deployment.loadBeanDeploymentArchive(WeldContainer.class)), WeldContainer.class);
    }
 
-   /*
-    * Users can subclass and override this method to customise the classes and
-    * resources that Weld finds when it boots up. 
-    */
-   protected BeanDeploymentArchive discoverBeansAndResources(Deployment deployment)
-   {
-      WeldSEBeanDeploymentArchive discovery = new WeldSEBeanDeploymentArchive("weld-se-main-archive");
-      final ResourceLoader resourceLoader = deployment.getServices().get(ResourceLoader.class);
-      URLScanner scanner = new URLScanner(resourceLoader, discovery);
-      scanner.scanResources(new String[]
-              {
-                 "META-INF/beans.xml"
-              });
-      return discovery;
-   }
-
    /**
     * Users can subclass and override this method to customise the deployment
-    * before weld boots up. For example, to add a custom ResourceLoader, you would
-    * subclass Weld like so:
-    * <code>
+    * before weld boots up. For example, to add a custom ResourceLoader, you
+    * would subclass Weld like so: <code>
     * public class MyWeld extends Weld {
-    *    @Override
-    *    protected void createDeployment() {
-    *       Deployment myDeployment = new MyDeployment();
-    *       deployment.getServices().add(ResourceLoader.class, new OSGIResourceLoader());
-    *    }
-    * }
-    * </code>
+    * 
+    * @Override protected WeldSEDeployment createDeployment() { WeldSEDeployment
+    *           myDeployment = super.createDeployment();
+    *           deployment.getServices().add(ResourceLoader.class, new
+    *           OSGIResourceLoader()); } } </code>
     */
-   private Deployment createDeployment()
+   protected WeldSEDeployment createDeployment()
    {
-      NewSEWeldDeployment deployment = new NewSEWeldDeployment();
-      deployment.getServices().add(ResourceLoader.class, new DefaultResourceLoader());
+      WeldSEDeployment deployment = new WeldSEDeployment(new URLScanner(RESOURCES));
+      deployment.getServices().add(ResourceLoader.class, new WeldSEResourceLoader());
       return deployment;
    }
+   
+   protected <T> T getInstanceByType(BeanManager manager, Class<T> type, Annotation... bindings)
+   {
+      final Bean<?> bean = manager.resolve(manager.getBeans(type));
+      if (bean == null)
+      {
+         throw new UnsatisfiedResolutionException("Unable to resolve a bean for " + type + " with bindings " + Arrays.asList(bindings));
+      }
+      CreationalContext<?> cc = manager.createCreationalContext(bean);
+      return type.cast(manager.getReference(bean, type, cc));
+   }
 
    /**
     * Shuts down Weld.
     */
    public void shutdown()
    {
-      WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).shutdown();
+      shutdownManager.shutdown();
    }
 }

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -19,6 +19,8 @@
 import javax.enterprise.event.Event;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+
 import org.jboss.weld.environment.se.beans.InstanceManager;
 
 /**
@@ -39,6 +41,7 @@
    private final InstanceManager instanceManager;
    private final BeanManager beanManager;
 
+   @Inject
    protected WeldContainer(InstanceManager instanceManager, BeanManager beanManager)
    {
       this.instanceManager = instanceManager;

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldSEBeanRegistrant.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldSEBeanRegistrant.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldSEBeanRegistrant.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -16,17 +16,18 @@
  */
 package org.jboss.weld.environment.se;
 
-import org.jboss.weld.environment.se.threading.RunnableDecorator;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
+
 import org.jboss.weld.context.api.BeanStore;
 import org.jboss.weld.context.beanstore.HashMapBeanStore;
 import org.jboss.weld.environment.se.beans.InstanceManager;
 import org.jboss.weld.environment.se.beans.ParametersFactory;
 import org.jboss.weld.environment.se.contexts.ThreadContext;
+import org.jboss.weld.environment.se.threading.RunnableDecorator;
 
 /**
  * Explicitly registers all of the 'built-in' Java SE related beans and contexts.
@@ -42,8 +43,8 @@
       event.addAnnotatedType(manager.createAnnotatedType(ShutdownManager.class));
       event.addAnnotatedType(manager.createAnnotatedType(ParametersFactory.class));
       event.addAnnotatedType(manager.createAnnotatedType(InstanceManager.class));
-      event.addAnnotatedType(manager.createAnnotatedType(Weld.class));
       event.addAnnotatedType(manager.createAnnotatedType(RunnableDecorator.class));
+      event.addAnnotatedType(manager.createAnnotatedType(WeldContainer.class));
    }
 
    public void registerWeldSEContexts(@Observes AfterBeanDiscovery event)

Deleted: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/AbstractScanner.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/AbstractScanner.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/AbstractScanner.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -1,51 +0,0 @@
-/**
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
- * contributors by the @authors tag. See the copyright.txt in the
- * distribution for a full listing of individual contributors.
- *
- * Licensed 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.jboss.weld.environment.se.discovery;
-
-import org.jboss.weld.resources.spi.ResourceLoader;
-
-/**
- *
- * @author Peter Royle
- */
-public abstract class AbstractScanner implements Scanner
-{
-
-   private final ResourceLoader resourceLoader;
-   private final WeldSEBeanDeploymentArchive weldDiscovery;
-
-   public AbstractScanner(ResourceLoader resourceLoader, WeldSEBeanDeploymentArchive webBeanDiscovery)
-   {
-      this.resourceLoader = resourceLoader;
-      this.weldDiscovery = webBeanDiscovery;
-   }
-
-
-   public ResourceLoader getResourceLoader()
-   {
-      return resourceLoader;
-   }
-
-   /**
-    * @return the webBeanDiscovery
-    */
-   public WeldSEBeanDeploymentArchive getWebBeanDiscovery()
-   {
-      return weldDiscovery;
-   }
-
-}

Deleted: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/NewSEWeldDeployment.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/NewSEWeldDeployment.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/NewSEWeldDeployment.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -1,68 +0,0 @@
-/**
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
- * contributors by the @authors tag. See the copyright.txt in the
- * distribution for a full listing of individual contributors.
- *
- * Licensed 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.jboss.weld.environment.se.discovery;
-
-import java.util.ArrayList;
-import java.util.List;
-import org.jboss.weld.bootstrap.api.ServiceRegistry;
-import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
-import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
-import org.jboss.weld.bootstrap.spi.Deployment;
-
-/**
- * Weld Deployment for Java SE environment.
- * 
- * @author Peter Royle
- */
-public class NewSEWeldDeployment implements Deployment
-{
-
-   final List<BeanDeploymentArchive> beanDeploymentArchives = new ArrayList<BeanDeploymentArchive>();
-   final ServiceRegistry serviceRegistry = new SimpleServiceRegistry();
-
-   public NewSEWeldDeployment()
-   {
-   }
-
-   /**
-    * {@inheritDoc}
-    * 
-    * @return A collection containing the singular logical BeanDeploymentArchive
-    *         consisting of all Bean classes and beans.xml descriptors in the
-    *         current classpath.
-    */
-   public List<BeanDeploymentArchive> getBeanDeploymentArchives()
-   {
-      return beanDeploymentArchives;
-   }
-
-   /**
-    * {@inheritDoc}
-    */
-   public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass)
-   {
-      // TODO (PR) (WELDSE-26): This is just a hack. What do we really need to do here?
-      return beanDeploymentArchives.get(0);
-   }
-
-   public ServiceRegistry getServices()
-   {
-      // TODO (PR) (WELDSE-26): Do I need to aggregate all services from all bean archives?
-      return serviceRegistry;
-   }
-
-}

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/Scanner.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/Scanner.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/Scanner.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -16,12 +16,15 @@
  */
 package org.jboss.weld.environment.se.discovery;
 
+import java.util.List;
 
+import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
+import org.jboss.weld.bootstrap.spi.Deployment;
+import org.jboss.weld.resources.spi.ResourceLoader;
+
 /**
- * The Scanner is used to find resources to be processed by Seam
+ * The Scanner is used to find resources to be processed by Weld SE
  * 
- * The processing is done by {@link DeploymentHandler}s
- * 
  * @author Pete Muir
  * 
  */
@@ -29,10 +32,15 @@
 {
 
    /**
-    * Scan for structures which contain any of the given resources in their root
-    * 
-    * @param resources The resources to scan for
+    * Scan for structures which should be deployed by Weld. For example,
+    * scanning for META-INF/beans.xml should return a graph of
+    * {@link BeanDeploymentArchive}s, representing their accessibility. For more
+    * on deployment structures see {@link Deployment}. 
     */
-   public void scanResources(String[] resources);
+   public void scan(ResourceLoader resourceLoader);   
 
+   public BeanDeploymentArchive getBeanDeploymentArchive(Class<?> clazz);
+
+   public List<BeanDeploymentArchive> getBeanDeploymentArchives();
+
 }

Deleted: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -1,137 +0,0 @@
-/**
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
- * contributors by the @authors tag. See the copyright.txt in the
- * distribution for a full listing of individual contributors.
- *
- * Licensed 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.jboss.weld.environment.se.discovery;
-
-import java.io.File;
-import java.io.UnsupportedEncodingException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.net.URLDecoder;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.jboss.weld.environment.se.discovery.handlers.FileSystemURLHandler;
-import org.jboss.weld.environment.se.discovery.handlers.URLHandler;
-import org.jboss.weld.environment.se.exceptions.ClasspathScanningException;
-import org.jboss.weld.resources.spi.ResourceLoader;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Multimap;
-
-/**
- * Implementation of {@link Scanner} which can scan a {@link URLClassLoader}
- * 
- * @author Thomas Heute
- * @author Gavin King
- * @author Norman Richards
- * @author Pete Muir
- * @author Peter Royle
- * 
- */
-public class URLScanner extends AbstractScanner
-{
-
-   private static final String FILE = "file";
-   private static final String JAR = "jar";
-   private final Map<String, URLHandler> urlHandlers = new HashMap<String, URLHandler>();
-   private static final Logger log = LoggerFactory.getLogger(URLScanner.class);
-
-   public URLScanner(ResourceLoader resourceLoader, WeldSEBeanDeploymentArchive weldDiscovery)
-   {
-      super(resourceLoader, weldDiscovery);
-      URLHandler fileSysHandler = new FileSystemURLHandler(resourceLoader, weldDiscovery);
-      urlHandlers.put(FILE, fileSysHandler);
-      urlHandlers.put(JAR, fileSysHandler);
-   }
-
-   public void setURLHandler(String type, URLHandler handler)
-   {
-      urlHandlers.put(type, handler);
-   }
-
-   public void scanResources(String[] resources)
-   {
-      Multimap<String, String> paths = HashMultimap.create();
-      for (String resourceName : resources)
-      {
-         // grab all the URLs for this resource
-         Collection<URL> urlEnum = getResourceLoader().getResources(resourceName);
-         for (URL url : urlEnum)
-         {
-
-            String urlPath = url.toExternalForm();
-
-            // determin resource type (eg: jar, file, bundle)
-            String urlType = "file";
-            int colonIndex = urlPath.indexOf(":");
-            if (colonIndex != -1)
-            {
-               urlType = urlPath.substring(0, colonIndex);
-            }
-
-            // Extra built-in support for simple file-based resources
-            if ("file".equals(urlType) || "jar".equals(urlType))
-            {
-               // switch to using getPath() instead of toExternalForm()
-               urlPath = url.getPath();
-
-               if (urlPath.indexOf('!') > 0)
-               {
-                  urlPath = urlPath.substring(0, urlPath.indexOf('!'));
-               } else
-               {
-                  // hack for /META-INF/beans.xml
-                  File dirOrArchive = new File(urlPath);
-                  if ((resourceName != null) && (resourceName.lastIndexOf('/') > 0))
-                  {
-                     dirOrArchive = dirOrArchive.getParentFile();
-                  }
-                  urlPath = dirOrArchive.getParent();
-               }
-            }
-
-            try
-            {
-               urlPath = URLDecoder.decode(urlPath, "UTF-8");
-            } catch (UnsupportedEncodingException ex)
-            {
-               throw new ClasspathScanningException("Error decoding URL using UTF-8");
-            }
-
-            log.debug("URL Type: " + urlType);
-
-            paths.put(urlType, urlPath);
-
-         }
-      }
-      for (String urlType : paths.keySet())
-      {
-         Collection<String> urlPaths = paths.get(urlType);
-         URLHandler handler = urlHandlers.get(urlType);
-         if (handler == null)
-         {
-            throw new ClasspathScanningException("No handler defined for URL type: " + urlType);
-         } else
-         {
-            handler.handle(urlPaths);
-         }
-      }
-   }
-}

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEBeanDeploymentArchive.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEBeanDeploymentArchive.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEBeanDeploymentArchive.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -22,6 +22,7 @@
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+
 import org.jboss.weld.bootstrap.api.ServiceRegistry;
 import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
 import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
@@ -42,7 +43,7 @@
    private final Collection<URL> weldUrls;
    private final ServiceRegistry serviceRegistry;
    private final List<BeanDeploymentArchive> beanDeploymentArchives;
-   private String id;
+   private final String id;
 
    public WeldSEBeanDeploymentArchive(String id)
    {
@@ -87,14 +88,6 @@
       return this.id;
    }
 
-   /**
-    * @param id the id to set
-    */
-   public void setId(String id)
-   {
-      this.id = id;
-   }
-
    public ServiceRegistry getServices()
    {
       return this.serviceRegistry;

Copied: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEDeployment.java (from rev 6305, java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/NewSEWeldDeployment.java)
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEDeployment.java	                        (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/WeldSEDeployment.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -0,0 +1,70 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed 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.jboss.weld.environment.se.discovery;
+
+import java.util.List;
+
+import org.jboss.weld.bootstrap.api.ServiceRegistry;
+import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
+import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
+import org.jboss.weld.bootstrap.spi.Deployment;
+
+/**
+ * Weld Deployment for Java SE environment.
+ * 
+ * @author Peter Royle
+ */
+public class WeldSEDeployment implements Deployment
+{
+
+   public static final String[] RESOURCES = { "META-INF/beans.xml" };
+
+   private final ServiceRegistry serviceRegistry;
+   private final Scanner scanner;
+
+   public WeldSEDeployment(Scanner scanner)
+   {
+      this.serviceRegistry = new SimpleServiceRegistry();
+      this.scanner = scanner;
+   }
+   
+   public Scanner getScanner()
+   {
+      return scanner;
+   }
+
+   /* 
+    * Returns collection containing the singular logical BeanDeploymentArchive
+    * consisting of all Bean classes and beans.xml descriptors in the current
+    * classpath.
+    */
+   public List<BeanDeploymentArchive> getBeanDeploymentArchives()
+   {
+      return scanner.getBeanDeploymentArchives();
+   }
+
+   public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass)
+   {
+      return scanner.getBeanDeploymentArchive(beanClass);
+   }
+
+   public ServiceRegistry getServices()
+   {
+      return serviceRegistry;
+   }
+
+}

Copied: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url (from rev 6305, java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/handlers)

Deleted: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/AbstractURLHandler.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/handlers/AbstractURLHandler.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/AbstractURLHandler.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -1,54 +0,0 @@
-/**
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
- * contributors by the @authors tag. See the copyright.txt in the
- * distribution for a full listing of individual contributors.
- *
- * Licensed 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.jboss.weld.environment.se.discovery.handlers;
-
-import org.jboss.weld.environment.se.discovery.WeldSEBeanDeploymentArchive;
-import org.jboss.weld.resources.spi.ResourceLoader;
-
-/**
- *
- * @author Peter Royle
- */
-public abstract class AbstractURLHandler implements URLHandler
-{
-
-   private final ResourceLoader resourceLoader;
-   private final WeldSEBeanDeploymentArchive weldDiscovery;
-
-   public AbstractURLHandler(ResourceLoader resourceLoader, WeldSEBeanDeploymentArchive webBeanDiscovery)
-   {
-      this.resourceLoader = resourceLoader;
-      this.weldDiscovery = webBeanDiscovery;
-   }
-
-   /**
-    * @return the resourceLoader
-    */
-   public ResourceLoader getResourceLoader()
-   {
-      return resourceLoader;
-   }
-
-   /**
-    * @return the webBeanDiscovery
-    */
-   public WeldSEBeanDeploymentArchive getWeldDiscovery()
-   {
-      return weldDiscovery;
-   }
-   
-}

Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/FileSystemURLHandler.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/handlers/FileSystemURLHandler.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/FileSystemURLHandler.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.jboss.weld.environment.se.discovery.handlers;
+package org.jboss.weld.environment.se.discovery.url;
 
 import java.io.File;
 import java.io.IOException;
@@ -27,30 +27,28 @@
 import java.util.zip.ZipFile;
 
 import org.jboss.weld.environment.se.discovery.WeldSEBeanDeploymentArchive;
-import org.jboss.weld.environment.se.discovery.Scanner;
 import org.jboss.weld.resources.spi.ResourceLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Abstract base class for {@link Scanner} providing common functionality
- * 
  * This class provides file-system orientated scanning
  * 
  * @author Pete Muir
  * 
  */
-public class FileSystemURLHandler extends AbstractURLHandler
+public class FileSystemURLHandler
 {
 
    private static final Logger log = LoggerFactory.getLogger(FileSystemURLHandler.class);
+   private final ResourceLoader resourceLoader;
 
-   public FileSystemURLHandler(ResourceLoader resourceLoader, WeldSEBeanDeploymentArchive webBeanDiscovery)
+   public FileSystemURLHandler(ResourceLoader resourceLoader)
    {
-      super(resourceLoader, webBeanDiscovery);
+      this.resourceLoader = resourceLoader;
    }
 
-   public void handle(Collection<String> paths)
+   public void handle(Collection<String> paths, WeldSEBeanDeploymentArchive beanDeploymentArchive)
    {
       for (String urlPath : paths)
       {
@@ -70,19 +68,21 @@
             File file = new File(urlPath);
             if (file.isDirectory())
             {
-               handleDirectory(file, null);
-            } else
+               handleDirectory(file, null, beanDeploymentArchive);
+            }
+            else
             {
-               handleArchiveByFile(file);
+               handleArchiveByFile(file, beanDeploymentArchive);
             }
-         } catch (IOException ioe)
+         }
+         catch (IOException ioe)
          {
             FileSystemURLHandler.log.warn("could not read entries", ioe);
          }
       }
    }
 
-   private void handleArchiveByFile(File file) throws IOException
+   private void handleArchiveByFile(File file, WeldSEBeanDeploymentArchive beanDeploymentArchive) throws IOException
    {
       try
       {
@@ -96,20 +96,21 @@
          {
             ZipEntry entry = entries.nextElement();
             String name = entry.getName();
-            handle(name, new URL(archiveUrl + name));
+            handle(name, new URL(archiveUrl + name), beanDeploymentArchive);
          }
-      } catch (ZipException e)
+      }
+      catch (ZipException e)
       {
          throw new RuntimeException("Error handling file " + file, e);
       }
    }
 
-   protected void handleDirectory(File file, String path)
+   protected void handleDirectory(File file, String path, WeldSEBeanDeploymentArchive beanDeploymentArchive)
    {
-      handleDirectory(file, path, new File[0]);
+      handleDirectory(file, path, new File[0], beanDeploymentArchive);
    }
 
-   private void handleDirectory(File file, String path, File[] excludedDirectories)
+   private void handleDirectory(File file, String path, File[] excludedDirectories, WeldSEBeanDeploymentArchive beanDeploymentArchive)
    {
       for (File excludedDirectory : excludedDirectories)
       {
@@ -129,13 +130,15 @@
 
          if (child.isDirectory())
          {
-            handleDirectory(child, newPath, excludedDirectories);
-         } else
+            handleDirectory(child, newPath, excludedDirectories, beanDeploymentArchive);
+         }
+         else
          {
             try
             {
-               handle(newPath, child.toURI().toURL());
-            } catch (MalformedURLException e)
+               handle(newPath, child.toURI().toURL(), beanDeploymentArchive);
+            }
+            catch (MalformedURLException e)
             {
                log.error("Error loading file " + newPath);
             }
@@ -143,25 +146,35 @@
       }
    }
 
-   protected void handle(String name, URL url)
+   protected void handle(String name, URL url, WeldSEBeanDeploymentArchive beanDeploymentArchive)
    {
       if (name.endsWith(".class"))
       {
          String className = filenameToClassname(name);
          try
          {
-            getWeldDiscovery().getBeanClasses().add(getResourceLoader().classForName(className));
-         } catch (NoClassDefFoundError e)
+            beanDeploymentArchive.getBeanClasses().add(getResourceLoader().classForName(className));
+         }
+         catch (NoClassDefFoundError e)
          {
             log.error("Error loading " + name, e);
          }
-      } else if (name.endsWith("beans.xml"))
+      }
+      else if (name.endsWith("beans.xml"))
       {
-         getWeldDiscovery().getUrls().add(url);
+         beanDeploymentArchive.getUrls().add(url);
       }
    }
 
    /**
+    * @return the resourceLoader
+    */
+   public ResourceLoader getResourceLoader()
+   {
+      return resourceLoader;
+   }
+
+   /**
     * Convert a path to a class file to a class name
     */
    public static String filenameToClassname(String filename)

Deleted: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLHandler.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/handlers/URLHandler.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLHandler.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -1,29 +0,0 @@
-/**
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
- * contributors by the @authors tag. See the copyright.txt in the
- * distribution for a full listing of individual contributors.
- *
- * Licensed 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.jboss.weld.environment.se.discovery.handlers;
-
-import java.util.Collection;
-
-/**
- * Interface for handling URL resolving.
- * @author Peter Royle
- */
-public interface URLHandler {
-
-   void handle(Collection<String> paths);
-
-}

Copied: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLScanner.java (from rev 6305, java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java)
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLScanner.java	                        (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/URLScanner.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -0,0 +1,129 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed 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.jboss.weld.environment.se.discovery.url;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
+import org.jboss.weld.environment.se.discovery.Scanner;
+import org.jboss.weld.environment.se.discovery.WeldSEBeanDeploymentArchive;
+import org.jboss.weld.environment.se.exceptions.ClasspathScanningException;
+import org.jboss.weld.resources.spi.ResourceLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of {@link Scanner} which can scan a {@link URLClassLoader}
+ * 
+ * @author Thomas Heute
+ * @author Gavin King
+ * @author Norman Richards
+ * @author Pete Muir
+ * @author Peter Royle
+ * 
+ */
+public class URLScanner implements Scanner
+{
+
+   private static final Logger log = LoggerFactory.getLogger(URLScanner.class);
+   private final WeldSEBeanDeploymentArchive beanDeploymentArchive;
+   private final String[] resources;
+
+   public URLScanner(String... resources)
+   {
+      this.beanDeploymentArchive = new WeldSEBeanDeploymentArchive("weld-se");
+      this.resources = resources;
+   }
+
+   public void scan(ResourceLoader resourceLoader)
+   {
+      FileSystemURLHandler handler = new FileSystemURLHandler(resourceLoader);
+      Collection<String> paths = new ArrayList<String>();
+      for (String resourceName : resources)
+      {
+         // grab all the URLs for this resource
+         Collection<URL> urlEnum = resourceLoader.getResources(resourceName);
+         for (URL url : urlEnum)
+         {
+
+            String urlPath = url.toExternalForm();
+
+            // determin resource type (eg: jar, file, bundle)
+            String urlType = "file";
+            int colonIndex = urlPath.indexOf(":");
+            if (colonIndex != -1)
+            {
+               urlType = urlPath.substring(0, colonIndex);
+            }
+
+            // Extra built-in support for simple file-based resources
+            if ("file".equals(urlType) || "jar".equals(urlType))
+            {
+               // switch to using getPath() instead of toExternalForm()
+               urlPath = url.getPath();
+
+               if (urlPath.indexOf('!') > 0)
+               {
+                  urlPath = urlPath.substring(0, urlPath.indexOf('!'));
+               }
+               else
+               {
+                  // hack for /META-INF/beans.xml
+                  File dirOrArchive = new File(urlPath);
+                  if ((resourceName != null) && (resourceName.lastIndexOf('/') > 0))
+                  {
+                     dirOrArchive = dirOrArchive.getParentFile();
+                  }
+                  urlPath = dirOrArchive.getParent();
+               }
+            }
+
+            try
+            {
+               urlPath = URLDecoder.decode(urlPath, "UTF-8");
+            }
+            catch (UnsupportedEncodingException ex)
+            {
+               throw new ClasspathScanningException("Error decoding URL using UTF-8");
+            }
+
+            log.debug("URL Type: " + urlType);
+
+            paths.add(urlPath);
+         }
+         handler.handle(paths, beanDeploymentArchive);
+      }
+   }
+   
+   public BeanDeploymentArchive getBeanDeploymentArchive(Class<?> clazz)
+   {
+      return beanDeploymentArchive;
+   }
+   
+   public List<BeanDeploymentArchive> getBeanDeploymentArchives()
+   {
+      return Collections.<BeanDeploymentArchive>singletonList(beanDeploymentArchive);
+   }
+}

Added: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/WeldSEResourceLoader.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/WeldSEResourceLoader.java	                        (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/WeldSEResourceLoader.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -0,0 +1,96 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.weld.environment.se.discovery.url;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collection;
+
+import org.jboss.weld.resources.spi.ResourceLoader;
+import org.jboss.weld.resources.spi.ResourceLoadingException;
+import org.jboss.weld.util.collections.EnumerationList;
+
+/**
+ * A simple resource loader.
+ * 
+ * Uses {@link WeldSEResourceLoader}'s classloader if the Thread Context 
+ * Classloader isn't available
+ * 
+ * @author Pete Muir
+ *
+ */
+public class WeldSEResourceLoader implements ResourceLoader
+{
+   
+   public Class<?> classForName(String name)
+   {
+      
+      try
+      {
+         if (Thread.currentThread().getContextClassLoader() != null)
+         {
+            return Thread.currentThread().getContextClassLoader().loadClass(name);
+         }
+         else
+         {
+            return Class.forName(name);
+         }
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new ResourceLoadingException(e);
+      }
+      catch (NoClassDefFoundError e)
+      {
+         throw new ResourceLoadingException(e);
+      }
+   }
+   
+   public URL getResource(String name)
+   {
+      if (Thread.currentThread().getContextClassLoader() != null)
+      {
+         return Thread.currentThread().getContextClassLoader().getResource(name);
+      }
+      else
+      {
+         return getClass().getResource(name);
+      }
+   }
+   
+   public Collection<URL> getResources(String name)
+   {
+      try
+      {
+         if (Thread.currentThread().getContextClassLoader() != null)
+         {
+            return new EnumerationList<URL>(Thread.currentThread().getContextClassLoader().getResources(name));
+         }
+         else
+         {
+            return new EnumerationList<URL>(getClass().getClassLoader().getResources(name));
+         }
+      }
+      catch (IOException e)
+      {
+         throw new ResourceLoadingException(e);
+      }
+   }
+   
+   public void cleanup() {}
+   
+}


Property changes on: java-se/trunk/src/main/java/org/jboss/weld/environment/se/discovery/url/WeldSEResourceLoader.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:eol-style
   + native

Modified: java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
===================================================================
--- java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java	2010-05-25 14:53:56 UTC (rev 6305)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java	2010-05-25 17:52:01 UTC (rev 6306)
@@ -16,13 +16,11 @@
  */
 package org.jboss.weld.environment.se.test;
 
-import javax.enterprise.inject.spi.BeanManager;
-
 import org.jboss.weld.environment.se.ShutdownManager;
 import org.jboss.weld.environment.se.Weld;
 import org.jboss.weld.environment.se.WeldContainer;
-import org.jboss.weld.environment.se.test.decorators.CarDoor;
 import org.jboss.weld.environment.se.test.decorators.AbstractDoor;
+import org.jboss.weld.environment.se.test.decorators.CarDoor;
 import org.jboss.weld.environment.se.test.decorators.CarDoorAlarm;
 import org.jboss.weld.environment.se.test.decorators.HouseDoor;
 import org.testng.Assert;
@@ -41,9 +39,7 @@
    @Test
    public void testDecorators()
    {
-
       WeldContainer weld = new Weld().initialize();
-      BeanManager manager = weld.getBeanManager();
 
       CarDoor carDoor = weld.instance().select(CarDoor.class).get();
       Assert.assertNotNull(carDoor);



More information about the weld-commits mailing list