View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.osgi.boot.utils;
20  
21  import java.util.Dictionary;
22  import java.util.Hashtable;
23  
24  import org.osgi.framework.Bundle;
25  import org.osgi.framework.FrameworkUtil;
26  import org.osgi.framework.ServiceReference;
27  import org.osgi.service.event.Event;
28  import org.osgi.service.event.EventAdmin;
29  
30  /**
31   * Utility class for emiting OSGi EventAdmin events
32   */
33  public class EventSender
34  {    
35      //OSGi Event Admin events for webapps
36      public static final String DEPLOYING_EVENT = "org/osgi/service/web/DEPLOYING";
37      public static final String DEPLOYED_EVENT = "org/osgi/service/web/DEPLOYED";
38      public static final String UNDEPLOYING_EVENT = "org/osgi/service/web/UNDEPLOYING";
39      public static final String UNDEPLOYED_EVENT = "org/osgi/service/web/UNDEPLOYED"; 
40      public static final String FAILED_EVENT = "org/osgi/service/web/FAILED"; 
41      
42      private static final EventSender __instance = new EventSender();
43      private Bundle _myBundle;
44      private EventAdmin _eventAdmin;
45      
46      private EventSender ()
47      {
48          _myBundle = FrameworkUtil.getBundle(EventSender.class);
49          ServiceReference ref = _myBundle.getBundleContext().getServiceReference(EventAdmin.class.getName());
50          if (ref != null)
51              _eventAdmin = (EventAdmin)_myBundle.getBundleContext().getService(ref);
52      }
53      
54      public static EventSender getInstance()
55      {
56          return __instance;
57      }
58  
59      public  void send (String topic, Bundle wab, String contextPath)
60      {
61          if (topic==null || wab==null || contextPath==null)
62              return;
63          
64          send(topic, wab, contextPath, null);
65      }
66      
67      public  void send (String topic, Bundle wab, String contextPath, Exception ex)
68      {        
69          if (_eventAdmin == null)
70              return; 
71          
72          Dictionary<String,Object> props = new Hashtable<String,Object>();
73          props.put("bundle.symbolicName", wab.getSymbolicName());
74          props.put("bundle.id", wab.getBundleId());
75          props.put("bundle", wab);
76          props.put("bundle.version", wab.getVersion());
77          props.put("context.path", contextPath);
78          props.put("timestamp", System.currentTimeMillis());
79          props.put("extender.bundle", _myBundle);
80          props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
81          props.put("extender.bundle.id", _myBundle.getBundleId());
82          props.put("extender.bundle.version", _myBundle.getVersion());
83          
84          if (FAILED_EVENT.equalsIgnoreCase(topic)  && ex != null)
85              props.put("exception", ex);
86  
87          _eventAdmin.sendEvent(new Event(topic, props));
88      }
89  }