View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.io.File;
22  import java.net.URL;
23  import java.util.Dictionary;
24  import java.util.Hashtable;
25  
26  import org.eclipse.jetty.deploy.App;
27  import org.eclipse.jetty.deploy.AppProvider;
28  import org.eclipse.jetty.deploy.DeploymentManager;
29  import org.eclipse.jetty.server.handler.ContextHandler;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  import org.eclipse.jetty.util.resource.Resource;
33  import org.osgi.framework.Bundle;
34  import org.osgi.framework.FrameworkUtil;
35  import org.osgi.framework.ServiceRegistration;
36  
37  
38  
39  /**
40   * AbstractOSGiApp
41   *
42   * Base class representing info about a webapp/ContextHandler that is deployed into Jetty.
43   * 
44   */
45  public abstract class AbstractOSGiApp extends App
46  {      
47      private static final Logger LOG = Log.getLogger(AbstractOSGiApp.class);
48      
49      protected Bundle _bundle;
50      protected Dictionary _properties;
51      protected ServiceRegistration _registration;
52  
53      /* ------------------------------------------------------------ */
54      public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, String originId)
55      {
56          super(manager, provider, originId);
57          _properties = bundle.getHeaders();
58          _bundle = bundle;
59      }
60      /* ------------------------------------------------------------ */
61      public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String originId)
62      {
63          super(manager, provider, originId);
64          _properties = properties;
65          _bundle = bundle;
66      }
67      
68      /* ------------------------------------------------------------ */
69      public String getBundleSymbolicName()
70      {
71          return _bundle.getSymbolicName();
72      }
73      
74      /* ------------------------------------------------------------ */
75      public String getBundleVersionAsString()
76      {
77         if (_bundle.getVersion() == null)
78             return null;
79         return _bundle.getVersion().toString();
80      }
81      
82      /* ------------------------------------------------------------ */
83      public Bundle getBundle()
84      {
85          return _bundle;
86      }
87      
88      /* ------------------------------------------------------------ */
89      public void setRegistration (ServiceRegistration registration)
90      {
91          _registration = registration;
92      }
93      
94      /* ------------------------------------------------------------ */
95      public ServiceRegistration getRegistration ()
96      {
97          return _registration;
98      }
99      
100     
101     /* ------------------------------------------------------------ */
102     public void registerAsOSGiService() throws Exception
103     {
104         if (_registration == null)
105         {
106             Dictionary<String,String> properties = new Hashtable<String,String>();
107             properties.put(OSGiWebappConstants.WATERMARK, OSGiWebappConstants.WATERMARK);
108             if (getBundleSymbolicName() != null)
109                 properties.put(OSGiWebappConstants.OSGI_WEB_SYMBOLICNAME, getBundleSymbolicName());
110             if (getBundleVersionAsString() != null)
111                 properties.put(OSGiWebappConstants.OSGI_WEB_VERSION, getBundleVersionAsString());
112             properties.put(OSGiWebappConstants.OSGI_WEB_CONTEXTPATH, getContextPath());
113             ServiceRegistration rego = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(ContextHandler.class.getName(), getContextHandler(), properties);
114             setRegistration(rego);
115         }
116     }
117 
118     /* ------------------------------------------------------------ */
119     protected void deregisterAsOSGiService() throws Exception
120     {
121         if (_registration == null)
122             return;
123 
124         _registration.unregister();
125         _registration = null;
126     }
127 
128     protected Resource getFileAsResource (String dir, String file)
129     {
130         Resource r = null;
131         try
132         {
133             File asFile = new File (dir, file);
134             if (asFile.exists())
135                 r = Resource.newResource(asFile);
136         }
137         catch (Exception e)
138         {
139             r = null;
140         } 
141         return r;
142     }
143     
144     protected Resource getFileAsResource (String file)
145     {
146         Resource r = null;
147         try
148         {
149             File asFile = new File (file);
150             if (asFile.exists())
151                 r = Resource.newResource(asFile);
152         }
153         catch (Exception e)
154         {
155             r = null;
156         } 
157         return r;
158     }
159     
160     protected Resource findFile (String fileName, String jettyHome, String bundleOverrideLocation, Bundle containingBundle)
161     {
162         Resource res = null;
163 
164         //try to find the context file in the filesystem
165         if (fileName.startsWith("/"))
166             res = getFileAsResource(fileName);
167         if (res != null)
168             return res;
169 
170         //try to find it relative to jetty home
171         if (jettyHome != null)
172         {
173             if (jettyHome.startsWith("\"") || jettyHome.startsWith("'"))
174                 jettyHome = jettyHome.substring(1);
175             if (jettyHome.endsWith("\"") || (jettyHome.endsWith("'")))
176                 jettyHome = jettyHome.substring(0,jettyHome.length()-1);
177 
178             res = getFileAsResource(jettyHome, fileName); 
179         }
180         if (res != null)
181             return res;
182        
183 
184         //try to find it relative to an override location that has been specified               
185         if (bundleOverrideLocation != null)
186         { 
187             try(Resource location=Resource.newResource(bundleOverrideLocation))
188             {
189                 res=location.addPath(fileName);
190             }
191             catch (Exception e)
192             {
193                 LOG.warn(e);
194             }
195         }        
196         if (res != null)
197             return res;
198         
199         //try to find it relative to the bundle in which it is being deployed
200         if (containingBundle != null)
201         {
202             if (fileName.startsWith("./"))
203                 fileName = fileName.substring(1);
204 
205             if (!fileName.startsWith("/"))
206                 fileName = "/" + fileName;
207 
208             URL entry = _bundle.getEntry(fileName);
209             if (entry != null)
210                 res = Resource.newResource(entry);
211         }
212         
213         return res;
214     }
215 }