1 package org.ocltf.test;
2
3 import java.net.URL;
4
5 import org.apache.commons.lang.StringUtils;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.ocltf.common.ComponentContainer;
9 import org.ocltf.model.ModelFacade;
10 import org.ocltf.model.repository.Repository;
11 import org.ocltf.model.repository.RepositoryException;
12 import org.ocltf.utils.FileResourceUtils;
13
14 /***
15 * Models are required for OCL translation. This class
16 * loads a models so that translation tests may be performed
17 * during development of a translation-library.
18 */
19 public class ModelLoader {
20
21 private static final Log logger = LogFactory.getLog(ModelLoader.class);
22
23 private static ModelLoader loader;
24
25 private final String MODELFACADE = "modelFacade";
26 private final String REPOSITORY = "repository";
27
28 /***
29 * Specifies the location of the model xmi which to load
30 */
31 private final String MODEL_XMI = "model.xmi";
32
33 private ModelFacade model = null;
34
35 private Repository repository = null;
36
37 private void loadModel() {
38 String methodName = "loadModel";
39 try {
40 this.repository = getRepository();
41 this.repository.init();
42 URL modelUrl = getModelResource();
43 this.repository.loadModel(modelUrl);
44 } catch (Exception ex) {
45 String errMsg = "Error performing " + methodName;
46 logger.error(errMsg, ex);
47 throw new RepositoryException(errMsg, ex);
48 }
49 }
50
51 /***
52 * Retrieves the model resource location.
53 *
54 * @return URL the URI of the model resource.
55 */
56 private URL getModelResource() {
57 String methodName = "getModelResource";
58 String modelXmiProperty = System.getProperty(MODEL_XMI);
59 URL modelXmiResource = null;
60 try {
61 if(StringUtils.isNotEmpty(modelXmiProperty)) {
62 if(logger.isInfoEnabled()) {
63 logger.info("property '"
64 + MODEL_XMI
65 + "' set, finding model file --> '"
66 + modelXmiProperty + "'");
67 }
68
69 modelXmiResource = FileResourceUtils.getResource(modelXmiProperty);
70
71 if(modelXmiResource == null) {
72 modelXmiResource = new URL(modelXmiProperty);
73 }
74 if(logger.isInfoEnabled()) {
75 logger.info("using model file --> '" + modelXmiResource + "'");
76 }
77 } else {
78 throw new TranslationTestProcessorException(methodName
79 + " no property '"
80 + MODEL_XMI
81 + "' was defined, please define this to specify the location of your model");
82 }
83 } catch (Exception ex) {
84 String errMsg = "Error performing getModelResource";
85 logger.error(errMsg);
86 throw new TranslationTestProcessorException(errMsg, ex);
87 }
88 return modelXmiResource;
89 }
90
91 /***
92 * Returns the shared instance of this ModelLoader.
93 *
94 * @return ModelLoader the shared instance.
95 */
96 public static ModelLoader instance() {
97 if(loader == null) {
98 loader = new ModelLoader();
99 }
100 return loader;
101 }
102
103 /***
104 * Tests loading and retrieving the model from the repository.
105 *
106 * @return ModelFacade the facade of the loaded model
107 */
108 public ModelFacade getModel() {
109 if(this.model == null) {
110 try {
111 this.loadModel();
112 this.model = this.getModelFacade();
113 this.model.setModel(loader.repository.getModel());
114 } catch (RepositoryException ex) {
115 String errMsg = "Error performing getModel";
116 logger.error(errMsg, ex);
117 throw new RepositoryException(errMsg, ex);
118 }
119 }
120 return this.model;
121 }
122
123 /***
124 * Gets the ModelFacade implementation to use.
125 * Defaults to the implementation specified in the RespositoryConfiguration
126 * if one isn't specified
127 * @return ModelFacade the ModelFacade instance.
128 */
129 protected ModelFacade getModelFacade() {
130 return (ModelFacade)ComponentContainer.instance().findComponent(
131 System.getProperty(MODELFACADE), ModelFacade.class);
132 }
133
134 /***
135 * Gets the Repository implementation to use.
136 * Defaults to the implementation specified in the RespositoryConfiguration
137 * if one isn't specified
138 * @return Repository the Repository instance.
139 * @throws Exception
140 */
141 protected Repository getRepository() {
142 return (Repository)ComponentContainer.instance().findComponent(
143 System.getProperty(REPOSITORY), Repository.class);
144 }
145
146 }