1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.ws.api.client; 27 28 import com.sun.istack.internal.NotNull; 29 import com.sun.istack.internal.Nullable; 30 import com.sun.xml.internal.ws.api.WSService; 31 import com.sun.xml.internal.ws.util.ServiceFinder; 32 33 import javax.xml.ws.Service; 34 import java.util.ArrayList; 35 import java.util.HashSet; 36 import java.util.List; 37 import java.util.Set; 38 39 /** 40 * Creates {@link ServiceInterceptor}. 41 * 42 * <p> 43 * Code that wishes to inject {@link ServiceInterceptor} into {@link WSService} 44 * must implement this class. There are two ways to have the JAX-WS RI 45 * recognize your {@link ServiceInterceptor}s. 46 * 47 * <h3>Use {@link ServiceFinder}</h3> 48 * <p> 49 * {@link ServiceInterceptorFactory}s discovered via {@link ServiceFinder} 50 * will be incorporated to all {@link WSService} instances. 51 * 52 * <h3>Register per-thread</h3> 53 * 54 * 55 * @author Kohsuke Kawaguchi 56 * @see ServiceInterceptor 57 * @see 2.1 EA3 58 */ 59 public abstract class ServiceInterceptorFactory { 60 public abstract ServiceInterceptor create(@NotNull WSService service); 61 62 /** 63 * Loads all {@link ServiceInterceptor}s and return aggregated one. 64 */ 65 public static @NotNull ServiceInterceptor load(@NotNull WSService service, @Nullable ClassLoader cl) { 66 List<ServiceInterceptor> l = new ArrayList<ServiceInterceptor>(); 67 68 // first service look-up 69 for( ServiceInterceptorFactory f : ServiceFinder.find(ServiceInterceptorFactory.class)) 70 l.add(f.create(service)); 71 72 // then thread-local 73 for( ServiceInterceptorFactory f : threadLocalFactories.get()) 74 l.add(f.create(service)); 75 76 return ServiceInterceptor.aggregate(l.toArray(new ServiceInterceptor[l.size()])); 77 } 78 79 private static ThreadLocal<Set<ServiceInterceptorFactory>> threadLocalFactories = new ThreadLocal<Set<ServiceInterceptorFactory>>() { 80 protected Set<ServiceInterceptorFactory> initialValue() { 81 return new HashSet<ServiceInterceptorFactory>(); 82 } 83 }; 84 85 /** 86 * Registers {@link ServiceInterceptorFactory} for this thread. 87 * 88 * <p> 89 * Once registered, {@link ServiceInterceptorFactory}s are consulted for every 90 * {@link Service} created in this thread, until it gets unregistered. 91 */ 92 public static boolean registerForThread(ServiceInterceptorFactory factory) { 93 return threadLocalFactories.get().add(factory); 94 } 95 96 /** 97 * Removes previously registered {@link ServiceInterceptorFactory} for this thread. 98 */ 99 public static boolean unregisterForThread(ServiceInterceptorFactory factory) { 100 return threadLocalFactories.get().remove(factory); 101 } 102 }