1 23 package com.sun.enterprise.admin.server.core.channel; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.UnknownHostException ; 30 import java.rmi.NoSuchObjectException ; 31 import java.rmi.RemoteException ; 32 import java.rmi.server.RemoteObject ; 33 import java.rmi.server.RemoteStub ; 34 import java.rmi.server.RMIClientSocketFactory ; 35 import java.rmi.server.RMIServerSocketFactory ; 36 import java.rmi.server.ServerNotActiveException ; 37 import java.rmi.server.UnicastRemoteObject ; 38 39 import com.sun.enterprise.admin.common.Status; 40 import com.sun.enterprise.admin.event.AdminEvent; 41 import com.sun.enterprise.admin.event.AdminEventMulticaster; 42 import com.sun.enterprise.admin.event.AdminEventResult; 43 import com.sun.enterprise.server.ss.ASSocketService; 44 45 import com.sun.enterprise.util.i18n.StringManager; 47 48 51 public class AdminChannelServer extends UnicastRemoteObject 52 implements RemoteAdminChannel { 53 54 private String localAddress = null; 55 56 private byte[] myKey; 57 58 63 private int instanceStatus = Status.kInstanceStartingCode; 64 65 69 private boolean restartNeeded = false; 70 71 private int conflictedPort = 0; 72 73 76 public AdminChannelServer() throws RemoteException { 77 super(); 78 79 restartNeeded = RRStateFactory.getState(); 81 } 82 83 87 public AdminChannelServer(int port, RMIClientSocketFactory csf, 88 RMIServerSocketFactory ssf) throws RemoteException { 89 super(port, csf, ssf); 90 91 restartNeeded = RRStateFactory.getState(); 93 } 94 95 98 public AdminEventResult sendNotification(byte[] key, AdminEvent event) 99 throws RemoteException { 100 if (!checkAccess()) { 101 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 102 throw new SecurityException ( msg ); 103 } 104 if (!keyMatches(key)) { 105 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 106 throw new IllegalArgumentException ( msg ); 107 } 108 return AdminEventMulticaster.multicastEvent(event); 109 } 110 111 114 public boolean pingServer(byte[] key) throws RemoteException { 115 if (!checkAccess()) { 116 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 117 throw new SecurityException ( msg ); 118 } 119 if (!keyMatches(key)) { 120 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 121 throw new IllegalArgumentException ( msg ); 122 } 123 return true; 124 } 125 126 136 public int getServerStatusCode(byte[] key) throws RemoteException { 137 if (!checkAccess()) { 138 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 139 throw new SecurityException ( msg ); 140 } 141 if (!keyMatches(key)) { 142 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 143 throw new IllegalArgumentException ( msg ); 144 } 145 return instanceStatus; 146 } 147 148 157 public boolean isRestartNeeded(byte[] key) throws RemoteException { 158 if (!checkAccess()) { 159 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 160 throw new SecurityException ( msg ); 161 } 162 if (!keyMatches(key)) { 163 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 164 throw new IllegalArgumentException ( msg ); 165 } 166 return restartNeeded; 167 } 168 169 175 public void setRestartNeeded(byte[] key, boolean needRestart) 176 throws RemoteException { 177 if (!checkAccess()) { 178 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 179 throw new SecurityException ( msg ); 180 } 181 if (!keyMatches(key)) { 182 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 183 throw new IllegalArgumentException ( msg ); 184 } 185 186 try { 187 RRStateFactory.saveState(needRestart); 189 } catch (IOException ioe) { 190 String msg = localStrings.getString( 191 "admin.server.core.channel.unable_saving_state_file"); 192 throw new RuntimeException (msg, ioe); 193 } 194 195 restartNeeded = needRestart; 196 } 197 198 204 public int getConflictedPort(byte[] key) { 205 if (!checkAccess()) { 206 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 207 throw new SecurityException ( msg ); 208 } 209 if (!keyMatches(key)) { 210 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 211 throw new IllegalArgumentException ( msg ); 212 } 213 return conflictedPort; 214 } 215 216 221 public void triggerServerExit(byte[] key) { 222 if (!checkAccess()) { 223 String msg = localStrings.getString( "admin.server.core.channel.unauthorized_access" ); 224 throw new SecurityException ( msg ); 225 } 226 if (!keyMatches(key)) { 227 String msg = localStrings.getString( "admin.server.core.channel.invalid_key" ); 228 throw new IllegalArgumentException ( msg ); 229 } 230 ASSocketService.triggerServerExit(); 231 } 232 233 237 void setSharedInfo(byte[] seed) { 238 myKey = seed; 239 } 240 241 244 RemoteStub getRemoteStub() throws NoSuchObjectException { 245 return (RemoteStub )RemoteObject.toStub(this); 246 } 247 248 252 void setLocalAddress(InetAddress address) { 253 localAddress = address.getHostAddress(); 254 } 255 256 259 void setChannelStarting() { 260 this.instanceStatus = Status.kInstanceStartingCode; 261 } 262 263 266 void setChannelReady() { 267 this.instanceStatus = Status.kInstanceRunningCode; 268 } 269 270 273 void setChannelStopping() { 274 this.instanceStatus = Status.kInstanceStoppingCode; 275 } 276 277 283 void setChannelAborting(int conflictedPort) { 284 this.conflictedPort = conflictedPort; 285 this.instanceStatus = Status.kInstanceFailedCode; 286 } 287 288 291 private boolean checkAccess() { 292 boolean allowed = true; 293 String addr = null; 294 if (AdminChannel.LOCAL_ONLY_ACCESS.equals(AdminChannel.getAccessLevel())) { 295 boolean matchAddress = true; 296 try { 297 addr = this.getClientHost(); 298 if (addr == null) { 299 AdminChannel.warn(CLIENT_HOST_NULL); 300 allowed = false; 301 matchAddress = false; 302 } 303 } catch (ServerNotActiveException snae) { 304 AdminChannel.warn(LOCAL_ACCESS); 305 AdminChannel.debug(snae); 306 matchAddress = false; 307 } 308 if (matchAddress) { 309 allowed = addressMatches(addr); 310 } 311 } 312 if (!allowed) { 313 AdminChannel.debug(ADDR_MISMATCH, 314 new Object [] {addr, getLocalAddress()}); 315 } 316 return allowed; 317 } 318 319 323 private boolean addressMatches(String addr) { 324 String localAddr = getLocalAddress(); 325 if (localAddr == null) { 326 return false; 327 } 328 return addr.equals(localAddr); 329 } 330 331 336 private String getLocalAddress() { 337 if (localAddress == null) { 338 InetAddress inetAddr = null; 339 try { 340 inetAddr = InetAddress.getLocalHost(); 341 } catch (UnknownHostException uhe) { 342 AdminChannel.warn(NO_LOCAL_HOST); 343 AdminChannel.debug(uhe); 344 } 345 if (inetAddr != null) { 346 localAddress = inetAddr.getHostAddress(); 347 } 348 } 349 return localAddress; 350 } 351 352 356 private boolean keyMatches(byte[] key) { 357 boolean matches = true; 358 if (AdminChannel.ENFORCE.equals(AdminChannel.getKeyCheckLevel())) { 359 matches = checkKeyLength(key); 360 for (int i = 0; matches && i < AdminChannel.SEED_LENGTH; i++) { 361 if (key[i] != myKey[i]) { 362 matches = false; 363 } 364 } 365 } else if (AdminChannel.REQUIRE_KEY.equals(AdminChannel.getKeyCheckLevel())) { 366 matches = checkKeyLength(key); 367 } 368 if (!matches) { 369 AdminChannel.debug(KEY_MISMATCH, 370 new Object [] {new String (key), new String (myKey)}); 371 } 372 return matches; 373 } 374 375 378 private boolean checkKeyLength(byte[] key) { 379 return (key.length == AdminChannel.SEED_LENGTH); 380 } 381 382 private final static String CLIENT_HOST_NULL = "channel.client_host_null"; 383 private final static String LOCAL_ACCESS = "channel.local_access"; 384 private final static String ADDR_MISMATCH = "channel.addr_mismatch"; 385 private final static String NO_LOCAL_HOST = "channel.no_local_host"; 386 private final static String KEY_MISMATCH = "channel.key_mismatch"; 387 388 private static StringManager localStrings = 390 StringManager.getManager( AdminChannelServer.class ); 391 } 392 | Popular Tags |