View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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   * EventSender
32   *
33   * Utility class for emiting OSGi EventAdmin events
34   * 
35   */
36  public class EventSender
37  {    
38      //OSGi Event Admin events for webapps
39      public static final String DEPLOYING_EVENT = "org/osgi/service/web/DEPLOYING";
40      public static final String DEPLOYED_EVENT = "org/osgi/service/web/DEPLOYED";
41      public static final String UNDEPLOYING_EVENT = "org/osgi/service/web/UNDEPLOYING";
42      public static final String UNDEPLOYED_EVENT = "org/osgi/service/web/UNDEPLOYED"; 
43      public static final String FAILED_EVENT = "org/osgi/service/web/FAILED"; 
44      
45      
46      private static final EventSender __instance = new EventSender();
47      private Bundle _myBundle;
48      private EventAdmin _eventAdmin;
49      
50      
51      
52      
53      /* ------------------------------------------------------------ */
54      /**
55       * 
56       */
57      private EventSender ()
58      {
59          _myBundle = FrameworkUtil.getBundle(EventSender.class);
60          ServiceReference ref = _myBundle.getBundleContext().getServiceReference(EventAdmin.class.getName());
61          if (ref != null)
62              _eventAdmin = (EventAdmin)_myBundle.getBundleContext().getService(ref);
63      }
64      
65      
66      
67  
68      /* ------------------------------------------------------------ */
69      /**
70       * @return
71       */
72      public static EventSender getInstance()
73      {
74          return __instance;
75      }
76  
77      
78      
79      /* ------------------------------------------------------------ */
80      /**
81       * @param topic
82       * @param wab
83       * @param contextPath
84       */
85      public  void send (String topic, Bundle wab, String contextPath)
86      {
87          if (topic==null || wab==null || contextPath==null)
88              return;
89          
90          send(topic, wab, contextPath, null);
91      }
92      
93      
94      
95      /* ------------------------------------------------------------ */
96      /**
97       * @param topic
98       * @param wab
99       * @param contextPath
100      * @param ex
101      */
102     public  void send (String topic, Bundle wab, String contextPath, Exception ex)
103     {        
104         if (_eventAdmin == null)
105             return; 
106         
107         Dictionary<String,Object> props = new Hashtable<String,Object>();
108         props.put("bundle.symbolicName", wab.getSymbolicName());
109         props.put("bundle.id", wab.getBundleId());
110         props.put("bundle", wab);
111         props.put("bundle.version", wab.getVersion());
112         props.put("context.path", contextPath);
113         props.put("timestamp", System.currentTimeMillis());
114         props.put("extender.bundle", _myBundle);
115         props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
116         props.put("extender.bundle.id", _myBundle.getBundleId());
117         props.put("extender.bundle.version", _myBundle.getVersion());
118         
119         if (FAILED_EVENT.equalsIgnoreCase(topic)  && ex != null)
120             props.put("exception", ex);
121 
122         _eventAdmin.sendEvent(new Event(topic, props));
123     }
124 }