Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created March 16, 2012 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnunberg/2052243 to your computer and use it in GitHub Desktop.
Save mnunberg/2052243 to your computer and use it in GitHub Desktop.
/*
* Copyright 2012 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.couchbase.mock.http;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.couchbase.mock.Bucket;
import org.couchbase.mock.CouchbaseMock.HarakiriMonitor;
/**
*
* @author M. Nunberg
*/
public class BucketsStreamingHandler implements Observer {
private final OutputStream output;
private final Bucket bucket;
private final HarakiriMonitor monitor;
private final CountDownLatch completed;
public BucketsStreamingHandler(HarakiriMonitor monitor, Bucket bucket, OutputStream output) {
this.output = output;
this.bucket = bucket;
this.monitor = monitor;
this.completed = new CountDownLatch(1);
}
@Override
public void update(Observable o, Object arg) {
try {
bucket.configReadLock();
output.write(StateGrabber.getBucketJSON(bucket).getBytes());
output.write(StateGrabber.getStreamDelimiter().getBytes());
output.flush();
bucket.configReadUnlock();
}
catch (IOException ex) {
Logger.getLogger(BucketsStreamingHandler.class.getName()).log(Level.SEVERE, null, ex);
completed.countDown();
}
}
public void startStreaming() throws IOException, InterruptedException {
bucket.configReadLock();
if (monitor != null) {
monitor.addObserver(this);
}
output.write(StateGrabber.getBucketJSON(bucket).getBytes());
output.write(StateGrabber.getStreamDelimiter().getBytes());
output.flush();
bucket.configReadUnlock();
completed.await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment