1 23 24 31 package com.sun.enterprise.admin.server.core.channel; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.io.ObjectOutputStream ; 38 import java.io.PrintWriter ; 39 import java.io.StringWriter ; 40 import java.net.InetAddress ; 41 import java.rmi.NoSuchObjectException ; 42 import java.rmi.RemoteException ; 43 import java.rmi.server.RemoteStub ; 44 import java.rmi.server.UnicastRemoteObject ; 45 import java.security.SecureRandom ; 46 import java.util.HashMap ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 import com.sun.enterprise.admin.common.constant.AdminConstants; 51 import com.sun.enterprise.admin.event.AdminEventResult; 52 import com.sun.enterprise.server.Constants; 53 import com.sun.enterprise.instance.ServerManager; 54 import com.sun.appserv.server.ServerLifecycleException; 55 56 import com.sun.enterprise.util.SystemPropertyConstants; 57 58 import com.sun.enterprise.util.i18n.StringManager; 60 61 65 public class AdminChannel { 66 67 70 static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 71 72 static String instanceRoot = null; 73 74 static final String fileSeparator = "/"; 75 76 static final int SEED_LENGTH = 16; 77 78 private static AdminChannelServer server = null; 79 80 private static HashMap rmiClientMap = new HashMap (); 81 82 private static StringManager localStrings = 84 StringManager.getManager( AdminChannel.class ); 85 86 90 public static void createRMIChannel() throws ServerLifecycleException { 91 try { 92 server = createServerObject(); 93 saveStubToFile(server.getRemoteStub()); 94 } catch (Exception e) { 95 warn(SERVER_CREATION_ERRCODE); 96 debug(e); 97 throw new ServerLifecycleException(e); 98 } 99 } 100 101 106 public static void destroyRMIChannel() throws ServerLifecycleException { 107 if (server != null) { 108 server.setChannelStopping(); 109 try { 110 UnicastRemoteObject.unexportObject(server, true); 111 } catch (NoSuchObjectException nsoe) { 112 throw new ServerLifecycleException(nsoe); 113 } 114 } 115 deleteStubFile(); 116 } 117 118 122 public static void createSharedSecret() throws ServerLifecycleException { 123 assertAdminServerChannelNotNull(); 124 String fileName = getSeedFileName(); 125 File seedFile = new File (fileName); 126 byte[] prevSeed = getPreviousSeed(seedFile); 127 SecureRandom sr = new SecureRandom (prevSeed); 128 byte[] seed = new byte[SEED_LENGTH]; 129 sr.nextBytes(seed); 130 saveSeedToFile(seed, seedFile); 131 server.setSharedInfo(seed); 132 server.setChannelStarting(); 133 } 134 135 138 public static void enableWebCoreReconfig() { 139 try { 140 ReconfigHelper.enableWebCoreReconfig(); 141 } catch (Throwable t) { 142 warn(RECONFIG_ENABLE_ERROR ); 145 debug(t); 146 } 147 } 148 149 152 public static RMIClient getRMIClient(String instanceName) { 153 RMIClient client = (RMIClient)rmiClientMap.get(instanceName); 156 if (client == null) { 157 client = new RMIClient(getStubFileName(), 158 getSeedFileName()); 159 rmiClientMap.put(instanceName, client); 160 } 161 return client; 162 } 163 164 169 public static void setRMIChannelReady() { 170 assertAdminServerChannelNotNull(); 171 server.setChannelReady(); 172 } 173 174 public static void setRMIChannelStopping() { 175 assertAdminServerChannelNotNull(); 176 server.setChannelStopping(); 177 } 178 179 184 public static void setRMIChannelAborting(int port) { 185 assertAdminServerChannelNotNull(); 186 server.setChannelAborting(port); 187 } 188 189 static final String stubFileName = "admch"; 190 191 static String getInstanceRoot() { 195 if (instanceRoot == null) { 196 instanceRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 197 } 198 return instanceRoot; 199 } 200 204 static String getStubFileName() { 205 return getInstanceRoot() + fileSeparator 206 + Constants.CONFIG_DIR_NAME + fileSeparator 207 + stubFileName; 208 } 209 210 static final String seedFileName = "admsn"; 211 212 static String getSeedFileName() { 213 return getInstanceRoot() + fileSeparator 214 + Constants.CONFIG_DIR_NAME + fileSeparator 215 + seedFileName; 216 } 217 218 223 private static AdminChannelServer createServerObject() 224 throws RemoteException { 225 AdminChannelServer server = null; 226 InetAddress localAddress = getLocalLoopbackAddress(); 227 if (localAddress == null) { 228 server = new AdminChannelServer(); 229 } else { 230 LocalRMIClientSocketFactory csf = 231 new LocalRMIClientSocketFactory(localAddress); 232 LocalRMIServerSocketFactory ssf = 233 new LocalRMIServerSocketFactory(localAddress); 234 server = new AdminChannelServer(0, csf, ssf); 235 server.setLocalAddress(localAddress); 236 } 237 return server; 238 } 239 240 244 private static InetAddress getLocalLoopbackAddress() { 245 InetAddress localAddr = null; 246 try { 247 localAddr = InetAddress.getByName(null); 248 if (!localAddr.isLoopbackAddress()) { 249 localAddr = null; 250 } 251 } catch (Throwable t) { 252 localAddr = null; 254 } 255 return localAddr; 256 } 257 258 264 private static byte[] getPreviousSeed(File seedFile) { 265 boolean haveSeed = false; 266 byte[] prevSeed = new byte[SEED_LENGTH]; 267 268 SecureRandom sr = 272 com.sun.enterprise.server.J2EEServer.secureRandom; 273 assert (sr != null); sr.setSeed(System.currentTimeMillis()); 275 276 if (seedFile.exists() && seedFile.canRead()) { 277 FileInputStream fis = null; 278 try { 279 fis = new FileInputStream (seedFile); 280 fis.read(prevSeed); 281 sr.setSeed(prevSeed); 282 sr.nextBytes(prevSeed); 283 haveSeed = true; 284 } catch (IOException ioe) { 285 warn(KEY_READ_ERROR); 286 debug(ioe); 287 } finally { 288 if (fis != null) { 289 try { 290 fis.close(); 291 } catch (IOException ioe) { 292 } 293 } 294 } 295 } 296 if (!haveSeed) { 297 sr.nextBytes(prevSeed); 298 } 299 return prevSeed; 300 } 301 302 305 private static void saveSeedToFile(byte[] seed, File seedFile) { 306 FileOutputStream fos = null; 307 try { 308 fos = new FileOutputStream (seedFile); 309 fos.write(seed); 310 } catch (IOException ioe) { 311 warn(KEY_WRITE_ERROR); 312 debug(ioe); 313 } finally { 314 if (fos != null) { 315 try { 316 fos.close(); 317 } catch (IOException ioe) { 318 } 319 } 320 } 321 } 322 323 326 private static void saveStubToFile(RemoteStub stub) { 327 String fileName = getStubFileName(); 328 try { 329 File file = new File (fileName); 330 FileOutputStream fos = new FileOutputStream (file); 331 ObjectOutputStream oos = new ObjectOutputStream (fos); 332 oos.writeObject(stub); 333 fos.close(); 334 } catch (Exception e) { 335 String msg = localStrings.getString( "admin.server.core.channel.unable_saving_stub_to_file", fileName ); 336 throw new RuntimeException ( msg, e ); 337 } 338 } 339 340 343 private static void deleteStubFile() { 344 String fileName = getStubFileName(); 345 new File (fileName).delete(); 346 } 347 348 352 private static final void assertAdminServerChannelNotNull() { 353 if (server == null) { 354 String msg = localStrings.getString( "admin.server.core.channel.admin_server_channel_not_initialized" ); 355 throw new RuntimeException ( msg ); 356 } 357 } 358 359 static void warn(String s) { 360 logger.warning(s); 361 } 362 363 static void warn(String msgkey, String obj1) { 364 logger.log(Level.WARNING, msgkey, obj1); 365 } 366 367 static void debug(String s) { 368 logger.fine(s); 369 } 370 371 static void debug(String msgkey, String obj1) { 372 logger.log(Level.FINE, msgkey, obj1); 373 } 374 375 static void debug(String msgkey, Object [] objarr) { 376 logger.log(Level.FINE, msgkey, objarr); 377 } 378 379 static void debug(Throwable t) { 380 logger.log(Level.FINE, t.getMessage(), t); 381 } 382 383 static void trace(Throwable t) { 384 logger.log(Level.FINEST, t.getMessage(), t); 385 } 386 387 static final String LOCAL_ONLY_ACCESS = "high"; 388 static final String ALLOW_ALL_ACCESS = "none"; 389 390 393 static String getAccessLevel() { 394 return LOCAL_ONLY_ACCESS; 395 } 396 397 static final String ENFORCE = "high"; 398 static final String REQUIRE_KEY = "medium"; 399 static final String NO_ENFORCE = "low"; 400 401 404 static String getKeyCheckLevel() { 405 return ENFORCE; 406 } 407 408 413 static boolean getClientAutoRefreshEnabled() { 414 return true; 415 } 416 417 421 static long getClientAutoRefreshInterval() { 422 return (1 * 60 * 1000); 424 } 425 426 static final String RECONFIG_ENABLE_ERROR = "channel.reconfig_enable_error"; 427 static final String SERVER_CREATION_ERRCODE = "channel.creation_error"; 428 static final String KEY_READ_ERROR = "channel.key_read_error"; 429 static final String KEY_WRITE_ERROR = "channel.key_write_error"; 430 } 431 | Popular Tags |