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 javax.security.auth.login.FailedLoginException;
25  
26  import org.osgi.framework.Bundle;
27  import org.osgi.framework.FrameworkUtil;
28  import org.osgi.framework.ServiceReference;
29  import org.osgi.service.event.Event;
30  import org.osgi.service.event.EventAdmin;
31  
32  public class EventSender
33  {    
34      //OSGi Event Admin events for webapps
35      public static final String DEPLOYING_EVENT = "org/osgi/service/web/DEPLOYING";
36      public static final String DEPLOYED_EVENT = "org/osgi/service/web/DEPLOYED";
37      public static final String UNDEPLOYING_EVENT = "org/osgi/service/web/UNDEPLOYING";
38      public static final String UNDEPLOYED_EVENT = "org/osgi/service/web/UNDEPLOYED"; 
39      public static final String FAILED_EVENT = "org/osgi/service/web/FAILED"; 
40      
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      
55      public static EventSender getInstance()
56      {
57          return __instance;
58      }
59  
60      public  void send (String topic, Bundle wab, String contextPath)
61      {
62          if (topic==null || wab==null || contextPath==null)
63              return;
64          
65          send(topic, wab, contextPath, null);
66      }
67      
68      
69      public  void send (String topic, Bundle wab, String contextPath, Exception ex)
70      {        
71          if (_eventAdmin == null)
72              return; 
73          
74          Dictionary<String,Object> props = new Hashtable<String,Object>();
75          props.put("bundle.symbolicName", wab.getSymbolicName());
76          props.put("bundle.id", wab.getBundleId());
77          props.put("bundle", wab);
78          props.put("bundle.version", wab.getVersion());
79          props.put("context.path", contextPath);
80          props.put("timestamp", System.currentTimeMillis());
81          props.put("extender.bundle", _myBundle);
82          props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
83          props.put("extender.bundle.id", _myBundle.getBundleId());
84          props.put("extender.bundle.version", _myBundle.getVersion());
85          
86          if (FAILED_EVENT.equalsIgnoreCase(topic)  && ex != null)
87              props.put("exception", ex);
88  
89          _eventAdmin.sendEvent(new Event(topic, props));
90      }
91  }