Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * 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.
  */
 
 /*
  * 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.drools.util;
 
 import java.util.List;
 import java.util.Map;
Maintains a mapping of service names to an ordered set of service providers when running in an OSGi container.

It is expected that a bundle using Drools will populate this map with properties from its own ClassLoader.

This is an adaptation of the technique described by Guillaume Nodet in his article Java EE specs in OSGi. The main changes were to add comments.

See also:
http://gnodet.blogspot.com/2008/05/jee-specs-in-osgi.html
 
 public final class OSGiLocator {

    
Maps a service name (the fully qualified name of the interface) to an ordered set of factories. Each factory instantiating a specific service provider (implementation).
 
     private static Map<StringList<Callable<Class< ? >>>> factories;

    
Private constructor used to prevent instantiation of this utility class (i.e., Utility Pattern).
 
     private OSGiLocator() {
         super();
     }

    
Removes the given service provider factory from the set of providers for the service.

Parameters:
serviceName The fully qualified name of the service interface.
factory A factory for creating a specific type of service provider. May be null in which case this method does nothing.
Throws:
java.lang.IllegalArgumentException if serviceName is null
 
     public static synchronized void unregister(final String serviceName,
                                                final Callable<Class< ? >> factory) {
         if ( serviceName == null ) {
             throw new IllegalArgumentException"serviceName cannot be null" );
         }
         if (  != null ) {
             List<Callable<Class< ? >>> l = .getserviceName );
             if ( l != null ) {
                 l.removefactory );
             }
         }
     }

    
Adds the given service provider factory to the set of providers for the service.

Parameters:
serviceName The fully qualified name of the service interface.
factory A factory for creating a specific type of service provider. May be null in which case this method does nothing.
Throws:
java.lang.IllegalArgumentException if serviceName is null
    public static synchronized void register(final String serviceName,
                                             final Callable<Class< ? >> factory) {
        if ( serviceName == null ) {
            throw new IllegalArgumentException"serviceName cannot be null" );
        }
        if ( factory != null ) {
            if (  == null ) {
                 = new HashMap<StringList<Callable<Class< ? >>>>();
            }
            List<Callable<Class< ? >>> l = .getserviceName );
            if ( l == null ) {
                l = new ArrayList<Callable<Class< ? >>>();
                .putserviceName,
                               l );
            }
            l.addfactory );
        }
    }

    
Finds the preferred provider for the given service. The preferred provider is the last one added to the set of providers.

Parameters:
serviceName The fully qualified name of the service interface.
Returns:
The last provider added for the service if any exists. Otherwise, it returns null.
Throws:
java.lang.IllegalArgumentException if serviceName is null
    public static synchronized Class< ? > locate(final String serviceName) {
        if ( serviceName == null ) {
            throw new IllegalArgumentException"serviceName cannot be null" );
        }
        if (  != null ) {
            List<Callable<Class< ? >>> l = .getserviceName );
            if ( l != null && !l.isEmpty() ) {
                Callable<Class< ? >> c = l.getl.size() - 1 );
                try {
                    return c.call();
                } catch ( Exception e ) {
                }
            }
        }
        return null;
    }

    
Finds all providers for the given service.

Parameters:
serviceName The fully qualified name of the service interface.
Returns:
The ordered set of providers for the service if any exists. Otherwise, it returns an empty list.
Throws:
java.lang.IllegalArgumentException if serviceName is null
    public static synchronized List<Class< ? >> locateAll(final String serviceName) {
        if ( serviceName == null ) {
            throw new IllegalArgumentException"serviceName cannot be null" );
        }
        List<Class< ? >> classes = new ArrayList<Class< ? >>();
        if (  != null ) {
            List<Callable<Class< ? >>> l = .getserviceName );
            if ( l != null ) {
                for ( Callable<Class< ? >> c : l ) {
                    try {
                        classes.addc.call() );
                    } catch ( Exception e ) {
                    }
                }
            }
        }
        return classes;
    }
New to GrepCode? Check out our FAQ X