Author: gnodet Date: Wed May 17 03:27:31 2006 New Revision: 407213 URL: http://svn.apache.org/viewcvs?rev=407213&view=rev Log: SM-432: Security: authorization broker Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/DefaultBroker.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationEntry.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationMap.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/DefaultAuthorizationMap.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/GroupPrincipal.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/PropertiesLoginModule.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/SecuredBroker.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/UserPrincipal.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/ incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/GroupPrincipalTest.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/PropertiesLoginModuleTest.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/SecuredBrokerTest.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/SpringSecuredBrokerTest.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/security/UserPrincipalTest.java incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/security/ incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/security/groups.properties incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/security/login.config incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/security/secure.xml incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/security/users.properties Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/Broker.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/AbstractFlow.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jca/JCAFlow.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jms/JMSFlow.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaFlow.java incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaQueue.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/nmr/SubscriptionPropertyCopyTest.java incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/nmr/SubscriptionTest.java Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java Wed May 17 03:27:31 2006 @@ -71,6 +71,7 @@ import org.apache.servicemix.jbi.management.ManagementContext; import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; import org.apache.servicemix.jbi.nmr.Broker; +import org.apache.servicemix.jbi.nmr.DefaultBroker; import org.apache.servicemix.jbi.nmr.flow.Flow; import org.jencks.factory.WorkManagerFactoryBean; import org.w3c.dom.DocumentFragment; @@ -96,7 +97,7 @@ private AtomicBoolean started = new AtomicBoolean(false); private AtomicBoolean containerInitialized = new AtomicBoolean(false); private IdGenerator idGenerator = new IdGenerator(); - protected Broker broker = new Broker(); + protected Broker broker = new DefaultBroker(); protected ServiceUnitManager serviceManager; protected ManagementContext managementContext = new ManagementContext(); protected EnvironmentContext environmentContext = new EnvironmentContext(); @@ -147,7 +148,7 @@ * @return Returns the flowName. */ public String getFlowName() { - String flowNames = broker.getFlowNames(); + String flowNames = getDefaultBroker().getFlowNames(); if (flowNames == null) { return null; } @@ -162,28 +163,28 @@ * @param flowName The flow to set. */ public void setFlowName(String flowName) { - broker.setFlowNames(flowName); + getDefaultBroker().setFlowNames(flowName); } /** * @return Returns the flowNames. */ public String getFlowNames() { - return broker.getFlowNames(); + return getDefaultBroker().getFlowNames(); } /** * @param flowNames The flows to set. */ public void setFlowNames(String flowNames) { - broker.setFlowNames(flowNames); + getDefaultBroker().setFlowNames(flowNames); } /** * @return the subscriptionFlowName */ public String getSubscriptionFlowName() { - return broker.getSubscriptionFlowName(); + return getDefaultBroker().getSubscriptionFlowName(); } /** @@ -191,7 +192,7 @@ * @param subscriptionFlowName */ public void setSubscriptionFlowName(String subscriptionFlowName) { - broker.setSubscriptionFlowName(subscriptionFlowName); + getDefaultBroker().setSubscriptionFlowName(subscriptionFlowName); } /** @@ -200,14 +201,14 @@ * @param flow */ public void setFlow(Flow flow) { - this.broker.setFlows(new Flow[] { flow }); + getDefaultBroker().setFlows(new Flow[] { flow }); } /** * @return the broker message Flow */ public Flow getFlow() { - Flow[] flows = this.broker.getFlows(); + Flow[] flows = getDefaultBroker().getFlows(); if (flows == null || flows.length == 0) { return null; } else if (flows.length > 1) { @@ -223,14 +224,14 @@ * @param flow */ public void setFlows(Flow[] flows) { - this.broker.setFlows(flows); + getDefaultBroker().setFlows(flows); } /** * @return the broker message Flows */ public Flow[] getFlows() { - return this.broker.getFlows(); + return getDefaultBroker().getFlows(); } /** @@ -257,10 +258,28 @@ } /** + * Return the DefaultBroker instance + */ + public DefaultBroker getDefaultBroker() { + if (broker == null || + broker instanceof DefaultBroker == false) { + throw new IllegalStateException("Broker is not a DefaultBroker"); + } + return (DefaultBroker) broker; + } + + /** * @return Return the NMR broker */ public Broker getBroker(){ return broker; + } + + /** + * Set the Broker to use + */ + public void setBroker(Broker broker) { + this.broker = broker; } /** Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/Broker.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/Broker.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/Broker.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/Broker.java Wed May 17 03:27:31 2006 @@ -15,263 +15,37 @@ */ package org.apache.servicemix.jbi.nmr; -import java.util.ArrayList; -import java.util.List; - import javax.jbi.JBIException; -import javax.jbi.component.Component; -import javax.jbi.messaging.MessagingException; -import javax.jbi.messaging.MessageExchange.Role; -import javax.jbi.servicedesc.ServiceEndpoint; -import javax.management.JMException; -import javax.management.MBeanOperationInfo; -import javax.resource.spi.work.WorkManager; -import javax.xml.namespace.QName; +import javax.jbi.messaging.MessageExchange; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.servicemix.jbi.container.ActivationSpec; import org.apache.servicemix.jbi.container.JBIContainer; -import org.apache.servicemix.jbi.framework.ComponentContextImpl; -import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; -import org.apache.servicemix.jbi.framework.ComponentNameSpace; -import org.apache.servicemix.jbi.framework.Registry; -import org.apache.servicemix.jbi.management.BaseSystemService; -import org.apache.servicemix.jbi.management.ManagementContext; -import org.apache.servicemix.jbi.management.OperationInfoHelper; -import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; -import org.apache.servicemix.jbi.nmr.flow.DefaultFlowChooser; -import org.apache.servicemix.jbi.nmr.flow.Flow; -import org.apache.servicemix.jbi.nmr.flow.FlowChooser; -import org.apache.servicemix.jbi.nmr.flow.FlowProvider; -import org.apache.servicemix.jbi.resolver.ConsumerComponentEndpointFilter; -import org.apache.servicemix.jbi.resolver.EndpointChooser; -import org.apache.servicemix.jbi.resolver.EndpointFilter; -import org.apache.servicemix.jbi.resolver.EndpointResolver; -import org.apache.servicemix.jbi.resolver.FirstChoicePolicy; -import org.apache.servicemix.jbi.resolver.ProducerComponentEndpointFilter; -import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint; -import org.apache.servicemix.jbi.servicedesc.ExternalEndpoint; -import org.apache.servicemix.jbi.servicedesc.InternalEndpoint; -import org.apache.servicemix.jbi.servicedesc.LinkedEndpoint; /** * The Broker handles Nomalised Message Routing within ServiceMix * * @version $Revision$ */ -public class Broker extends BaseSystemService implements BrokerMBean { - - private Registry registry; - private String flowNames = "seda"; - private String subscriptionFlowName = null; - private WorkManager workManager; - private Flow[] flows; - private final static Log log = LogFactory.getLog(Broker.class); - private EndpointChooser defaultServiceChooser = new FirstChoicePolicy(); - private EndpointChooser defaultInterfaceChooser = new FirstChoicePolicy(); - private SubscriptionManager subscriptionManager = new SubscriptionManager(); - private FlowChooser defaultFlowChooser = new DefaultFlowChooser(); - - /** - * Constructor - */ - public Broker() { - } - - /** - * Get the description - * - * @return description - */ - public String getDescription() { - return "Normalized Message Router"; - } - - /** - * @return Returns the workManager. - */ - public WorkManager getWorkManager() { - return workManager; - } - - /** - * @param workManager - * The workManager to set. - */ - public void setWorkManager(WorkManager workManager) { - this.workManager = workManager; - } - - public SubscriptionManager getSubscriptionManager() { - return subscriptionManager; - } - - /** - * Sets the subscription manager - */ - public void setSubscriptionManager(SubscriptionManager subscriptionManager) { - this.subscriptionManager = subscriptionManager; - } +public interface Broker extends BrokerMBean { + public JBIContainer getContainer(); + /** * initialize the broker * * @param container * @throws JBIException */ - public void init(JBIContainer container) throws JBIException { - super.init(container); - this.workManager = container.getWorkManager(); - this.registry = container.getRegistry(); - // Create and initialize flows - if (this.flows == null) { - String[] names = flowNames.split(","); - flows = new Flow[names.length]; - for (int i = 0; i < names.length; i++) { - flows[i] = FlowProvider.getFlow(names[i]); - flows[i].init(this, names[i]); - } - } else { - for (int i = 0; i < flows.length; i++) { - flows[i].init(this, null); - } - } - subscriptionManager.init(this, registry); - } + public void init(JBIContainer container) throws JBIException; - protected Class getServiceMBean() { - return BrokerMBean.class; - } - - /** - * Get the name of the Container - * - * @return containerName - */ - public String getContainerName() { - return container.getName(); - } - - /** - * Get the ManagementContext - * - * @return the managementContext - */ - public ManagementContext getManagementContext() { - return container.getManagementContext(); - } - - /** - * Get the Registry - * - * @return the registry - */ - public Registry getRegistry() { - return registry; - } - - /** - * start brokering - * - * @throws JBIException - */ - public void start() throws JBIException { - for (int i = 0; i < flows.length; i++) { - flows[i].start(); - } - super.start(); - } - - /** - * stop brokering - * - * @throws JBIException - */ - public void stop() throws JBIException { - for (int i = 0; i < flows.length; i++) { - flows[i].stop(); - } - super.stop(); - } - - /** - * shutdown all Components - * - * @throws JBIException - */ - public void shutDown() throws JBIException { - stop(); - for (int i = 0; i < flows.length; i++) { - flows[i].shutDown(); - } - super.shutDown(); - container.getManagementContext().unregisterMBean(this); - } - - /** - * @return Returns the flow. - */ - public String getFlowNames() { - return flowNames; - } - - /** - * @param flowName - * The flow to set. - */ - public void setFlowNames(String flowNames) { - this.flowNames = flowNames; - } - - /** - * @return the subscriptionFlowName - */ - public String getSubscriptionFlowName() { - return subscriptionFlowName; - } - - /** - * Set the subscription flow name - * @param subscriptionFlowName - */ - public void setSubscriptionFlowName(String subscriptionFlowName) { - this.subscriptionFlowName = subscriptionFlowName; - } - - /** - * Set the flow - * - * @param flow - */ - public void setFlows(Flow[] flows) { - this.flows = flows; - } - - /** - * @return the Flow - */ - public Flow[] getFlows() { - return this.flows; - } - /** * suspend the flow to prevent any message exchanges */ - public void suspend() { - for (int i = 0; i < flows.length; i++) { - flows[i].suspend(); - } - } + public void suspend(); /** * resume message exchange processing */ - public void resume() { - for (int i = 0; i < flows.length; i++) { - flows[i].resume(); - } - } + public void resume(); /** * Route an ExchangePacket to a destination @@ -279,250 +53,6 @@ * @param exchange * @throws JBIException */ - public void sendExchangePacket(MessageExchangeImpl exchange) throws JBIException { - if (exchange.getRole() == Role.PROVIDER && exchange.getDestinationId() == null) { - resolveAddress(exchange); - } - - boolean foundRoute = false; - // If we found a destination, or this is a reply - if (exchange.getEndpoint() != null || exchange.getRole() == Role.CONSUMER) { - foundRoute = true; - Flow flow = defaultFlowChooser.chooseFlow(flows, exchange); - if (flow == null) { - throw new MessagingException("Unable to choose a flow for exchange: " + exchange); - } - flow.send(exchange); - } - - if (exchange.getRole() == Role.PROVIDER) { - getSubscriptionManager().dispatchToSubscribers(exchange); - } - - if (!foundRoute) { - boolean throwException = true; - ActivationSpec activationSpec = exchange.getActivationSpec(); - if (activationSpec != null) { - throwException = activationSpec.isFailIfNoDestinationEndpoint(); - } - if (throwException) { - throw new MessagingException("Could not find route for exchange: " + exchange + " for service: " + exchange.getService() + " and interface: " - + exchange.getInterfaceName()); - } else if (exchange.getMirror().getSyncState() == MessageExchangeImpl.SYNC_STATE_SYNC_SENT) { - exchange.handleAccept(); - ComponentContextImpl ctx = (ComponentContextImpl) getSubscriptionManager().getContext(); - exchange.setDestinationId(ctx.getComponentNameSpace()); - // TODO: this will fail if exchange is InOut - getSubscriptionManager().done(exchange); - } - } - } + public void sendExchangePacket(MessageExchange exchange) throws JBIException; - protected void resolveAddress(MessageExchangeImpl exchange) throws JBIException { - ServiceEndpoint theEndpoint = exchange.getEndpoint(); - if (theEndpoint != null) { - if (theEndpoint instanceof ExternalEndpoint) { - throw new JBIException("External endpoints can not be used for routing: should be an internal or dynamic endpoint."); - } - if (theEndpoint instanceof AbstractServiceEndpoint == false) { - throw new JBIException("Component-specific endpoints can not be used for routing: should be an internal or dynamic endpoint."); - } - } - // Resolve linked endpoints - if (theEndpoint instanceof LinkedEndpoint) { - QName svcName = ((LinkedEndpoint) theEndpoint).getToService(); - String epName = ((LinkedEndpoint) theEndpoint).getToEndpoint(); - ServiceEndpoint ep = registry.getInternalEndpoint(svcName, epName); - if (ep == null) { - throw new JBIException("Could not resolve linked endpoint: " + theEndpoint); - } - theEndpoint = ep; - } - - // get the context which created the exchange - ComponentContextImpl context = exchange.getSourceContext(); - if (theEndpoint == null) { - QName serviceName = exchange.getService(); - QName interfaceName = exchange.getInterfaceName(); - - // check in order, ServiceName then InterfaceName - // check to see if there is a match on the serviceName - if (serviceName != null) { - ServiceEndpoint[] endpoints = registry.getEndpointsForService(serviceName); - endpoints = getMatchingEndpoints(endpoints, exchange); - theEndpoint = getServiceChooser(exchange).chooseEndpoint(endpoints, context, exchange); - if (theEndpoint == null) { - log.warn("ServiceName (" + serviceName + ") specified for routing, but can't find it registered"); - } - } - if (theEndpoint == null && interfaceName != null) { - ServiceEndpoint[] endpoints = registry.getEndpointsForInterface(interfaceName); - endpoints = getMatchingEndpoints(endpoints, exchange); - theEndpoint = (InternalEndpoint) getInterfaceChooser(exchange).chooseEndpoint(endpoints, context, exchange); - if (theEndpoint == null) { - log.warn("InterfaceName (" + interfaceName + ") specified for routing, but can't find any matching components"); - } - } - if (theEndpoint == null) { - // lets use the resolver on the activation spec if - // applicable - ActivationSpec activationSpec = exchange.getActivationSpec(); - if (activationSpec != null) { - EndpointResolver destinationResolver = activationSpec.getDestinationResolver(); - if (destinationResolver != null) { - try { - EndpointFilter filter = createEndpointFilter(context, exchange); - theEndpoint = (InternalEndpoint) destinationResolver.resolveEndpoint(context, exchange, filter); - } - catch (JBIException e) { - throw new MessagingException("Failed to resolve endpoint: " + e, e); - } - } - } - } - } - if (theEndpoint != null) { - exchange.setEndpoint(theEndpoint); - } - if (log.isTraceEnabled()) { - log.trace("Routing exchange " + exchange + " to: " + theEndpoint); - } - } - - /** - * Filter the given endpoints by asking to the provider and consumer - * if they are both ok to process the exchange. - * - * @param endpoints an array of internal endpoints to check - * @param exchange the exchange that will be serviced - * @return an array of endpoints on which both consumer and provider agrees - */ - protected ServiceEndpoint[] getMatchingEndpoints(ServiceEndpoint[] endpoints, MessageExchangeImpl exchange) { - List filtered = new ArrayList(); - ComponentMBeanImpl consumer = getRegistry().getComponent(exchange.getSourceId()); - - for (int i = 0; i < endpoints.length; i++) { - ComponentNameSpace id = ((InternalEndpoint) endpoints[i]).getComponentNameSpace(); - if (id != null) { - ComponentMBeanImpl provider = getRegistry().getComponent(id); - if (provider != null) { - if (!consumer.getComponent().isExchangeWithProviderOkay(endpoints[i], exchange) || - !provider.getComponent().isExchangeWithConsumerOkay(endpoints[i], exchange)) { - continue; - } - } - } - filtered.add(endpoints[i]); - } - return (ServiceEndpoint[]) filtered.toArray(new ServiceEndpoint[filtered.size()]); - } - - /** - * @return the default EndpointChooser - */ - public EndpointChooser getDefaultInterfaceChooser() { - return defaultInterfaceChooser; - } - - /** - * Set the default EndpointChooser - * - * @param defaultInterfaceChooser - */ - public void setDefaultInterfaceChooser(EndpointChooser defaultInterfaceChooser) { - this.defaultInterfaceChooser = defaultInterfaceChooser; - } - - /** - * @return the default EndpointChooser - */ - public EndpointChooser getDefaultServiceChooser() { - return defaultServiceChooser; - } - - /** - * Set default EndpointChooser - * - * @param defaultServiceChooser - */ - public void setDefaultServiceChooser(EndpointChooser defaultServiceChooser) { - this.defaultServiceChooser = defaultServiceChooser; - } - - /** - * Returns the endpoint chooser for endpoints found by service which will - * use the chooser on the exchange's activation spec if available otherwise - * will use the default - * - * @param exchange - * @return the EndpointChooser - */ - protected EndpointChooser getServiceChooser(MessageExchangeImpl exchange) { - EndpointChooser chooser = null; - ActivationSpec activationSpec = exchange.getActivationSpec(); - if (activationSpec != null) { - chooser = activationSpec.getServiceChooser(); - } - if (chooser == null) { - chooser = defaultServiceChooser; - } - return chooser; - } - - /** - * Returns the endpoint chooser for endpoints found by service which will - * use the chooser on the exchange's activation spec if available otherwise - * will use the default - * - * @param exchange - * @return the EndpointChooser - */ - protected EndpointChooser getInterfaceChooser(MessageExchangeImpl exchange) { - EndpointChooser chooser = null; - ActivationSpec activationSpec = exchange.getActivationSpec(); - if (activationSpec != null) { - chooser = activationSpec.getInterfaceChooser(); - } - if (chooser == null) { - chooser = defaultInterfaceChooser; - } - return chooser; - } - - /** - * Factory method to create an endpoint filter for the given component - * context and message exchange - * - * @param context - * @param exchange - * @return the EndpointFilter - */ - protected EndpointFilter createEndpointFilter(ComponentContextImpl context, MessageExchangeImpl exchange) { - Component component = context.getComponent(); - if (exchange.getRole() == Role.PROVIDER) { - return new ConsumerComponentEndpointFilter(component); - } - else { - return new ProducerComponentEndpointFilter(component); - } - } - - /** - * Get an array of MBeanOperationInfo - * - * @return array of OperationInfos - * @throws JMException - */ - public MBeanOperationInfo[] getOperationInfos() throws JMException { - OperationInfoHelper helper = new OperationInfoHelper(); - helper.addOperation(getObjectToManage(), "suspend", "suspend the NMR processing"); - helper.addOperation(getObjectToManage(), "resume", "resume the NMR processing"); - - return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos()); - } - - public JBIContainer getContainer() { - return container; - } - } Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/DefaultBroker.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/DefaultBroker.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/DefaultBroker.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/DefaultBroker.java Wed May 17 03:27:31 2006 @@ -0,0 +1,544 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.nmr; + +import java.util.ArrayList; +import java.util.List; + +import javax.jbi.JBIException; +import javax.jbi.component.Component; +import javax.jbi.messaging.MessageExchange; +import javax.jbi.messaging.MessagingException; +import javax.jbi.messaging.MessageExchange.Role; +import javax.jbi.servicedesc.ServiceEndpoint; +import javax.management.JMException; +import javax.management.MBeanOperationInfo; +import javax.resource.spi.work.WorkManager; +import javax.xml.namespace.QName; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.servicemix.jbi.container.ActivationSpec; +import org.apache.servicemix.jbi.container.JBIContainer; +import org.apache.servicemix.jbi.framework.ComponentContextImpl; +import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; +import org.apache.servicemix.jbi.framework.ComponentNameSpace; +import org.apache.servicemix.jbi.framework.Registry; +import org.apache.servicemix.jbi.management.BaseSystemService; +import org.apache.servicemix.jbi.management.ManagementContext; +import org.apache.servicemix.jbi.management.OperationInfoHelper; +import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; +import org.apache.servicemix.jbi.nmr.flow.DefaultFlowChooser; +import org.apache.servicemix.jbi.nmr.flow.Flow; +import org.apache.servicemix.jbi.nmr.flow.FlowChooser; +import org.apache.servicemix.jbi.nmr.flow.FlowProvider; +import org.apache.servicemix.jbi.resolver.ConsumerComponentEndpointFilter; +import org.apache.servicemix.jbi.resolver.EndpointChooser; +import org.apache.servicemix.jbi.resolver.EndpointFilter; +import org.apache.servicemix.jbi.resolver.EndpointResolver; +import org.apache.servicemix.jbi.resolver.FirstChoicePolicy; +import org.apache.servicemix.jbi.resolver.ProducerComponentEndpointFilter; +import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint; +import org.apache.servicemix.jbi.servicedesc.ExternalEndpoint; +import org.apache.servicemix.jbi.servicedesc.InternalEndpoint; +import org.apache.servicemix.jbi.servicedesc.LinkedEndpoint; + +/** + * The Broker handles Nomalised Message Routing within ServiceMix + * + * @version $Revision: 384328 $ + */ +public class DefaultBroker extends BaseSystemService implements Broker { + + private Registry registry; + private String flowNames = "seda"; + private String subscriptionFlowName = null; + private WorkManager workManager; + private Flow[] flows; + private final static Log log = LogFactory.getLog(DefaultBroker.class); + private EndpointChooser defaultServiceChooser = new FirstChoicePolicy(); + private EndpointChooser defaultInterfaceChooser = new FirstChoicePolicy(); + private SubscriptionManager subscriptionManager = new SubscriptionManager(); + private FlowChooser defaultFlowChooser = new DefaultFlowChooser(); + + /** + * Constructor + */ + public DefaultBroker() { + } + + /** + * Get the description + * + * @return description + */ + public String getDescription() { + return "Normalized Message Router"; + } + + /** + * @return Returns the workManager. + */ + public WorkManager getWorkManager() { + return workManager; + } + + /** + * @param workManager + * The workManager to set. + */ + public void setWorkManager(WorkManager workManager) { + this.workManager = workManager; + } + + public SubscriptionManager getSubscriptionManager() { + return subscriptionManager; + } + + /** + * Sets the subscription manager + */ + public void setSubscriptionManager(SubscriptionManager subscriptionManager) { + this.subscriptionManager = subscriptionManager; + } + + /** + * initialize the broker + * + * @param container + * @throws JBIException + */ + public void init(JBIContainer container) throws JBIException { + super.init(container); + this.workManager = container.getWorkManager(); + this.registry = container.getRegistry(); + // Create and initialize flows + if (this.flows == null) { + String[] names = flowNames.split(","); + flows = new Flow[names.length]; + for (int i = 0; i < names.length; i++) { + flows[i] = FlowProvider.getFlow(names[i]); + flows[i].init(this, names[i]); + } + } else { + for (int i = 0; i < flows.length; i++) { + flows[i].init(this, null); + } + } + subscriptionManager.init(this, registry); + } + + protected Class getServiceMBean() { + return BrokerMBean.class; + } + + /** + * Get the name of the Container + * + * @return containerName + */ + public String getContainerName() { + return container.getName(); + } + + /** + * Get the ManagementContext + * + * @return the managementContext + */ + public ManagementContext getManagementContext() { + return container.getManagementContext(); + } + + /** + * Get the Registry + * + * @return the registry + */ + public Registry getRegistry() { + return registry; + } + + /** + * start brokering + * + * @throws JBIException + */ + public void start() throws JBIException { + for (int i = 0; i < flows.length; i++) { + flows[i].start(); + } + super.start(); + } + + /** + * stop brokering + * + * @throws JBIException + */ + public void stop() throws JBIException { + for (int i = 0; i < flows.length; i++) { + flows[i].stop(); + } + super.stop(); + } + + /** + * shutdown all Components + * + * @throws JBIException + */ + public void shutDown() throws JBIException { + stop(); + for (int i = 0; i < flows.length; i++) { + flows[i].shutDown(); + } + super.shutDown(); + container.getManagementContext().unregisterMBean(this); + } + + /** + * @return Returns the flow. + */ + public String getFlowNames() { + return flowNames; + } + + /** + * @param flowName + * The flow to set. + */ + public void setFlowNames(String flowNames) { + this.flowNames = flowNames; + } + + /** + * @return the subscriptionFlowName + */ + public String getSubscriptionFlowName() { + return subscriptionFlowName; + } + + /** + * Set the subscription flow name + * @param subscriptionFlowName + */ + public void setSubscriptionFlowName(String subscriptionFlowName) { + this.subscriptionFlowName = subscriptionFlowName; + } + + /** + * Set the flow + * + * @param flow + */ + public void setFlows(Flow[] flows) { + this.flows = flows; + } + + /** + * @return the Flow + */ + public Flow[] getFlows() { + return this.flows; + } + + /** + * suspend the flow to prevent any message exchanges + */ + public void suspend() { + for (int i = 0; i < flows.length; i++) { + flows[i].suspend(); + } + } + + /** + * resume message exchange processing + */ + public void resume() { + for (int i = 0; i < flows.length; i++) { + flows[i].resume(); + } + } + + /** + * Route an ExchangePacket to a destination + * + * @param exchange + * @throws JBIException + */ + public void sendExchangePacket(MessageExchange me) throws JBIException { + MessageExchangeImpl exchange = (MessageExchangeImpl) me; + if (exchange.getRole() == Role.PROVIDER && exchange.getDestinationId() == null) { + resolveAddress(exchange); + } + + boolean foundRoute = false; + // If we found a destination, or this is a reply + if (exchange.getEndpoint() != null || exchange.getRole() == Role.CONSUMER) { + foundRoute = true; + Flow flow = defaultFlowChooser.chooseFlow(flows, exchange); + if (flow == null) { + throw new MessagingException("Unable to choose a flow for exchange: " + exchange); + } + flow.send(exchange); + } + + if (exchange.getRole() == Role.PROVIDER) { + getSubscriptionManager().dispatchToSubscribers(exchange); + } + + if (!foundRoute) { + boolean throwException = true; + ActivationSpec activationSpec = exchange.getActivationSpec(); + if (activationSpec != null) { + throwException = activationSpec.isFailIfNoDestinationEndpoint(); + } + if (throwException) { + throw new MessagingException("Could not find route for exchange: " + exchange + " for service: " + exchange.getService() + " and interface: " + + exchange.getInterfaceName()); + } else if (exchange.getMirror().getSyncState() == MessageExchangeImpl.SYNC_STATE_SYNC_SENT) { + exchange.handleAccept(); + ComponentContextImpl ctx = (ComponentContextImpl) getSubscriptionManager().getContext(); + exchange.setDestinationId(ctx.getComponentNameSpace()); + // TODO: this will fail if exchange is InOut + getSubscriptionManager().done(exchange); + } + } + } + + protected void resolveAddress(MessageExchangeImpl exchange) throws JBIException { + ServiceEndpoint theEndpoint = exchange.getEndpoint(); + if (theEndpoint != null) { + if (theEndpoint instanceof ExternalEndpoint) { + throw new JBIException("External endpoints can not be used for routing: should be an internal or dynamic endpoint."); + } + if (theEndpoint instanceof AbstractServiceEndpoint == false) { + throw new JBIException("Component-specific endpoints can not be used for routing: should be an internal or dynamic endpoint."); + } + } + // Resolve linked endpoints + if (theEndpoint instanceof LinkedEndpoint) { + QName svcName = ((LinkedEndpoint) theEndpoint).getToService(); + String epName = ((LinkedEndpoint) theEndpoint).getToEndpoint(); + ServiceEndpoint ep = registry.getInternalEndpoint(svcName, epName); + if (ep == null) { + throw new JBIException("Could not resolve linked endpoint: " + theEndpoint); + } + theEndpoint = ep; + } + + // get the context which created the exchange + ComponentContextImpl context = exchange.getSourceContext(); + if (theEndpoint == null) { + QName serviceName = exchange.getService(); + QName interfaceName = exchange.getInterfaceName(); + + // check in order, ServiceName then InterfaceName + // check to see if there is a match on the serviceName + if (serviceName != null) { + ServiceEndpoint[] endpoints = registry.getEndpointsForService(serviceName); + endpoints = getMatchingEndpoints(endpoints, exchange); + theEndpoint = getServiceChooser(exchange).chooseEndpoint(endpoints, context, exchange); + if (theEndpoint == null) { + log.warn("ServiceName (" + serviceName + ") specified for routing, but can't find it registered"); + } + } + if (theEndpoint == null && interfaceName != null) { + ServiceEndpoint[] endpoints = registry.getEndpointsForInterface(interfaceName); + endpoints = getMatchingEndpoints(endpoints, exchange); + theEndpoint = (InternalEndpoint) getInterfaceChooser(exchange).chooseEndpoint(endpoints, context, exchange); + if (theEndpoint == null) { + log.warn("InterfaceName (" + interfaceName + ") specified for routing, but can't find any matching components"); + } + } + if (theEndpoint == null) { + // lets use the resolver on the activation spec if + // applicable + ActivationSpec activationSpec = exchange.getActivationSpec(); + if (activationSpec != null) { + EndpointResolver destinationResolver = activationSpec.getDestinationResolver(); + if (destinationResolver != null) { + try { + EndpointFilter filter = createEndpointFilter(context, exchange); + theEndpoint = (InternalEndpoint) destinationResolver.resolveEndpoint(context, exchange, filter); + } + catch (JBIException e) { + throw new MessagingException("Failed to resolve endpoint: " + e, e); + } + } + } + } + } + if (theEndpoint != null) { + exchange.setEndpoint(theEndpoint); + } + if (log.isTraceEnabled()) { + log.trace("Routing exchange " + exchange + " to: " + theEndpoint); + } + } + + /** + * Filter the given endpoints by asking to the provider and consumer + * if they are both ok to process the exchange. + * + * @param endpoints an array of internal endpoints to check + * @param exchange the exchange that will be serviced + * @return an array of endpoints on which both consumer and provider agrees + */ + protected ServiceEndpoint[] getMatchingEndpoints(ServiceEndpoint[] endpoints, MessageExchangeImpl exchange) { + List filtered = new ArrayList(); + ComponentMBeanImpl consumer = getRegistry().getComponent(exchange.getSourceId()); + + for (int i = 0; i < endpoints.length; i++) { + ComponentNameSpace id = ((InternalEndpoint) endpoints[i]).getComponentNameSpace(); + if (id != null) { + ComponentMBeanImpl provider = getRegistry().getComponent(id); + if (provider != null) { + if (!consumer.getComponent().isExchangeWithProviderOkay(endpoints[i], exchange) || + !provider.getComponent().isExchangeWithConsumerOkay(endpoints[i], exchange)) { + continue; + } + } + } + filtered.add(endpoints[i]); + } + return (ServiceEndpoint[]) filtered.toArray(new ServiceEndpoint[filtered.size()]); + } + + /** + * @return the default EndpointChooser + */ + public EndpointChooser getDefaultInterfaceChooser() { + return defaultInterfaceChooser; + } + + /** + * Set the default EndpointChooser + * + * @param defaultInterfaceChooser + */ + public void setDefaultInterfaceChooser(EndpointChooser defaultInterfaceChooser) { + this.defaultInterfaceChooser = defaultInterfaceChooser; + } + + /** + * @return the default EndpointChooser + */ + public EndpointChooser getDefaultServiceChooser() { + return defaultServiceChooser; + } + + /** + * Set default EndpointChooser + * + * @param defaultServiceChooser + */ + public void setDefaultServiceChooser(EndpointChooser defaultServiceChooser) { + this.defaultServiceChooser = defaultServiceChooser; + } + + /** + * @return the defaultFlowChooser + */ + public FlowChooser getDefaultFlowChooser() { + return defaultFlowChooser; + } + + /** + * @param defaultFlowChooser the defaultFlowChooser to set + */ + public void setDefaultFlowChooser(FlowChooser defaultFlowChooser) { + this.defaultFlowChooser = defaultFlowChooser; + } + + /** + * Returns the endpoint chooser for endpoints found by service which will + * use the chooser on the exchange's activation spec if available otherwise + * will use the default + * + * @param exchange + * @return the EndpointChooser + */ + protected EndpointChooser getServiceChooser(MessageExchangeImpl exchange) { + EndpointChooser chooser = null; + ActivationSpec activationSpec = exchange.getActivationSpec(); + if (activationSpec != null) { + chooser = activationSpec.getServiceChooser(); + } + if (chooser == null) { + chooser = defaultServiceChooser; + } + return chooser; + } + + /** + * Returns the endpoint chooser for endpoints found by service which will + * use the chooser on the exchange's activation spec if available otherwise + * will use the default + * + * @param exchange + * @return the EndpointChooser + */ + protected EndpointChooser getInterfaceChooser(MessageExchangeImpl exchange) { + EndpointChooser chooser = null; + ActivationSpec activationSpec = exchange.getActivationSpec(); + if (activationSpec != null) { + chooser = activationSpec.getInterfaceChooser(); + } + if (chooser == null) { + chooser = defaultInterfaceChooser; + } + return chooser; + } + + /** + * Factory method to create an endpoint filter for the given component + * context and message exchange + * + * @param context + * @param exchange + * @return the EndpointFilter + */ + protected EndpointFilter createEndpointFilter(ComponentContextImpl context, MessageExchangeImpl exchange) { + Component component = context.getComponent(); + if (exchange.getRole() == Role.PROVIDER) { + return new ConsumerComponentEndpointFilter(component); + } + else { + return new ProducerComponentEndpointFilter(component); + } + } + + /** + * Get an array of MBeanOperationInfo + * + * @return array of OperationInfos + * @throws JMException + */ + public MBeanOperationInfo[] getOperationInfos() throws JMException { + OperationInfoHelper helper = new OperationInfoHelper(); + helper.addOperation(getObjectToManage(), "suspend", "suspend the NMR processing"); + helper.addOperation(getObjectToManage(), "resume", "resume the NMR processing"); + + return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos()); + } + + public JBIContainer getContainer() { + return container; + } + +} \ No newline at end of file Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/AbstractFlow.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/AbstractFlow.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/AbstractFlow.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/AbstractFlow.java Wed May 17 03:27:31 2006 @@ -64,9 +64,9 @@ this.broker = broker; this.name = name; // register self with the management context - ObjectName objectName = broker.getManagementContext().createObjectName(this); + ObjectName objectName = broker.getContainer().getManagementContext().createObjectName(this); try { - broker.getManagementContext().registerMBean(objectName, this, LifeCycleMBean.class); + broker.getContainer().getManagementContext().registerMBean(objectName, this, LifeCycleMBean.class); } catch (JMException e) { throw new JBIException("Failed to register MBean with the ManagementContext", e); @@ -165,7 +165,7 @@ log.debug("Called Flow doRouting"); ComponentNameSpace id = me.getRole() == Role.PROVIDER ? me.getDestinationId() : me.getSourceId(); //As the MessageExchange could come from another container - ensure we get the local Component - ComponentMBeanImpl lcc = broker.getRegistry().getComponent(id.getName()); + ComponentMBeanImpl lcc = broker.getContainer().getRegistry().getComponent(id.getName()); if (lcc != null) { if (lcc.getDeliveryChannel() != null) { lcc.getDeliveryChannel().processInBound(me); Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jca/JCAFlow.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jca/JCAFlow.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jca/JCAFlow.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jca/JCAFlow.java Wed May 17 03:27:31 2006 @@ -233,7 +233,7 @@ * @throws JBIException */ public void init(Broker broker, String subType) throws JBIException { - log.info(broker.getContainerName() + ": Initializing jca flow"); + log.info(broker.getContainer().getName() + ": Initializing jca flow"); super.init(broker, subType); // Create and register endpoint listener endpointListener = new EndpointAdapter() { @@ -262,7 +262,7 @@ // Inbound connector ActiveMQActivationSpec ac = new ActiveMQActivationSpec(); ac.setDestinationType("javax.jms.Queue"); - ac.setDestination(INBOUND_PREFIX + broker.getContainerName()); + ac.setDestination(INBOUND_PREFIX + broker.getContainer().getName()); containerConnector = new JCAConnector(); containerConnector.setBootstrapContext(getBootstrapContext()); containerConnector.setActivationSpec(ac); @@ -438,7 +438,7 @@ } // broadcast change to the network if (broadcast) { - log.info(broker.getContainerName() + ": broadcasting info for " + event); + log.info(broker.getContainer().getName() + ": broadcasting info for " + event); sendJmsMessage(broadcastTopic, event, false, false); } } catch (Exception e) { @@ -455,7 +455,7 @@ } // broadcast change to the network if (broadcast) { - log.info(broker.getContainerName() + ": broadcasting info for " + event); + log.info(broker.getContainer().getName() + ": broadcasting info for " + event); sendJmsMessage(broadcastTopic, event, false, false); } } catch (Exception e) { @@ -499,13 +499,13 @@ } public void onRemoteEndpointRegistered(EndpointEvent event) { - log.info(broker.getContainerName() + ": adding remote endpoint: " + event.getEndpoint()); - broker.getRegistry().registerRemoteEndpoint(event.getEndpoint()); + log.info(broker.getContainer().getName() + ": adding remote endpoint: " + event.getEndpoint()); + broker.getContainer().getRegistry().registerRemoteEndpoint(event.getEndpoint()); } public void onRemoteEndpointUnregistered(EndpointEvent event) { - log.info(broker.getContainerName() + ": removing remote endpoint: " + event.getEndpoint()); - broker.getRegistry().unregisterRemoteEndpoint(event.getEndpoint()); + log.info(broker.getContainer().getName() + ": removing remote endpoint: " + event.getEndpoint()); + broker.getContainer().getRegistry().unregisterRemoteEndpoint(event.getEndpoint()); } /** @@ -580,7 +580,7 @@ } if (me.getDestinationId() == null) { ServiceEndpoint se = me.getEndpoint(); - se = broker.getRegistry() + se = broker.getContainer().getRegistry() .getInternalEndpoint(se.getServiceName(), se.getEndpointName()); me.setEndpoint(se); me.setDestinationId(((InternalEndpoint) se).getComponentNameSpace()); @@ -603,7 +603,7 @@ if (obj instanceof ConsumerInfo) { ConsumerInfo info = (ConsumerInfo) obj; subscriberSet.add(info.getConsumerId().getConnectionId()); - ServiceEndpoint[] endpoints = broker.getRegistry().getEndpointsForInterface(null); + ServiceEndpoint[] endpoints = broker.getContainer().getRegistry().getEndpointsForInterface(null); for (int i = 0; i < endpoints.length; i++) { if (endpoints[i] instanceof InternalEndpoint && ((InternalEndpoint) endpoints[i]).isLocal()) { onInternalEndpointRegistered(new EndpointEvent(endpoints[i], @@ -668,7 +668,7 @@ public BootstrapContext getBootstrapContext() { if (bootstrapContext == null) { - GeronimoWorkManager wm = (GeronimoWorkManager) broker.getWorkManager(); + GeronimoWorkManager wm = (GeronimoWorkManager) broker.getContainer().getWorkManager(); bootstrapContext = new BootstrapContextImpl(wm); } return bootstrapContext; @@ -679,7 +679,7 @@ } public String toString(){ - return broker.getContainerName() + " JCAFlow"; + return broker.getContainer().getName() + " JCAFlow"; } private void sendJmsMessage(Destination dest, Serializable object, boolean persistent, boolean transacted) throws JMSException { Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jms/JMSFlow.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jms/JMSFlow.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jms/JMSFlow.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/jms/JMSFlow.java Wed May 17 03:27:31 2006 @@ -210,7 +210,7 @@ * @throws JBIException */ public void init(Broker broker, String subType) throws JBIException { - log.info(broker.getContainerName() + ": Initializing jms flow"); + log.info(broker.getContainer().getName() + ": Initializing jms flow"); super.init(broker, subType); // Create and register endpoint listener endpointListener = new EndpointAdapter() { @@ -246,10 +246,10 @@ } else { connection = (ActiveMQConnection) connectionFactory.createConnection(); } - connection.setClientID(broker.getContainerName()); + connection.setClientID(broker.getContainer().getName()); connection.start(); inboundSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue queue = inboundSession.createQueue(INBOUND_PREFIX + broker.getContainerName()); + Queue queue = inboundSession.createQueue(INBOUND_PREFIX + broker.getContainer().getName()); MessageConsumer inboundQueue = inboundSession.createConsumer(queue); inboundQueue.setMessageListener(this); queueProducer = inboundSession.createProducer(null); @@ -270,7 +270,7 @@ */ public void start() throws JBIException { if (started.compareAndSet(false, true)) { - log.info(broker.getContainerName() + ": Starting jms flow"); + log.info(broker.getContainer().getName() + ": Starting jms flow"); super.start(); try { broadcastConsumer = broadcastSession.createConsumer(broadcastTopic, null, true); @@ -302,14 +302,14 @@ }); // Start queue consumers for all components - for (Iterator it = broker.getRegistry().getComponents().iterator(); it.hasNext();) { + for (Iterator it = broker.getContainer().getRegistry().getComponents().iterator(); it.hasNext();) { ComponentMBeanImpl cmp = (ComponentMBeanImpl) it.next(); if (cmp.isStarted()) { onComponentStarted(new ComponentEvent(cmp, ComponentEvent.COMPONENT_STARTED)); } } // Start queue consumers for all endpoints - ServiceEndpoint[] endpoints = broker.getRegistry().getEndpointsForInterface(null); + ServiceEndpoint[] endpoints = broker.getContainer().getRegistry().getEndpointsForInterface(null); for (int i = 0; i < endpoints.length; i++) { if (endpoints[i] instanceof InternalEndpoint && ((InternalEndpoint) endpoints[i]).isLocal()) { onInternalEndpointRegistered(new EndpointEvent(endpoints[i], @@ -330,7 +330,7 @@ */ public void stop() throws JBIException { if (started.compareAndSet(true, false)) { - log.info(broker.getContainerName() + ": Stopping jms flow"); + log.info(broker.getContainer().getName() + ": Stopping jms flow"); super.stop(); for (Iterator it = subscriberSet.iterator(); it.hasNext();) { String id = (String) it.next(); @@ -385,7 +385,7 @@ consumerMap.put(key, consumer); } if (broadcast) { - log.info(broker.getContainerName() + ": broadcasting info for " + event); + log.info(broker.getContainer().getName() + ": broadcasting info for " + event); ObjectMessage msg = broadcastSession.createObjectMessage(event); topicProducer.send(msg); } @@ -403,7 +403,7 @@ } if (broadcast) { ObjectMessage msg = broadcastSession.createObjectMessage(event); - log.info(broker.getContainerName() + ": broadcasting info for " + event); + log.info(broker.getContainer().getName() + ": broadcasting info for " + event); topicProducer.send(msg); } } catch (Exception e) { @@ -441,13 +441,13 @@ } public void onRemoteEndpointRegistered(EndpointEvent event) { - log.info(broker.getContainerName() + ": adding remote endpoint: " + event.getEndpoint()); - broker.getRegistry().registerRemoteEndpoint(event.getEndpoint()); + log.info(broker.getContainer().getName() + ": adding remote endpoint: " + event.getEndpoint()); + broker.getContainer().getRegistry().registerRemoteEndpoint(event.getEndpoint()); } public void onRemoteEndpointUnregistered(EndpointEvent event) { - log.info(broker.getContainerName() + ": removing remote endpoint: " + event.getEndpoint()); - broker.getRegistry().unregisterRemoteEndpoint(event.getEndpoint()); + log.info(broker.getContainer().getName() + ": removing remote endpoint: " + event.getEndpoint()); + broker.getContainer().getRegistry().unregisterRemoteEndpoint(event.getEndpoint()); } /** @@ -518,7 +518,7 @@ // Dispatch the message in another thread so as to free the jms session // else if a component do a sendSync into the jms flow, the whole // flow is deadlocked - broker.getWorkManager().scheduleWork(new Work() { + broker.getContainer().getWorkManager().scheduleWork(new Work() { public void release() { } @@ -526,7 +526,7 @@ try { if (me.getDestinationId() == null) { ServiceEndpoint se = me.getEndpoint(); - se = broker.getRegistry() + se = broker.getContainer().getRegistry() .getInternalEndpoint(se.getServiceName(), se.getEndpointName()); me.setEndpoint(se); me.setDestinationId(((InternalEndpoint) se).getComponentNameSpace()); @@ -549,7 +549,7 @@ if (obj instanceof ConsumerInfo) { ConsumerInfo info = (ConsumerInfo) obj; subscriberSet.add(info.getConsumerId().getConnectionId()); - ServiceEndpoint[] endpoints = broker.getRegistry().getEndpointsForInterface(null); + ServiceEndpoint[] endpoints = broker.getContainer().getRegistry().getEndpointsForInterface(null); for (int i = 0; i < endpoints.length; i++) { if (endpoints[i] instanceof InternalEndpoint && ((InternalEndpoint) endpoints[i]).isLocal()) { onInternalEndpointRegistered(new EndpointEvent(endpoints[i], Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaFlow.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaFlow.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaFlow.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaFlow.java Wed May 17 03:27:31 2006 @@ -238,12 +238,12 @@ protected void registerQueue(ComponentNameSpace cns, SedaQueue queue) { try { - ObjectName objectName = broker.getManagementContext().createObjectName(queue); + ObjectName objectName = broker.getContainer().getManagementContext().createObjectName(queue); if (getSubType() != null) { objectName = new ObjectName(objectName + ",subtype=" + getSubType()); } queue.setObjectName(objectName); - broker.getManagementContext().registerMBean(objectName, queue, LifeCycleMBean.class); + broker.getContainer().getManagementContext().registerMBean(objectName, queue, LifeCycleMBean.class); } catch (JMException e) { log.error("Failed to register SedaQueue: " + queue + " with the ManagementContext"); @@ -252,7 +252,7 @@ protected void unregisterQueue(SedaQueue queue) { try { - broker.getManagementContext().unregisterMBean(queue.getObjectName()); + broker.getContainer().getManagementContext().unregisterMBean(queue.getObjectName()); } catch (JBIException e) { log.error("Failed to unregister SedaQueue: " + queue + " from the ManagementContext"); Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaQueue.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaQueue.java?rev=407213&r1=407212&r2=407213&view=diff ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaQueue.java (original) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/nmr/flow/seda/SedaQueue.java Wed May 17 03:27:31 2006 @@ -142,7 +142,7 @@ synchronized (running) { try { started.set(true); - flow.getBroker().getWorkManager().startWork(this); + flow.getBroker().getContainer().getWorkManager().startWork(this); running.wait(); super.start(); } catch (Exception e) { @@ -210,7 +210,7 @@ try { me = (MessageExchangeImpl) queue.poll(1000); if (me != null) { - flow.getBroker().getWorkManager().scheduleWork(new Work() { + flow.getBroker().getContainer().getWorkManager().scheduleWork(new Work() { public void release() { } public void run() { Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationEntry.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationEntry.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationEntry.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationEntry.java Wed May 17 03:27:31 2006 @@ -0,0 +1,118 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.StringTokenizer; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; + +/** + * + * @author gnodet + * @org.apache.xbean.XBean + */ +public class AuthorizationEntry { + + private Set acls; + private QName service; + private String endpoint; + + public AuthorizationEntry() { + } + + public AuthorizationEntry(QName service, String endpoint, String roles) { + this.service = service; + this.endpoint = endpoint; + setRoles(roles); + } + + /** + * @return the endpoint + */ + public String getEndpoint() { + return endpoint; + } + + /** + * @param endpoint the endpoint to set + */ + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + /** + * @return the service + */ + public QName getService() { + return service; + } + + /** + * @param service the service to set + */ + public void setService(QName service) { + // Hack a bit to support wildcards + // If the attribute was service="*:*", then the namespace is not found, but the prefix is set + if (XMLConstants.NULL_NS_URI.equals(service.getNamespaceURI()) && + service.getPrefix() != null && service.getPrefix().length() > 0) { + service = new QName(service.getPrefix(), service.getLocalPart()); + } + this.service = service; + } + + /** + * @return the acls + */ + public Set getAcls() { + return acls; + } + + /** + * @param acls the acls to set + */ + public void setAcls(Set acls) { + this.acls = acls; + } + + public void setRoles(String roles) { + this.acls = new HashSet(); + StringTokenizer iter = new StringTokenizer(roles, ","); + while (iter.hasMoreTokens()) { + String name = iter.nextToken().trim(); + this.acls.add(new GroupPrincipal(name)); + } + } + + public String getRoles() { + StringBuffer sb = new StringBuffer(); + for (Iterator iter = this.acls.iterator(); iter.hasNext();) { + GroupPrincipal p = (GroupPrincipal) iter.next(); + sb.append(p); + if (iter.hasNext()) { + sb.append(","); + } + } + return sb.toString(); + } + + public String toString() { + return "AuthorizationEntry[service=" + service + ", endpoint=" + endpoint + ", roles=" + getRoles() + "]"; + } +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationMap.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationMap.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationMap.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/AuthorizationMap.java Wed May 17 03:27:31 2006 @@ -0,0 +1,26 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.util.Set; + +import javax.jbi.servicedesc.ServiceEndpoint; + +public interface AuthorizationMap { + + Set getAcls(ServiceEndpoint endpoint); + +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/DefaultAuthorizationMap.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/DefaultAuthorizationMap.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/DefaultAuthorizationMap.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/DefaultAuthorizationMap.java Wed May 17 03:27:31 2006 @@ -0,0 +1,103 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +import javax.jbi.servicedesc.ServiceEndpoint; +import javax.xml.namespace.QName; + +/** + * + * @author gnodet + * @org.apache.xbean.XBean element="authorizationMap" + */ +public class DefaultAuthorizationMap implements AuthorizationMap { + + private AuthorizationEntry defaultEntry; + private List authorizationEntries; + + public DefaultAuthorizationMap() { + } + + public DefaultAuthorizationMap(List authorizationEntries) { + this.authorizationEntries = authorizationEntries; + } + + /** + * @return the authorizationEntries + */ + public List getAuthorizationEntries() { + return authorizationEntries; + } + + /** + * @param authorizationEntries the authorizationEntries to set + * @org.apache.xbean.ElementType class="org.apache.servicemix.jbi.security.AuthorizationEntry" + */ + public void setAuthorizationEntries(List authorizationEntries) { + this.authorizationEntries = authorizationEntries; + } + + /** + * @return the defaultEntry + */ + public AuthorizationEntry getDefaultEntry() { + return defaultEntry; + } + + /** + * @param defaultEntry the defaultEntry to set + */ + public void setDefaultEntry(AuthorizationEntry defaultEntry) { + this.defaultEntry = defaultEntry; + } + + public Set getAcls(ServiceEndpoint endpoint) { + Set acls = new HashSet(); + for (Iterator iter = authorizationEntries.iterator(); iter.hasNext();) { + AuthorizationEntry entry = (AuthorizationEntry) iter.next(); + if (match(entry, endpoint)) { + acls.addAll(entry.getAcls()); + } + } + if (defaultEntry != null) { + acls.add(defaultEntry); + } + return acls; + } + + protected boolean match(AuthorizationEntry entry, ServiceEndpoint endpoint) { + return match(entry.getService(), endpoint.getServiceName()) && + match(entry.getEndpoint(), endpoint.getEndpointName()); + } + + private boolean match(QName acl, QName target) { + return match(acl.getNamespaceURI(), target.getNamespaceURI()) && + match(acl.getLocalPart(), target.getLocalPart()); + } + + private boolean match(String acl, String target) { + return acl == null || + acl.equals("*") || + Pattern.matches(acl, target); + } + +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/GroupPrincipal.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/GroupPrincipal.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/GroupPrincipal.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/GroupPrincipal.java Wed May 17 03:27:31 2006 @@ -0,0 +1,59 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.security.Principal; + + +/** + * + */ +public class GroupPrincipal implements Principal { + + private final String name; + private transient int hash; + + public GroupPrincipal(String name) { + if (name == null) throw new IllegalArgumentException("name cannot be null"); + this.name = name; + } + + public String getName() { + return name; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final GroupPrincipal that = (GroupPrincipal) o; + + if (!name.equals(that.name)) return false; + + return true; + } + + public int hashCode() { + if (hash == 0) { + hash = name.hashCode(); + } + return hash; + } + + public String toString() { + return name; + } +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/PropertiesLoginModule.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/PropertiesLoginModule.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/PropertiesLoginModule.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/PropertiesLoginModule.java Wed May 17 03:27:31 2006 @@ -0,0 +1,170 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.io.File; +import java.io.IOException; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.LoginException; +import javax.security.auth.login.FailedLoginException; +import javax.security.auth.spi.LoginModule; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +/** + * @version $Rev: $ $Date: $ + */ +public class PropertiesLoginModule implements LoginModule { + + private final String USER_FILE = "org.apache.servicemix.security.properties.user"; + private final String GROUP_FILE = "org.apache.servicemix.security.properties.group"; + + private static final Log log = LogFactory.getLog(PropertiesLoginModule.class); + + private Subject subject; + private CallbackHandler callbackHandler; + + private boolean debug; + private String usersFile; + private String groupsFile; + private Properties users = new Properties(); + private Properties groups = new Properties(); + private String user; + private Set principals = new HashSet(); + private File baseDir; + + public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { + this.subject = subject; + this.callbackHandler = callbackHandler; + + if( System.getProperty("java.security.auth.login.config")!=null ) { + baseDir=new File(System.getProperty("java.security.auth.login.config")).getParentFile(); + } else { + baseDir = new File("."); + } + + debug = "true".equalsIgnoreCase((String) options.get("debug")); + usersFile = (String) options.get(USER_FILE)+""; + groupsFile = (String) options.get(GROUP_FILE)+""; + + if (debug) { + log.debug("Initialized debug=" + debug + " usersFile=" + usersFile + " groupsFile=" + groupsFile+" basedir="+baseDir); + } + } + + public boolean login() throws LoginException { + File f = new File(baseDir,usersFile); + try { + users.load(new java.io.FileInputStream(f)); + } catch (IOException ioe) { + throw new LoginException("Unable to load user properties file " + f); + } + f = new File(baseDir, groupsFile); + try { + groups.load(new java.io.FileInputStream(f)); + } catch (IOException ioe) { + throw new LoginException("Unable to load group properties file " + f); + } + + Callback[] callbacks = new Callback[2]; + + callbacks[0] = new NameCallback("Username: "); + callbacks[1] = new PasswordCallback("Password: ", false); + try { + callbackHandler.handle(callbacks); + } catch (IOException ioe) { + throw new LoginException(ioe.getMessage()); + } catch (UnsupportedCallbackException uce) { + throw new LoginException(uce.getMessage() + " not available to obtain information from user"); + } + user = ((NameCallback) callbacks[0]).getName(); + char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword(); + if (tmpPassword == null) tmpPassword = new char[0]; + + String password = users.getProperty(user); + + if (password == null) throw new FailedLoginException("User does not exist"); + if (!password.equals(new String(tmpPassword))) throw new FailedLoginException("Password does not match"); + + users.clear(); + + if (debug) { + log.debug("login " + user); + } + return true; + } + + public boolean commit() throws LoginException { + principals.add(new UserPrincipal(user)); + + for (Enumeration enumeration = groups.keys(); enumeration.hasMoreElements();) { + String name = (String) enumeration.nextElement(); + String[] userList = ((String) groups.getProperty(name) + "").split(","); + for (int i = 0; i < userList.length; i++) { + if (user.equals(userList[i])) { + principals.add(new GroupPrincipal(name)); + break; + } + } + } + + subject.getPrincipals().addAll(principals); + + clear(); + + if (debug) { + log.debug("commit"); + } + return true; + } + + public boolean abort() throws LoginException { + clear(); + + if (debug) { + log.debug("abort"); + } + return true; + } + + public boolean logout() throws LoginException { + subject.getPrincipals().removeAll(principals); + principals.clear(); + + if (debug) { + log.debug("logout"); + } + return true; + } + + private void clear() { + groups.clear(); + user = null; + } +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/SecuredBroker.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/SecuredBroker.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/SecuredBroker.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/SecuredBroker.java Wed May 17 03:27:31 2006 @@ -0,0 +1,79 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.util.Set; + +import javax.jbi.JBIException; +import javax.jbi.messaging.MessageExchange; +import javax.jbi.messaging.MessageExchange.Role; +import javax.jbi.servicedesc.ServiceEndpoint; +import javax.security.auth.Subject; + +import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; +import org.apache.servicemix.jbi.nmr.DefaultBroker; + +/** + * + * @author gnodet + * @org.apache.xbean.XBean + */ +public class SecuredBroker extends DefaultBroker { + + private AuthorizationMap authorizationMap; + + public SecuredBroker() { + } + + public SecuredBroker(AuthorizationMap authorizationMap) { + this.authorizationMap = authorizationMap; + } + + /** + * @return the authorizationMap + */ + public AuthorizationMap getAuthorizationMap() { + return authorizationMap; + } + + /** + * @param authorizationMap the authorizationMap to set + */ + public void setAuthorizationMap(AuthorizationMap authorizationMap) { + this.authorizationMap = authorizationMap; + } + + public void sendExchangePacket(MessageExchange me) throws JBIException { + MessageExchangeImpl exchange = (MessageExchangeImpl) me; + if (exchange.getRole() == Role.PROVIDER && exchange.getDestinationId() == null) { + resolveAddress(exchange); + ServiceEndpoint se = exchange.getEndpoint(); + if (se != null) { + Subject subject = exchange.getMessage("in").getSecuritySubject(); + if (subject == null) { + throw new SecurityException("User not authenticated"); + } + Set acls = authorizationMap.getAcls(se); + acls.retainAll(subject.getPrincipals()); + if (acls.size() == 0) { + throw new SecurityException("Endpoint is not authorized for this user"); + } + } + } + super.sendExchangePacket(me); + } + +} Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/UserPrincipal.java URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/UserPrincipal.java?rev=407213&view=auto ============================================================================== --- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/UserPrincipal.java (added) +++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/security/UserPrincipal.java Wed May 17 03:27:31 2006 @@ -0,0 +1,59 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * 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.apache.servicemix.jbi.security; + +import java.security.Principal; + + +/** + * @version $Rev: $ $Date: $ + */ +public class UserPrincipal implements Principal { + + private final String name; + private transient int hash; + + public UserPrincipal(String name) { + if (name == null) throw new IllegalArgumentException("name cannot be null"); + this.name = name; + } + + public String getName() { + return name; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final UserPrincipal that = (UserPrincipal) o; + + if (!name.equals(that.name)) return false; + + return true; + } + + public int hashCode() { + if (hash == 0) { + hash = name.hashCode(); + } + return hash; + } + + public String toString() { + return name; + } +}