View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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         //If the bundle defines a Web-ContextPath then its probably a webapp and the BundleWebAppProvider should deploy it
110         if ((String)bundle.getHeaders().get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH) != null)
111         {
112             if (LOG.isDebugEnabled()) LOG.debug("BundleContextProvider ignoring bundle {} with {} set", bundle.getSymbolicName(), OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
113             return false;
114         }
115         
116         String contextFiles  = (String)bundle.getHeaders().get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
117         if (contextFiles == null)
118             contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);
119         
120         if (contextFiles == null)
121             return false;
122         
123         
124         boolean added = false;
125         //bundle defines JETTY_CONTEXT_FILE_PATH header,
126         //a comma separated list of context xml files that each define a ContextHandler
127         //TODO: (could be WebAppContexts)       
128         String[] tmp = contextFiles.split(",;");
129         for (String contextFile : tmp)
130         {
131             String originId = bundle.getSymbolicName() + "-" + bundle.getVersion().toString() + "-"+contextFile;
132             OSGiApp app = new OSGiApp(getDeploymentManager(), this, originId, bundle, contextFile);
133             _appMap.put(originId,app);
134             List<App> apps = _bundleMap.get(bundle);
135             if (apps == null)
136             {
137                 apps = new ArrayList<App>();
138                 _bundleMap.put(bundle, apps);
139             }
140             apps.add(app);
141             getDeploymentManager().addApp(app);
142             added = true;
143         }
144 
145         return added; //true if even 1 context from this bundle was added
146     }
147     
148     
149     /* ------------------------------------------------------------ */
150     /** 
151      * Bundle has been removed. If it was a context we deployed, undeploy it.
152      * @param bundle
153      * 
154      * @return true if this was a context we had deployed, false otherwise
155      */
156     public boolean bundleRemoved (Bundle bundle) throws Exception
157     {
158         List<App> apps = _bundleMap.remove(bundle);
159         boolean removed = false;
160         if (apps != null)
161         {
162             for (App app:apps)
163             {
164                 _appMap.remove(app.getOriginId());
165                 getDeploymentManager().removeApp(app);
166                 removed = true;
167             }
168         }
169         return removed; //true if even 1 context was removed associated with this bundle
170     }
171     
172    
173 }