1
2
3
4
5
6
7
8
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
25
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
35 private ModelFactory ()
36 {
37
38 }
39
40 public static UMOModel createModel(String type) throws ModelServiceNotFoundException
41 {
42 String location = SpiUtils.SERVICE_ROOT + MODEL_SERVICE_PATH;
43 InputStream is = SpiUtils.findServiceDescriptor(MODEL_SERVICE_PATH, type + ".properties", TransportFactory.class);
44 try
45 {
46 if (is != null)
47 {
48 Properties props = new Properties();
49 props.load(is);
50 String clazz = props.getProperty("model");
51 try
52 {
53 UMOModel model = (UMOModel) ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS,
54 ModelFactory.class);
55 BeanUtils.populateWithoutFail(model, props, false);
56 if (model.getName() == null)
57 {
58 model.setName(DEFAULT_MODEL_NAME);
59 }
60 return model;
61 }
62 catch (Exception e)
63 {
64 throw new ModelServiceNotFoundException(location + "/" + type, e);
65 }
66 }
67 else
68 {
69 throw new ModelServiceNotFoundException(location + "/" + type);
70 }
71 }
72 catch (IOException e)
73 {
74 throw new ModelServiceNotFoundException(location + "/" + type, e);
75 }
76 }
77 }