1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.wsrp4j.exception;
18  
19  import java.io.InputStream;
20  import java.util.Properties;
21  
22  import org.apache.wsrp4j.log.LogManager;
23  import org.apache.wsrp4j.log.Logger;
24  
25  /**
26   Holds all exception messages
27   */
28  public class Messages {
29  
30      /** defines the lowest message code for common messages */
31      public static final int COMMON_LOWER_BOUND = 1000;
32  
33      /** defines the highest message code for common messages */
34      public static final int COMMON_UPPER_BOUND = 1999;
35  
36      /** defines the lowest message code for producer messages */
37      public static final int PRODUCER_LOWER_BOUND = 2000;
38  
39      /** defines the highest message code for producer messages */
40      public static final int PRODUCER_UPPER_BOUND = 2999;
41  
42      /** defines the lowest message code for provider messages */
43      public static final int PROVIDER_LOWER_BOUND = 3000;
44  
45      /** defines the highest message code for provider messages */
46      public static final int PROVIDER_UPPER_BOUND = 3999;
47  
48      /** defines the lowest message code for consumer messages */
49      public static final int CONSUMER_LOWER_BOUND = 6000;
50  
51      /** defines the highest message code for consumer messages */
52      public static final int CONSUMER_UPPER_BOUND = 6999;
53  
54      private static final String FILE_MSG_PROPERTIES = "wsrp-messages.properties";
55  
56      private static final String MSG_EXCEPTION_ON_LOAD = "Error while loading messages from "
57              + FILE_MSG_PROPERTIES + ".";
58  
59      private static final String MSG_NO_MSG_FOUND = "No message found.";
60  
61      private static final String MSG_NO_MSG_FOUND_FOR = "No message found for ";
62  
63      private static final String METHOD_INIT = "<init>";
64  
65      private static final String METHOD_GET = "get()";
66  
67      private static Logger logger = LogManager.getLogManager().getLogger(
68              Messages.class);
69  
70      private static Properties msgMap = new Properties();
71  
72      /**
73       Private constructor loads messages from <code>messages.properties</code> file in
74       <code>org.apache.wsrp4j.exception</code>
75       */
76      private Messages() {
77  
78          //load properties file
79          try {
80              InputStream in = getClass().getClassLoader().getResourceAsStream(
81                      FILE_MSG_PROPERTIES);
82              msgMap.load(in);
83          }
84          catch (Exception e) {
85              logger.text(Logger.ERROR, METHOD_INIT, e, MSG_EXCEPTION_ON_LOAD);
86          }
87      }
88  
89      /**
90       Returns an error message for a message code
91       @param msgCode code that identifies a message
92       @return String representing a message
93       */
94      public static String get(int msgCode) {
95          String msg = (String) msgMap.get(new Integer(msgCode).toString());
96          if (msg == null) {
97              msg = MSG_NO_MSG_FOUND;
98              if (logger.isLogging(Logger.TRACE_LOW)) {
99                  logger.text(Logger.TRACE_LOW, METHOD_GET, MSG_NO_MSG_FOUND_FOR
100                         + msgCode + " in " + FILE_MSG_PROPERTIES + ".");
101             }
102         }
103         return msg;
104     }
105 }