Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.plugins » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.plugins;
   15   
   16   import java.util.ArrayList;
   17   import java.util.Collections;
   18   import java.util.Iterator;
   19   import java.util.List;
   20   import java.util.ServiceLoader;
   21   
   22   import org.apache.webbeans.config.WebBeansFinder;
   23   import org.apache.webbeans.exception.WebBeansConfigurationException;
   24   import org.apache.webbeans.logger.WebBeansLogger;
   25   
   26   /**
   27    * <p>OpenWebBeans plugins are used to extend 'core' functionality of the injection
   28    * mechanism with functionality of other frameworks.</p>
   29    * 
   30    * <p>Core functionality are all parts which are available in a standard
   31    * JDK-1.5 SE runtime. Extended functionality are things like JPA, JSF, EJB etc.</p>
   32    * 
   33    * <p>The plugin mechanism is based on the ServiceProvider functionality 
   34    * {@link http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html#Service%20Provider}.
   35    * A jar containing an OWB plugin has to expose itself in the file
   36    * <code>META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin</code></p> 
   37    *
   38    * TODO: what about ordering the plugins via an ordinal?
   39    */
   40   public class PluginLoader
   41   {
   42       /**Logger instance*/
   43       private static WebBeansLogger logger = WebBeansLogger.getLogger(PluginLoader.class);
   44   
   45       /** unmodifiable list with all found OWB plugins */
   46       private List<OpenWebBeansPlugin> plugins = null;
   47   
   48       /**
   49        * @return singleton PluginLoader 
   50        */
   51       public static PluginLoader getInstance()
   52       {
   53           return (PluginLoader) WebBeansFinder.getSingletonInstance(PluginLoader.class.getName());
   54       }
   55       
   56       public PluginLoader()
   57       {
   58       }
   59   
   60       /**
   61        * load and startup all registered plugins.
   62        * This must be called once the WebApplication is started.
   63        * @throws WebBeansConfigurationException
   64        */
   65       public void startUp() throws WebBeansConfigurationException
   66       {
   67           logger.debug("PluginLoader startUp called");
   68           ArrayList<OpenWebBeansPlugin> ps = new ArrayList<OpenWebBeansPlugin>();
   69   
   70           ServiceLoader<OpenWebBeansPlugin> owbPluginsLoader = ServiceLoader.load(OpenWebBeansPlugin.class);
   71           Iterator<OpenWebBeansPlugin> pluginIter = owbPluginsLoader.iterator();
   72           while(pluginIter.hasNext()) 
   73           {
   74             OpenWebBeansPlugin plugin = pluginIter.next();
   75             logger.info("adding OpenWebBeansPlugin " + plugin.getClass().getSimpleName());
   76             ps.add(plugin);
   77           }   
   78           
   79           // just to make sure the plugins aren't modified afterwards
   80           plugins = Collections.unmodifiableList(ps);
   81       }
   82       
   83       /**
   84        * Tell all the plugins to free up all locked resources.
   85        * This must be called before the WebApplication gets undeployed or stopped.
   86        * @throws WebBeansConfigurationException
   87        */
   88       public void shutDown() throws WebBeansConfigurationException
   89       {
   90           logger.debug("PluginLoader shutDown called");
   91           
   92           if (plugins == null)
   93           {
   94               logger.warn("No plugins to shutDown!");
   95               return;
   96           }
   97   
   98           ArrayList<String> failedShutdown = new ArrayList<String>();
   99   
  100           for (OpenWebBeansPlugin plugin : plugins)
  101           {
  102               try 
  103               {
  104                   plugin.shutDown();
  105               }
  106               catch (Exception e)
  107               {
  108                   // we catch ALL exceptions, since we like to continue shutting down all other plugins!
  109                   String pluginName = plugin.getClass().getSimpleName();
  110                   logger.error("error while shutdown the pugin " + pluginName, e);
  111                   failedShutdown.add(pluginName);
  112               }
  113           }
  114           
  115           if (!failedShutdown.isEmpty())
  116           {
  117               throw new WebBeansConfigurationException("got Exceptions while sending shutdown to the following plugins: "
  118                                                        + failedShutdown.toString());
  119           }
  120       }
  121       
  122       /**
  123        * @return an unmodifiable list of all registered {code OpenWebBeansPlugin}s
  124        */
  125       public List<OpenWebBeansPlugin> getPlugins()
  126       {
  127           return plugins;
  128       }
  129    
  130       /**
  131        * Gets ejb plugin
  132        * 
  133        * @return ejb plugin
  134        */
  135       public OpenWebBeansEjbPlugin getEjbPlugin()
  136       {
  137           for(OpenWebBeansPlugin plugin : this.plugins)
  138           {
  139               if(plugin instanceof OpenWebBeansEjbPlugin)
  140               {
  141                   return (OpenWebBeansEjbPlugin)plugin;
  142               }
  143           }
  144           
  145           return null;
  146       }
  147       
  148       /**
  149        * Gets ejb plugin
  150        * 
  151        * @return ejb plugin
  152        */
  153       public OpenWebBeansJmsPlugin getJmsPlugin()
  154       {
  155           for(OpenWebBeansPlugin plugin : this.plugins)
  156           {
  157               if(plugin instanceof OpenWebBeansJmsPlugin)
  158               {
  159                   return (OpenWebBeansJmsPlugin)plugin;
  160               }
  161           }
  162           
  163           return null;
  164       }
  165       
  166       /**
  167        * Gets ejb plugin
  168        * 
  169        * @return ejb plugin
  170        */
  171       public OpenWebBeansResourcePlugin getResourcePlugin()
  172       {
  173           for(OpenWebBeansPlugin plugin : this.plugins)
  174           {
  175               if(plugin instanceof OpenWebBeansResourcePlugin)
  176               {
  177                   return (OpenWebBeansResourcePlugin)plugin;
  178               }
  179           }
  180           
  181           return null;
  182       }
  183       
  184   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.plugins » [javadoc | source]