Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.servicemix.specs.locator;
 
 import java.util.List;
 import java.util.Map;
 
 public class OsgiLocator {
 
     public static final long DEFAULT_TIMEOUT = 0l;
     public static final String TIMEOUT = "org.apache.servicemix.specs.timeout";
 
     private static Map<StringList<Callable<Class>>> factories;
     
     private static ReadWriteLock lock = new ReentrantReadWriteLock();
 
     private OsgiLocator() {
     }
 
     public static void unregister(String idCallable<Classfactory) {
         .writeLock().lock();
         try {
             if ( != null) {
                 List<Callable<Class>> l = .get(id);
                 if (l != null) {
                     l.remove(factory);
                 }
             }
         } finally {
             .writeLock().unlock();
         }
     }
 
     public static void register(String idCallable<Classfactory) {
         .writeLock().lock();
         try {
             if ( == null) {
                  = new HashMap<StringList<Callable<Class>>>();
             }
             List<Callable<Class>> l = .get(id);
             if (l ==  null) {
                 l = new ArrayList<Callable<Class>>();
                 .put(idl);
             }
             l.add(0, factory);
             synchronized () {
                 .notifyAll();
             }
         } finally {
             .writeLock().unlock();
         }
     }
 
 
     public static <T> Class<? extends T> locate(Class<T> factoryId) {
         return locate(factoryIdfactoryId.getName());
     }
 
     private static long getTimeout() {
         long timeout = ;
         try {
             String prop = System.getProperty();
             if (prop != null) {
                 timeout = Long.parseLong(prop);
             }
         } catch (Throwable t) { }
         return timeout;
     }
 
     public static <T> Class<? extends T> locate(Class<T> factoryClassString factoryId) {
         long timeout = getTimeout();
         if (timeout <= 0) {
             return doLocate(factoryClassfactoryId);
         }
         long t0 = System.currentTimeMillis();
         long t1 = t0;
         while (t1 - t0 < timeout) {
             Class<? extends T> impl = doLocate(factoryClassfactoryId);
             if (impl != null) {
                 return impl;
            }
            synchronized () {
                try {
                    .wait(timeout - (t1 - t0));
                } catch (InterruptedException e) {
                    return null;
                }
            }
            t1 = System.currentTimeMillis();
        }
        return null;
    }
    private static <T> Class<? extends T> doLocate(Class<T> factoryClassString factoryId) {
        .readLock().lock();
        try {
            if ( != null) {
                List<Callable<Class>> l = .get(factoryId);
                if (l != null && !l.isEmpty()) {
                    // look up the System property first
                    String factoryClassName = System.getProperty(factoryId);
                    try {
                        for (Callable<Classi : l) {
                            Class c = null;
                            try {
                                c = i.call();
                            } catch (Exception ex) {
                                // do nothing here
                            }
                            if (c != null && factoryClass == c.getClassLoader().loadClass(factoryClass.getName())
                                     && (factoryClassName == null || c.getName().equals(factoryClassName)))
                            {
                                return c;
                            }
                        }
                    } catch (Exception ex) {
                        // do nothing here
                    }  
                }
            }
            return null;
        } finally {
            .readLock().unlock();
        }
    }
    public static <T> List<Class<? extends T>> locateAll(Class<T> factoryId) {
        return locateAll(factoryIdfactoryId.getName());
    }
    public static <T> List<Class<? extends T>> locateAll(Class<T> factoryClassString factoryId) {
        .readLock().lock();
        try {
            List<Class<? extends T>> classes = new ArrayList<Class<? extends T>>();
            if ( != null) {
                List<Callable<Class>> l = .get(factoryId);
                if (l != null) {
                    for (Callable<Classi : l) {
                    	try {
                            Class c = i.call();
                            if (c != null && factoryClass.isAssignableFrom(c)) {
                        	    classes.add(c);
                            }
                    	} catch (Exception e) {
                    	}
    				}
                }
            }
            return classes;
        } finally {
            .readLock().unlock();
        }
    }
New to GrepCode? Check out our FAQ X