KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > util > PortNumber


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: PortNumber.java,v 1.2 2005/04/07 15:07:08 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.rmi.util;
26
27 import org.objectweb.carol.util.configuration.TraceCarol;
28
29
30 /**
31  * Convert String port number to int and do some check
32  * @author Florent Benoit
33  * */

34 public class PortNumber {
35
36
37     /**
38      * Utility class, no constructor
39      */

40     private PortNumber() {
41
42     }
43
44     /**
45      * Return port number based on given string
46      * @param portStr port number in stringified format
47      * @param propertyName name of the property of the port number in carol.properties file (for error messages)
48      * @return port number (or 0)
49      */

50     public static int strToint(String JavaDoc portStr, String JavaDoc propertyName) {
51             int port = 0;
52             try {
53                 port = Integer.parseInt(portStr);
54             } catch (NumberFormatException JavaDoc nfe) {
55                 TraceCarol.error("Invalid port number for property '" + propertyName + "'. Value set was '" + portStr + "'. It should be 0(random) or greater than 0. Error : " + nfe.getMessage());
56             }
57             if (port < 0) {
58                 String JavaDoc errMsg = "Invalid port number for property '" + propertyName + "'. It should be 0(random) or greater than 0.";
59                 TraceCarol.error(errMsg);
60                 throw new IllegalArgumentException JavaDoc(errMsg);
61             }
62             if (port == 0) {
63                 if (TraceCarol.isDebugJndiCarol()) {
64                     TraceCarol.debugCarol("Port was 0, will use a random port");
65                 }
66             }
67             return port;
68     }
69 }
70
Popular Tags