View Javadoc

1   /*
2    * $Id: DefaultModelServiceDescriptor.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.model;
12  
13  import org.mule.api.config.MuleProperties;
14  import org.mule.api.model.Model;
15  import org.mule.api.model.ModelServiceDescriptor;
16  import org.mule.api.registry.AbstractServiceDescriptor;
17  import org.mule.api.registry.ServiceException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.util.BeanUtils;
20  import org.mule.util.ClassUtils;
21  
22  import java.util.Properties;
23  
24  public class DefaultModelServiceDescriptor extends AbstractServiceDescriptor implements ModelServiceDescriptor
25  {
26      private String modelClass;
27      private Properties properties;
28      
29      public DefaultModelServiceDescriptor(String service, Properties properties)
30      {
31          super(service);
32          this.properties = properties;
33          modelClass = removeProperty(MuleProperties.MODEL_CLASS, properties);
34      }
35  
36      public Model createModel() throws ServiceException
37      {
38          if (modelClass != null)
39          {
40              try 
41              {
42                  Model model = (Model)ClassUtils.instanciateClass(modelClass, ClassUtils.NO_ARGS, getClass());
43                  BeanUtils.populateWithoutFail(model, properties, false);
44                  return model;
45              }
46              catch (Exception e)
47              {
48                  throw new ServiceException(CoreMessages.failedToCreate(modelClass), e);
49              }            
50          }
51          else return null;
52      }
53  
54      @SuppressWarnings("unchecked")
55      public Class<Model> getModelClass() throws ServiceException
56      {
57          try 
58          {
59              return ClassUtils.getClass(modelClass);
60          }
61          catch (ClassNotFoundException e)
62          {
63              throw new ServiceException(CoreMessages.cannotLoadFromClasspath(modelClass), e);
64          }
65      }
66  }