ServiceURLStreamHandlerFactory.java

00001 /*
00002  * Copyright (c) 2003-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package org.knopflerfish.framework;
00036 
00037 import java.net.*;
00038 import org.osgi.service.url.*;
00039 import org.osgi.framework.*;
00040 import java.util.Map;
00041 import java.util.HashMap;
00042 
00047 public class ServiceURLStreamHandlerFactory 
00048   implements URLStreamHandlerFactory
00049 {
00050   Framework framework;
00051 
00052   //
00053   // Special framework handlers 
00054   //
00055   // String protocol -> URLStreamHandler
00056   Map handlers = new HashMap();
00057 
00058   // JVM classpath handlers. Initialized once at startup
00059   String[] jvmPkgs = null;
00060 
00061   //
00062   // OSGi URLStreamHandlerService wrappers
00063   // This map is not really necessary since the JVM
00064   // caches handlers anyway, but it just seems nice to have.
00065   // 
00066   // String (protocol) -> URLStreamHandlerWrapper
00067   Map wrapMap   = new HashMap();
00068 
00069 
00070   ServiceURLStreamHandlerFactory(Framework fw) {
00071     this.framework = fw;
00072 
00073     // Initialize JVM classpath packages
00074     String s = System.getProperty("java.protocol.handler.pkgs", "");
00075     
00076     jvmPkgs = Util.splitwords(s, "|");
00077 
00078     for(int i = 0; i < jvmPkgs.length; i++) {
00079       jvmPkgs[i] = jvmPkgs[i].trim();
00080       if(Debug.url) {
00081         Debug.println("JVMClassPath - URLHandler jvmPkgs[" + i + "]=" + jvmPkgs[i]);
00082       }
00083     }
00084   }
00085 
00086   public URLStreamHandler createURLStreamHandler(String protocol) {
00087     synchronized(handlers) {
00088 
00089       if(Debug.url) {
00090         Debug.println("createURLStreamHandler protocol=" + protocol);
00091       }
00092 
00093       // Check for
00094       // 1. JVM classpath handlers
00095       // 2. Framework built-in handlers
00096       // 2. OSGi-based handlers
00097       // 3. system handlers
00098 
00099       URLStreamHandler handler = getJVMClassPathHandler(protocol);
00100       if(handler != null) {
00101         if(Debug.url) {
00102           Debug.println("using JVMClassPath handler for " + protocol);
00103         }
00104         return handler;
00105       }
00106 
00107         
00108       handler = (URLStreamHandler)handlers.get(protocol);
00109       if(handler != null) {
00110         if(Debug.url) {
00111           Debug.println("using predefined handler for " + protocol);
00112         }
00113         return handler;
00114       }
00115 
00116       handler = getServiceHandler(protocol);
00117       if(handler != null) {
00118         if(Debug.url) {
00119           Debug.println("Using service URLHandler for " + protocol);
00120         }
00121         return handler;
00122       }
00123 
00124       if(Debug.url) {
00125         Debug.println("Using default URLHandler for " + protocol);
00126       }
00127       return null;
00128     }
00129   }
00130 
00131   URLStreamHandler getServiceHandler(String protocol) {
00132     try {
00133       String filter = 
00134         "(" + 
00135         URLConstants.URL_HANDLER_PROTOCOL + 
00136         "=" + protocol + 
00137         ")";
00138 //    TODO true or false?
00139       ServiceReference[] srl = framework.services
00140         .get(URLStreamHandlerService.class.getName(), filter, null, false);
00141       
00142       if(srl != null && srl.length > 0) {
00143         URLStreamHandlerWrapper wrapper = (URLStreamHandlerWrapper)
00144           wrapMap.get(protocol);
00145         
00146         if(wrapper == null) {
00147           wrapper = new URLStreamHandlerWrapper(framework, protocol);
00148           wrapMap.put(protocol, wrapper);
00149         }
00150 
00151         return wrapper;
00152       }
00153     } catch (InvalidSyntaxException e) {
00154       throw new RuntimeException("Failed to get service: " + e);
00155     }
00156 
00157     // no handler found
00158     return null;
00159   }
00160 
00161 
00165   URLStreamHandler getJVMClassPathHandler(String protocol) {
00166 
00167     for(int i = 0; i < jvmPkgs.length; i++) {
00168       String className = jvmPkgs[i] + "." + protocol + ".Handler";
00169       try { 
00170         if(Debug.url) {
00171           Debug.println("JVMClassPath - trying URLHandler class=" + className);
00172         }
00173         Class clazz = Class.forName(className);
00174         URLStreamHandler handler = (URLStreamHandler)clazz.newInstance();
00175 
00176         if(Debug.url) {
00177           Debug.println("JVMClassPath - created URLHandler class=" + className);
00178         }
00179 
00180         return handler;
00181       } catch (Throwable t) {
00182         if(Debug.url) {
00183           Debug.println("JVMClassPath - no URLHandler class " + className);
00184         }
00185       }
00186     }
00187     
00188     if(Debug.url) {
00189       Debug.println("JVMClassPath - no URLHandler for " + protocol);
00190     }
00191     
00192     return null;
00193   }
00194 
00195 
00206   void setURLStreamHandler(String protocol, URLStreamHandler handler) {
00207     handlers.put(protocol, handler);
00208   }
00209 }
00210 

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4