| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.common; |
| 16 |
|
| 17 |
import java.util.StringTokenizer; |
| 18 |
|
| 19 |
/** |
| 20 |
* The purpose of this class is to parse resource manager contact strings. |
| 21 |
* It can handle literal IPv6 addresses enclosed in square brackets |
| 22 |
* ('[' and ']'). |
| 23 |
*/ |
| 24 |
public class ResourceManagerContact { |
| 25 |
|
| 26 |
public final static String DEFAULT_SERVICE = "/jobmanager"; |
| 27 |
public final static int DEFAULT_PORT = 2119; |
| 28 |
|
| 29 |
protected String hostName = null; |
| 30 |
protected int portNumber = -1; |
| 31 |
protected String serviceName = null; |
| 32 |
protected String globusDN = null; |
| 33 |
|
| 34 |
/* just for the super classes */ |
| 35 |
protected ResourceManagerContact() { |
| 36 |
} |
| 37 |
|
| 38 |
public ResourceManagerContact(String contact) { |
| 39 |
parse(contact); |
| 40 |
} |
| 41 |
|
| 42 |
protected void parse(String contact) { |
| 43 |
|
| 44 |
char c; |
| 45 |
int i; |
| 46 |
|
| 47 |
i = getHostToken(contact); |
| 48 |
|
| 49 |
if (i == -1) { |
| 50 |
hostName = contact; |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
hostName = contact.substring(0, i); |
| 55 |
c = contact.charAt(i); |
| 56 |
|
| 57 |
if (c == '/') { |
| 58 |
parseService(contact, i); |
| 59 |
} else { |
| 60 |
|
| 61 |
int j = getToken(contact, i+1); |
| 62 |
if (j == -1) { |
| 63 |
portNumber = parsePort(contact.substring(i+1)); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
portNumber = parsePort(contact.substring(i+1, j)); |
| 68 |
c = contact.charAt(j); |
| 69 |
|
| 70 |
if (c == ':') { |
| 71 |
globusDN = contact.substring(j+1); |
| 72 |
return; |
| 73 |
} else if (c == '/') { |
| 74 |
parseService(contact, j); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private int parsePort(String port) { |
| 80 |
if (port.length() == 0) { |
| 81 |
return DEFAULT_PORT; |
| 82 |
} else { |
| 83 |
return Integer.parseInt(port); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private void parseService(String contact, int from) { |
| 88 |
int pos = contact.indexOf(":", from); |
| 89 |
if (pos == -1) { |
| 90 |
serviceName = contact.substring(from); |
| 91 |
} else { |
| 92 |
serviceName = contact.substring(from, pos); |
| 93 |
globusDN = contact.substring(pos+1); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private int getToken(String contact, int from) { |
| 98 |
int len = contact.length(); |
| 99 |
char c; |
| 100 |
int i; |
| 101 |
|
| 102 |
for (i=from;i<len;i++) { |
| 103 |
c = contact.charAt(i); |
| 104 |
|
| 105 |
if (c == '/' || c == ':') { |
| 106 |
return i; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
private int getHostToken(String contact) { |
| 114 |
int len = contact.length(); |
| 115 |
|
| 116 |
if (contact.charAt(0) == '[') { |
| 117 |
int from = contact.indexOf(']'); |
| 118 |
if (from == -1) { |
| 119 |
throw new IllegalArgumentException( |
| 120 |
"Missing ']' in IPv6 address: "+ contact |
| 121 |
); |
| 122 |
} |
| 123 |
return (from+1 == len) ? -1 : from+1; |
| 124 |
} |
| 125 |
|
| 126 |
return getToken(contact, 0); |
| 127 |
} |
| 128 |
|
| 129 |
public static String convertDN(String globusDN) { |
| 130 |
if (globusDN == null) return null; |
| 131 |
|
| 132 |
StringTokenizer tokens = new StringTokenizer(globusDN, "/"); |
| 133 |
StringBuffer buf = new StringBuffer(); |
| 134 |
while( tokens.hasMoreTokens() ) { |
| 135 |
buf.insert(0, tokens.nextToken()); |
| 136 |
if (tokens.hasMoreTokens()) |
| 137 |
buf.insert(0, ", "); |
| 138 |
} |
| 139 |
|
| 140 |
return buf.toString(); |
| 141 |
} |
| 142 |
|
| 143 |
public String getHostName() { |
| 144 |
return hostName; |
| 145 |
} |
| 146 |
|
| 147 |
public int getPortNumber() { |
| 148 |
if (portNumber == -1) { |
| 149 |
return DEFAULT_PORT; |
| 150 |
} else { |
| 151 |
return portNumber; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public String getServiceName() { |
| 156 |
if (serviceName == null) { |
| 157 |
return DEFAULT_SERVICE; |
| 158 |
} else { |
| 159 |
return serviceName; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public String getSubject() { |
| 164 |
return globusDN; |
| 165 |
} |
| 166 |
|
| 167 |
public String getGlobusDN() { |
| 168 |
return globusDN; |
| 169 |
} |
| 170 |
|
| 171 |
public String getDN() { |
| 172 |
return globusDN; |
| 173 |
} |
| 174 |
|
| 175 |
public String toString() { |
| 176 |
StringBuffer buf = new StringBuffer(); |
| 177 |
|
| 178 |
buf.append("Host name : " + hostName + "\n"); |
| 179 |
buf.append("Port number : " + getPortNumber() + "\n"); |
| 180 |
buf.append("Service : " + getServiceName() + "\n"); |
| 181 |
buf.append("DN : " + getGlobusDN() + "\n"); |
| 182 |
|
| 183 |
return buf.toString(); |
| 184 |
} |
| 185 |
|
| 186 |
} |