1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
40
41 public class BundleContextProvider extends AbstractContextProvider implements BundleProvider
42 {
43 private static final Logger LOG = Log.getLogger(AbstractContextProvider.class);
44
45 private Map<String, App> _appMap = new HashMap<String, App>();
46
47 private Map<Bundle, List<App>> _bundleMap = new HashMap<Bundle, List<App>>();
48
49 private ServiceRegistration _serviceRegForBundles;
50
51
52 public BundleContextProvider(ServerInstanceWrapper wrapper)
53 {
54 super(wrapper);
55 }
56
57
58
59 @Override
60 protected void doStart() throws Exception
61 {
62
63 Dictionary<String,String> properties = new Hashtable<String,String>();
64 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());
65 _serviceRegForBundles = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(BundleProvider.class.getName(), this, properties);
66 super.doStart();
67 }
68
69
70 @Override
71 protected void doStop() throws Exception
72 {
73
74 if (_serviceRegForBundles != null)
75 {
76 try
77 {
78 _serviceRegForBundles.unregister();
79 }
80 catch (Exception e)
81 {
82 LOG.warn(e);
83 }
84 }
85 }
86
87
88
89
90
91 public boolean bundleAdded (Bundle bundle) throws Exception
92 {
93 if (bundle == null)
94 return false;
95
96
97 if ((String)bundle.getHeaders().get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH) != null)
98 {
99 if (LOG.isDebugEnabled()) LOG.debug("BundleContextProvider ignoring bundle {} with {} set", bundle.getSymbolicName(), OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
100 return false;
101 }
102
103 String contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
104 if (contextFiles == null)
105 contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);
106
107 if (contextFiles == null)
108 return false;
109
110
111 boolean added = false;
112
113
114
115 String[] tmp = contextFiles.split("[,;]");
116 for (String contextFile : tmp)
117 {
118 String originId = bundle.getSymbolicName() + "-" + bundle.getVersion().toString() + "-"+contextFile;
119 OSGiApp app = new OSGiApp(getDeploymentManager(), this, originId, bundle, contextFile);
120 _appMap.put(originId,app);
121 List<App> apps = _bundleMap.get(bundle);
122 if (apps == null)
123 {
124 apps = new ArrayList<App>();
125 _bundleMap.put(bundle, apps);
126 }
127 apps.add(app);
128 getDeploymentManager().addApp(app);
129 added = true;
130 }
131
132 return added;
133 }
134
135
136
137
138
139
140
141
142
143 public boolean bundleRemoved (Bundle bundle) throws Exception
144 {
145 List<App> apps = _bundleMap.remove(bundle);
146 boolean removed = false;
147 if (apps != null)
148 {
149 for (App app:apps)
150 {
151 _appMap.remove(app.getOriginId());
152 getDeploymentManager().removeApp(app);
153 removed = true;
154 }
155 }
156 return removed;
157 }
158
159
160 }