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 018package org.apache.logging.log4j.util; 019 020import java.util.Hashtable; 021 022import org.apache.logging.log4j.spi.Provider; 023import org.osgi.framework.BundleActivator; 024import org.osgi.framework.BundleContext; 025import org.osgi.framework.ServiceRegistration; 026 027/** 028 * Utility class to register Log4j2 providers in an OSGI environment. 029 */ 030public abstract class ProviderActivator implements BundleActivator { 031 032 public static final String API_VERSION = "APIVersion"; 033 034 private final Provider provider; 035 private ServiceRegistration<Provider> providerRegistration = null; 036 037 protected ProviderActivator(final Provider provider) { 038 this.provider = provider; 039 } 040 041 @Override 042 public void start(final BundleContext context) throws Exception { 043 final Hashtable<String, String> props = new Hashtable<>(); 044 props.put(API_VERSION, provider.getVersions()); 045 providerRegistration = context.registerService(Provider.class, provider, props); 046 } 047 048 @Override 049 public void stop(BundleContext context) throws Exception { 050 if (providerRegistration != null) { 051 providerRegistration.unregister(); 052 } 053 } 054 055}