View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.ksb.impl.registry;
17  
18  import java.util.List;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.cxf.Bus;
24  import org.apache.cxf.frontend.ClientProxyFactoryBean;
25  import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
26  import org.kuali.rice.core.api.config.property.ConfigContext;
27  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
28  import org.kuali.rice.core.api.exception.RiceRuntimeException;
29  import org.kuali.rice.core.cxf.interceptors.ImmutableCollectionsInInterceptor;
30  import org.kuali.rice.ksb.api.registry.RemoveAndPublishResult;
31  import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
32  import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
33  import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
34  import org.kuali.rice.ksb.api.registry.ServiceInfo;
35  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
36  import org.kuali.rice.ksb.security.soap.CXFWSS4JInInterceptor;
37  import org.kuali.rice.ksb.security.soap.CXFWSS4JOutInterceptor;
38  import org.kuali.rice.ksb.util.KSBConstants;
39  
40  /**
41   * TODO... 
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   *
45   */
46  public class LazyRemoteServiceRegistryConnector implements ServiceRegistry {
47  
48  	private final Object initLock = new Object();
49  	private volatile ServiceRegistry delegate;
50  	
51  	// injected
52  	private Bus cxfBus;
53  	
54  	public void setCxfBus(Bus cxfBus) {
55  		this.cxfBus = cxfBus;
56  	}
57  	
58  	@Override
59  	public List<ServiceInfo> getOnlineServicesByName(QName serviceName)
60  			throws RiceIllegalArgumentException {
61  		return getDelegate().getOnlineServicesByName(serviceName);
62  	}
63  
64  	@Override
65  	public List<ServiceInfo> getAllOnlineServices() {
66  		return getDelegate().getAllOnlineServices();
67  	}
68  
69  	@Override
70  	public List<ServiceInfo> getAllServices() {
71  		return getDelegate().getAllServices();
72  	}
73  	
74  	@Override
75  	public List<ServiceInfo> getAllServicesForInstance(String instanceId) {
76  		return getDelegate().getAllServicesForInstance(instanceId);
77  	}
78  
79  	@Override
80  	public ServiceDescriptor getServiceDescriptor(String serviceDescriptorId)
81  			throws RiceIllegalArgumentException {
82  		return getDelegate().getServiceDescriptor(serviceDescriptorId);
83  	}
84  
85  	@Override
86  	public List<ServiceDescriptor> getServiceDescriptors(
87  			List<String> serviceDescriptorIds)
88  			throws RiceIllegalArgumentException {
89  		return getDelegate().getServiceDescriptors(serviceDescriptorIds);
90  	}
91  
92  	@Override
93  	public ServiceEndpoint publishService(ServiceEndpoint serviceEndpoint)
94  			throws RiceIllegalArgumentException {
95  		return getDelegate().publishService(serviceEndpoint);
96  	}
97  
98  	@Override
99  	public List<ServiceEndpoint> publishServices(List<ServiceEndpoint> serviceEndpoints)
100 			throws RiceIllegalArgumentException {
101 		return getDelegate().publishServices(serviceEndpoints);
102 	}
103 
104 	@Override
105 	public ServiceEndpoint removeServiceEndpoint(String serviceId)
106 			throws RiceIllegalArgumentException {
107 		return getDelegate().removeServiceEndpoint(serviceId);
108 	}
109 
110 	@Override
111 	public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds)
112 			throws RiceIllegalArgumentException {
113 		return getDelegate().removeServiceEndpoints(serviceIds);
114 	}
115 
116 	@Override
117 	public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds,
118 			List<ServiceEndpoint> publishServiceEndpoints) {
119 		return getDelegate().removeAndPublish(removeServiceIds, publishServiceEndpoints);
120 	}
121 
122 	@Override
123 	public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
124 		return getDelegate().updateStatus(serviceId, status);
125 	}
126 
127 	@Override
128 	public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
129 		return getDelegate().updateStatuses(serviceIds, status);
130 	}
131 
132 	@Override
133 	public void takeInstanceOffline(String instanceId)
134 			throws RiceIllegalArgumentException {
135 		getDelegate().takeInstanceOffline(instanceId);
136 	}
137 	
138 	private ServiceRegistry getDelegate() {
139 		// double-checked locking idiom - see Effective Java, Item 71
140 		ServiceRegistry internalDelegate = this.delegate;
141 		if (internalDelegate == null) {
142 			synchronized (initLock) {
143 				internalDelegate = this.delegate;
144 				if (internalDelegate == null) {
145 					this.delegate = internalDelegate = initializeRemoteServiceRegistry();
146 				}
147 			}
148 		}
149 		return internalDelegate;
150 	}
151 	
152 	protected ServiceRegistry initializeRemoteServiceRegistry() {
153 		String registryBootstrapUrl = ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.REGISTRY_SERVICE_URL);
154 		if (StringUtils.isBlank(registryBootstrapUrl)) {
155 			throw new RiceRuntimeException("Failed to load registry bootstrap service from url: " + registryBootstrapUrl);
156 		}
157 		ClientProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
158 		clientFactory.setServiceClass(ServiceRegistry.class);
159 		clientFactory.setBus(cxfBus);
160 		clientFactory.setAddress(registryBootstrapUrl);
161 		
162 		// Set security interceptors
163 		clientFactory.getOutInterceptors().add(new CXFWSS4JOutInterceptor(true));
164 		clientFactory.getInInterceptors().add(new CXFWSS4JInInterceptor(true));
165 
166         //Set transformation interceptors
167         clientFactory.getInInterceptors().add(new ImmutableCollectionsInInterceptor());
168 		
169 		Object service = clientFactory.create();
170 		if (!(service instanceof ServiceRegistry)) {
171 			throw new RiceRuntimeException("Endpoint to service registry at URL '" + registryBootstrapUrl + "' was not an instance of ServiceRegistry, instead was: " + service);
172 		}
173 		return (ServiceRegistry)service;
174 	}
175 	
176 }