blob: 9def2e83120110697175f34f431b1a540ca7e895 [file] [log] [blame]
Richard S. Hallddf2e142009-09-30 17:03:45 +00001/*
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +00002 * 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. Hall44002cf2009-02-11 21:47:26 +000019package org.apache.felix.log;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000020
21import org.osgi.framework.BundleActivator;
22import org.osgi.framework.BundleContext;
23import org.osgi.service.log.LogReaderService;
24import 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. Hallddf2e142009-09-30 17:03:45 +000048public final class Activator implements BundleActivator
49{
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000050 /** 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. Hall0b8e3ba2006-10-25 13:26:32 +000052 /** The default value for the maximum size property. */
53 private static final int DEFAULT_MAX_SIZE = 100;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000054 /** 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. Hall0b8e3ba2006-10-25 13:26:32 +000056 /** The default value for the store debug property. */
57 private static final boolean DEFAULT_STORE_DEBUG = false;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000058 /** 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. Hallddf2e142009-09-30 17:03:45 +000066 private static int getMaxSize(final BundleContext context)
67 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000068 int maxSize = DEFAULT_MAX_SIZE;
69
70 String maxSizePropValue = context.getProperty(MAX_SIZE_PROPERTY);
Richard S. Hallddf2e142009-09-30 17:03:45 +000071 if (maxSizePropValue != null)
72 {
73 try
74 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000075 maxSize = Integer.parseInt(maxSizePropValue);
Richard S. Hallddf2e142009-09-30 17:03:45 +000076 }
77 catch (NumberFormatException e)
78 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000079 // 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. Hallddf2e142009-09-30 17:03:45 +000091 private static boolean getStoreDebug(final BundleContext context)
92 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000093 boolean storeDebug = DEFAULT_STORE_DEBUG;
94
95 String storeDebugPropValue = context.getProperty(STORE_DEBUG_PROPERTY);
Richard S. Hallddf2e142009-09-30 17:03:45 +000096 if (storeDebugPropValue != null)
97 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000098 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. Hallddf2e142009-09-30 17:03:45 +0000110 public void start(final BundleContext context) throws Exception
111 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000112 // 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. Hallddf2e142009-09-30 17:03:45 +0000122 new LogServiceFactory(m_log), null);
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000123
124 context.registerService(LogReaderService.class.getName(),
Richard S. Hallddf2e142009-09-30 17:03:45 +0000125 new LogReaderServiceFactory(m_log), null);
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000126 }
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. Hallddf2e142009-09-30 17:03:45 +0000133 public void stop(final BundleContext context) throws Exception
134 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000135 // close the log
136 m_log.close();
137 }
Richard S. Hallddf2e142009-09-30 17:03:45 +0000138}