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;
20  
21  import java.util.ArrayList;
22  import java.util.Dictionary;
23  import java.util.HashMap;
24  import java.util.Hashtable;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.jetty.deploy.App;
29  import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  import org.osgi.framework.Bundle;
33  import org.osgi.framework.FrameworkUtil;
34  import org.osgi.framework.ServiceRegistration;
35  
36  
37  
38  /**
39   * BundleContextProvider
40   *
41   * Handles deploying OSGi bundles that define a context xml file for configuring them.
42   * 
43   *
44   */
45  public class BundleContextProvider extends AbstractContextProvider implements BundleProvider
46  {    
47      private static final Logger LOG = Log.getLogger(AbstractContextProvider.class);
48      
49  
50      private Map<String, App> _appMap = new HashMap<String, App>();
51      
52      private Map<Bundle, List<App>> _bundleMap = new HashMap<Bundle, List<App>>();
53      
54      private ServiceRegistration _serviceRegForBundles;
55      
56  
57      
58    
59      /* ------------------------------------------------------------ */
60      public BundleContextProvider(ServerInstanceWrapper wrapper)
61      {
62          super(wrapper);
63      }
64      
65      
66      /* ------------------------------------------------------------ */
67      @Override
68      protected void doStart() throws Exception
69      {
70          //register as an osgi service for deploying contexts defined in a bundle, advertising the name of the jetty Server instance we are related to
71          Dictionary<String,String> properties = new Hashtable<String,String>();
72          properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());
73          _serviceRegForBundles = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(BundleProvider.class.getName(), this, properties);
74          super.doStart();
75      }
76  
77      /* ------------------------------------------------------------ */
78      @Override
79      protected void doStop() throws Exception
80      {
81          //unregister ourselves
82          if (_serviceRegForBundles != null)
83          {
84              try
85              {
86                  _serviceRegForBundles.unregister();
87              }
88              catch (Exception e)
89              {
90                  LOG.warn(e);
91              }
92          }
93      }
94  
95  
96  
97  
98      /* ------------------------------------------------------------ */
99      /**
100      * @param bundle
101      * @param contextFiles
102      * @return
103      */
104     public boolean bundleAdded (Bundle bundle) throws Exception
105     {
106         if (bundle == null)
107             return false;
108 
109         String contextFiles  = (String)bundle.getHeaders().get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
110         if (contextFiles == null)
111             contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);
112         
113         if (contextFiles == null)
114             return false;
115         
116         boolean added = false;
117         //bundle defines JETTY_CONTEXT_FILE_PATH header,
118         //a comma separated list of context xml files that each define a ContextHandler
119         //TODO: (could be WebAppContexts)       
120         String[] tmp = contextFiles.split(",;");
121         for (String contextFile : tmp)
122         {
123             String originId = bundle.getSymbolicName() + "-" + bundle.getVersion().toString() + "-"+contextFile;
124             OSGiApp app = new OSGiApp(getDeploymentManager(), this, originId, bundle, contextFile);
125             _appMap.put(originId,app);
126             List<App> apps = _bundleMap.get(bundle);
127             if (apps == null)
128             {
129                 apps = new ArrayList<App>();
130                 _bundleMap.put(bundle, apps);
131             }
132             apps.add(app);
133             getDeploymentManager().addApp(app);
134             added = true;
135         }
136 
137         return added; //true if even 1 context from this bundle was added
138     }
139     
140     
141     /* ------------------------------------------------------------ */
142     /** 
143      * Bundle has been removed. If it was a context we deployed, undeploy it.
144      * @param bundle
145      * 
146      * @return true if this was a context we had deployed, false otherwise
147      */
148     public boolean bundleRemoved (Bundle bundle) throws Exception
149     {
150         List<App> apps = _bundleMap.remove(bundle);
151         boolean removed = false;
152         if (apps != null)
153         {
154             for (App app:apps)
155             {
156                 _appMap.remove(app.getOriginId());
157                 getDeploymentManager().removeApp(app);
158                 removed = true;
159             }
160         }
161         return removed; //true if even 1 context was removed associated with this bundle
162     }
163     
164    
165 }