1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.osgi.boot.internal.webapp;
14
15 import java.net.URL;
16 import java.util.Dictionary;
17
18 import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
19 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleEvent;
23 import org.osgi.framework.BundleListener;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class JettyContextHandlerExtender implements BundleListener
50 {
51
52
53
54
55
56
57
58 public void bundleChanged(BundleEvent event)
59 {
60 switch (event.getType())
61 {
62 case BundleEvent.STARTED:
63 register(event.getBundle());
64 break;
65 case BundleEvent.STOPPING:
66 unregister(event.getBundle());
67 break;
68 }
69 }
70
71
72
73
74 public void init(BundleContext context)
75 {
76 Bundle bundles[] = context.getBundles();
77 for (int i = 0; i < bundles.length; i++)
78 {
79 if ((bundles[i].getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0)
80 {
81 register(bundles[i]);
82 }
83 }
84 }
85
86 private void register(Bundle bundle)
87 {
88 Dictionary<?, ?> dic = bundle.getHeaders();
89 String warFolderRelativePath = (String)dic.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH);
90 if (warFolderRelativePath != null)
91 {
92 String contextPath = (String)dic.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
93 if (contextPath == null || !contextPath.startsWith("/"))
94 {
95 throw new IllegalArgumentException();
96 }
97
98
99 try
100 {
101 JettyBootstrapActivator.registerWebapplication(bundle,warFolderRelativePath,contextPath);
102 }
103 catch (Throwable e)
104 {
105
106 e.printStackTrace();
107 }
108 }
109 else if (dic.get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH) != null)
110 {
111 String contextFileRelativePath = (String)dic.get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
112 if (contextFileRelativePath == null)
113 {
114
115 return;
116 }
117
118 String[] pathes = contextFileRelativePath.split(",;");
119 for (String path : pathes)
120 {
121 try
122 {
123 JettyBootstrapActivator.registerContext(bundle,path.trim());
124 }
125 catch (Throwable e)
126 {
127
128 e.printStackTrace();
129 }
130 }
131 }
132 else
133 {
134
135
136
137 URL rfc66Webxml = bundle.getEntry("/WEB-INF/web.xml");
138 if (rfc66Webxml == null)
139 {
140 return;
141 }
142
143
144
145
146
147
148
149
150 String rfc66ContextPath = getWebContextPath(bundle,dic);
151 try
152 {
153 JettyBootstrapActivator.registerWebapplication(bundle,".",rfc66ContextPath);
154 }
155 catch (Throwable e)
156 {
157
158 e.printStackTrace();
159 }
160 }
161 }
162
163 private String getWebContextPath(Bundle bundle, Dictionary<?, ?> dic)
164 {
165 String rfc66ContextPath = (String)dic.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
166 if (rfc66ContextPath == null)
167 {
168
169
170
171
172
173 String location = bundle.getLocation();
174 String toks[] = location.replace('\\','/').split("/");
175 rfc66ContextPath = toks[toks.length - 1];
176
177 int lastDot = rfc66ContextPath.lastIndexOf('.');
178 if (lastDot != -1)
179 {
180 rfc66ContextPath = rfc66ContextPath.substring(0,lastDot);
181 }
182 }
183 if (!rfc66ContextPath.startsWith("/"))
184 {
185 rfc66ContextPath = "/" + rfc66ContextPath;
186 }
187 return rfc66ContextPath;
188 }
189
190 private void unregister(Bundle bundle)
191 {
192
193
194
195
196 }
197
198 }