View Javadoc

1   package org.apache.helix.manager.zk;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  import org.apache.log4j.Logger;
25  import org.apache.zookeeper.AsyncCallback.DataCallback;
26  import org.apache.zookeeper.AsyncCallback.StatCallback;
27  import org.apache.zookeeper.AsyncCallback.StringCallback;
28  import org.apache.zookeeper.AsyncCallback.VoidCallback;
29  import org.apache.zookeeper.KeeperException.Code;
30  import org.apache.zookeeper.data.Stat;
31  
32  public class ZkAsyncCallbacks
33  {
34    private static Logger LOG = Logger.getLogger(ZkAsyncCallbacks.class);
35  
36    static class GetDataCallbackHandler extends DefaultCallback implements DataCallback
37    {
38      byte[] _data;
39      Stat   _stat;
40  
41      @Override
42      public void handle()
43      {
44        // TODO Auto-generated method stub
45      }
46  
47      @Override
48      public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat)
49      {
50        if (rc == 0)
51        {
52          _data = data;
53          _stat = stat;
54        }
55        callback(rc, path, ctx);
56      }
57    }
58  
59    static class SetDataCallbackHandler extends DefaultCallback implements StatCallback
60    {
61      Stat _stat;
62  
63      @Override
64      public void handle()
65      {
66        // TODO Auto-generated method stub
67      }
68  
69      @Override
70      public void processResult(int rc, String path, Object ctx, Stat stat)
71      {
72        if (rc == 0)
73        {
74          _stat = stat;
75        }
76        callback(rc, path, ctx);
77      }
78      
79      public Stat getStat()
80      {
81        return _stat;
82      }
83    }
84    
85    static class ExistsCallbackHandler extends DefaultCallback implements StatCallback
86    {
87      Stat _stat;
88  
89      @Override
90      public void handle()
91      {
92        // TODO Auto-generated method stub
93      }
94  
95      @Override
96      public void processResult(int rc, String path, Object ctx, Stat stat)
97      {
98        if (rc == 0)
99        {
100         _stat = stat;
101       }
102       callback(rc, path, ctx);
103     }
104 
105   }
106 
107   static class CreateCallbackHandler extends DefaultCallback implements StringCallback
108   {
109     @Override
110     public void processResult(int rc, String path, Object ctx, String name)
111     {
112       callback(rc, path, ctx);
113     }
114 
115     @Override
116     public void handle()
117     {
118       // TODO Auto-generated method stub
119     }
120   }
121 
122   static class DeleteCallbackHandler extends DefaultCallback implements VoidCallback
123   {
124     @Override
125     public void processResult(int rc, String path, Object ctx)
126     {
127       callback(rc, path, ctx);
128     }
129 
130     @Override
131     public void handle()
132     {
133       // TODO Auto-generated method stub
134     }
135 
136   }
137 
138   /**
139    * Default callback for zookeeper async api
140    */
141   static abstract class DefaultCallback
142   {
143     AtomicBoolean _lock = new AtomicBoolean(false);
144     int           _rc   = -1;
145 
146     public void callback(int rc, String path, Object ctx)
147     {
148       if (rc != 0)
149       {
150         LOG.warn(this + ", rc:" + Code.get(rc) + ", path: " + path);
151       }
152       _rc = rc;
153       handle();
154       
155       synchronized (_lock)
156       {
157         _lock.set(true);
158         _lock.notify();
159       }
160     }
161 
162     public boolean waitForSuccess()
163     {
164       try
165       {
166         synchronized (_lock)
167         {
168           while (!_lock.get())
169           {
170             _lock.wait();
171           }
172         }
173       }
174       catch (InterruptedException e)
175       {
176         // TODO Auto-generated catch block
177         e.printStackTrace();
178       }
179       return true;
180     }
181 
182     public int getRc()
183     {
184       return _rc;
185     }
186 
187     abstract public void handle();
188   }
189 
190 }