/**
*
* Copyright 2005 Unity Systems, LLC. http://www.unity-systems.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.client;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jbi.JBIException;
import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.xml.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.servicemix.jbi.deployment.Descriptor;
import org.servicemix.jbi.deployment.Services;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
/**
*
* A registry of EndPoints that can be defined by either the component or by a
* service unit, this can be used to activate the service end points defined in
* the services element of a jbi descriptor, it can also be used as a clean way
* of mapping between the declaration of interfaces in your jbi.xml and your
* ServiceEndPoints
*
* @author <a href="mailto:pdodds@unity-systems.com">Philip Dodds </a>
*
*/
public class ClientEndPointRegistry {
private static final Log log = LogFactory
.getLog(ClientEndPointRegistry.class.getName());
private ComponentContext componentContext;
private List endPoints = new ArrayList();
/**
* Constructor for an ClientEndPointRegistry requires the component context
*
* @param componentContext
*/
public ClientEndPointRegistry(ComponentContext componentContext) {
this.componentContext = componentContext;
}
/**
* Activates all the EndPoints for the given component
*
* @throws JBIException
*/
public void activeComponentEndpoints() throws JBIException {
activeServiceUnitEndpoints(null);
}
/**
* Activates all the EndPoints for a named Service Unit
*
* @param serviceUnitName
* @throws JBIException
*/
public void activeServiceUnitEndpoints(String serviceUnitName)
throws JBIException {
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if (isMatchingServiceUnitName(serviceUnitName, endPoint)) {
if (endPoint.getPerspective() == EndPointDefinition.PROVIDES)
endPoint.setServiceEndPoint(componentContext
.activateEndpoint(endPoint.getServiceName(),
endPoint.getEndPointName()));
}
}
}
/**
* Adds the services from this descriptor to the Registry
*
* @param services
* @param serviceUnitName
*/
public void addServices(Services services, String serviceUnitName) {
EndPointDefinition[] endPointDefinitions = EndPointDefinitionFactory
.getServiceEndPoints(services);
for (int i = 0; i < endPointDefinitions.length; i++) {
// TODO need to determine if we already have it
endPointDefinitions[i].setServiceUnitName(serviceUnitName);
endPoints.add(endPointDefinitions[i]);
}
}
/**
* De-activates all the EndPoints for a component
*
* @throws JBIException
*/
public void deactiveComponentEndpoints() throws JBIException {
deactiveServiceUnitEndpoints(null);
}
/**
* De-activates all the EndPoints for a given service unit
*
* @param serviceUnitName
* @throws JBIException
*/
public void deactiveServiceUnitEndpoints(String serviceUnitName)
throws JBIException {
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if (isMatchingServiceUnitName(serviceUnitName, endPoint)) {
if (endPoint.getServiceEndPoint() != null) {
if (endPoint.getPerspective() == EndPointDefinition.PROVIDES) {
componentContext.deactivateEndpoint(endPoint
.getServiceEndPoint());
endPoint.setServiceEndPoint(null);
}
} else {
log.warn("Endpoint for service-name:"
+ endPoint.getServiceName() + ", end-point:"
+ endPoint.getEndPointName() + " not active");
}
}
}
}
/**
* Returns an EndPointDefinition for a given qualified interface name
*
* @param interfaceName
* @return
*/
public EndPointDefinition getEndPointDefinition(QName interfaceName) {
if (interfaceName == null)
return null;
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if (endPoint.getInterfaceNames().contains(interfaceName))
return endPoint;
}
return null;
}
/**
* Gets the EndPointDefinition for a given qualified servicename and
* endpoint name
*
* @param service
* @param endpoint
* @return
*/
public EndPointDefinition getEndPointDefinition(QName service,
String endpoint) {
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if ((service.equals(endPoint.getServiceName()) && (endpoint
.equals(endPoint.getEndPointName()))))
return endPoint;
}
return null;
}
/**
* Gets the EndPointDefinition for a given ServiceEndPoint (@See
* ServiceEndPoint)
*
* @param serviceEndPoint
* @return
*/
public EndPointDefinition getEndPointDefinition(
ServiceEndpoint serviceEndPoint) {
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if (serviceEndPoint.equals(endPoint.getServiceEndPoint())) {
return endPoint;
}
}
return null;
}
/**
* Returns the locally registered EndPointDefinition for a given service
* name
*
* @param service
* The qualified service name
* @return
*/
public EndPointDefinition getEndPointDefinitionByService(QName service) {
for (Iterator iterator = endPoints.iterator(); iterator.hasNext();) {
EndPointDefinition endPoint = (EndPointDefinition) iterator.next();
if ((service.equals(endPoint.getServiceName())))
return endPoint;
}
return null;
}
/**
* Gets a list of the defined EndPointDefinitions
*
* @return A list of the defined EndPointDefinitions
*/
public List getEndPoints() {
return endPoints;
}
/**
* Part of the JBI Spec
*
* TODO Needs implementing
*
* @param serviceEndpoint
* @return
*/
public Document getServiceDescription(ServiceEndpoint serviceEndpoint) {
// TODO Auto-generated method stub
return null;
}
/**
* Part of the JBI Spec
*
* TODO Needs implementing
*
* @param serviceEndpoint
* @param messageExchange
* @return
*/
public boolean isExchangeWithConsumerOkey(ServiceEndpoint serviceEndpoint,
MessageExchange messageExchange) {
// TODO Auto-generated method stub
return true;
}
/**
* Part of the JBI Spec
*
* TODO Needs implementing
*
* @param serviceEndpoint
* @param messageExchange
* @return
*/
public boolean isExchangeWithProviderOkay(ServiceEndpoint serviceEndpoint,
MessageExchange messageExchange) {
// TODO Auto-generated method stub
return true;
}
/**
* Helper method to handle the check whether the service unit name matches
* this endpoint, it also handles the fact that null service unit names are
* present
*
* @param serviceUnitName
* The name of the service unit or null for the component
* @param endPoint
* The EndPointDefinition to compare with
* @return True if the EndPointDefinition is for this service unit
*/
private boolean isMatchingServiceUnitName(String serviceUnitName,
EndPointDefinition endPoint) {
if (endPoint.getServiceUnitName() == null && serviceUnitName == null)
return true;
if (endPoint.getServiceUnitName() != null
&& endPoint.getServiceUnitName().equals(serviceUnitName))
return true;
return false;
}
/**
* Checks an installation root for a descriptor and then looks for a
* services element within it, if found it loads them into the registry as
* component endpoints to be maintained with the component
*
* @param installationRoot
* The installation root
*/
public void loadComponentServices(String installationRoot) {
loadServiceUnitServices(installationRoot, null);
}
/**
* Checks an installation root for a descriptor and then looks for a
* services element within it, if found it loads them into the registry as
* component endpoints to be maintained with the component
*
* @param installationRoot
* The installation root
*/
public void loadServiceUnitServices(String installationRoot,
String serviceUnitName) {
if (DescriptorFactory.hasDescriptor(installationRoot)) {
log.debug("Descriptor exists, parsing");
Descriptor descriptor = DescriptorFactory
.getDescriptor(installationRoot);
if (descriptor.getServices() != null) {
log
.debug("Found services, assuming 'fixed provider', building endpoints");
addServices(descriptor.getServices(), serviceUnitName);
}
}
}
/**
* Part of the JBI Spec
*
* TODO Needs implementing
*
* @param documentFragment
* @return
*/
public ServiceEndpoint resolveEndpointReference(
DocumentFragment documentFragment) {
System.out.println("Resolving EndPoint Reference " + documentFragment);
return null;
}
}