View Javadoc

1   package org.andromda.cartridges.spring;
2   
3   import java.util.Collection;
4   import java.util.LinkedHashSet;
5   import java.util.List;
6   import java.util.ArrayList;
7   import java.util.Iterator;
8   import java.util.LinkedHashMap;
9   import java.util.Map;
10  
11  import org.andromda.cartridges.spring.metafacades.SpringService;
12  import org.andromda.metafacades.uml.Service;
13  import org.andromda.metafacades.uml.ModelElementFacade;
14  import org.apache.commons.collections.Closure;
15  import org.apache.commons.collections.CollectionUtils;
16  import org.apache.commons.collections.Predicate;
17  
18  
19  /***
20   * Contains utilities used within the Spring cartridge.
21   *
22   * @author Chad Brandon
23   * @author Joel Kozikowski
24   */
25  public class SpringUtils
26  {
27      /***
28       * Retrieves all roles from the given <code>services</code> collection.
29       *
30       * @param services the collection services.
31       * @return all roles from the collection.
32       */
33      public Collection getAllRoles(Collection services)
34      {
35          final Collection allRoles = new LinkedHashSet();
36          CollectionUtils.forAllDo(
37              services,
38              new Closure()
39              {
40                  public void execute(Object object)
41                  {
42                      if (object != null && Service.class.isAssignableFrom(object.getClass()))
43                      {
44                          allRoles.addAll(((Service)object).getAllRoles());
45                      }
46                  }
47              });
48          return allRoles;
49      }
50  
51      /***
52       * Indicates if any remote EJBs are present in the collection
53       * of services.
54       *
55       * @param services the collection of services to check.
56       * @return true/false.
57       */
58      public boolean remoteEjbsPresent(final Collection services)
59      {
60          boolean present = services != null && !services.isEmpty();
61          if (present)
62          {
63              present =
64                  CollectionUtils.find(
65                      services,
66                      new Predicate()
67                      {
68                          public boolean evaluate(final Object object)
69                          {
70                              boolean valid = false;
71                              if (object instanceof SpringService)
72                              {
73                                  final SpringService service = (SpringService)object;
74                                  valid = service.isEjbRemoteView();
75                              }
76                              return valid;
77                          }
78                      }) != null;
79          }
80          return present;
81      }
82  
83      /***
84       * Indicates if any local EJBs are present in the collection
85       * of services.
86       *
87       * @param services the collection of services to check.
88       * @return true/false.
89       */
90      public boolean localEjbsPresent(final Collection services)
91      {
92          boolean present = services != null && !services.isEmpty();
93          if (present)
94          {
95              present =
96                  CollectionUtils.find(
97                      services,
98                      new Predicate()
99                      {
100                         public boolean evaluate(final Object object)
101                         {
102                             boolean valid = false;
103                             if (object instanceof SpringService)
104                             {
105                                 final SpringService service = (SpringService)object;
106                                 valid = service.isEjbLocalView();
107                             }
108                             return valid;
109                         }
110                     }) != null;
111         }
112         return present;
113     }
114 
115     /***
116      * Indicates if any Spring remotable services are present.
117      *
118      * @param services the collection of services to check.
119      * @return true/false.
120      */
121     public boolean remotableServicesPresent(final Collection services)
122     {
123         boolean present = services != null && !services.isEmpty();
124         if (present)
125         {
126             present =
127                 CollectionUtils.find(
128                     services,
129                     new Predicate()
130                     {
131                         public boolean evaluate(final Object object)
132                         {
133                             boolean valid = false;
134                             if (object instanceof SpringService)
135                             {
136                                 final SpringService service = (SpringService)object;
137                                 valid = service.isRemotable();
138                             }
139                             return valid;
140                         }
141                     }) != null;
142         }
143         return present;
144     }
145 
146     /***
147      * Based on the given <code>value</code>, this method will return
148      * a formatted Spring property (including the handling of 'null').
149      *
150      * @param value the value from which to create the spring value.
151      * @return the spring value.
152      */
153     public String getSpringPropertyValue(final String value)
154     {
155         String propertyValue = "";
156         if (value != null)
157         {
158             if ("null".equalsIgnoreCase(value))
159             {
160                 propertyValue = "<null/>";
161             }
162             else
163             {
164                 propertyValue = "<value>" + value + "</value>";
165             }
166         }
167         return propertyValue;
168     }
169     
170     /***
171      * Removes generics from string. Currently used to strip generics 
172      * from ejb-jar.xml method parameters.
173      * 
174      * @param String containing generics
175      * @return String with generics stripped
176      */
177     public String removeGenerics(final String parameter)
178     {
179 		int position = parameter.indexOf("<");
180 		String result = parameter;
181 		if(position != -1)
182         {
183 			result = result.substring(0, position);
184         }
185 		return result;
186     }
187     
188     /***
189      * Are we generating code for a rich client?
190      */
191     private boolean richClient = false;
192 
193     
194     /***
195      * Sets if code is being generated for a rich client.
196      */
197     public void setRichClient(final boolean richClientProperty)
198     {
199         this.richClient = richClientProperty;
200     }
201     
202     /***
203      * Returns TRUE if code is being generated for a rich client environment 
204      */
205     public boolean isRichClient()
206     {
207         return this.richClient;
208     }
209  
210     /***
211      * Returns the class name part of a fully qualified name
212      * @param fullyQualifiedName
213      * @return just the "class name" part of the fully qualified name
214      */
215     public String getClassName(String fullyQualifiedName)
216     {
217        String className;
218        if (fullyQualifiedName != null && fullyQualifiedName.length() > 0)
219        {
220            int lastDot = fullyQualifiedName.lastIndexOf('.');
221            if (lastDot >= 0)
222                className = fullyQualifiedName.substring(lastDot+1);
223            else
224                className = fullyQualifiedName;
225        }
226        else
227           className = "";
228        
229        return className;
230     }
231 
232     
233     /***
234      * Returns the package name part of a fully qualified name
235      * @param fullyQualifiedName
236      * @return just the "package" part of the fully qualified name
237      */
238     public String getPackageName(String fullyQualifiedName)
239     {
240        String packageName;
241        if (fullyQualifiedName != null && fullyQualifiedName.length() > 0)
242        {
243            int lastDot = fullyQualifiedName.lastIndexOf('.');
244            if (lastDot >= 0)
245                packageName = fullyQualifiedName.substring(0, lastDot);
246            else
247                packageName = "";
248        }
249        else
250           packageName = "";
251        
252        return packageName/package-summary.html">strong> packageName;
253     }
254 
255     /***
256      * Returns an ordered set containing the argument model elements, model elements with a name that is already
257      * used by another model element in the argument collection will not be returned.
258      * The first operation with a name not already encountered will be returned, the order inferred by the
259      * argument's iterator will determine the order of the returned list.
260      *
261      * @param modelElements a collection of model elements, elements that are not model elements will be ignored
262      * @return the argument model elements without, elements with a duplicate name will only be recorded once
263      */
264     public List filterUniqueByName(Collection modelElements)
265     {
266         final Map filteredElements = new LinkedHashMap();
267 
268         for (final Iterator elementIterator = modelElements.iterator(); elementIterator.hasNext();)
269         {
270             final Object object = elementIterator.next();
271             if (object instanceof ModelElementFacade)
272             {
273                 final ModelElementFacade modelElement = (ModelElementFacade)object;
274                 if (!filteredElements.containsKey(modelElement.getName()))
275                 {
276                     filteredElements.put(modelElement.getName(), modelElement);
277                 }
278             }
279         }
280 
281         return new ArrayList(filteredElements.values());
282     }
283 }