Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 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 | */ |
Richard S. Hall | 44002cf | 2009-02-11 21:47:26 +0000 | [diff] [blame] | 19 | package org.apache.felix.log; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 20 | |
| 21 | import org.osgi.framework.BundleActivator; |
| 22 | import org.osgi.framework.BundleContext; |
| 23 | import org.osgi.service.log.LogReaderService; |
| 24 | import org.osgi.service.log.LogService; |
| 25 | |
| 26 | /** |
| 27 | * The bundle activator for the OSGi log service (see section 101 of the service |
| 28 | * compendium). |
| 29 | * <p> |
| 30 | * The log service provides a general purpose message logger for the OSGi service |
| 31 | * platform. It consists of two services, one for logging information and another |
| 32 | * for retrieving current or previously recorded log information. |
| 33 | * <p> |
| 34 | * The service knows about the following properties which are read at bundle |
| 35 | * startup: |
| 36 | * <dl> |
| 37 | * <dt>org.apache.felix.log.maxSize</dt> |
| 38 | * <dd>Determines the maximum size of the log used to maintain historic |
| 39 | * log information. A value of -1 means the log has no maximum size; |
| 40 | * a value of 0 means that no historic log information will be maintained. |
| 41 | * The default value is 100.</dd> |
| 42 | * |
| 43 | * <dt>org.apache.felix.log.storeDebug</dt> |
| 44 | * <dd>Determines whether or not debug messages will be stored as part of |
| 45 | * the historic log information. The default value is false.</dd> |
| 46 | * </dl> |
| 47 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 48 | public final class Activator implements BundleActivator |
| 49 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 50 | /** The name of the property that defines the maximum size of the log. */ |
| 51 | private static final String MAX_SIZE_PROPERTY = "org.apache.felix.log.maxSize"; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 52 | /** The default value for the maximum size property. */ |
| 53 | private static final int DEFAULT_MAX_SIZE = 100; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 54 | /** The name of the property that defines whether debug messages are stored. */ |
| 55 | private static final String STORE_DEBUG_PROPERTY = "org.apache.felix.log.storeDebug"; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 56 | /** The default value for the store debug property. */ |
| 57 | private static final boolean DEFAULT_STORE_DEBUG = false; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 58 | /** The log. */ |
| 59 | private Log m_log; |
| 60 | |
| 61 | /** |
| 62 | * Returns the maximum size for the log. |
| 63 | * @param context the bundle context (used to look up a property) |
| 64 | * @return the maximum size for the log |
| 65 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 66 | private static int getMaxSize(final BundleContext context) |
| 67 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 68 | int maxSize = DEFAULT_MAX_SIZE; |
| 69 | |
| 70 | String maxSizePropValue = context.getProperty(MAX_SIZE_PROPERTY); |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 71 | if (maxSizePropValue != null) |
| 72 | { |
| 73 | try |
| 74 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 75 | maxSize = Integer.parseInt(maxSizePropValue); |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 76 | } |
| 77 | catch (NumberFormatException e) |
| 78 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 79 | // the property value is invalid - ignore |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return maxSize; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns whether or not to store debug messages. |
| 88 | * @param context the bundle context (used to look up a property) |
| 89 | * @return whether or not to store debug messages |
| 90 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 91 | private static boolean getStoreDebug(final BundleContext context) |
| 92 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 93 | boolean storeDebug = DEFAULT_STORE_DEBUG; |
| 94 | |
| 95 | String storeDebugPropValue = context.getProperty(STORE_DEBUG_PROPERTY); |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 96 | if (storeDebugPropValue != null) |
| 97 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 98 | storeDebug = Boolean.valueOf(storeDebugPropValue).booleanValue(); |
| 99 | } |
| 100 | |
| 101 | return storeDebug; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Called by the OSGi framework when the bundle is started. |
| 106 | * Used to register the service implementations with the framework. |
| 107 | * @param context the bundle context |
| 108 | * @throws Exception if an error occurs |
| 109 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 110 | public void start(final BundleContext context) throws Exception |
| 111 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 112 | // create the log instance |
| 113 | m_log = new Log(getMaxSize(context), getStoreDebug(context)); |
| 114 | |
| 115 | // register the listeners |
| 116 | context.addBundleListener(m_log); |
| 117 | context.addFrameworkListener(m_log); |
| 118 | context.addServiceListener(m_log); |
| 119 | |
| 120 | // register the services with the framework |
| 121 | context.registerService(LogService.class.getName(), |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 122 | new LogServiceFactory(m_log), null); |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 123 | |
| 124 | context.registerService(LogReaderService.class.getName(), |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 125 | new LogReaderServiceFactory(m_log), null); |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Called by the OSGi framework when the bundle is stopped. |
| 130 | * @param context the bundle context |
| 131 | * @throws Exception if an error occurs |
| 132 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 133 | public void stop(final BundleContext context) throws Exception |
| 134 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 135 | // close the log |
| 136 | m_log.close(); |
| 137 | } |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 138 | } |