View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Intalio, Inc.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.osgi.boot.utils.internal;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.osgi.framework.Bundle;
19  import org.osgi.framework.BundleActivator;
20  import org.osgi.framework.BundleContext;
21  import org.osgi.framework.InvalidSyntaxException;
22  import org.osgi.framework.ServiceEvent;
23  import org.osgi.framework.ServiceListener;
24  import org.osgi.framework.ServiceReference;
25  import org.osgi.service.packageadmin.PackageAdmin;
26  
27  /**
28   * When the PackageAdmin service is activated we can look for the fragments
29   * attached to this bundle and "activate" them.
30   * 
31   */
32  public class PackageAdminServiceTracker implements ServiceListener
33  {
34      private BundleContext _context;
35  
36      private List<BundleActivator> _activatedFragments = new ArrayList<BundleActivator>();
37      private boolean _fragmentsWereActivated = false;
38  
39      public PackageAdminServiceTracker(BundleContext context)
40      {
41          _context = context;
42          if (!setup())
43          {
44              try
45              {
46                  _context.addServiceListener(this,"(objectclass=" + PackageAdmin.class.getName() + ")");
47              }
48              catch (InvalidSyntaxException e)
49              {
50                  e.printStackTrace(); // won't happen
51              }
52          }
53      }
54  
55      /**
56       * @return true if the fragments were activated by this method.
57       */
58      private boolean setup()
59      {
60          ServiceReference sr = _context.getServiceReference(PackageAdmin.class.getName());
61          _fragmentsWereActivated = sr != null;
62          if (sr != null)
63              invokeFragmentActivators(sr);
64          return _fragmentsWereActivated;
65      }
66  
67      /**
68       * Invokes the optional BundleActivator in each fragment. By convention the
69       * bundle activator for a fragment must be in the package that is defined by
70       * the symbolic name of the fragment and the name of the class must be
71       * 'FragmentActivator'.
72       * 
73       * @param event
74       *            The <code>ServiceEvent</code> object.
75       */
76      public void serviceChanged(ServiceEvent event)
77      {
78          if (event.getType() == ServiceEvent.REGISTERED)
79          {
80              invokeFragmentActivators(event.getServiceReference());
81          }
82      }
83  
84      private void invokeFragmentActivators(ServiceReference sr)
85      {
86          PackageAdmin admin = (PackageAdmin)_context.getService(sr);
87          Bundle[] fragments = admin.getFragments(_context.getBundle());
88          if (fragments == null)
89          {
90              return;
91          }
92          for (Bundle frag : fragments)
93          {
94              // find a convention to look for a class inside the fragment.
95              try
96              {
97                  String fragmentActivator = frag.getSymbolicName() + ".FragmentActivator";
98                  Class<?> c = Class.forName(fragmentActivator);
99                  if (c != null)
100                 {
101                     BundleActivator bActivator = (BundleActivator)c.newInstance();
102                     bActivator.start(_context);
103                     _activatedFragments.add(bActivator);
104                 }
105             }
106             catch (NullPointerException e)
107             {
108                 // e.printStackTrace();
109             }
110             catch (InstantiationException e)
111             {
112                 // e.printStackTrace();
113             }
114             catch (IllegalAccessException e)
115             {
116                 // e.printStackTrace();
117             }
118             catch (ClassNotFoundException e)
119             {
120                 // e.printStackTrace();
121             }
122             catch (Exception e)
123             {
124                 e.printStackTrace();
125             }
126         }
127     }
128 
129     public void stop()
130     {
131         for (BundleActivator fragAct : _activatedFragments)
132         {
133             try
134             {
135                 fragAct.stop(_context);
136             }
137             catch (Exception e)
138             {
139                 e.printStackTrace();
140             }
141         }
142     }
143 
144 }