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 String contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
110 if (contextFiles == null)
111 contextFiles = (String)bundle.getHeaders().get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);
112
113 if (contextFiles == null)
114 return false;
115
116 boolean added = false;
117
118
119
120 String[] tmp = contextFiles.split(",;");
121 for (String contextFile : tmp)
122 {
123 String originId = bundle.getSymbolicName() + "-" + bundle.getVersion().toString() + "-"+contextFile;
124 OSGiApp app = new OSGiApp(getDeploymentManager(), this, originId, bundle, contextFile);
125 _appMap.put(originId,app);
126 List<App> apps = _bundleMap.get(bundle);
127 if (apps == null)
128 {
129 apps = new ArrayList<App>();
130 _bundleMap.put(bundle, apps);
131 }
132 apps.add(app);
133 getDeploymentManager().addApp(app);
134 added = true;
135 }
136
137 return added;
138 }
139
140
141
142
143
144
145
146
147
148 public boolean bundleRemoved (Bundle bundle) throws Exception
149 {
150 List<App> apps = _bundleMap.remove(bundle);
151 boolean removed = false;
152 if (apps != null)
153 {
154 for (App app:apps)
155 {
156 _appMap.remove(app.getOriginId());
157 getDeploymentManager().removeApp(app);
158 removed = true;
159 }
160 }
161 return removed;
162 }
163
164
165 }