Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0d612d8b6bf604ddd5e835d36419e5834f5e2271 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.osgi.boot.utils;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * EventSender
 *
 * Utility class for emiting OSGi EventAdmin events
 * 
 */
public class EventSender
{    
    //OSGi Event Admin events for webapps
    public static final String DEPLOYING_EVENT = "org/osgi/service/web/DEPLOYING";
    public static final String DEPLOYED_EVENT = "org/osgi/service/web/DEPLOYED";
    public static final String UNDEPLOYING_EVENT = "org/osgi/service/web/UNDEPLOYING";
    public static final String UNDEPLOYED_EVENT = "org/osgi/service/web/UNDEPLOYED"; 
    public static final String FAILED_EVENT = "org/osgi/service/web/FAILED"; 
    
    
    private static final EventSender __instance = new EventSender();
    private Bundle _myBundle;
    private EventAdmin _eventAdmin;
    
    
    
    
    /* ------------------------------------------------------------ */
    /**
     * 
     */
    private EventSender ()
    {
        _myBundle = FrameworkUtil.getBundle(EventSender.class);
        ServiceReference ref = _myBundle.getBundleContext().getServiceReference(EventAdmin.class.getName());
        if (ref != null)
            _eventAdmin = (EventAdmin)_myBundle.getBundleContext().getService(ref);
    }
    
    
    

    /* ------------------------------------------------------------ */
    /**
     * @return
     */
    public static EventSender getInstance()
    {
        return __instance;
    }

    
    
    /* ------------------------------------------------------------ */
    /**
     * @param topic
     * @param wab
     * @param contextPath
     */
    public  void send (String topic, Bundle wab, String contextPath)
    {
        if (topic==null || wab==null || contextPath==null)
            return;
        
        send(topic, wab, contextPath, null);
    }
    
    
    
    /* ------------------------------------------------------------ */
    /**
     * @param topic
     * @param wab
     * @param contextPath
     * @param ex
     */
    public  void send (String topic, Bundle wab, String contextPath, Exception ex)
    {        
        if (_eventAdmin == null)
            return; 
        
        Dictionary<String,Object> props = new Hashtable<String,Object>();
        props.put("bundle.symbolicName", wab.getSymbolicName());
        props.put("bundle.id", wab.getBundleId());
        props.put("bundle", wab);
        props.put("bundle.version", wab.getVersion());
        props.put("context.path", contextPath);
        props.put("timestamp", System.currentTimeMillis());
        props.put("extender.bundle", _myBundle);
        props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
        props.put("extender.bundle.id", _myBundle.getBundleId());
        props.put("extender.bundle.version", _myBundle.getVersion());
        
        if (FAILED_EVENT.equalsIgnoreCase(topic)  && ex != null)
            props.put("exception", ex);

        _eventAdmin.sendEvent(new Event(topic, props));
    }
}

Back to the top