package examples.cluster.rmi;

import weblogic.rmi.*;
import javax.naming.*;
import weblogic.jndi.*;
import weblogic.common.*;
import java.util.Hashtable;

/**
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class HelloClusterImpl implements HelloCluster{   
  public static final String BINDNAME = "HelloCluster";
  private static T3ServicesDef services;

  /**
   * Returns a string.
   *
   * @return                  String message
   * @exception               weblogic.rmi.RemoteException
   */
  public String sayHello() throws RemoteException {
    String temp =  "Hello World from : " + getLocation();
    System.out.println(temp);
    return temp;
  }
  
  private static T3ServicesDef  getT3Services(){
    if(services==null){
      return services = T3Services.getT3Services();
    }else{
      return services;
    }
  }
  
  private String getLocation(){
    String toReturn = null;
    try{
    toReturn = getT3Services().config().getProperty("weblogic.system.name");
    }catch(T3Exception ugh){
      System.out.println("HelloClusterImpl failed getLocation");
      ugh.printStackTrace();
    }
    if(toReturn==null){
      return "";
    }else{
      return toReturn;
    }
  }
  /**
   * Allows the WebLogic Server to instantiate this implementation
   * and bind it in the registry.
   */
  public static void main(String args[]) {
    try {
      Context context = new InitialContext();
      context.bind(BINDNAME,new HelloClusterImpl());
      log("\n\n===========================HelloClusterImpl bound " +BINDNAME+
	  "===========================\n\n");
    } catch(NamingException e) {
      log("Could not bind service to '"+BINDNAME+"'",e);
    }
  }
    private static void log(String toLog){
      try{
	getT3Services().log().info(toLog);
      }catch(T3Exception ugh){
	System.out.println("HelloClusterImpl failed getLocation");
	ugh.printStackTrace();
      }
    }
  
    private static void log(String toLog, Throwable t){
      try{
	getT3Services().log().error(toLog, t);
      }catch(T3Exception ugh){
	System.out.println("HelloClusterImpl failed getLocation");
	ugh.printStackTrace();
      }
    }
}





