blob: 42cfefe83600cae117a553e6fcdb4d652aa96b6f [file] [log] [blame]
Richard S. Hall515592e2005-11-08 08:57:20 +00001/*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.felix.framework;
18
19import java.io.IOException;
20import java.net.ContentHandler;
21import java.net.URLConnection;
22import java.util.HashMap;
23import java.util.Map;
24
25import org.osgi.framework.BundleContext;
26import org.osgi.service.url.URLConstants;
27
28/**
29 * <p>
30 * This class implements a content handler proxy. When the content handler
31 * proxy instance is created, it is associated with a particular mime type
32 * and will answer all future requests for content of that type. It does
33 * not directly handle the content requests, but delegates the requests to
34 * an underlying content handler service.
35 * </p>
36 * <p>
37 * The proxy for a particular mime type is used for all framework instances
38 * that may contain their own content handler services. When performing a
39 * content handler operation, the proxy retrieves the handler service from
40 * the framework instance associated with the current call stack and delegates
41 * the call to the handler service.
42 * </p>
43 * <p>
44 * The proxy will create simple content handler service trackers for each
45 * framework instance. The trackers will listen to service events in its
46 * respective framework instance to maintain a reference to the "best"
47 * content handler service at any given time.
48 * </p>
49**/
50class URLHandlersContentHandlerProxy extends ContentHandler
51{
52 private Map m_trackerMap = new HashMap();
53 private String m_mimeType = null;
54
55 public URLHandlersContentHandlerProxy(String mimeType)
56 {
57 m_mimeType = mimeType;
58 }
59
60 //
61 // ContentHandler interface method.
62 //
63
64 public synchronized Object getContent(URLConnection urlc) throws IOException
65 {
66 ContentHandler svc = getContentHandlerService();
67 if (svc == null)
68 {
69 throw new IOException("Content handler unavailable: "
70 + urlc.getContentType());
71 }
72 return svc.getContent(urlc);
73 }
74
75 /**
76 * <p>
77 * Private method to retrieve the content handler service from the
78 * framework instance associated with the current call stack. A
79 * simple service tracker is created and cached for the associated
80 * framework instance when this method is called.
81 * </p>
82 * @return the content handler service from the framework instance
83 * associated with the current call stack or <tt>null</tt>
84 * is no service is available.
85 **/
86 private ContentHandler getContentHandlerService()
87 {
88 // Get the framework instance associated with call stack.
89 Felix framework = URLHandlers.getFrameworkFromContext();
90
91 // If the framework has disabled the URL Handlers service,
92 // then it will not be found so just return null.
93 if (framework == null)
94 {
95 return null;
96 }
97
98 // Get the service tracker for the framework instance or create one.
99 URLHandlersServiceTracker tracker =
100 (URLHandlersServiceTracker) m_trackerMap.get(framework);
101 if (tracker == null)
102 {
103 // Get the framework's system bundle context.
104 BundleContext context =
105 ((SystemBundleActivator)
106 ((SystemBundle) framework.getBundle(0)).getActivator())
107 .getBundleContext();
108 // Create a filter for the mime type.
109 String filter =
110 "(&(objectClass="
111 + ContentHandler.class.getName()
112 + ")("
113 + URLConstants.URL_CONTENT_MIMETYPE
114 + "="
115 + m_mimeType
116 + "))";
117 // Create a simple service tracker for the framework.
118 tracker = new URLHandlersServiceTracker(context, filter);
119 // Cache the simple service tracker.
120 m_trackerMap.put(framework, tracker);
121 }
122 return (ContentHandler) tracker.getService();
123 }
124}