KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > model > ModelFactory


1 /*
2  * $Id: ModelFactory.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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.ConnectorFactory;
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 JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.Properties JavaDoc;
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 class ModelFactory
28 {
29
30     public static final String JavaDoc MODEL_SERVICE_PATH = "org/mule/models";
31
32     public static UMOModel createModel(String JavaDoc type) throws ModelServiceNotFoundException
33     {
34         String JavaDoc location = SpiUtils.SERVICE_ROOT + MODEL_SERVICE_PATH;
35         InputStream JavaDoc is = SpiUtils.findServiceDescriptor(MODEL_SERVICE_PATH, type, ConnectorFactory.class);
36         try
37         {
38             if (is != null)
39             {
40                 Properties JavaDoc props = new Properties JavaDoc();
41                 props.load(is);
42                 String JavaDoc clazz = props.getProperty("model");
43                 try
44                 {
45                     UMOModel model = (UMOModel)ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS,
46                         ModelFactory.class);
47                     BeanUtils.populateWithoutFail(model, props, false);
48                     return model;
49                 }
50                 catch (Exception JavaDoc e)
51                 {
52                     throw new ModelServiceNotFoundException(location + "/" + type, e);
53                 }
54             }
55             else
56             {
57                 throw new ModelServiceNotFoundException(location + "/" + type);
58             }
59         }
60         catch (IOException JavaDoc e)
61         {
62             throw new ModelServiceNotFoundException(location + "/" + type, e);
63         }
64     }
65 }
66
Popular Tags