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      /**
60       * Basefault error code for a {@link ValidationException}.
61       */
62      private static final int ERROR_CODE = 1000;
63  
64      /**
65       * Default language identifier in base fault.
66       */
67      public static final String LOCAL_EN = "en";
68  
69      /**
70       * Definition of main error message.
71       */
72      private static final String DESCRIPTION_MESSAGE = "{0}: {1}";
73  
74      /**
75       * Definition of cause error message.
76       */
77      private static final String CAUSE_MESSAGE = "Caused by {0}: {1}";
78  
79      /**
80       * Definition of stack trace error message.
81       */
82      private static final String STACKTRACE_MESSAGE = "\tat {0}";
83  
84      /**
85       * exception timestamp
86       */
87      private Calendar timestamp = new GregorianCalendar();
88  
89      private static final long serialVersionUID = 1L;
90  
91      /**
92       * default constructor
93       */
94      public AgreementFactoryException()
95      {
96          super();
97      }
98  
99      /**
100      * @param message
101      *            error message
102      */
103     public AgreementFactoryException( String message )
104     {
105         super( message );
106         // setErrorCode();
107     }
108 
109     /**
110      * Constructs an exception with the given message using a {@link ContinuingFaultType} as a cause.
111      * 
112      * @param message
113      *            the fault message
114      * @param fault
115      *            the root cause of the exception
116      */
117     public AgreementFactoryException( String message, ContinuingFaultType fault )
118     {
119         super( message );
120         FaultCause cause = fault.getFaultCause();
121         if ( cause != null )
122         {
123             XmlObject[] causeDoc =
124                 cause.selectChildren( ContinuingFaultDocument.type.getDocumentElementName() );
125 
126             if ( causeDoc.length > 0 )
127             {
128                 ContinuingFaultType cf = (ContinuingFaultType) causeDoc[0];
129                 AgreementFactoryException rootEx =
130                     new AgreementFactoryException( cf.getDescriptionArray( 0 ).getStringValue(), cf );
131                 initCause( rootEx );
132             }
133         }
134     }
135 
136     /**
137      * @param message
138      *            the error message
139      * @param cause
140      *            the exception cause
141      */
142     public AgreementFactoryException( String message, Throwable cause )
143     {
144         super( message, cause );
145     }
146 
147     /**
148      * @param cause
149      *            the exception cause
150      */
151     public AgreementFactoryException( Throwable cause )
152     {
153         super( cause );
154     }
155 
156     /**
157      * @return the base fault document representing this exception.
158      */
159     public BaseFaultType getBaseFault()
160     {
161         BaseFaultDocument baseFaultDocument = BaseFaultDocument.Factory.newInstance();
162         BaseFaultType baseFault = baseFaultDocument.addNewBaseFault();
163 
164         baseFault.addNewErrorCode();
165         baseFault.getErrorCode().setDialect( WsagConstants.WSAG4J_NAMESPACE );
166         baseFault.getErrorCode().set( XmlString.Factory.newValue( getErrorCode() ) );
167 
168         baseFault.setTimestamp( timestamp );
169 
170         Description description = baseFault.addNewDescription();
171         description.setLang( LOCAL_EN );
172         description.setStringValue( MessageFormat.format( DESCRIPTION_MESSAGE, new Object[] {
173             this.getClass().getName(), getMessage() } ) );
174 
175         StackTraceElement[] stackTrace = getStackTrace();
176         for ( int i = 0; i < stackTrace.length; i++ )
177         {
178             description = baseFault.addNewDescription();
179             description.setLang( LOCAL_EN );
180             description.setStringValue( MessageFormat.format( STACKTRACE_MESSAGE,
181                                                               new Object[] { stackTrace[i] } ) );
182         }
183 
184         if ( getCause() != null )
185         {
186             description = baseFault.addNewDescription();
187             description.setLang( LOCAL_EN );
188             description.setStringValue( MessageFormat.format( CAUSE_MESSAGE, new Object[] {
189                 getCause().getClass().getName(), getCause().getMessage() } ) );
190         }
191 
192         return baseFault;
193     }
194 
195     /**
196      * Specific exceptions must override this method.
197      * 
198      * @return the base fault error code for this exception
199      */
200     protected String getErrorCode()
201     {
202         return Integer.toString( ERROR_CODE );
203     }
204 
205 }