blob: d0e17cb6dded885c8f3c6f99dc805408801e720f [file] [log] [blame]
Humberto Cervantes Macedabd7de442006-04-04 16:05:02 +00001/*
2 * Copyright 2006 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.felix.wireadmin;
18
19import java.util.Dictionary;
20import java.util.Properties;
21
22import org.osgi.framework.BundleActivator;
23import org.osgi.framework.BundleContext;
24import org.osgi.framework.BundleException;
25import org.osgi.framework.ServiceRegistration;
26import org.osgi.service.wireadmin.WireAdmin;
27import org.osgi.service.wireadmin.WireConstants;
28import org.osgi.service.wireadmin.WireAdminListener;
29import org.osgi.service.wireadmin.WireAdminEvent;
30/**
31 * The activator
32 *
33 * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
34 */
35public class Activator implements BundleActivator {
36
37 private final static String WIREADMIN_PID="org.apache.felix.wireadmin";
38 private ServiceRegistration m_reg=null;
39 private WireAdminImpl m_wai=null;
40
41 /**
42 * Called upon starting of the bundle.
43 *
44 * @param context The bundle context passed by the framework
45 * @exception Exception
46 */
47 public void start(BundleContext bundleContext) throws BundleException {
48
49 m_wai= new WireAdminImpl(bundleContext);
50
51 // Register the service
52 Dictionary properties=new Properties();
53 properties.put(WireConstants.WIREADMIN_PID,WIREADMIN_PID);
54 m_reg = bundleContext.registerService(WireAdmin.class.getName(),m_wai,properties);
55
56 // Event dispatching does not start until the reference is set
57 m_wai.setServiceReference(m_reg.getReference());
58
59 if(bundleContext.getProperty("fr.imag.adele.wireadmin.traceEvt") != null)
60 {
61 String value = bundleContext.getProperty("fr.imag.adele.wireadmin.traceEvt");
62 if(value.equals("true"))
63 {
64 Dictionary props=new Properties();
65 props.put(WireConstants.WIREADMIN_EVENTS,new Integer(0x80|0x40|0x20|0x10|0x08|0x04|0x02|0x01));
66 properties.put(WireConstants.WIREADMIN_PID,WIREADMIN_PID);
67 bundleContext.registerService(WireAdminListener.class.getName(),new eventTracer(),props);
68 }
69 }
70 }
71
72 /**
73 * Called upon stopping the bundle.
74 *
75 * @param context The bundle context passed by the framework
76 * @exception Exception
77 */
78 public void stop(BundleContext bundleContext) throws BundleException
79 {
80 m_wai.releaseAll();
81 m_wai = null;
82 }
83
84 class eventTracer implements WireAdminListener
85 {
86 public void wireAdminEvent(WireAdminEvent evt)
87 {
88 int type = evt.getType();
89 if((type & WireAdminEvent.WIRE_CREATED)!=0)
90 {
91 WireAdminImpl.traceln("Received event WIRE_CREATED");
92 }
93 if((type & WireAdminEvent.WIRE_CONNECTED)!=0)
94 {
95 WireAdminImpl.traceln("Received event WIRE_CONNECTED");
96 }
97 if((type & WireAdminEvent.WIRE_UPDATED)!=0)
98 {
99 WireAdminImpl.traceln("Received event WIRE_UPDATED");
100 }
101 if((type & WireAdminEvent.WIRE_TRACE)!=0)
102 {
103 WireAdminImpl.traceln("Received event WIRE_TRACE");
104 }
105 if((type & WireAdminEvent.WIRE_DISCONNECTED)!=0)
106 {
107 WireAdminImpl.traceln("Received event WIRE_DISCONNECTED");
108 }
109 if((type & WireAdminEvent.WIRE_DELETED)!=0)
110 {
111 WireAdminImpl.traceln("Received event WIRE_DELETED");
112 }
113 if((type & WireAdminEvent.PRODUCER_EXCEPTION)!=0)
114 {
115 WireAdminImpl.traceln("Received event PRODUCER_EXCEPTION");
116 evt.getThrowable().printStackTrace();
117 }
118 if((type & WireAdminEvent.CONSUMER_EXCEPTION)!=0)
119 {
120 WireAdminImpl.traceln("Received event CONSUMER_EXCEPTION");
121 evt.getThrowable().printStackTrace();
122 }
123 }
124 }
125}