1 19 20 package org.apache.cayenne.remote; 21 22 import java.io.Serializable ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Map ; 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 43 public class RemoteSession implements Serializable { 44 45 static final Collection SUBJECTS = Arrays.asList(new Object [] { 46 DataChannel.GRAPH_CHANGED_SUBJECT, DataChannel.GRAPH_FLUSHED_SUBJECT, 47 DataChannel.GRAPH_ROLLEDBACK_SUBJECT 48 }); 49 50 protected String name; 51 protected String sessionId; 52 53 protected String eventBridgeFactory; 54 protected Map eventBridgeParameters; 55 56 private RemoteSession() { 58 59 } 60 61 64 public RemoteSession(String sessionId) { 65 this(sessionId, null, null); 66 } 67 68 72 public RemoteSession(String sessionId, String eventBridgeFactory, 73 Map eventBridgeParameters) { 74 75 if (sessionId == null) { 76 throw new IllegalArgumentException ("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 91 public String getSessionId() { 92 return sessionId; 93 } 94 95 98 public String getName() { 99 return name; 100 } 101 102 public void setName(String name) { 103 this.name = name; 104 } 105 106 public boolean isServerEventsEnabled() { 107 return eventBridgeFactory != null; 108 } 109 110 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 parameters = eventBridgeParameters != null 127 ? eventBridgeParameters 128 : Collections.EMPTY_MAP; 129 130 return factory.createEventBridge(SUBJECTS, name, parameters); 132 } 133 catch (Exception ex) { 134 throw new CayenneRuntimeException("Error creating EventBridge.", ex); 135 } 136 } 137 138 public String 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 |