org.apache.cayenne.remote.ClientChannel Maven / Gradle / Ivy
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.cayenne.remote;
import java.util.List;
import java.util.ListIterator;
import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.DataChannel;
import org.apache.cayenne.DataChannelSyncCallbackAction;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.QueryResponse;
import org.apache.cayenne.event.DefaultEventManager;
import org.apache.cayenne.event.EventBridge;
import org.apache.cayenne.event.EventManager;
import org.apache.cayenne.event.EventSubject;
import org.apache.cayenne.graph.CompoundDiff;
import org.apache.cayenne.graph.GraphDiff;
import org.apache.cayenne.graph.GraphDiffCompressor;
import org.apache.cayenne.graph.GraphEvent;
import org.apache.cayenne.map.EntityResolver;
import org.apache.cayenne.query.EntityResultSegment;
import org.apache.cayenne.query.Query;
import org.apache.cayenne.query.QueryMetadata;
import org.apache.cayenne.util.DeepMergeOperation;
import org.apache.cayenne.util.ToStringBuilder;
/**
* A {@link org.apache.cayenne.DataChannel} implementation that accesses a remote server
* via a ClientConnection.
*
* @since 1.2
*/
public class ClientChannel implements DataChannel {
protected ClientConnection connection;
protected EventManager eventManager;
protected EntityResolver entityResolver;
protected boolean channelEventsEnabled;
protected GraphDiffCompressor diffCompressor;
EventBridge remoteChannelListener;
/**
* Creates a new channel accessing remote server via provided connection. Channel
* created using this constructor will post no events of its own and provide its users
* with a multithreaded EventManager.
*
* @deprecated since 3.1 use
* {@link #ClientChannel(ClientConnection, boolean, EventManager, boolean)}
*/
@Deprecated
public ClientChannel(ClientConnection connection) {
this(connection, false);
}
/**
* @deprecated since 3.1 use
* {@link #ClientChannel(ClientConnection, boolean, EventManager, boolean)}
*/
@Deprecated
public ClientChannel(ClientConnection connection, boolean channelEventsEnabled) {
this(connection, channelEventsEnabled, new DefaultEventManager(2));
}
/**
* @deprecated since 3.1 use
* {@link #ClientChannel(ClientConnection, boolean, EventManager, boolean)}
*/
@Deprecated
public ClientChannel(ClientConnection connection, boolean channelEventsEnabled,
EventManager eventManager) throws CayenneRuntimeException {
this(connection, channelEventsEnabled, eventManager, false);
}
/**
* @param remoteEventsOptional if true, failure to start an EventBridge will not
* result in an exception.
* @since 3.0
*/
public ClientChannel(ClientConnection connection, boolean channelEventsEnabled,
EventManager eventManager, boolean remoteEventsOptional)
throws CayenneRuntimeException {
this.connection = connection;
this.diffCompressor = new GraphDiffCompressor();
this.eventManager = eventManager;
this.channelEventsEnabled = eventManager != null && channelEventsEnabled;
if (!remoteEventsOptional) {
setupRemoteChannelListener();
}
else {
try {
setupRemoteChannelListener();
}
catch (CayenneRuntimeException e) {
}
}
}
/**
* @since 3.1
*/
public ClientConnection getConnection() {
return connection;
}
/**
* @since 3.1
*/
public boolean isChannelEventsEnabled() {
return channelEventsEnabled;
}
public EventManager getEventManager() {
return eventManager;
}
public QueryResponse onQuery(ObjectContext context, Query query) {
QueryResponse response = (QueryResponse) send(
new QueryMessage(query),
QueryResponse.class);
// if needed, register objects in provided context, rewriting the response
// (assuming all lists are mutable)
if (context != null) {
EntityResolver resolver = context.getEntityResolver();
QueryMetadata info = query.getMetaData(resolver);
if (!info.isFetchingDataRows()) {
response.reset();
while (response.next()) {
if (response.isList()) {
List objects = response.currentList();
if (!objects.isEmpty()) {
DeepMergeOperation merger = new DeepMergeOperation(context);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy