View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.crossx.broker;
12  
13  import java.io.OutputStream;
14  import java.io.PrintWriter;
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.log4j.Logger;
22  import org.eclipse.emf.common.util.URI;
23  import org.eclipse.emf.ecore.EObject;
24  import org.mod4j.crossx.mm.crossx.ModelInfo;
25  import org.mod4j.crossx.mm.crossx.Symbol;
26  
27  /**
28   * This class is a singleton with static members only.
29   * It keeps all CrossX information for DSL models
30   * 
31   * @author Jos Warmer
32   * 
33   */
34  public class CrossxEnvironment {
35  
36      private static final Logger logger = Logger.getLogger(CrossxEnvironment.class.getName());
37  
38      static private boolean started = false;
39  
40      /**
41       * Private constructor, this class has static nethods only.
42       */
43      private CrossxEnvironment() {
44  
45      }
46  
47      /**
48       * Has the environment been started yet?
49       * 
50       * @return
51       */
52      public static boolean isStarted() {
53          return started;
54      }
55  
56      public static void setStarted(boolean started) {
57          CrossxEnvironment.started = started;
58      }
59  
60      /**
61       * A CrossxEnvoironment consists of a number of CrossxLocations, kept in the variable.
62       */
63      static private Map<String, CrossxLocation> environment = new HashMap<String, CrossxLocation>();
64  
65      /**
66       * A collection of all locations in this environment.
67       * This is intended to be used for e.g. displaying a tree of all CrossX information.
68       * @return
69       */
70      public static Map<String, CrossxLocation> getAll() {
71          return Collections.unmodifiableMap(environment);
72      }
73  
74      /**
75       * Add 'modelinfo' to the location named 'location' in the current environment
76       * 
77       * @param location
78       *            the name of the location to add to
79       * @param modelinfo
80       *            the ModelInfo to add
81       */
82      public static void addModelInfo(String location, ModelInfo modelinfo) {
83  //        print("CrossxEnvironment::AddModelInfo [" + location + "] [" + modelinfo.getModelname() + "]");
84          CrossxLocation atLocation = findLocation(location);
85          atLocation.addModelInfo(modelinfo);
86      }
87  
88      /**
89       * Remove'modelinfo' to the location named 'location' in the current environment
90       * 
91       * @param location
92       *            the name of the location to add to
93       * @param modelinfo
94       *            the ModelInfo to add
95       */
96      public static void removeModelInfo(String location, String resource) {
97          print("CrossxEnvironment::RemoveModelInfo [" + location + "] [" + resource + "]");
98          CrossxLocation atLocation = findLocation(location);
99          atLocation.removeModelInfo(resource);
100     }
101 
102     /**
103      * Find the CrossxLocation named 'location' or create a new one if it does not exist.
104      * 
105      * @param location
106      * @return
107      */
108     private static CrossxLocation findLocation(String location) {
109         CrossxLocation result = environment.get(location);
110         if (result == null) {
111             result = new CrossxLocation(location);
112             if (pr != null) {
113                 result.setPrintWriter(pr);
114             }
115             environment.put(location, result);
116         }
117         return result;
118     }
119 
120     /**
121      * Lookup a symbol in a model of a certain type
122      * 
123      * @param model
124      *            The model in which to look for the symbol
125      * @param symbolname
126      *            The name of the symbol to find
127      * @param elemType
128      *            The type of the symbol to find
129      * @return the symbol found, or null if no such symbol could be found
130      */
131     static public Symbol lookupSymbol(String model, String symbolname, String elemType) {
132         if (environment == null) {
133             System.err.println("CrossxEnvironment::find environment = null");
134         }
135         if (environment.isEmpty() ) {
136             System.err.println("CrossxEnvironment::find environment isEmpty()");
137         }
138         for (CrossxLocation location : environment.values()) {
139             Symbol result = location.lookup(model, symbolname, elemType);
140             if (result != null) {
141                 return result;
142             }
143         }
144         return null;
145     }
146 
147     /**
148      * Find all symbols of type 'elemType'.
149      * 
150      * @param elemType
151      * @return The list of names (String) of all found symbols. If there is no such symbol, an empty list.
152      */
153     static public List<Symbol> findAllSymbols(String projectLocation, String elemType) {
154         CrossxLocation loc = environment.get(projectLocation);
155         if (loc != null) {
156             return loc.findAllSymbols(elemType);
157         } else {
158             return new ArrayList<Symbol>();
159         }
160     }
161 
162     /**
163      * Find all symbols of type 'elemType' in model named 'modelname' in all locations.
164      * 
165      * @param elemType
166      * @return The list of names (String) of all found symbols. If there is no such symbol, an empty list.
167      */
168     static public List<Symbol> findAllFromModel(String modelname, String elemType) {
169         logger.info("CrossxEnvironment::findAllFromModel [" + modelname + "::" + elemType + "]");
170         List<Symbol> result = new ArrayList<Symbol>();
171         for (CrossxLocation location : environment.values()) {
172             result.addAll(location.findAllFromModel(modelname, elemType));
173         }
174         logger.info("CrossxEnvironment::findAllFromModel result [" + result + "]");
175         return result;
176     }
177 
178     /**
179      * Find all symbols of type 'elemType' in model named 'modelname' in project named 'project'.
180      * 
181      * @param project
182      * @param modelname
183      * @param elemType
184      * @return
185      */
186     static public List<Symbol> findAllSymbolsFromModelInProject(String project, String modelname, String elemType) {
187         logger.info("CrossxEnvironment::findAllSymbolsFromModelInProject [" + modelname + "]");
188         List<Symbol> result = new ArrayList<Symbol>();
189         for (CrossxLocation location : environment.values()) {
190             if (location.getName().equals(project)) {
191                 result.addAll(location.findAllSymbolsFromModel(modelname, elemType));
192             }
193         }
194         return result;
195     }
196 
197     /**
198      * Find all models in the current environment.
199      * 
200      * @return a list of names of all of the models
201      */
202     public static List<ModelInfo> findAllModels() {
203         List<ModelInfo> result = new ArrayList<ModelInfo>();
204         for (CrossxLocation location : environment.values()) {
205             for (ModelInfo info : location.getAll()) {
206                 logger.info("FindAll Models model [" + info.getModelname() + "]");
207                 result.add(info);
208             }
209         }
210         return result;
211     }
212 
213     /**
214      * Find all models in project named 'project'
215      * 
216      * @param project
217      * @return a list of names of the models
218      */
219     public static List<ModelInfo> findAllModelsInProject(String project) {
220         List<ModelInfo> result = new ArrayList<ModelInfo>();
221         for (CrossxLocation location : environment.values()) {
222             if (location.getName().equals(project)) {
223                 for (ModelInfo info : location.getAll()) {
224                     logger.info("FindAll Models model [" + info.getModelname() + "]");
225                     result.add(info);
226                 }
227             }
228         }
229         return result;
230     }
231 
232     /**
233      * Find all symbols of type 'elemType' in all models in the environment
234      * 
235      * @param symbolType
236      * @return a list of names of all symbols found
237      */
238     static public List<Symbol> findAllEverywhere(String symbolType) {
239         List<Symbol> result = new ArrayList<Symbol>();
240         for (CrossxLocation location : environment.values()) {
241             result.addAll(location.findAll(symbolType));
242         }
243         return result;
244     }
245 
246     static private PrintWriter pr = null;
247 
248     /**
249      * sets the error stream to 's;'
250      * 
251      * @param s
252      */
253     static public void setPrintStream(OutputStream s) {
254         pr = new PrintWriter(s);
255         for (CrossxLocation loc : environment.values()) {
256             loc.setPrintWriter(pr);
257         }
258     }
259 
260     /**
261      * Print to the error output or the given printstream
262      * 
263      * @param text
264      */
265     private static void print(String text) {
266         if (pr == null) {
267             System.err.println(text);
268         } else {
269             pr.println(text);
270             pr.flush();
271         }
272     }
273 }