KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > ClientServerChannel


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access;
21
22 import org.apache.cayenne.DataChannel;
23 import org.apache.cayenne.ObjectContext;
24 import org.apache.cayenne.QueryResponse;
25 import org.apache.cayenne.cache.QueryCache;
26 import org.apache.cayenne.event.EventManager;
27 import org.apache.cayenne.graph.GraphDiff;
28 import org.apache.cayenne.intercept.DataChannelCallbackInterceptor;
29 import org.apache.cayenne.map.EntityResolver;
30 import org.apache.cayenne.query.Query;
31
32 /**
33  * A DataChannel that provides a server-side end of the bridge between client and server
34  * objects in a Remote Object Persistence stack.
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 public class ClientServerChannel implements DataChannel {
40
41     protected DataContext serverContext;
42     protected boolean lifecycleCallbacksEnabled;
43
44     public ClientServerChannel(DataDomain domain) {
45         this(domain.createDataContext());
46     }
47
48     ClientServerChannel(DataContext serverContext) {
49         this.serverContext = serverContext;
50     }
51
52     /**
53      * @deprecated since 3.0 as DataChannel events (incorrectly called "lifecycleEvents"
54      * in 2.0) are no longer posted by ClientServerChannel.
55      */

56     public ClientServerChannel(DataDomain domain, boolean lifecycleEventsEnabled) {
57         this(domain);
58     }
59
60     /**
61      * @deprecated Since 3.0 - always returns false. This method was a misnomer referring
62      * to DataChannel events, not lifecycle events introduced in 3.0.
63      * Currently ClientServerChannel posts no channel events.
64      */

65     public boolean isLifecycleEventsEnabled() {
66         return false;
67     }
68
69     /**
70      * @deprecated Since 3.0 - does nothing. This method was a misnomer referring to
71      * DataChannel events, not lifecycle events introduced in 3.0. Currently
72      * ClientServerChannel posts no channel events.
73      */

74     public void setLifecycleEventsEnabled(boolean lifecycleEventsEnabled) {
75
76     }
77
78     public QueryResponse onQuery(ObjectContext context, Query query) {
79         return new ClientServerChannelQueryAction(this, query).execute();
80     }
81
82     QueryCache getQueryCache() {
83         return serverContext.getQueryCache();
84     }
85
86     DataChannel getParentChannel() {
87         return serverContext;
88     }
89
90     public EntityResolver getEntityResolver() {
91         return serverContext.getEntityResolver();
92     }
93
94     public EventManager getEventManager() {
95         return serverContext != null ? serverContext.getEventManager() : null;
96     }
97
98     public GraphDiff onSync(
99             ObjectContext originatingContext,
100             GraphDiff changes,
101             int syncType) {
102
103         return getParentChannel().onSync(null, changes, syncType);
104     }
105
106     /**
107      * @since 3.0
108      */

109     public boolean isLifecycleCallbacksEnabled() {
110         return lifecycleCallbacksEnabled;
111     }
112
113     /**
114      * Enables or disables lifecycle event callbacks for the underlying ObjectContext used
115      * by this channel. Enabling callbacks allows server side logic to be applied to the
116      * persistent objects during select and commit operations.
117      *
118      * @since 3.0
119      */

120     public void setLifecycleCallbacksEnabled(boolean lifecycleCallbacksEnabled) {
121         if (lifecycleCallbacksEnabled != this.lifecycleCallbacksEnabled) {
122             this.lifecycleCallbacksEnabled = lifecycleCallbacksEnabled;
123
124             if (lifecycleCallbacksEnabled) {
125                 enableCallbacks();
126             }
127             else {
128                 disableCallbacks();
129             }
130         }
131     }
132
133     void enableCallbacks() {
134         DataChannelCallbackInterceptor interceptor = new DataChannelCallbackInterceptor();
135
136         // must call pre-persist and pre-remove on commit
137
interceptor.setContextCallbacksEnabled(true);
138         interceptor.setChannel(serverContext.getParentDataDomain());
139
140         serverContext.setChannel(interceptor);
141     }
142
143     void disableCallbacks() {
144         serverContext.setChannel(serverContext.getParentDataDomain());
145     }
146 }
147
Popular Tags