View Javadoc

1   /*
2    * $Id: DefaultModelServiceDescriptor.java 10787 2008-02-12 18:51:50Z dfeist $
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.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  /**
25   * @inheritDocs
26   */
27  public class DefaultModelServiceDescriptor extends AbstractServiceDescriptor implements ModelServiceDescriptor
28  {
29      private String modelClass;
30      private Properties properties;
31      
32      public DefaultModelServiceDescriptor(String service, Properties properties)
33      {
34          super(service);
35          this.properties = properties;
36          modelClass = removeProperty(MuleProperties.MODEL_CLASS, properties);
37      }
38  
39      public Model createModel() throws ServiceException
40      {
41          if (modelClass != null)
42          {
43              try {
44                  Model model = (Model)ClassUtils.instanciateClass(modelClass, ClassUtils.NO_ARGS, getClass());
45                  BeanUtils.populateWithoutFail(model, properties, false);
46                  return model;
47              }
48              catch (Exception e)
49              {
50                  throw new ServiceException(CoreMessages.failedToCreate(modelClass), e);
51              }            
52          }
53          else return null;
54      }
55  
56      public Class getModelClass() throws ServiceException
57      {
58          try {
59              return ClassUtils.getClass(modelClass);
60          }
61          catch (ClassNotFoundException e)
62          {
63              throw new ServiceException(CoreMessages.cannotLoadFromClasspath(modelClass), e);
64          }
65      }
66  }