Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2013, impossibl.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of impossibl.com nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.impossibl.postgres.protocol.v30;
import com.impossibl.postgres.protocol.BindExecCommand;
import com.impossibl.postgres.protocol.CloseCommand;
import com.impossibl.postgres.protocol.Command;
import com.impossibl.postgres.protocol.FunctionCallCommand;
import com.impossibl.postgres.protocol.Notice;
import com.impossibl.postgres.protocol.PrepareCommand;
import com.impossibl.postgres.protocol.Protocol;
import com.impossibl.postgres.protocol.QueryCommand;
import com.impossibl.postgres.protocol.ResultField;
import com.impossibl.postgres.protocol.ResultField.Format;
import com.impossibl.postgres.protocol.SSLRequestCommand;
import com.impossibl.postgres.protocol.ServerObjectType;
import com.impossibl.postgres.protocol.StartupCommand;
import com.impossibl.postgres.protocol.TransactionStatus;
import com.impossibl.postgres.protocol.TypeRef;
import com.impossibl.postgres.system.BasicContext;
import com.impossibl.postgres.system.Context;
import com.impossibl.postgres.system.Context.KeyData;
import com.impossibl.postgres.types.Registry;
import com.impossibl.postgres.types.Type;
import static com.impossibl.postgres.protocol.TransactionStatus.Active;
import static com.impossibl.postgres.protocol.TransactionStatus.Failed;
import static com.impossibl.postgres.protocol.TransactionStatus.Idle;
import static com.impossibl.postgres.utils.ChannelBuffers.readCString;
import static com.impossibl.postgres.utils.ChannelBuffers.writeCString;
import static com.impossibl.postgres.utils.guava.Strings.nullToEmpty;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.WeakReference;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.FINEST;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
public class ProtocolImpl implements Protocol {
private static Logger logger = Logger.getLogger(ProtocolImpl.class.getName());
public abstract static class ExecutionTimerTask implements TimerTask {
enum State {
NotStarted,
Running,
Cancelling,
Completed
}
private AtomicReference state = new AtomicReference<>(State.NotStarted);
private Thread thread;
public abstract void run();
@Override
public void run(Timeout timeout) throws Exception {
try {
thread = Thread.currentThread();
if (!state.compareAndSet(State.NotStarted, State.Running))
return;
run();
}
catch (Throwable e) {
// Ignore...
}
finally {
state.set(State.Completed);
synchronized (state) {
state.notify();
}
}
}
void cancel() {
if (this.state.getAndSet(State.Cancelling) == State.Running) {
thread.interrupt();
synchronized (state) {
while (state.get() == State.Cancelling) {
try {
state.wait();
}
catch (InterruptedException e) {
// Ignore
}
}
}
}
}
}
// Frontend messages
private static final byte PASSWORD_MSG_ID = 'p';
private static final byte FLUSH_MSG_ID = 'H';
private static final byte TERMINATE_MSG_ID = 'X';
private static final byte SYNC_MSG_ID = 'S';
private static final byte QUERY_MSG_ID = 'Q';
private static final byte PARSE_MSG_ID = 'P';
private static final byte BIND_MSG_ID = 'B';
private static final byte DESCRIBE_MSG_ID = 'D';
private static final byte EXECUTE_MSG_ID = 'E';
private static final byte CLOSE_MSG_ID = 'C';
private static final byte FUNCTION_CALL_MSG_ID = 'F';
// Backend messages
private static final byte BACKEND_KEY_MSG_ID = 'K';
private static final byte AUTHENTICATION_MSG_ID = 'R';
private static final byte ERROR_MSG_ID = 'E';
private static final byte NOTICE_MSG_ID = 'N';
private static final byte NOTIFICATION_MSG_ID = 'A';
private static final byte COMMAND_COMPLETE_MSG_ID = 'C';
private static final byte PARAMETER_STATUS_MSG_ID = 'S';
private static final byte READY_FOR_QUERY_MSG_ID = 'Z';
private static final byte PARAMETER_DESC_MSG_ID = 't';
private static final byte ROW_DESC_MSG_ID = 'T';
private static final byte ROW_DATA_MSG_ID = 'D';
private static final byte PORTAL_SUSPENDED_MSG_ID = 's';
private static final byte NO_DATA_MSG_ID = 'n';
private static final byte EMPTY_QUERY_MSG_ID = 'I';
private static final byte PARSE_COMPLETE_MSG_ID = '1';
private static final byte BIND_COMPLETE_MSG_ID = '2';
private static final byte CLOSE_COMPLETE_MSG_ID = '3';
private static final byte FUNCTION_RESULT_MSG_ID = 'V';
private final ProtocolListener NULL_LISTENER = new BaseProtocolListener() {
@Override
public void exception(Throwable cause) throws IOException {
lastException = cause;
}
};
AtomicBoolean connected = new AtomicBoolean(true);
ProtocolShared.Ref sharedRef;
Channel channel;
WeakReference contextRef;
TransactionStatus txStatus;
ProtocolListener listener;
Timeout executionTimeout;
Throwable lastException;
public ProtocolImpl(ProtocolShared.Ref sharedRef, Channel channel, BasicContext context) {
this.sharedRef = sharedRef;
this.channel = channel;
this.contextRef = new WeakReference<>(context);
this.txStatus = Idle;
}
public BasicContext getContext() {
return contextRef.get();
}
public Throwable getLastException() {
return lastException;
}
@Override
public boolean isConnected() {
return connected.get();
}
@Override
public void shutdown() {
//Ensure only one thread can ever succeed in calling shutdown
if (!connected.getAndSet(false)) {
return;
}
try {
ChannelBuffer msg = ChannelBuffers.dynamicBuffer();
writeTerminate(msg);
channel.write(msg).addListener(ChannelFutureListener.CLOSE);
}
catch (Exception e) {
//Close anyway...
channel.close().awaitUninterruptibly(100);
}
sharedRef.release();
}
private void kill() {
connected.set(false);
channel.close().awaitUninterruptibly();
}
@Override
public void abort(Executor executor) {
if (!connected.get())
return;
//Shutdown socket (also guarantees no more commands begin execution)
shutdown();
//Issue cancel request from separate socket (per Postgres protocol). This
//is a convenience to the server as the abort does not depend on its
//success to complete properly
executor.execute(new Runnable() {
@Override
public void run() {
sendCancelRequest();
}
});
//Copy listener so canceling thread cannot nullify it
ProtocolListener localListener = this.listener;
synchronized (localListener) {
localListener.abort();
localListener.notifyAll();
}
}
void setListener(ProtocolListener listener) {
this.listener = listener;
}
@Override
public SSLRequestCommand createSSLRequest() {
return new SSLRequestCommandImpl();
}
@Override
public StartupCommand createStartup(Map settings) {
return new StartupCommandImpl(settings);
}
@Override
public PrepareCommand createPrepare(String statementName, String sqlText, List parameterTypes) {
return new PrepareCommandImpl(statementName, sqlText, parameterTypes);
}
@Override
public BindExecCommand createBindExec(String portalName, String statementName, List parameterTypes, List