001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.servicemix.specs.locator; 018 019 import java.util.concurrent.Callable; 020 import java.util.List; 021 import java.util.Map; 022 import java.util.HashMap; 023 import java.util.ArrayList; 024 import java.util.Set; 025 import java.util.HashSet; 026 027 public class OsgiLocator { 028 029 private static Map<String, List<Callable<Class>>> factories; 030 031 private OsgiLocator() { 032 } 033 034 public static synchronized void unregister(String id, Callable<Class> factory) { 035 if (factories != null) { 036 List<Callable<Class>> l = factories.get(id); 037 if (l != null) { 038 l.remove(factory); 039 } 040 } 041 } 042 043 public static synchronized void register(String id, Callable<Class> factory) { 044 if (factories == null) { 045 factories = new HashMap<String, List<Callable<Class>>>(); 046 } 047 List<Callable<Class>> l = factories.get(id); 048 if (l == null) { 049 l = new ArrayList<Callable<Class>>(); 050 factories.put(id, l); 051 } 052 l.add(factory); 053 } 054 055 public static synchronized Class locate(String factoryId) { 056 if (factories != null) { 057 List<Callable<Class>> l = factories.get(factoryId); 058 if (l != null && !l.isEmpty()) { 059 Callable<Class> c = l.get(l.size() - 1); 060 try { 061 return c.call(); 062 } catch (Exception e) { 063 } 064 } 065 } 066 return null; 067 } 068 069 public static synchronized List<Class> locateAll(String factoryId) { 070 List<Class> classes = new ArrayList<Class>(); 071 if (factories != null) { 072 List<Callable<Class>> l = factories.get(factoryId); 073 if (l != null) { 074 for (Callable<Class> c : l) { 075 try { 076 classes.add(c.call()); 077 } catch (Exception e) { 078 } 079 } 080 } 081 } 082 return classes; 083 } 084 085 }