Java service provider-lookup mechanism(发现机制)-2

 

public interface Coherence {

    public String naming();
}

 

public class DefaultCoherence implements Coherence {

    public String naming() {
        return "default coherence protocol";
    }
}

 

 

public class Jobs implements Coherence {

    public String naming() {
        return "jobs coherence protocol";
    }
}

  

 

 

 

classpath下META-INF\services\com.xxx.ddal.coherence.Coherence 写道
com.xxx.ddal.coherence.DefaultCoherence
com.xxx.ddal.coherence.Jobs

 

 

 

@Test
public void test() {
    ServiceLoader<Coherence> serviceLoader = ServiceLoader.load(Coherence.class);
    for (Coherence coherence : serviceLoader) {
        System.out.println(coherence.getClass());
        System.out.println(coherence.naming());
    }
}

 

 

输出 写道
class com.xxx.ddal.coherence.DefaultCoherence
default coherence protocol
class com.xxx.ddal.coherence.Jobs
jobs coherence protocol

 

 

@Test
public void test() {
//		String cn = "com.sun.xml.internal.ws.api.message.saaj.SAAJFactory";
	String cn = "javax.print.PrintServiceLookup";
	Class<?> clazz = null;
	try {
		clazz = Class.forName(cn);
	} catch (ClassNotFoundException e) {
		Assert.fail("Class not found: " + cn);
	}
    ServiceLoader<?> serviceLoader = ServiceLoader.load(clazz);
    for (Object obj : serviceLoader) {
    	System.out.println(obj.getClass());
    }
}

 

输出 写道
class sun.print.Win32PrintServiceLookup

  

 

 

public class ProviderService extends Provider {

	static private final String DEFAULT_JAXWSPROVIDER = 
			"com.sun.xml.internal.ws.spi.ProviderImpl";
	
	private Provider wrapper;
	
	public ProviderService() {
		Class<?> klass = null;
		try {
			klass = Class.forName(DEFAULT_JAXWSPROVIDER);
			wrapper = (Provider) klass.newInstance();
		} catch (Exception e) {
			throw new WebServiceException("Provider " + DEFAULT_JAXWSPROVIDER + " error: " + e.getMessage(), e);
		}
		
	}
	@Override
	public ServiceDelegate createServiceDelegate(URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
		return wrapper.createServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
	}

	@Override
	public Endpoint createEndpoint(String bindingId, Object implementor) {
		return wrapper.createEndpoint(bindingId, implementor);
	}

	@Override
	public Endpoint createAndPublishEndpoint(String address, Object implementor) {
		return wrapper.createAndPublishEndpoint(address, implementor);
	}

	@Override
	public EndpointReference readEndpointReference(Source eprInfoset) {
		return wrapper.readEndpointReference(eprInfoset);
	}

	@Override
	public <T> T getPort(EndpointReference endpointReference, Class<T> serviceEndpointInterface,
			WebServiceFeature... features) {
		return wrapper.getPort(endpointReference, serviceEndpointInterface, features);
	}

	@Override
	public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName,
			List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
		return wrapper.createW3CEndpointReference(address, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters);
	}
}

 

 

META-INF\services\javax.xml.ws.spi.Provider 写道
  com.javax.jws.distributed.ProviderService

 

 
Provider provider = Provider.provider(); // ProviderService

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值