1 19 20 package org.apache.cayenne.remote.service; 21 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.DataChannel; 28 import org.apache.cayenne.access.ClientServerChannel; 29 import org.apache.cayenne.access.DataDomain; 30 import org.apache.cayenne.conf.Configuration; 31 import org.apache.cayenne.conf.DefaultConfiguration; 32 import org.apache.cayenne.remote.ClientMessage; 33 import org.apache.cayenne.remote.RemoteService; 34 import org.apache.cayenne.remote.RemoteSession; 35 import org.apache.cayenne.util.Util; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 46 public abstract class BaseRemoteService implements RemoteService { 47 48 public static final String EVENT_BRIDGE_FACTORY_PROPERTY = "cayenne.RemoteService.EventBridge.factory"; 49 50 private final Log logObj = LogFactory.getLog(BaseRemoteService.class); 52 53 protected DataDomain domain; 54 55 protected String eventBridgeFactoryName; 56 protected Map eventBridgeParameters; 57 58 public String getEventBridgeFactoryName() { 59 return eventBridgeFactoryName; 60 } 61 62 public Map getEventBridgeParameters() { 63 return eventBridgeParameters != null ? Collections 64 .unmodifiableMap(eventBridgeParameters) : Collections.EMPTY_MAP; 65 } 66 67 71 protected void initService(Map properties) throws CayenneRuntimeException { 72 73 logObj.debug(this.getClass().getName() + " is starting"); 75 76 initCayenneStack(properties); 77 initEventBridgeParameters(properties); 78 79 logObj.debug(getClass().getName() + " started"); 80 } 81 82 86 protected void destroyService() { 87 logObj.debug(getClass().getName() + " destroyed"); 88 } 89 90 93 public DataChannel getRootChannel() { 94 return domain; 95 } 96 97 100 protected abstract ServerSession createServerSession(); 101 102 107 protected abstract ServerSession createServerSession(String name); 108 109 114 protected abstract ServerSession getServerSession(); 115 116 public RemoteSession establishSession() { 117 logObj.debug("Session requested by client"); 118 119 RemoteSession session = createServerSession().getSession(); 120 121 logObj.debug("Established client session: " + session); 122 return session; 123 } 124 125 public RemoteSession establishSharedSession(String name) { 126 logObj.debug("Shared session requested by client. Group name: " + name); 127 128 if (name == null) { 129 throw new CayenneRuntimeException("Invalid null shared session name"); 130 } 131 132 return createServerSession(name).getSession(); 133 } 134 135 public Object processMessage(ClientMessage message) throws Throwable { 136 137 if (message == null) { 138 throw new IllegalArgumentException ("Null client message."); 139 } 140 141 ServerSession handler = getServerSession(); 142 143 if (handler == null) { 144 throw new MissingSessionException("No session associated with request."); 145 } 146 147 logObj.debug("processMessage, sessionId: " + handler.getSession().getSessionId()); 148 149 try { 151 return DispatchHelper.dispatch(handler.getChannel(), message); 152 } 153 catch (Throwable th) { 154 th = Util.unwindException(th); 155 logObj.info("error processing message", th); 156 157 160 StringBuffer buffer = new StringBuffer (); 161 buffer.append("Exception processing message ").append( 162 message.getClass().getName()); 163 164 String exceptionText = th.getLocalizedMessage(); 165 if (exceptionText != null) { 166 buffer.append(". Root cause: ").append(exceptionText); 167 } 168 169 throw new CayenneRuntimeException(buffer.toString()); 170 } 171 } 172 173 protected RemoteSession createRemoteSession( 174 String sessionId, 175 String name, 176 boolean enableEvents) { 177 RemoteSession session = (enableEvents) ? new RemoteSession( 178 sessionId, 179 eventBridgeFactoryName, 180 eventBridgeParameters) : new RemoteSession(sessionId); 181 182 session.setName(name); 183 return session; 184 } 185 186 194 protected DataChannel createChannel() { 195 return new ClientServerChannel(domain); 196 } 197 198 201 protected void initCayenneStack(Map properties) { 202 Configuration cayenneConfig = new DefaultConfiguration( 203 Configuration.DEFAULT_DOMAIN_FILE); 204 205 try { 206 cayenneConfig.initialize(); 207 cayenneConfig.didInitialize(); 208 } 209 catch (Exception ex) { 210 throw new CayenneRuntimeException("Error starting Cayenne", ex); 211 } 212 213 this.domain = cayenneConfig.getDomain(); 216 } 217 218 221 protected void initEventBridgeParameters(Map properties) { 222 String eventBridgeFactoryName = (String ) properties 223 .get(BaseRemoteService.EVENT_BRIDGE_FACTORY_PROPERTY); 224 225 if (eventBridgeFactoryName != null) { 226 227 Map eventBridgeParameters = new HashMap (properties); 228 eventBridgeParameters.remove(BaseRemoteService.EVENT_BRIDGE_FACTORY_PROPERTY); 229 230 this.eventBridgeFactoryName = eventBridgeFactoryName; 231 this.eventBridgeParameters = eventBridgeParameters; 232 } 233 } 234 } 235 | Popular Tags |