001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.geronimo.osgi.registry; 020 021 import java.util.ArrayList; 022 import java.util.List; 023 024 import org.osgi.framework.Bundle; 025 import org.osgi.framework.BundleActivator; 026 import org.osgi.framework.BundleContext; 027 import org.osgi.framework.ServiceRegistration; 028 import org.osgi.framework.ServiceReference; 029 import org.osgi.service.log.LogService; 030 import org.osgi.util.tracker.BundleTracker; 031 import org.osgi.util.tracker.ServiceTracker; 032 import org.osgi.util.tracker.ServiceTrackerCustomizer; 033 034 import org.apache.geronimo.osgi.registry.api.ProviderRegistry; 035 036 /** 037 * The activator that starts and manages the life-cycle of 038 * the class factory registry. 039 */ 040 public class Activator implements BundleActivator { 041 // tracker to watch for bundle updates 042 protected BundleTracker bt; 043 // service tracker for a logging service 044 protected ServiceTracker lst; 045 // Our provider registry 046 protected ProviderRegistryImpl registry; 047 // The service registration for the provider registry 048 protected ServiceRegistration registryRegistration; 049 // our bundle context 050 protected BundleContext context; 051 // an array of all active logging services. 052 List<LogService> logServices = new ArrayList<LogService>(); 053 054 055 @Override 056 public synchronized void start(final BundleContext context) throws Exception { 057 this.context = context; 058 lst = new LogServiceTracker(context, LogService.class.getName(), null); 059 lst.open(); 060 061 registry = new ProviderRegistryImpl(this); 062 // register this as a service 063 registryRegistration = context.registerService(ProviderRegistry.class.getName(), registry, null); 064 065 org.osgi.framework.ServiceReference ref = context.getServiceReference(ProviderRegistry.class.getName()); 066 067 bt = new BundleTracker(context, Bundle.ACTIVE, new ProviderBundleTrackerCustomizer(this, context.getBundle(), registry)); 068 bt.open(); 069 } 070 071 @Override 072 public synchronized void stop(BundleContext context) throws Exception { 073 bt.close(); 074 lst.close(); 075 registryRegistration.unregister(); 076 } 077 078 void log(int level, String message) { 079 synchronized (logServices) { 080 for (LogService log : logServices) { 081 log.log(level, message); 082 } 083 } 084 } 085 086 void log(int level, String message, Throwable th) { 087 synchronized (logServices) { 088 for (LogService log : logServices) { 089 log.log(level, message, th); 090 } 091 } 092 } 093 094 private final class LogServiceTracker extends ServiceTracker { 095 private LogServiceTracker(BundleContext context, String clazz, 096 ServiceTrackerCustomizer customizer) { 097 super(context, clazz, customizer); 098 } 099 100 @Override 101 public Object addingService(ServiceReference reference) { 102 Object svc = super.addingService(reference); 103 if (svc instanceof LogService) { 104 synchronized (logServices) { 105 logServices.add((LogService) svc); 106 } 107 } 108 return svc; 109 } 110 111 @Override 112 public void removedService(ServiceReference reference, Object service) { 113 synchronized (logServices) { 114 logServices.remove(service); 115 } 116 super.removedService(reference, service); 117 } 118 } 119 }