1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.synapse.mediators.spring; 21 22 import org.apache.axiom.om.OMAttribute; 23 import org.apache.axiom.om.OMElement; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.synapse.Mediator; 27 import org.apache.synapse.config.xml.XMLConfigConstants; 28 import org.apache.synapse.config.xml.AbstractMediatorFactory; 29 30 import javax.xml.namespace.QName; 31 32 /** 33 * Creates an instance of a Spring mediator that refers to the given Spring 34 * configuration and bean. Optionally, one could specify an inlined Spring 35 * configuration as opposed to a globally defined Spring configuration 36 * <p/> 37 * <spring bean="exampleBean1" key="string""/> 38 */ 39 public class SpringMediatorFactory extends AbstractMediatorFactory { 40 41 private static final Log log = LogFactory.getLog(SpringMediatorFactory.class); 42 43 private static final QName TAG_NAME = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE + "/spring", "spring"); 44 45 /** 46 * Create a Spring mediator instance referring to the bean and configuration given 47 * by the OMElement declaration 48 * 49 * @param elem the OMElement that specifies the Spring mediator configuration 50 * @return the Spring mediator instance created 51 */ 52 public Mediator createMediator(OMElement elem) { 53 54 SpringMediator sm = new SpringMediator(); 55 OMAttribute bean = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "bean")); 56 OMAttribute key = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key")); 57 58 if (bean == null) { 59 handleException("The 'bean' attribute is required for a Spring mediator definition"); 60 } else if (key == null) { 61 handleException("A 'key' attribute is required for a Spring mediator definition"); 62 } else { 63 64 // after successfully creating the mediator 65 // set its common attributes such as tracing etc 66 processTraceState(sm,elem); 67 sm.setBeanName(bean.getAttributeValue()); 68 sm.setConfigKey(key.getAttributeValue()); 69 return sm; 70 } 71 return null; 72 } 73 74 public QName getTagQName() { 75 return TAG_NAME; 76 } 77 }