001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.config.entities;
006:
007: import com.opensymphony.util.TextUtils;
008: import com.opensymphony.xwork.config.ExternalReferenceResolver;
009: import com.opensymphony.xwork.util.location.Located;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.util.*;
014: import java.io.Serializable;
015:
016: /**
017: * Configuration for Package.
018: * <p/>
019: * In the xml configuration file this is defined as the <code>package</code> tag.
020: *
021: * @author Rainer Hermanns
022: * @version $Revision: 1515 $
023: */
024: public class PackageConfig extends Located implements Comparable,
025: Serializable {
026:
027: private static final Log LOG = LogFactory
028: .getLog(PackageConfig.class);
029:
030: private ExternalReferenceResolver externalRefResolver = null;
031: private Map actionConfigs = new LinkedHashMap();
032: private Map globalResultConfigs = new LinkedHashMap();
033: private Map interceptorConfigs = new LinkedHashMap();
034: private Map resultTypeConfigs = new LinkedHashMap();
035: private List globalExceptionMappingConfigs = new ArrayList();
036: private Set parents = new HashSet();
037: private String defaultInterceptorRef;
038: private String defaultActionRef;
039: private String defaultResultType;
040: private String name;
041: private String namespace = "";
042: private boolean isAbstract = false;
043:
044: public PackageConfig() {
045: }
046:
047: public PackageConfig(String name) {
048: this .name = name;
049: }
050:
051: public PackageConfig(String name, String namespace,
052: boolean isAbstract,
053: ExternalReferenceResolver externalRefResolver) {
054: this (name);
055: this .namespace = TextUtils.noNull(namespace);
056: this .isAbstract = isAbstract;
057: this .externalRefResolver = externalRefResolver;
058: }
059:
060: public PackageConfig(String name, String namespace,
061: boolean isAbstract,
062: ExternalReferenceResolver externalRefResolver, List parents) {
063: this (name, namespace, isAbstract, externalRefResolver);
064:
065: for (Iterator iterator = parents.iterator(); iterator.hasNext();) {
066: PackageConfig parent = (PackageConfig) iterator.next();
067: addParent(parent);
068: }
069: }
070:
071: public void setAbstract(boolean isAbstract) {
072: this .isAbstract = isAbstract;
073: }
074:
075: public boolean isAbstract() {
076: return isAbstract;
077: }
078:
079: public Map getActionConfigs() {
080: return actionConfigs;
081: }
082:
083: /**
084: * returns the Map of all the ActionConfigs available in the current package.
085: * ActionConfigs defined in ancestor packages will be included in this Map.
086: *
087: * @return a Map of ActionConfig Objects with the action name as the key
088: * @see ActionConfig
089: */
090: public Map getAllActionConfigs() {
091: Map retMap = new LinkedHashMap();
092:
093: if (!parents.isEmpty()) {
094: for (Iterator iterator = parents.iterator(); iterator
095: .hasNext();) {
096: PackageConfig parentContext = (PackageConfig) iterator
097: .next();
098: retMap.putAll(parentContext.getAllActionConfigs());
099: }
100: }
101:
102: retMap.putAll(getActionConfigs());
103:
104: return retMap;
105: }
106:
107: /**
108: * returns the Map of all the global ResultConfigs available in the current package.
109: * Global ResultConfigs defined in ancestor packages will be included in this Map.
110: *
111: * @return a Map of Result Objects with the result name as the key
112: * @see ResultConfig
113: */
114: public Map getAllGlobalResults() {
115: Map retMap = new LinkedHashMap();
116:
117: if (!parents.isEmpty()) {
118: for (Iterator iterator = parents.iterator(); iterator
119: .hasNext();) {
120: PackageConfig parentContext = (PackageConfig) iterator
121: .next();
122: retMap.putAll(parentContext.getAllGlobalResults());
123: }
124: }
125:
126: retMap.putAll(getGlobalResultConfigs());
127:
128: return retMap;
129: }
130:
131: /**
132: * returns the Map of all InterceptorConfigs and InterceptorStackConfigs available in the current package.
133: * InterceptorConfigs defined in ancestor packages will be included in this Map.
134: *
135: * @return a Map of InterceptorConfig and InterceptorStackConfig Objects with the ref-name as the key
136: * @see InterceptorConfig
137: * @see InterceptorStackConfig
138: */
139: public Map getAllInterceptorConfigs() {
140: Map retMap = new LinkedHashMap();
141:
142: if (!parents.isEmpty()) {
143: for (Iterator iterator = parents.iterator(); iterator
144: .hasNext();) {
145: PackageConfig parentContext = (PackageConfig) iterator
146: .next();
147: retMap.putAll(parentContext.getAllInterceptorConfigs());
148: }
149: }
150:
151: retMap.putAll(getInterceptorConfigs());
152:
153: return retMap;
154: }
155:
156: /**
157: * returns the Map of all the ResultTypeConfigs available in the current package.
158: * ResultTypeConfigs defined in ancestor packages will be included in this Map.
159: *
160: * @return a Map of ResultTypeConfig Objects with the result type name as the key
161: * @see ResultTypeConfig
162: */
163: public Map getAllResultTypeConfigs() {
164: Map retMap = new LinkedHashMap();
165:
166: if (!parents.isEmpty()) {
167: for (Iterator iterator = parents.iterator(); iterator
168: .hasNext();) {
169: PackageConfig parentContext = (PackageConfig) iterator
170: .next();
171: retMap.putAll(parentContext.getAllResultTypeConfigs());
172: }
173: }
174:
175: retMap.putAll(getResultTypeConfigs());
176:
177: return retMap;
178: }
179:
180: /**
181: * returns the List of all the ExceptionMappingConfigs available in the current package.
182: * ExceptionMappingConfigs defined in ancestor packages will be included in this list.
183: *
184: * @return a List of ExceptionMappingConfigs Objects with the result type name as the key
185: * @see ExceptionMappingConfig
186: */
187: public List getAllExceptionMappingConfigs() {
188: List allExceptionMappings = new ArrayList();
189:
190: if (!parents.isEmpty()) {
191: for (Iterator iterator = parents.iterator(); iterator
192: .hasNext();) {
193: PackageConfig parentContext = (PackageConfig) iterator
194: .next();
195: allExceptionMappings.addAll(parentContext
196: .getAllExceptionMappingConfigs());
197: }
198: }
199:
200: allExceptionMappings.addAll(getGlobalExceptionMappingConfigs());
201:
202: return allExceptionMappings;
203: }
204:
205: public void setDefaultInterceptorRef(String name) {
206: defaultInterceptorRef = name;
207: }
208:
209: public String getDefaultInterceptorRef() {
210: return defaultInterceptorRef;
211: }
212:
213: public void setDefaultActionRef(String name) {
214: defaultActionRef = name;
215: }
216:
217: public String getDefaultActionRef() {
218: return defaultActionRef;
219: }
220:
221: /**
222: * sets the default Result type for this package
223: *
224: * @param defaultResultType
225: */
226: public void setDefaultResultType(String defaultResultType) {
227: this .defaultResultType = defaultResultType;
228: }
229:
230: /**
231: * Returns the default result type for this package.
232: */
233: public String getDefaultResultType() {
234: return defaultResultType;
235: }
236:
237: /**
238: * @param externalRefResolver The externalRefResolver to set.
239: */
240: public void setExternalRefResolver(
241: ExternalReferenceResolver externalRefResolver) {
242: this .externalRefResolver = externalRefResolver;
243: }
244:
245: /**
246: * Gets the Reference resolver for this package. If the resolver for this package is
247: * not specified, the method will try and find one on one of the parent packages
248: *
249: * @return Returns the externalRefResolver.
250: */
251: public ExternalReferenceResolver getExternalRefResolver() {
252: //If this resolver is null, lets look to see if our parents have one
253: if (externalRefResolver == null) {
254: PackageConfig packageConfig;
255:
256: for (Iterator iter = getParents().iterator(); iter
257: .hasNext();) {
258: packageConfig = (PackageConfig) iter.next();
259:
260: if (packageConfig.getExternalRefResolver() != null) {
261: externalRefResolver = packageConfig
262: .getExternalRefResolver();
263:
264: break;
265: }
266: }
267: }
268:
269: return externalRefResolver;
270: }
271:
272: /**
273: * gets the default interceptor-ref name. If this is not set on this PackageConfig, it searches the parent
274: * PackageConfigs in order until it finds one.
275: */
276: public String getFullDefaultInterceptorRef() {
277: if ((defaultInterceptorRef == null) && !parents.isEmpty()) {
278: for (Iterator iterator = parents.iterator(); iterator
279: .hasNext();) {
280: PackageConfig parent = (PackageConfig) iterator.next();
281: String parentDefault = parent
282: .getFullDefaultInterceptorRef();
283:
284: if (parentDefault != null) {
285: return parentDefault;
286: }
287: }
288: }
289:
290: return defaultInterceptorRef;
291: }
292:
293: /**
294: * gets the default action-ref name. If this is not set on this PackageConfig, it searches the parent
295: * PackageConfigs in order until it finds one.
296: */
297: public String getFullDefaultActionRef() {
298: if ((defaultActionRef == null) && !parents.isEmpty()) {
299: for (Iterator iterator = parents.iterator(); iterator
300: .hasNext();) {
301: PackageConfig parent = (PackageConfig) iterator.next();
302: String parentDefault = parent.getFullDefaultActionRef();
303:
304: if (parentDefault != null) {
305: return parentDefault;
306: }
307: }
308: }
309: return defaultActionRef;
310: }
311:
312: /**
313: * Returns the default result type for this package.
314: * <p/>
315: * If there is no default result type, but this package has parents - we will try to
316: * look up the default result type of a parent.
317: */
318: public String getFullDefaultResultType() {
319: if ((defaultResultType == null) && !parents.isEmpty()) {
320: for (Iterator iterator = parents.iterator(); iterator
321: .hasNext();) {
322: PackageConfig parent = (PackageConfig) iterator.next();
323: String parentDefault = parent
324: .getFullDefaultResultType();
325:
326: if (parentDefault != null) {
327: return parentDefault;
328: }
329: }
330: }
331:
332: return defaultResultType;
333: }
334:
335: /**
336: * gets the global ResultConfigs local to this package
337: *
338: * @return a Map of ResultConfig objects keyed by result name
339: * @see ResultConfig
340: */
341: public Map getGlobalResultConfigs() {
342: return globalResultConfigs;
343: }
344:
345: /**
346: * gets the InterceptorConfigs and InterceptorStackConfigs local to this package
347: *
348: * @return a Map of InterceptorConfig and InterceptorStackConfig objects keyed by ref-name
349: * @see InterceptorConfig
350: * @see InterceptorStackConfig
351: */
352: public Map getInterceptorConfigs() {
353: return interceptorConfigs;
354: }
355:
356: public void setName(String name) {
357: this .name = name;
358: }
359:
360: public String getName() {
361: return name;
362: }
363:
364: public void setNamespace(String namespace) {
365: if (namespace == null) {
366: this .namespace = "";
367: } else {
368: this .namespace = namespace;
369: }
370: }
371:
372: public String getNamespace() {
373: return namespace;
374: }
375:
376: public List getParents() {
377: return new ArrayList(parents);
378: }
379:
380: /**
381: * gets the ResultTypeConfigs local to this package
382: *
383: * @return a Map of ResultTypeConfig objects keyed by result name
384: * @see ResultTypeConfig
385: */
386: public Map getResultTypeConfigs() {
387: return resultTypeConfigs;
388: }
389:
390: /**
391: * gets the ExceptionMappingConfigs local to this package
392: *
393: * @return a Map of ExceptionMappingConfig objects keyed by result name
394: * @see ExceptionMappingConfig
395: */
396: public List getGlobalExceptionMappingConfigs() {
397: return globalExceptionMappingConfigs;
398: }
399:
400: public void addActionConfig(String name, ActionConfig action) {
401: actionConfigs.put(name, action);
402: }
403:
404: public void addAllParents(List parents) {
405: for (Iterator iterator = parents.iterator(); iterator.hasNext();) {
406: PackageConfig config = (PackageConfig) iterator.next();
407: addParent(config);
408: }
409: }
410:
411: public void addGlobalResultConfig(ResultConfig resultConfig) {
412: globalResultConfigs.put(resultConfig.getName(), resultConfig);
413: }
414:
415: public void addGlobalResultConfigs(Map resultConfigs) {
416: globalResultConfigs.putAll(resultConfigs);
417: }
418:
419: public void addExceptionMappingConfig(
420: ExceptionMappingConfig exceptionMappingConfig) {
421: globalExceptionMappingConfigs.add(exceptionMappingConfig);
422: }
423:
424: public void addGlobalExceptionMappingConfigs(
425: List exceptionMappingConfigs) {
426: globalExceptionMappingConfigs.addAll(exceptionMappingConfigs);
427: }
428:
429: public void addInterceptorConfig(InterceptorConfig config) {
430: interceptorConfigs.put(config.getName(), config);
431: }
432:
433: public void addInterceptorStackConfig(InterceptorStackConfig config) {
434: interceptorConfigs.put(config.getName(), config);
435: }
436:
437: public void addParent(PackageConfig parent) {
438: if (this .equals(parent)) {
439: LOG.error("A package cannot extend itself: " + name);
440: }
441:
442: parents.add(parent);
443: }
444:
445: public void addResultTypeConfig(ResultTypeConfig config) {
446: resultTypeConfigs.put(config.getName(), config);
447: }
448:
449: public boolean equals(Object o) {
450: if (this == o) {
451: return true;
452: }
453:
454: if (!(o instanceof PackageConfig)) {
455: return false;
456: }
457:
458: final PackageConfig packageConfig = (PackageConfig) o;
459:
460: if (isAbstract != packageConfig.isAbstract) {
461: return false;
462: }
463:
464: if ((actionConfigs != null) ? (!actionConfigs
465: .equals(packageConfig.actionConfigs))
466: : (packageConfig.actionConfigs != null)) {
467: return false;
468: }
469:
470: if ((defaultResultType != null) ? (!defaultResultType
471: .equals(packageConfig.defaultResultType))
472: : (packageConfig.defaultResultType != null)) {
473: return false;
474: }
475:
476: if ((globalResultConfigs != null) ? (!globalResultConfigs
477: .equals(packageConfig.globalResultConfigs))
478: : (packageConfig.globalResultConfigs != null)) {
479: return false;
480: }
481:
482: if ((interceptorConfigs != null) ? (!interceptorConfigs
483: .equals(packageConfig.interceptorConfigs))
484: : (packageConfig.interceptorConfigs != null)) {
485: return false;
486: }
487:
488: if ((name != null) ? (!name.equals(packageConfig.name))
489: : (packageConfig.name != null)) {
490: return false;
491: }
492:
493: if ((namespace != null) ? (!namespace
494: .equals(packageConfig.namespace))
495: : (packageConfig.namespace != null)) {
496: return false;
497: }
498:
499: if ((parents != null) ? (!parents.equals(packageConfig.parents))
500: : (packageConfig.parents != null)) {
501: return false;
502: }
503:
504: if ((resultTypeConfigs != null) ? (!resultTypeConfigs
505: .equals(packageConfig.resultTypeConfigs))
506: : (packageConfig.resultTypeConfigs != null)) {
507: return false;
508: }
509:
510: if ((globalExceptionMappingConfigs != null) ? (!globalExceptionMappingConfigs
511: .equals(packageConfig.globalExceptionMappingConfigs))
512: : (packageConfig.globalExceptionMappingConfigs != null)) {
513: return false;
514: }
515:
516: return true;
517: }
518:
519: public int hashCode() {
520: int result;
521: result = ((name != null) ? name.hashCode() : 0);
522: result = (29 * result)
523: + ((parents != null) ? parents.hashCode() : 0);
524: result = (29 * result)
525: + ((actionConfigs != null) ? actionConfigs.hashCode()
526: : 0);
527: result = (29 * result)
528: + ((globalResultConfigs != null) ? globalResultConfigs
529: .hashCode() : 0);
530: result = (29 * result)
531: + ((interceptorConfigs != null) ? interceptorConfigs
532: .hashCode() : 0);
533: result = (29 * result)
534: + ((resultTypeConfigs != null) ? resultTypeConfigs
535: .hashCode() : 0);
536: result = (29 * result)
537: + ((globalExceptionMappingConfigs != null) ? globalExceptionMappingConfigs
538: .hashCode()
539: : 0);
540: result = (29 * result)
541: + ((defaultResultType != null) ? defaultResultType
542: .hashCode() : 0);
543: result = (29 * result)
544: + ((namespace != null) ? namespace.hashCode() : 0);
545: result = (29 * result) + (isAbstract ? 1 : 0);
546:
547: return result;
548: }
549:
550: public void removeParent(PackageConfig parent) {
551: parents.remove(parent);
552: }
553:
554: public String toString() {
555: return "{PackageConfig Name:" + name + " namespace:"
556: + namespace + " abstract:" + isAbstract + " parents:"
557: + parents + "}";
558: }
559:
560: public int compareTo(Object o) {
561: PackageConfig other = (PackageConfig) o;
562: String full = namespace + "!" + name;
563: String otherFull = other.namespace + "!" + other.name;
564:
565: // note, this isn't perfect (could come from different parents), but it is "good enough"
566: return full.compareTo(otherFull);
567: }
568:
569: }
|