|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.javamoney.moneta.convert.internal.yahoo; |
| 20 | + |
| 21 | +import org.osgi.framework.*; |
| 22 | + |
| 23 | +import java.net.URL; |
| 24 | +import java.util.*; |
| 25 | +import java.util.logging.Level; |
| 26 | +import java.util.logging.Logger; |
| 27 | + |
| 28 | +/** |
| 29 | + * ServiceContext implementation based on OSGI Service mechanisms. |
| 30 | + */ |
| 31 | +final class OSGIServiceHelper { |
| 32 | + |
| 33 | + private static final Logger LOG = Logger.getLogger(OSGIServiceHelper.class.getName()); |
| 34 | + |
| 35 | + private OSGIServiceHelper(){} |
| 36 | + |
| 37 | + public Enumeration<URL> getResources(BundleContext bundleContext, String resource) { |
| 38 | + LOG.finest("TAMAYA Loading resources: " + resource); |
| 39 | + List<URL> result = new ArrayList<>(); |
| 40 | + URL url = bundleContext.getBundle().getEntry(resource); |
| 41 | + if(url != null) { |
| 42 | + LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + |
| 43 | + bundleContext.getBundle().getSymbolicName()); |
| 44 | + result.add(url); |
| 45 | + } |
| 46 | + for(Bundle bundle: bundleContext.getBundles()) { |
| 47 | + url = bundle.getEntry(resource); |
| 48 | + if (url != null && !result.contains(url)) { |
| 49 | + LOG.finest("TAMAYA Resource: " + resource + " found in registered bundle " + bundle.getSymbolicName()); |
| 50 | + result.add(url); |
| 51 | + } |
| 52 | + } |
| 53 | + for(Bundle bundle: bundleContext.getBundles()) { |
| 54 | + url = bundle.getEntry(resource); |
| 55 | + if (url != null && !result.contains(url)) { |
| 56 | + LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + bundle.getSymbolicName()); |
| 57 | + result.add(url); |
| 58 | + } |
| 59 | + } |
| 60 | + return Collections.enumeration(result); |
| 61 | + } |
| 62 | + |
| 63 | + public static <T> void registerService(Bundle bundle, Class<T> serviceClass, Class<? extends T> implClass, |
| 64 | + int ranking) { |
| 65 | + try { |
| 66 | + // Load the service class |
| 67 | + LOG.info("Loaded Service Factory (" + serviceClass.getName() + "): " + implClass.getName()); |
| 68 | + // Provide service properties |
| 69 | + Hashtable<String, String> props = new Hashtable<>(); |
| 70 | + props.put(Constants.VERSION_ATTRIBUTE, bundle.getVersion().toString()); |
| 71 | + String vendor = bundle.getHeaders().get(Constants.BUNDLE_VENDOR); |
| 72 | + props.put(Constants.SERVICE_VENDOR, (vendor != null ? vendor : "anonymous")); |
| 73 | + // Translate annotated @Priority into a service ranking |
| 74 | + props.put(Constants.SERVICE_RANKING, |
| 75 | + String.valueOf(ranking)); |
| 76 | + |
| 77 | + // Register the service factory on behalf of the intercepted bundle |
| 78 | + JDKUtilServiceFactory factory = new JDKUtilServiceFactory(implClass); |
| 79 | + BundleContext bundleContext = bundle.getBundleContext(); |
| 80 | + bundleContext.registerService(serviceClass.getName(), factory, props); |
| 81 | + LOG.info("Registered Tamaya service class: " + implClass.getName() + "(" + serviceClass.getName() + ")"); |
| 82 | + } catch (Exception e) { |
| 83 | + LOG.log(Level.SEVERE, "Failed to load service: " + implClass.getName(), e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static <T> void unregisterService(Bundle bundle, Class<T> serviceClass, Class<? extends T> implClass) { |
| 88 | + try { |
| 89 | + LOG.fine("Unloading Service (" + serviceClass.getName() + "): " + implClass.getName()); |
| 90 | + ServiceReference<?> ref = bundle.getBundleContext().getServiceReference(implClass); |
| 91 | + if (ref != null) { |
| 92 | + bundle.getBundleContext().ungetService(ref); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + LOG.log(Level.SEVERE, "Failed to unload service: " + implClass.getName(), e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Service factory simply instantiating the configured service. |
| 101 | + */ |
| 102 | + static class JDKUtilServiceFactory implements ServiceFactory { |
| 103 | + private final Class<?> serviceClass; |
| 104 | + |
| 105 | + public JDKUtilServiceFactory(Class<?> serviceClass) { |
| 106 | + this.serviceClass = serviceClass; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Object getService(Bundle bundle, ServiceRegistration registration) { |
| 111 | + try { |
| 112 | + LOG.fine("Creating Service...:" + serviceClass.getName()); |
| 113 | + return serviceClass.newInstance(); |
| 114 | + } catch (Exception ex) { |
| 115 | + ex.printStackTrace(); |
| 116 | + throw new IllegalStateException("Failed to create service: " + serviceClass.getName(), ex); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments