View Javadoc

1   package org.apache.torque.generator.outlet.java;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.torque.generator.GeneratorException;
25  import org.apache.torque.generator.control.ControllerState;
26  import org.apache.torque.generator.qname.Namespace;
27  import org.apache.torque.generator.qname.QualifiedName;
28  import org.apache.torque.generator.source.SourceElement;
29  import org.apache.torque.generator.source.SourcePath;
30  import org.apache.torque.generator.variable.Variable;
31  
32  /**
33   * Utility methods to retrieve information out of the source model or the
34   * configuration. The methods provide adequate logging and error handling.
35   */
36  public final class OutletUtils
37  {
38      /**
39       * Private constructor for utility class.
40       */
41      private OutletUtils()
42      {
43      }
44  
45      /** The logger for the class. */
46      private static Log log = LogFactory.getLog(OutletUtils.class);
47  
48      /**
49       * Retrieves the value of a source element attribute. The source element
50       * must be found and the attribute must be set, otherwise an exception is
51       * thrown.
52       *
53       * @param elementName The name of the source element relative to the
54       *        current source element; a dot (.) denotes the current element.
55       * @param attributeName The name of the attribute of the element.
56       * @param controllerState The controller state.
57       * @param clazz the class in which the attribute should be retrieved;
58       *        used only for logging.
59       *
60       * @return the value of the specified attribute, not null.
61       *
62       * @throws GeneratorException if the source element cannot be found
63       *         or the specified attribute is not set.
64       */
65      public static String getSourceElementAttribute(
66              String elementName,
67              String attributeName,
68              ControllerState controllerState,
69              Class<?> clazz)
70          throws GeneratorException
71      {
72          SourceElement sourceElement = SourcePath.getElement(
73                  controllerState.getSourceElement(),
74                  elementName,
75                  false);
76          Object attribute
77                  = sourceElement.getAttribute(attributeName);
78          if (attribute == null)
79          {
80              throw new GeneratorException(
81                      "Source element attribute not set in "
82                      + clazz.getName()
83                      + "\n"
84                      + "The attribute "
85                      + attributeName
86                      + " of the source element "
87                      + elementName
88                      + " is not set.");
89          }
90          return attribute.toString();
91      }
92  
93      /**
94       * Reads an option with a given name. The option must be set to a value
95       * different from null.
96       *
97       * @param optionName the name of the option to read, not null.
98       * @param controllerState the current state of the controller, not null.
99       * @param clazz the class from which this method is called, not null.
100      *        Only used for logging purposes.
101      *
102      * @return the value of the option.
103      *
104      * @throws GeneratorException if the option is not set or set to null.
105      */
106     public static String getOption(
107             String optionName,
108             ControllerState controllerState,
109             Class<?> clazz)
110         throws GeneratorException
111     {
112         Object optionValue = controllerState.getOption(optionName);
113         if (optionValue == null)
114         {
115             throw new GeneratorException("Invalid configuration of "
116                     + clazz.getName()
117                     + "\n"
118                     + "The option "
119                     + optionName
120                     + " is not set.");
121         }
122         return optionValue.toString();
123     }
124 
125     /**
126      * Retrieve a value from either a preset value, an option, a variable,
127      * or a source element attribute. Exactly one of these must be set to a
128      * value different from zero.
129      *
130      * @param presetValue the plain result, or null if the preset value should
131      *        not be used.
132      * @param optionName the name of the option to access, or null if
133      *        no option value should be returned.
134      * @param variableName the name of the variable to access, or null if
135      *        no variable should be accessed.
136      * @param sourceElementName the name of the source element relative to the
137      *        current element which attribute should be read. Null if no source
138      *        attribute value should be used.
139      * @param sourceElementAttribute the name of the attribute of the above
140      *        source element.
141      * @param controllerState the current state of the controller, not null.
142      * @param clazz the class from which this method is called, not null.
143      *        Used only for logging purposes.
144      * @param expectedFieldNames the field names in which the information
145      *        is expected; for logging purposes only.
146      *
147      * @return the desired value, not null.
148      *
149      * @throws GeneratorException if the value is not set or more than one
150      *         possibility to get the value exists.
151      */
152     public static String getFromDifferentPlaces(
153             String presetValue,
154             String optionName,
155             String variableName,
156             String sourceElementName,
157             String sourceElementAttribute,
158             ControllerState controllerState,
159             Class<?> clazz,
160             String expectedFieldNames)
161         throws GeneratorException
162     {
163         if (optionName != null
164                 && sourceElementName == null
165                 && presetValue == null
166                 && variableName == null)
167         {
168              return OutletUtils.getOption(
169                      optionName,
170                      controllerState,
171                      clazz);
172         }
173         else if (sourceElementName != null
174                 && optionName == null
175                 && presetValue == null
176                 && variableName == null)
177         {
178              return OutletUtils.getSourceElementAttribute(
179                      sourceElementName,
180                      sourceElementAttribute,
181                      controllerState,
182                      clazz);
183         }
184         else if (variableName != null
185                 && sourceElementName == null
186                 && optionName == null
187                 && presetValue == null)
188         {
189             Namespace namespace
190                     = controllerState.getOutlet().getName().getNamespace();
191             QualifiedName variableQualifiedName = new QualifiedName(
192                     variableName,
193                     namespace);
194             Variable variable
195                     = controllerState.getVariableStore().getInHierarchy(
196                             variableQualifiedName);
197             if (variable == null)
198             {
199                 log.info("clazz.getName() : Variable " + variableQualifiedName
200                         + " is not set, returning the empty String");
201                 return "";
202             }
203             if (variable.getValue() == null)
204             {
205                 log.info("clazz.getName() : Variable " + variableQualifiedName
206                         + " is set to null, returning the empty String");
207                 return "";
208             }
209             return variable.getValue().toString();
210         }
211         else if (presetValue != null
212                 && sourceElementName == null
213                 && optionName == null
214                 && variableName == null)
215         {
216             return presetValue;
217         }
218         else
219         {
220             throw new GeneratorException("Invalid configuration of "
221                     + clazz.getName()
222                     + "\n"
223                     + "Make sure that one and only one of "
224                     + expectedFieldNames
225                     + " are set.");
226         }
227     }
228 }