1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.connections.socket;
25 
26 /**
27  * Helper class for <code>socketAcceptor</code> and
28  * <code>socketConnector</code>.
29  *
30  * <p>FIXME:  Once those classes have been moved from <code>jurt</code> to
31  * <code>javaunohelper</code>, they should use
32  * <code>com.sun.star.lib.uno.helper.UnoUrl</code> either instead of this class
33  * or underneath this class.</p>
34  */
35 final class ConnectionDescriptor {
ConnectionDescriptor(String description)36     public ConnectionDescriptor(String description)
37         throws com.sun.star.lang.IllegalArgumentException {
38         for (int i = description.indexOf(','); i >= 0;) {
39             int j = description.indexOf(',', i + 1);
40             int k = j < 0 ? description.length() : j;
41             int l = description.indexOf('=', i + 1);
42             if (l < 0 || l >= k) {
43                 throw new com.sun.star.lang.IllegalArgumentException(
44                     "parameter lacks '='");
45             }
46             String key = description.substring(i + 1, l);
47             String value = description.substring(l + 1, k);
48             if (key.equalsIgnoreCase("host")) {
49                 host = value;
50             } else if (key.equalsIgnoreCase("port")) {
51                 try {
52                     port = Integer.valueOf(value).intValue();
53                 } catch (NumberFormatException e) {
54                     throw new com.sun.star.lang.IllegalArgumentException(
55                         e.toString());
56                 }
57                 if (port < 0 || port > 65535) {
58                     throw new com.sun.star.lang.IllegalArgumentException(
59                         "port parameter must have value between 0 and 65535,"
60                         + " inclusive");
61                 }
62             } else if (key.equalsIgnoreCase("backlog")) {
63                 try {
64                     backlog = Integer.valueOf(value).intValue();
65                 } catch (NumberFormatException e) {
66                     throw new com.sun.star.lang.IllegalArgumentException(
67                         e.toString());
68                 }
69             } else if (key.equalsIgnoreCase("tcpnodelay")) {
70                 if (value.equals("0")) {
71                     tcpNoDelay = Boolean.FALSE;
72                 } else if (value.equals("1")) {
73                     tcpNoDelay = Boolean.TRUE;
74                 } else {
75                     throw new com.sun.star.lang.IllegalArgumentException(
76                         "tcpnodelay parameter must have 0/1 value");
77                 }
78             }
79             i = j;
80         }
81     }
82 
83     public String getHost() {
84         return host;
85     }
86 
87     public int getPort() {
88         return port;
89     }
90 
91     public int getBacklog() {
92         return backlog;
93     }
94 
95     public Boolean getTcpNoDelay() {
96         return tcpNoDelay;
97     }
98 
99     private String host = null;
100     private int port = 6001;
101     private int backlog = 50;
102     private Boolean tcpNoDelay = null;
103 }
104