View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.api.exceptions;
36  
37  import java.text.MessageFormat;
38  import java.util.Calendar;
39  import java.util.GregorianCalendar;
40  
41  import org.apache.xmlbeans.XmlObject;
42  import org.apache.xmlbeans.XmlString;
43  import org.oasisOpen.docs.wsrf.bf2.BaseFaultDocument;
44  import org.oasisOpen.docs.wsrf.bf2.BaseFaultType;
45  import org.oasisOpen.docs.wsrf.bf2.BaseFaultType.Description;
46  import org.oasisOpen.docs.wsrf.bf2.BaseFaultType.FaultCause;
47  import org.ogf.graap.wsag.api.WsagConstants;
48  import org.ogf.schemas.graap.wsAgreement.ContinuingFaultDocument;
49  import org.ogf.schemas.graap.wsAgreement.ContinuingFaultType;
50  
51  /**
52   * AgreementFactoryException
53   * 
54   * @author Oliver Waeldrich
55   */
56  public class AgreementFactoryException extends WSAgreementException
57  {
58      /**
59       * Default language identifier in base fault.
60       */
61      public static final String LOCAL_EN = "en";
62  
63      /**
64       * Definition of main error message.
65       */
66      private static final String DESCRIPTION_MESSAGE = "{0}: {1}";
67  
68      /**
69       * Definition of cause error message.
70       */
71      private static final String CAUSE_MESSAGE = "Caused by {0}: {1}";
72  
73      /**
74       * Definition of stack trace error message.
75       */
76      private static final String STACKTRACE_MESSAGE = "\tat {0}";
77  
78      /**
79       * exception timestamp
80       */
81      private final Calendar timestamp = new GregorianCalendar();
82  
83      private static final long serialVersionUID = 1L;
84  
85      /**
86       * default constructor
87       */
88      public AgreementFactoryException()
89      {
90          super();
91      }
92  
93      /**
94       * @param message
95       *            error message
96       */
97      public AgreementFactoryException( String message )
98      {
99          super( message );
100         // setErrorCode();
101     }
102 
103     /**
104      * Constructs an exception with the given message using a {@link ContinuingFaultType} as a cause.
105      * 
106      * @param message
107      *            the fault message
108      * @param fault
109      *            the root cause of the exception
110      */
111     public AgreementFactoryException( String message, ContinuingFaultType fault )
112     {
113         super( message );
114         FaultCause cause = fault.getFaultCause();
115         if ( cause != null )
116         {
117             XmlObject[] causeDoc =
118                 cause.selectChildren( ContinuingFaultDocument.type.getDocumentElementName() );
119 
120             if ( causeDoc.length > 0 )
121             {
122                 ContinuingFaultType cf = (ContinuingFaultType) causeDoc[0];
123                 AgreementFactoryException rootEx =
124                     new AgreementFactoryException( cf.getDescriptionArray( 0 ).getStringValue(), cf );
125                 initCause( rootEx );
126             }
127         }
128     }
129 
130     /**
131      * @param message
132      *            the error message
133      * @param cause
134      *            the exception cause
135      */
136     public AgreementFactoryException( String message, Throwable cause )
137     {
138         super( message, cause );
139     }
140 
141     /**
142      * @param cause
143      *            the exception cause
144      */
145     public AgreementFactoryException( Throwable cause )
146     {
147         super( cause );
148     }
149 
150     /**
151      * @return the base fault document representing this exception.
152      */
153     public BaseFaultType getBaseFault()
154     {
155         BaseFaultDocument baseFaultDocument = BaseFaultDocument.Factory.newInstance();
156         BaseFaultType baseFault = baseFaultDocument.addNewBaseFault();
157 
158         baseFault.addNewErrorCode();
159         baseFault.getErrorCode().setDialect( WsagConstants.WSAG4J_NAMESPACE );
160         baseFault.getErrorCode().set( XmlString.Factory.newValue( getErrorCode() ) );
161 
162         baseFault.setTimestamp( timestamp );
163 
164         Description description = baseFault.addNewDescription();
165         description.setLang( LOCAL_EN );
166         description.setStringValue( MessageFormat.format( DESCRIPTION_MESSAGE, new Object[] {
167             this.getClass().getName(), getMessage() } ) );
168 
169         StackTraceElement[] stackTrace = getStackTrace();
170         for ( int i = 0; i < stackTrace.length; i++ )
171         {
172             description = baseFault.addNewDescription();
173             description.setLang( LOCAL_EN );
174             description.setStringValue( MessageFormat.format( STACKTRACE_MESSAGE,
175                 new Object[] { stackTrace[i] } ) );
176         }
177 
178         if ( getCause() != null )
179         {
180             description = baseFault.addNewDescription();
181             description.setLang( LOCAL_EN );
182             description.setStringValue( MessageFormat.format( CAUSE_MESSAGE, new Object[] {
183                 getCause().getClass().getName(), getCause().getMessage() } ) );
184         }
185 
186         return baseFault;
187     }
188 
189     @Override
190     public int getErrorCode()
191     {
192         return AGREEMENT_FACTORY_ERROR;
193     }
194 }