1 11 12 package org.eclipse.osgi.framework.internal.protocol; 13 14 import java.io.IOException ; 15 import java.net.ContentHandler ; 16 import java.net.URLConnection ; 17 import org.osgi.framework.*; 18 import org.osgi.service.url.URLConstants; 19 import org.osgi.util.tracker.ServiceTracker; 20 import org.osgi.util.tracker.ServiceTrackerCustomizer; 21 22 30 public class ContentHandlerProxy extends ContentHandler implements ServiceTrackerCustomizer { 31 protected ContentHandler realHandler; 32 33 protected ServiceTracker contentHandlerServiceTracker; 35 36 protected BundleContext context; 37 protected ServiceReference contentHandlerServiceReference; 38 39 protected String contentType; 40 41 protected int ranking = Integer.MIN_VALUE; 42 43 public ContentHandlerProxy(String contentType, ServiceReference reference, BundleContext context) { 44 this.context = context; 45 this.contentType = contentType; 46 47 setNewHandler(reference, getRank(reference)); 50 51 contentHandlerServiceTracker = new ServiceTracker(context, ContentHandler .class.getName(), this); 52 StreamHandlerFactory.secureAction.open(contentHandlerServiceTracker); 53 } 54 55 private void setNewHandler(ServiceReference reference, int rank) { 56 if (contentHandlerServiceReference != null) 57 context.ungetService(contentHandlerServiceReference); 58 59 contentHandlerServiceReference = reference; 60 ranking = rank; 61 62 if (reference == null) 63 realHandler = new DefaultContentHandler(); 64 else 65 realHandler = (ContentHandler ) StreamHandlerFactory.secureAction.getService(reference, context); 66 } 67 68 71 public Object addingService(ServiceReference reference) { 72 Object prop = reference.getProperty(URLConstants.URL_CONTENT_MIMETYPE); 74 if (!(prop instanceof String [])) 75 return null; 76 String [] contentTypes = (String []) prop; 77 for (int i = 0; i < contentTypes.length; i++) { 78 if (contentTypes[i].equals(contentType)) { 79 int newServiceRanking = getRank(reference); 81 if (newServiceRanking > ranking || contentHandlerServiceReference == null) 82 setNewHandler(reference, newServiceRanking); 83 return (reference); 84 } 85 } 86 87 return (null); 89 } 90 91 94 95 public void modifiedService(ServiceReference reference, Object service) { 96 int newrank = getRank(reference); 97 if (reference == contentHandlerServiceReference) { 98 if (newrank < ranking) { 99 ServiceReference newReference = contentHandlerServiceTracker.getServiceReference(); 103 if (newReference != contentHandlerServiceReference && newReference != null) { 104 setNewHandler(newReference, ((Integer ) newReference.getProperty(Constants.SERVICE_RANKING)).intValue()); 105 } 106 } 107 } else if (newrank > ranking) { 108 setNewHandler(reference, newrank); 111 } 112 } 113 114 117 public void removedService(ServiceReference reference, Object service) { 118 if (reference != contentHandlerServiceReference) 120 return; 121 ServiceReference newReference = contentHandlerServiceTracker.getServiceReference(); 124 setNewHandler(newReference, getRank(newReference)); 126 } 127 128 131 132 public Object getContent(URLConnection uConn) throws IOException { 133 return realHandler.getContent(uConn); 134 } 135 136 private int getRank(ServiceReference reference) { 137 if (reference == null) 138 return Integer.MIN_VALUE; 139 Object property = reference.getProperty(Constants.SERVICE_RANKING); 140 return (property instanceof Integer ) ? ((Integer ) property).intValue() : 0; 141 } 142 143 class DefaultContentHandler extends ContentHandler { 144 145 148 public Object getContent(URLConnection uConn) throws IOException { 149 return uConn.getInputStream(); 150 } 151 } 152 } 153 | Popular Tags |