View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.model;
8   
9   import org.mule.api.config.MuleProperties;
10  import org.mule.api.model.Model;
11  import org.mule.api.model.ModelServiceDescriptor;
12  import org.mule.api.registry.AbstractServiceDescriptor;
13  import org.mule.api.registry.ServiceException;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.util.BeanUtils;
16  import org.mule.util.ClassUtils;
17  
18  import java.util.Properties;
19  
20  public class DefaultModelServiceDescriptor extends AbstractServiceDescriptor implements ModelServiceDescriptor
21  {
22      private String modelClass;
23      private Properties properties;
24      
25      public DefaultModelServiceDescriptor(String service, Properties properties)
26      {
27          super(service);
28          this.properties = properties;
29          modelClass = removeProperty(MuleProperties.MODEL_CLASS, properties);
30      }
31  
32      public Model createModel() throws ServiceException
33      {
34          if (modelClass != null)
35          {
36              try 
37              {
38                  Model model = (Model)ClassUtils.instanciateClass(modelClass, ClassUtils.NO_ARGS, getClass());
39                  BeanUtils.populateWithoutFail(model, properties, false);
40                  return model;
41              }
42              catch (Exception e)
43              {
44                  throw new ServiceException(CoreMessages.failedToCreate(modelClass), e);
45              }            
46          }
47          else return null;
48      }
49  
50      @SuppressWarnings("unchecked")
51      public Class<Model> getModelClass() throws ServiceException
52      {
53          try 
54          {
55              return ClassUtils.getClass(modelClass);
56          }
57          catch (ClassNotFoundException e)
58          {
59              throw new ServiceException(CoreMessages.cannotLoadFromClasspath(modelClass), e);
60          }
61      }
62  }