Coverage Report - org.mule.impl.model.ModelFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ModelFactory
56%
10/18
75%
6/8
5
 
 1  
 /*
 2  
  * $Id: ModelFactory.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.impl.model;
 12  
 
 13  
 import org.mule.providers.service.TransportFactory;
 14  
 import org.mule.umo.model.UMOModel;
 15  
 import org.mule.util.BeanUtils;
 16  
 import org.mule.util.ClassUtils;
 17  
 import org.mule.util.SpiUtils;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.util.Properties;
 22  
 
 23  
 /**
 24  
  * Will locate the model service in META-INF/service using the model type as the key
 25  
  * and construct the model.
 26  
  */
 27  
 public final class ModelFactory
 28  
 {
 29  
 
 30  
     public static final String DEFAULT_MODEL_NAME = "main";
 31  
 
 32  
     public static final String MODEL_SERVICE_PATH = "org/mule/models";
 33  
 
 34  
     /** Do not instanciate. */
 35  
     private ModelFactory ()
 36  0
     {
 37  
         // no-op
 38  0
     }
 39  
 
 40  
     public static UMOModel createModel(String type) throws ModelServiceNotFoundException
 41  
     {
 42  414
         String location = SpiUtils.SERVICE_ROOT + MODEL_SERVICE_PATH;
 43  418
         InputStream is = SpiUtils.findServiceDescriptor(MODEL_SERVICE_PATH, type + ".properties", TransportFactory.class);
 44  
         try
 45  
         {
 46  414
             if (is != null)
 47  
             {
 48  414
                 Properties props = new Properties();
 49  414
                 props.load(is);
 50  414
                 String clazz = props.getProperty("model");
 51  
                 try
 52  
                 {
 53  414
                     UMOModel model = (UMOModel) ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS,
 54  
                         ModelFactory.class);
 55  414
                     BeanUtils.populateWithoutFail(model, props, false);
 56  414
                     if (model.getName() == null)
 57  
                     {
 58  0
                         model.setName(DEFAULT_MODEL_NAME);
 59  
                     }
 60  414
                     return model;
 61  
                 }
 62  0
                 catch (Exception e)
 63  
                 {
 64  0
                     throw new ModelServiceNotFoundException(location + "/" + type, e);
 65  
                 }
 66  
             }
 67  
             else
 68  
             {
 69  0
                 throw new ModelServiceNotFoundException(location + "/" + type);
 70  
             }
 71  
         }
 72  0
         catch (IOException e)
 73  
         {
 74  0
             throw new ModelServiceNotFoundException(location + "/" + type, e);
 75  
         }
 76  
     }
 77  
 }