KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > RemoteSession


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.remote;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.commons.lang.builder.HashCodeBuilder;
29 import org.apache.commons.lang.builder.ToStringBuilder;
30 import org.apache.cayenne.CayenneRuntimeException;
31 import org.apache.cayenne.DataChannel;
32 import org.apache.cayenne.event.EventBridge;
33 import org.apache.cayenne.event.EventBridgeFactory;
34
35 /**
36  * A descriptor used by default service implementation to pass session parameters to the
37  * client. It provides the client with details on how to invoke the service and how to
38  * listen for the server events.
39  *
40  * @since 1.2
41  * @author Andrus Adamchik
42  */

43 public class RemoteSession implements Serializable JavaDoc {
44
45     static final Collection JavaDoc SUBJECTS = Arrays.asList(new Object JavaDoc[] {
46             DataChannel.GRAPH_CHANGED_SUBJECT, DataChannel.GRAPH_FLUSHED_SUBJECT,
47             DataChannel.GRAPH_ROLLEDBACK_SUBJECT
48     });
49
50     protected String JavaDoc name;
51     protected String JavaDoc sessionId;
52
53     protected String JavaDoc eventBridgeFactory;
54     protected Map JavaDoc eventBridgeParameters;
55
56     // private constructor used by hessian deserialization mechanism
57
private RemoteSession() {
58
59     }
60
61     /**
62      * Creates a HessianServiceDescriptor without server events support.
63      */

64     public RemoteSession(String JavaDoc sessionId) {
65         this(sessionId, null, null);
66     }
67
68     /**
69      * Creates a HessianServiceDescriptor. If <code>eventBridgeFactory</code> argument
70      * is not null, session will support server events.
71      */

72     public RemoteSession(String JavaDoc sessionId, String JavaDoc eventBridgeFactory,
73             Map JavaDoc eventBridgeParameters) {
74
75         if (sessionId == null) {
76             throw new IllegalArgumentException JavaDoc("Null sessionId");
77         }
78
79         this.sessionId = sessionId;
80         this.eventBridgeFactory = eventBridgeFactory;
81         this.eventBridgeParameters = eventBridgeParameters;
82     }
83
84     public int hashCode() {
85         return new HashCodeBuilder(71, 5).append(sessionId).toHashCode();
86     }
87
88     /**
89      * Returns server session id. This is often the same as HttpSession id.
90      */

91     public String JavaDoc getSessionId() {
92         return sessionId;
93     }
94
95     /**
96      * Returns session group name. Group name is used for shared sessions.
97      */

98     public String JavaDoc getName() {
99         return name;
100     }
101
102     public void setName(String JavaDoc name) {
103         this.name = name;
104     }
105
106     public boolean isServerEventsEnabled() {
107         return eventBridgeFactory != null;
108     }
109
110     /**
111      * Creates an EventBridge that will listen for server events. Returns null if server
112      * events support is not configured in the descriptor.
113      *
114      * @throws CayenneRuntimeException if EventBridge startup fails for any reason.
115      */

116     public EventBridge createServerEventBridge() throws CayenneRuntimeException {
117
118         if (!isServerEventsEnabled()) {
119             return null;
120         }
121
122         try {
123             EventBridgeFactory factory = (EventBridgeFactory) Class.forName(
124                     eventBridgeFactory).newInstance();
125
126             Map JavaDoc parameters = eventBridgeParameters != null
127                     ? eventBridgeParameters
128                     : Collections.EMPTY_MAP;
129
130             // must use "name", not the sessionId as an external subject for the event bridge
131
return factory.createEventBridge(SUBJECTS, name, parameters);
132         }
133         catch (Exception JavaDoc ex) {
134             throw new CayenneRuntimeException("Error creating EventBridge.", ex);
135         }
136     }
137
138     public String JavaDoc toString() {
139         ToStringBuilder builder = new ToStringBuilder(this)
140                 .append("sessionId", sessionId);
141
142         if (eventBridgeFactory != null) {
143             builder.append("eventBridgeFactory", eventBridgeFactory);
144         }
145
146         if (name != null) {
147             builder.append("name", name);
148         }
149
150         return builder.toString();
151     }
152 }
153
Popular Tags