Save This Page
Home » synapse-1.2-src » org.apache.synapse.mediators.spring » [javadoc | source]
    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.synapse.MessageContext;
   23   import org.apache.synapse.Mediator;
   24   import org.apache.synapse.ManagedLifecycle;
   25   import org.apache.synapse.SynapseException;
   26   import org.apache.synapse.core.SynapseEnvironment;
   27   import org.apache.synapse.mediators.AbstractMediator;
   28   import org.apache.synapse.config.SynapseConfigUtils;
   29   import org.apache.synapse.config.Entry;
   30   import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
   31   import org.springframework.context.ApplicationContext;
   32   import org.springframework.context.support.GenericApplicationContext;
   33   import org.springframework.core.io.InputStreamResource;
   34   
   35   /**
   36    * This mediator allows Spring beans implementing the org.apache.synapse.Mediator
   37    * interface to mediate messages passing through Synapse.
   38    *
   39    * A Spring mediator is instantiated by Spring (see www.springframework.org). The mediator
   40    * refers to a Spring bean name, and also either a Spring configuration defined to Synapse
   41    * or an inlined Spring configuration.
   42    */
   43   public class SpringMediator extends AbstractMediator implements ManagedLifecycle {
   44   
   45       /**
   46        * The Spring bean ref to be used
   47        */
   48       private String beanName = null;
   49       /**
   50        * The named Spring config to be used
   51        */
   52       private String configKey = null;
   53       /**
   54        * The Spring ApplicationContext to be used
   55        */
   56       private ApplicationContext appContext = null;
   57   
   58       public boolean mediate(MessageContext synCtx) {
   59   
   60           boolean traceOn = isTraceOn(synCtx);
   61           boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
   62   
   63           if (traceOrDebugOn) {
   64               traceOrDebug(traceOn, "Start : Spring mediator");
   65   
   66               if (traceOn && trace.isTraceEnabled()) {
   67                   trace.trace("Message : " + synCtx.getEnvelope());
   68               }
   69           }
   70   
   71           Entry entry = synCtx.getConfiguration().getEntryDefinition(configKey);
   72   
   73           // if the configKey refers to a dynamic property
   74           if (entry != null && entry.isDynamic()) {
   75               if (!entry.isCached() || entry.isExpired()) {
   76                   buildAppContext(synCtx, traceOrDebugOn, traceOn);
   77               }
   78           // if the property is not a DynamicProperty, we will create an ApplicationContext only once
   79           } else {
   80               if (appContext == null) {
   81                   buildAppContext(synCtx, traceOrDebugOn, traceOn);
   82               }
   83           }
   84   
   85           if (appContext != null) {
   86   
   87               Object o = appContext.getBean(beanName);    
   88               if (o != null && Mediator.class.isAssignableFrom(o.getClass())) {
   89                   Mediator m = (Mediator) o;
   90                   if (traceOrDebugOn) {
   91                       traceOrDebug(traceOn, "Loaded mediator from bean : " + beanName + " executing...");
   92                   }
   93                   return m.mediate(synCtx);
   94   
   95               } else {
   96                   if (traceOrDebugOn) {
   97                       traceOrDebug(traceOn, "Unable to load mediator from bean : " + beanName);
   98                   }
   99                   handleException("Could not load bean named : " + beanName +
  100                       " from the Spring configuration with key : " + configKey, synCtx);
  101               }
  102           } else {
  103               handleException("Cannot reference application context with key : " + configKey, synCtx);
  104           }
  105   
  106           if (traceOrDebugOn) {
  107               traceOrDebug(traceOn, "End : Spring mediator");
  108           }
  109           return true;
  110       }
  111   
  112       private synchronized void buildAppContext(MessageContext synCtx,
  113           boolean traceOrDebugOn, boolean traceOn) {
  114   
  115           if (traceOrDebugOn) {
  116               traceOrDebug(traceOn, "Creating Spring ApplicationContext from key : " + configKey);
  117           }
  118           GenericApplicationContext appContext = new GenericApplicationContext();
  119           XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
  120           xbdr.setValidating(false);
  121   
  122           Object springConfig = synCtx.getEntry(configKey);
  123           if(springConfig == null) {
  124             String errorMessage = "Cannot look up Spring configuration " + configKey;
  125             log.error(errorMessage);
  126             throw new SynapseException(errorMessage);
  127           }
  128   
  129           xbdr.loadBeanDefinitions(
  130               new InputStreamResource(
  131                   SynapseConfigUtils.getStreamSource(springConfig).getInputStream()));
  132           appContext.refresh();
  133           if (traceOrDebugOn) {
  134               traceOrDebug(traceOn, "Spring ApplicationContext from key : " + configKey + " created");
  135           }
  136           this.appContext = appContext;
  137       }
  138   
  139       public void setBeanName(String beanName) {
  140           this.beanName = beanName;
  141       }
  142   
  143       public String getBeanName() {
  144           return beanName;
  145       }
  146   
  147       public String getConfigKey() {
  148           return configKey;
  149       }
  150   
  151       public void setConfigKey(String configKey) {
  152           this.configKey = configKey;
  153       }
  154   
  155       public ApplicationContext getAppContext() {
  156           return appContext;
  157       }
  158   
  159       public void setAppContext(ApplicationContext appContext) {
  160           this.appContext = appContext;
  161       }
  162   
  163       public String getType() {
  164           return "SpringMediator";
  165       }
  166   
  167   
  168     public void init(SynapseEnvironment se) {
  169           MessageContext synCtx = se.createMessageContext();
  170           buildAppContext(synCtx, log.isDebugEnabled(), false);
  171     }
  172   
  173     public void destroy() {
  174     }
  175   }

Save This Page
Home » synapse-1.2-src » org.apache.synapse.mediators.spring » [javadoc | source]