[jboss-cvs] JBossCache/src/org/jboss/cache/loader/tcp ...

Manik Surtani msurtani at jboss.com
Wed Sep 6 11:30:55 EDT 2006


  User: msurtani
  Date: 06/09/06 11:30:55

  Modified:    src/org/jboss/cache/loader/tcp  TcpCacheServer.java
  Log:
  Removed TreeCache dependency on ServiceMBeanSupport
  
  Revision  Changes    Path
  1.17      +208 -152  JBossCache/src/org/jboss/cache/loader/tcp/TcpCacheServer.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TcpCacheServer.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/tcp/TcpCacheServer.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -b -r1.16 -r1.17
  --- TcpCacheServer.java	31 Aug 2006 14:56:46 -0000	1.16
  +++ TcpCacheServer.java	6 Sep 2006 15:30:55 -0000	1.17
  @@ -31,179 +31,214 @@
   
   /**
    * TCP-IP based CacheServer, configure TcpDelegatingCacheLoader with host and port of this server
  + *
    * @author Bela Ban
  - * @version $Id: TcpCacheServer.java,v 1.16 2006/08/31 14:56:46 vblagojevic Exp $
  + * @version $Id: TcpCacheServer.java,v 1.17 2006/09/06 15:30:55 msurtani Exp $
    */
  -public class TcpCacheServer extends ServiceMBeanSupport implements TcpCacheServerMBean {
  +public class TcpCacheServer extends ServiceMBeanSupport implements TcpCacheServerMBean
  +{
      ServerSocket    srv_sock;
  -   InetAddress     bind_addr=null;
  -   int             port=7500;
  +   InetAddress bind_addr = null;
  +   int port = 7500;
      TreeCache       cache;
      //TreeCacheMBean cache;
      ObjectName      cache_name;
      String          config;
  -   boolean         running=true;
  -   List            conns=new LinkedList();
  +   boolean running = true;
  +   List conns = new LinkedList();
      String          agendId;
  -   static Log      mylog=LogFactory.getLog(TcpCacheServer.class);
  +   static Log mylog = LogFactory.getLog(TcpCacheServer.class);
   
   
  -   public TcpCacheServer() {
  +   public TcpCacheServer()
  +   {
      }
   
  -   public String getBindAddress() {
  -      return bind_addr != null? bind_addr.toString() : "n/a";
  +   public String getBindAddress()
  +   {
  +      return bind_addr != null ? bind_addr.toString() : "n/a";
      }
   
  -   public void setBindAddress(String bind_addr) throws UnknownHostException {
  -      if(bind_addr != null)
  -         this.bind_addr=InetAddress.getByName(bind_addr);
  +   public void setBindAddress(String bind_addr) throws UnknownHostException
  +   {
  +      if (bind_addr != null)
  +         this.bind_addr = InetAddress.getByName(bind_addr);
      }
   
  -   public int getPort() {
  +   public int getPort()
  +   {
         return port;
      }
   
  -   public void setPort(int port) {
  -      this.port=port;
  +   public void setPort(int port)
  +   {
  +      this.port = port;
      }
   
  -   public String getMBeanServerName() {
  +   public String getMBeanServerName()
  +   {
         return agendId;
      }
   
  -   public void setMBeanServerName(String name) {
  -      agendId=name;
  +   public void setMBeanServerName(String name)
  +   {
  +      agendId = name;
      }
   
  -   public String getConfig() {
  +   public String getConfig()
  +   {
         return config;
      }
   
  -   public void setConfig(String config) {
  -      this.config=config;
  +   public void setConfig(String config)
  +   {
  +      this.config = config;
      }
   
      //public TreeCacheMBean getCache() {
  -    public TreeCache getCache() {
  +   public TreeCache getCache()
  +   {
         return cache;
      }
   
  -   public void setCache(TreeCache cache) {
  +   public void setCache(TreeCache cache)
  +   {
   //public void setCache(TreeCacheMBean cache) {
  -      this.cache=cache;
  +      this.cache = cache;
      }
   
  -   public String getCacheName() {
  -      return cache_name != null? cache_name.toString() : "n/a";
  +   public String getCacheName()
  +   {
  +      return cache_name != null ? cache_name.toString() : "n/a";
      }
   
  -   public void setCacheName(String cache_name) throws MalformedObjectNameException {
  -      this.cache_name=new ObjectName(cache_name);
  +   public void setCacheName(String cache_name) throws MalformedObjectNameException
  +   {
  +      this.cache_name = new ObjectName(cache_name);
      }
   
  -   public void createService() throws Exception {
  +   public void createService() throws Exception
  +   {
         super.createService();
      }
   
  -   public void startService() throws Exception {
  +   public void startService() throws Exception
  +   {
         Socket client_sock;
         Connection conn;
   
  -      if(cache == null) {
  +      if (cache == null)
  +      {
            // 1. check whether we have an object name, pointing to the cache MBean
  -         if(cache_name != null && server != null) {
  -            cache=(TreeCache)MBeanProxyExt.create(TreeCache.class, cache_name, server);
  +         if (cache_name != null && server != null)
  +         {
  +            cache = (TreeCache) MBeanProxyExt.create(TreeCache.class, cache_name, server);
            }
         }
   
  -      if(cache == null) { // still not set
  -         if(config != null) {
  -            cache=new TreeCache();
  +      if (cache == null)
  +      { // still not set
  +         if (config != null)
  +         {
  +            cache = new TreeCache();
               cache.setConfiguration(new XmlConfigurationParser().parseFile(this.config));
  -            cache.createService();
  -            cache.startService();
  +            cache.create();
  +            cache.start();
            }
         }
   
  -      if(cache == null)
  +      if (cache == null)
            throw new CacheException("cache reference is not set");
   
   
  -      srv_sock=new ServerSocket(port, 10, bind_addr);
  +      srv_sock = new ServerSocket(port, 10, bind_addr);
         System.out.println("TcpCacheServer listening on : " + srv_sock.getInetAddress() + ":" + srv_sock.getLocalPort());
  -      while(running) {
  -         client_sock=srv_sock.accept();
  -         conn=new Connection(client_sock, cache);
  +      while (running)
  +      {
  +         client_sock = srv_sock.accept();
  +         conn = new Connection(client_sock, cache);
            conns.add(conn);
            conn.start();
         }
      }
   
  -   public void stopService() {
  -      running=false;
  -      for(Iterator it=conns.iterator(); it.hasNext();) {
  -         Connection conn=(Connection)it.next();
  +   public void stopService()
  +   {
  +      running = false;
  +      for (Iterator it = conns.iterator(); it.hasNext();)
  +      {
  +         Connection conn = (Connection) it.next();
            conn.close();
         }
         conns.clear();
  -      if(srv_sock != null) {
  -         try {
  +      if (srv_sock != null)
  +      {
  +         try
  +         {
               srv_sock.close();
            }
  -         catch(IOException e) {
  +         catch (IOException e)
  +         {
            }
         }
      }
   
   
  -   public String getConnections() {
  -      StringBuffer sb=new StringBuffer();
  +   public String getConnections()
  +   {
  +      StringBuffer sb = new StringBuffer();
         sb.append(conns.size() + " connections:\n");
  -      for(Iterator it=conns.iterator(); it.hasNext();) {
  -         Connection c=(Connection)it.next();
  +      for (Iterator it = conns.iterator(); it.hasNext();)
  +      {
  +         Connection c = (Connection) it.next();
            sb.append(c).append("\n");
         }
         return sb.toString();
      }
   
   
  -   public void destroy() {
  +   public void destroy()
  +   {
         super.destroy();
      }
   
   
  -   private static class Connection implements Runnable {
  -      Socket             sock=null;
  -      ObjectInputStream  input=null;
  -      ObjectOutputStream output=null;
  +   private static class Connection implements Runnable
  +   {
  +      Socket sock = null;
  +      ObjectInputStream input = null;
  +      ObjectOutputStream output = null;
         //TreeCacheMBean     c;
          TreeCache c;
  -      Thread             t=null;
  +      Thread t = null;
   
  -       public Connection(Socket sock, TreeCache cache) throws IOException {
  +      public Connection(Socket sock, TreeCache cache) throws IOException
  +      {
         //public Connection(Socket sock, TreeCacheMBean cache) throws IOException {
  -         this.sock=sock;
  -         output=new ObjectOutputStream(sock.getOutputStream());
  -         input=new ObjectInputStream(sock.getInputStream());
  -         c=cache;
  +         this.sock = sock;
  +         output = new ObjectOutputStream(sock.getOutputStream());
  +         input = new ObjectInputStream(sock.getInputStream());
  +         c = cache;
         }
   
   
  -      public void start() {
  -         t=new Thread(this, "TcpCacheServer.Connection");
  +      public void start()
  +      {
  +         t = new Thread(this, "TcpCacheServer.Connection");
            t.setDaemon(true);
            t.start();
         }
   
  -      public void close() {
  -         t=null;
  -         try {if(output != null) output.close();} catch(Throwable th) {}
  -         try {if(input != null) input.close();} catch(Throwable th) {}
  -         try {if(sock != null) sock.close();} catch(Throwable th) {}
  +      public void close()
  +      {
  +         t = null;
  +         try {if (output != null) output.close();} catch (Throwable th) {}
  +         try {if (input != null) input.close();} catch (Throwable th) {}
  +         try {if (sock != null) sock.close();} catch (Throwable th) {}
         }
   
  -      public void run() {
  +      public void run()
  +      {
            int op;
            Fqn fqn;
            Object key, val, retval;
  @@ -212,106 +247,116 @@
            boolean flag;
            byte[] state;
   
  -         while(t != null && Thread.currentThread().equals(t)) {
  -            try {
  -               op=input.readInt();
  +         while (t != null && Thread.currentThread().equals(t))
  +         {
  +            try
  +            {
  +               op = input.readInt();
               }
  -            catch(IOException e) {
  +            catch (IOException e)
  +            {
                  mylog.warn("failed reading data, thread will terminate", e);
  -               try {if(output != null) output.close();} catch(Throwable th) {}
  -               try {if(input != null) input.close();} catch(Throwable th) {}
  -               try {if(sock != null) sock.close();} catch(Throwable th) {}
  +               try {if (output != null) output.close();} catch (Throwable th) {}
  +               try {if (input != null) input.close();} catch (Throwable th) {}
  +               try {if (sock != null) sock.close();} catch (Throwable th) {}
                  break;
               }
   
  -            try {
  -               switch(op) {
  +            try
  +            {
  +               switch (op)
  +               {
                     case DelegatingCacheLoader.delegateGetChildrenNames:
  -                     fqn=(Fqn)input.readObject();
  -                     Set children=c.getChildrenNames(fqn);
  +                     fqn = (Fqn) input.readObject();
  +                     Set children = c.getChildrenNames(fqn);
                        output.writeObject(children);  // this may be null - that's okay
                        break;
                     case DelegatingCacheLoader.delegateGetKey:
  -                     fqn=(Fqn)input.readObject();
  -                     key=input.readObject();
  -                     retval=c.get(fqn, key);
  +                     fqn = (Fqn) input.readObject();
  +                     key = input.readObject();
  +                     retval = c.get(fqn, key);
                        output.writeObject(retval);
                        break;
                     case DelegatingCacheLoader.delegateGet:
  -                     fqn=(Fqn)input.readObject();
  -                     n=c.get(fqn);
  -                     if(n == null) { // node doesn't exist - return null
  +                     fqn = (Fqn) input.readObject();
  +                     n = c.get(fqn);
  +                     if (n == null)
  +                     { // node doesn't exist - return null
                           output.writeObject(n);
                           break;
                        }
  -                     map=n.getData();
  -                     if(map == null) map=new HashMap();
  +                     map = n.getData();
  +                     if (map == null) map = new HashMap();
                        output.writeObject(map);
                        break;
                     case DelegatingCacheLoader.delegateExists:
  -                     fqn=(Fqn)input.readObject();
  -                     flag=c.exists(fqn);
  +                     fqn = (Fqn) input.readObject();
  +                     flag = c.exists(fqn);
                        output.writeObject(Boolean.valueOf(flag));
                        break;
                     case DelegatingCacheLoader.delegatePutKeyVal:
  -                     fqn=(Fqn)input.readObject();
  -                     key=input.readObject();
  -                     val=input.readObject();
  -                     retval=c.put(fqn, key, val);
  +                     fqn = (Fqn) input.readObject();
  +                     key = input.readObject();
  +                     val = input.readObject();
  +                     retval = c.put(fqn, key, val);
                        output.writeObject(retval);
                        break;
                     case DelegatingCacheLoader.delegatePut:
  -                     fqn=(Fqn)input.readObject();
  -                     map=(Map)input.readObject();
  +                     fqn = (Fqn) input.readObject();
  +                     map = (Map) input.readObject();
                        c.put(fqn, map);
                        output.writeObject(Boolean.TRUE);
                        break;
   
                     case DelegatingCacheLoader.putList:
  -                     int length=input.readInt();
  -                     retval=Boolean.TRUE;
  -                     if(length > 0) {
  +                     int length = input.readInt();
  +                     retval = Boolean.TRUE;
  +                     if (length > 0)
  +                     {
                           Modification mod;
  -                        List mods=new ArrayList(length);
  -                        for(int i=0; i < length; i++) {
  -                           mod=new Modification();
  +                        List mods = new ArrayList(length);
  +                        for (int i = 0; i < length; i++)
  +                        {
  +                           mod = new Modification();
                              mod.readExternal(input);
                              mods.add(mod);
                           }
  -                        try {
  +                        try
  +                        {
                              handleModifications(mods);
                           }
  -                        catch(Exception ex) {
  -                           retval=ex;
  +                        catch (Exception ex)
  +                        {
  +                           retval = ex;
                           }
                        }
                        output.writeObject(retval);
                        break;
                     case DelegatingCacheLoader.delegateRemoveKey:
  -                     fqn=(Fqn)input.readObject();
  -                     key=input.readObject();
  -                     retval=c.remove(fqn, key);
  +                     fqn = (Fqn) input.readObject();
  +                     key = input.readObject();
  +                     retval = c.remove(fqn, key);
                        output.writeObject(retval);
                        break;
                     case DelegatingCacheLoader.delegateRemove:
  -                     fqn=(Fqn)input.readObject();
  +                     fqn = (Fqn) input.readObject();
                        c.remove(fqn);
                        output.writeObject(Boolean.TRUE);
                        break;
                     case DelegatingCacheLoader.delegateRemoveData:
  -                     fqn=(Fqn)input.readObject();
  +                     fqn = (Fqn) input.readObject();
                        c.removeData(fqn);
                        output.writeObject(Boolean.TRUE);
                        break;
                     case DelegatingCacheLoader.delegateLoadEntireState:
  -                     ObjectOutputStream os=(ObjectOutputStream)input.readObject();
  -                     if(c.getCacheLoader() != null)
  +                     ObjectOutputStream os = (ObjectOutputStream) input.readObject();
  +                     if (c.getCacheLoader() != null)
                           c.getCacheLoader().loadEntireState(os);
                        output.writeObject(Boolean.TRUE);
                        break;
                     case DelegatingCacheLoader.delegateStoreEntireState:
  -                     ObjectInputStream is = (ObjectInputStream)input.readObject();
  -                     if(c.getCacheLoader() != null)
  +                     ObjectInputStream is = (ObjectInputStream) input.readObject();
  +                     if (c.getCacheLoader() != null)
                           c.getCacheLoader().storeEntireState(is);
                        output.writeObject(Boolean.TRUE);
                        break;
  @@ -321,12 +366,15 @@
                  }
                  output.flush();
               }
  -            catch(Exception e) {
  -               try {
  +            catch (Exception e)
  +            {
  +               try
  +               {
                     output.writeObject(e);
                     output.flush();
                  }
  -               catch(IOException e1) {
  +               catch (IOException e1)
  +               {
                     e1.printStackTrace();
                  }
               }
  @@ -334,18 +382,23 @@
         }
   
   
  -      public String toString() {
  -         StringBuffer sb=new StringBuffer();
  -         if(sock != null) {
  +      public String toString()
  +      {
  +         StringBuffer sb = new StringBuffer();
  +         if (sock != null)
  +         {
               sb.append(sock.getRemoteSocketAddress());
            }
            return sb.toString();
         }
   
  -      protected void handleModifications(List modifications) throws CacheException {
  -         for(Iterator it=modifications.iterator(); it.hasNext();) {
  -            Modification m=(Modification)it.next();
  -            switch(m.getType()) {
  +      protected void handleModifications(List modifications) throws CacheException
  +      {
  +         for (Iterator it = modifications.iterator(); it.hasNext();)
  +         {
  +            Modification m = (Modification) it.next();
  +            switch (m.getType())
  +            {
                  case Modification.PUT_DATA:
                     c.put(m.getFqn(), m.getData());
                     break;
  @@ -375,31 +428,34 @@
      }
   
   
  -
  -
  -   public static void main(String[] args) throws Exception {
  -      String bind_addr=null;
  -      int port=7500;
  +   public static void main(String[] args) throws Exception
  +   {
  +      String bind_addr = null;
  +      int port = 7500;
         TcpCacheServer server;
  -      String config=null;
  +      String config = null;
   
  -      for(int i=0; i < args.length; i++) {
  -         if(args[i].equals("-bind_addr")) {
  -            bind_addr=args[++i];
  +      for (int i = 0; i < args.length; i++)
  +      {
  +         if (args[i].equals("-bind_addr"))
  +         {
  +            bind_addr = args[++i];
               continue;
            }
  -         if(args[i].equals("-port")) {
  -            port=Integer.parseInt(args[++i]);
  +         if (args[i].equals("-port"))
  +         {
  +            port = Integer.parseInt(args[++i]);
               continue;
            }
  -         if(args[i].equals("-config")) {
  -            config=args[++i];
  +         if (args[i].equals("-config"))
  +         {
  +            config = args[++i];
               continue;
            }
            help();
            return;
         }
  -      server=new TcpCacheServer();
  +      server = new TcpCacheServer();
         server.setBindAddress(bind_addr);
         server.setPort(port);
         server.setConfig(config);
  @@ -408,8 +464,8 @@
      }
   
   
  -
  -   private static void help() {
  +   private static void help()
  +   {
         System.out.println("TcpCacheServer [-bind_addr <address>] [-port <port>] [-config <config file>] [-help]");
      }
   }
  
  
  



More information about the jboss-cvs-commits mailing list