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.deploy.DeploymentManager;
30 import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
31 import org.eclipse.jetty.osgi.boot.utils.EventSender;
32 import org.eclipse.jetty.server.handler.ContextHandler;
33 import org.eclipse.jetty.util.log.Log;
34 import org.eclipse.jetty.util.log.Logger;
35 import org.eclipse.jetty.webapp.WebAppContext;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
39 import org.osgi.framework.ServiceRegistration;
40
41
42
43
44
45
46
47
48
49
50 public class BundleContextProvider extends AbstractContextProvider implements BundleProvider
51 {
52 private static final Logger LOG = Log.getLogger(AbstractContextProvider.class);
53
54
55 private Map<String, App> _appMap = new HashMap<String, App>();
56
57 private Map<Bundle, List<App>> _bundleMap = new HashMap<Bundle, List<App>>();
58
59 private ServiceRegistration _serviceRegForBundles;
60
61
62
63
64
65 public BundleContextProvider(ServerInstanceWrapper wrapper)
66 {
67 super(wrapper);
68 }
69
70
71
72 @Override
73 protected void doStart() throws Exception
74 {
75
76 Dictionary<String,String> properties = new Hashtable<String,String>();
77 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());
78 _serviceRegForBundles = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(BundleProvider.class.getName(), this, properties);
79 super.doStart();
80 }
81
82
83 @Override
84 protected void doStop() throws Exception
85 {
86
87 if (_serviceRegForBundles != null)
88 {
89 try
90 {
91 _serviceRegForBundles.unregister();
92 }
93 catch (Exception e)
94 {
95 LOG.warn(e);
96 }
97 }
98 }
99
100
101
102
103
104
105
106
107
108
109 public boolean bundleAdded (Bundle bundle) throws Exception
110 {
111 if (bundle == null)
112 return false;
113
114 String contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
115 if (contextFiles == null)
116 contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);
117
118 if (contextFiles == null)
119 return false;
120
121 boolean added = false;
122
123
124
125 String[] tmp = contextFiles.split(",;");
126 for (String contextFile : tmp)
127 {
128 String originId = bundle.getSymbolicName() + "-" + bundle.getVersion().toString() + "-"+contextFile;
129 OSGiApp app = new OSGiApp(getDeploymentManager(), this, originId, bundle, contextFile);
130 _appMap.put(originId,app);
131 List<App> apps = _bundleMap.get(bundle);
132 if (apps == null)
133 {
134 apps = new ArrayList<App>();
135 _bundleMap.put(bundle, apps);
136 }
137 apps.add(app);
138 getDeploymentManager().addApp(app);
139 }
140
141 return added;
142 }
143
144
145
146
147
148
149
150
151
152 public boolean bundleRemoved (Bundle bundle) throws Exception
153 {
154 List<App> apps = _bundleMap.remove(bundle);
155 boolean removed = false;
156 if (apps != null)
157 {
158 for (App app:apps)
159 {
160 _appMap.remove(app.getOriginId());
161 getDeploymentManager().removeApp(app);
162 removed = true;
163 }
164 }
165 return removed;
166 }
167
168
169 }