Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id ACFCE200CFC for ; Thu, 28 Sep 2017 22:03:44 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id AB6401609CD; Thu, 28 Sep 2017 20:03:44 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A10441609B4 for ; Thu, 28 Sep 2017 22:03:43 +0200 (CEST) Received: (qmail 37474 invoked by uid 500); 28 Sep 2017 20:03:42 -0000 Mailing-List: contact commits-help@tamaya.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tamaya.incubator.apache.org Delivered-To: mailing list commits@tamaya.incubator.apache.org Received: (qmail 37465 invoked by uid 99); 28 Sep 2017 20:03:42 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Sep 2017 20:03:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 548231A3BED for ; Thu, 28 Sep 2017 20:03:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.222 X-Spam-Level: X-Spam-Status: No, score=-4.222 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id kEqZ6S8GQ9ID for ; Thu, 28 Sep 2017 20:03:40 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 280F36125C for ; Thu, 28 Sep 2017 20:03:39 +0000 (UTC) Received: (qmail 37022 invoked by uid 99); 28 Sep 2017 20:03:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Sep 2017 20:03:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C669FF5BFB; Thu, 28 Sep 2017 20:03:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: anatole@apache.org To: commits@tamaya.incubator.apache.org Date: Thu, 28 Sep 2017 20:03:45 -0000 Message-Id: <15665c25bd7849379eb9ae4b86a850cd@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [09/12] incubator-tamaya git commit: TAMAYA-274: Fixed basic service loading in OSGI. Added missing requirements. archived-at: Thu, 28 Sep 2017 20:03:44 -0000 TAMAYA-274: Fixed basic service loading in OSGI. Added missing requirements. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/ec4079dc Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ec4079dc Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ec4079dc Branch: refs/heads/master Commit: ec4079dc3205c3825e6287be8d17013b91ade8e0 Parents: 48af147 Author: anatole Authored: Sun Sep 17 01:51:20 2017 +0200 Committer: Anatole Tresch Committed: Thu Sep 28 22:01:27 2017 +0200 ---------------------------------------------------------------------- .../tamaya/core/internal/OSGIServiceLoader.java | 96 +++++++++++++------- .../examples/minimal/TestConfigProvider.java | 49 ++++++++++ 2 files changed, 110 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ec4079dc/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java b/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java index 4c12df9..0854b0d 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java @@ -48,6 +48,13 @@ public class OSGIServiceLoader implements BundleListener { public OSGIServiceLoader(BundleContext context){ this.context = Objects.requireNonNull(context); + // Check for matching bundles already installed... + for(Bundle bundle:context.getBundles()){ + switch(bundle.getState()){ + case Bundle.ACTIVE: + checkAndLoadBundle(bundle); + } + } } public BundleContext getBundleContext(){ @@ -62,38 +69,46 @@ public class OSGIServiceLoader implements BundleListener { @Override public void bundleChanged(BundleEvent bundleEvent) { - // Parse and create metadata on STARTING - if (bundleEvent.getType() == BundleEvent.STARTING) { + // Parse and create metadata when installed + if (bundleEvent.getType() == BundleEvent.STARTED) { Bundle bundle = bundleEvent.getBundle(); - if (bundle.getEntry(META_INF_SERVICES) == null) { - return; - } - synchronized (resourceBundles){ - resourceBundles.add(bundle); - log.info("Registered ServiceLoader bundle: " + bundle.getSymbolicName()); - } - Enumeration entryPaths = bundle.getEntryPaths("META-INF/services/"); - while (entryPaths.hasMoreElements()) { - String entryPath = entryPaths.nextElement(); - if(!entryPath.endsWith("/")) { - processEntryPath(bundle, entryPath); - } - } - } else if (bundleEvent.getType() == BundleEvent.STOPPING) { + checkAndLoadBundle(bundle); + } else if (bundleEvent.getType() == BundleEvent.STOPPED) { Bundle bundle = bundleEvent.getBundle(); - if (bundle.getEntry(META_INF_SERVICES) == null) { - return; - } - synchronized (resourceBundles) { - resourceBundles.remove(bundle); - log.finest("Unregistered ServiceLoader bundle: " + bundle.getSymbolicName()); + checkAndUnloadBundle(bundle); + } + } + + private void checkAndUnloadBundle(Bundle bundle) { + if (bundle.getEntry(META_INF_SERVICES) == null) { + return; + } + synchronized (resourceBundles) { + resourceBundles.remove(bundle); + log.fine("Unregistered ServiceLoader bundle: " + bundle.getSymbolicName()); + } + Enumeration entryPaths = bundle.getEntryPaths("META-INF/services/"); + while (entryPaths.hasMoreElements()) { + String entryPath = entryPaths.nextElement(); + if(!entryPath.endsWith("/")) { + removeEntryPath(bundle, entryPath); } - Enumeration entryPaths = bundle.getEntryPaths("META-INF/services/"); - while (entryPaths.hasMoreElements()) { - String entryPath = entryPaths.nextElement(); - if(!entryPath.endsWith("/")) { - removeEntryPath(bundle, entryPath); - } + } + } + + private void checkAndLoadBundle(Bundle bundle) { + if (bundle.getEntry(META_INF_SERVICES) == null) { + return; + } + synchronized (resourceBundles){ + resourceBundles.add(bundle); + log.info("Registered ServiceLoader bundle: " + bundle.getSymbolicName()); + } + Enumeration entryPaths = bundle.getEntryPaths("META-INF/services/"); + while (entryPaths.hasMoreElements()) { + String entryPath = entryPaths.nextElement(); + if(!entryPath.endsWith("/")) { + processEntryPath(bundle, entryPath); } } } @@ -106,10 +121,9 @@ public class OSGIServiceLoader implements BundleListener { return; } Class serviceClass = bundle.loadClass(serviceName); - log.info("Loaded Tamaya service class: " + serviceClass.getName() +"("+serviceName+")"); URL child = bundle.getEntry(entryPath); InputStream inStream = child.openStream(); - + log.info("Loading Services " + serviceClass.getName() +" from bundle...: " + bundle.getSymbolicName()); BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8")); String implClassName = br.readLine(); while (implClassName != null){ @@ -124,12 +138,14 @@ public class OSGIServiceLoader implements BundleListener { if (implClassName.length() > 0) { try { // Load the service class + log.fine("Loading Class " + implClassName +" from bundle...: " + bundle.getSymbolicName()); Class implClass = bundle.loadClass(implClassName); if (!serviceClass.isAssignableFrom(implClass)) { log.warning("Configured service: " + implClassName + " is not assignble to " + serviceClass.getName()); continue; } + log.info("Loaded Service Factory ("+serviceName+"): " + implClassName); // Provide service properties Hashtable props = new Hashtable<>(); props.put(Constants.VERSION_ATTRIBUTE, bundle.getVersion().toString()); @@ -142,12 +158,16 @@ public class OSGIServiceLoader implements BundleListener { // Register the service factory on behalf of the intercepted bundle JDKUtilServiceFactory factory = new JDKUtilServiceFactory(implClass); BundleContext bundleContext = bundle.getBundleContext(); - log.info("Registering Tamaya service class: " + serviceClass.getName() +"("+serviceName+")"); bundleContext.registerService(serviceName, factory, props); + log.info("Registered Tamaya service class: " + implClassName +"("+serviceName+")"); } catch(Exception e){ log.log(Level.SEVERE, - "Failed to load service class using ServiceLoader logic: " + implClassName, e); + "Failed to load service: " + implClassName, e); + } + catch(NoClassDefFoundError err){ + log.log(Level.SEVERE, + "Failed to load service: " + implClassName, err); } } implClassName = br.readLine(); @@ -186,6 +206,7 @@ public class OSGIServiceLoader implements BundleListener { } implClassName = implClassName.trim(); if (implClassName.length() > 0) { + log.fine("Unloading Service ("+serviceName+"): " + implClassName); try { // Load the service class Class implClass = bundle.loadClass(implClassName); @@ -201,7 +222,11 @@ public class OSGIServiceLoader implements BundleListener { } catch(Exception e){ log.log(Level.SEVERE, - "Failed to unload service class using ServiceLoader logic: " + implClassName, e); + "Failed to unload service: " + implClassName, e); + } + catch(NoClassDefFoundError err){ + log.log(Level.SEVERE, + "Failed to unload service: " + implClassName, err); } } implClassName = br.readLine(); @@ -230,11 +255,12 @@ public class OSGIServiceLoader implements BundleListener { @Override public Object getService(Bundle bundle, ServiceRegistration registration) { try { + log.fine("Creating Service...:" + serviceClass.getName()); return serviceClass.newInstance(); } catch (Exception ex) { ex.printStackTrace(); - throw new IllegalStateException("Cannot create service: " + serviceClass.getName(), ex); + throw new IllegalStateException("Failed to create service: " + serviceClass.getName(), ex); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ec4079dc/examples/01-minimal/src/main/java/org/apache/tamaya/examples/minimal/TestConfigProvider.java ---------------------------------------------------------------------- diff --git a/examples/01-minimal/src/main/java/org/apache/tamaya/examples/minimal/TestConfigProvider.java b/examples/01-minimal/src/main/java/org/apache/tamaya/examples/minimal/TestConfigProvider.java new file mode 100644 index 0000000..f9fb853 --- /dev/null +++ b/examples/01-minimal/src/main/java/org/apache/tamaya/examples/minimal/TestConfigProvider.java @@ -0,0 +1,49 @@ +/* + * 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.tamaya.examples.minimal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.core.internal.DefaultConfigurationProvider; + +/** + * Configuration provider that allows to set and reset a configuration + * different per thread. + */ +public class TestConfigProvider extends DefaultConfigurationProvider{ + + private ThreadLocal threadedConfig = new ThreadLocal<>(); + + @Override + public Configuration getConfiguration() { + Configuration config = threadedConfig.get(); + if(config!=null){ + return config; + } + return super.getConfiguration(); + } + + @Override + public void setConfiguration(Configuration config) { + if(config==null){ + threadedConfig.remove(); + }else { + threadedConfig.set(config); + } + } +}