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
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
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
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
101
102
103
104 public boolean bundleAdded (Bundle bundle) throws Exception
105 {
106 if (bundle == null)
107 return false;
108
109
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
126
127
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;
146 }
147
148
149
150
151
152
153
154
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;
170 }
171
172
173 }