View Javadoc
1   /*
2    * #%L
3    * ServerVersionException.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.error;
21  
22  import java.io.IOException;
23  
24  import com.allanbank.mongodb.MongoDbException;
25  import com.allanbank.mongodb.Version;
26  import com.allanbank.mongodb.client.Message;
27  import com.allanbank.mongodb.client.VersionRange;
28  
29  /**
30   * ServerVersionException is thrown to report that an attempt was made to send a
31   * request to a server that required a more recent version of the server.
32   * 
33   * @api.yes This class is part of the driver's API. Public and protected members
34   *          will be deprecated for at least 1 non-bugfix release (version
35   *          numbers are <major>.<minor>.<bugfix>) before being
36   *          removed or modified.
37   * @copyright 2013, Allanbank Consulting, Inc., All Rights Reserved
38   */
39  public class ServerVersionException extends MongoDbException {
40  
41      /** Serialization version for the class. */
42      private static final long serialVersionUID = -8577756570001826274L;
43  
44      /**
45       * Creates a suitable message for the exception.
46       * 
47       * @param operation
48       *            The name of the command/operation.
49       * @param range
50       *            The required server version range for the operation.
51       * @param actual
52       *            The actual version from the server.
53       * @return The message for the exception.
54       */
55      private static String message(final String operation,
56              final VersionRange range, final Version actual) {
57          if (Version.VERSION_0.equals(range.getLowerBounds())) {
58              return "Attempted to send the '" + operation
59                      + "' operation to a version " + actual
60                      + " server but the operation is only supported "
61                      + "before version " + range.getUpperBounds() + ".";
62          }
63          else if (Version.UNKNOWN.equals(range.getUpperBounds())) {
64              return "Attempted to send the '" + operation
65                      + "' operation to a version " + actual
66                      + " server but the operation is only supported "
67                      + "after version " + range.getLowerBounds() + ".";
68          }
69          else {
70              return "Attempted to send the '" + operation
71                      + "' operation to a version " + actual
72                      + " server but the operation is only supported "
73                      + "from version " + range.getLowerBounds() + " to "
74                      + range.getUpperBounds() + ".";
75          }
76      }
77  
78      /** The actual server version. */
79      private final Version myActualVersion;
80  
81      /** The operation's message. */
82      private transient Message myMessage;
83  
84      /** The name of the operation. */
85      private final String myOperation;
86  
87      /** The required server version for the operation. */
88      private final VersionRange myRange;
89  
90      /**
91       * Creates a new ServerVersionException.
92       * 
93       * @param operation
94       *            The name of the command/operation.
95       * @param range
96       *            The required server version range for the operation.
97       * @param actual
98       *            The actual version from the server.
99       * @param message
100      *            The operation's message.
101      */
102     public ServerVersionException(final String operation,
103             final VersionRange range, final Version actual,
104             final Message message) {
105         super(message(operation, range, actual));
106 
107         myOperation = operation;
108         myActualVersion = actual;
109         myRange = range;
110         myMessage = message;
111     }
112 
113     /**
114      * Returns the actual server version.
115      * 
116      * @return The actual server version.
117      */
118     public Version getActualVersion() {
119         return myActualVersion;
120     }
121 
122     /**
123      * Returns the maximum (exclusive) server version for the operation.
124      * 
125      * @return The required server version for the operation.
126      */
127     public Version getMaximumVersion() {
128         return myRange.getUpperBounds();
129     }
130 
131     /**
132      * Returns the name of the operation.
133      * 
134      * @return The name of the operation.
135      */
136     public String getOperation() {
137         return myOperation;
138     }
139 
140     /**
141      * Returns the required (inclusive) server version for the operation.
142      * 
143      * @return The required server version for the operation.
144      */
145     public Version getRequiredVersion() {
146         return myRange.getLowerBounds();
147     }
148 
149     /**
150      * Returns the operation's message.
151      * 
152      * @return The operation's message.
153      */
154     public Message getSentMessage() {
155         return myMessage;
156     }
157 
158     /**
159      * Reads the serialized configuration and sets the transient field to known
160      * values.
161      * 
162      * @param stream
163      *            The stream to read from.
164      * @throws IOException
165      *             On a failure reading from the stream.
166      * @throws ClassNotFoundException
167      *             On a failure locating a type in the stream.
168      */
169     private void readObject(final java.io.ObjectInputStream stream)
170             throws IOException, ClassNotFoundException {
171         stream.defaultReadObject();
172 
173         myMessage = null;
174     }
175 }