1 56 package org.objectstyle.cayenne.service; 57 58 import java.util.Collections ; 59 import java.util.HashMap ; 60 import java.util.Map ; 61 62 import javax.servlet.ServletConfig ; 63 import javax.servlet.ServletException ; 64 65 import org.apache.log4j.Logger; 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.access.DataDomain; 68 import org.objectstyle.cayenne.conf.Configuration; 69 import org.objectstyle.cayenne.conf.DefaultConfiguration; 70 import org.objectstyle.cayenne.distribution.ClientMessage; 71 import org.objectstyle.cayenne.distribution.ClientMessageHandler; 72 import org.objectstyle.cayenne.distribution.HessianService; 73 import org.objectstyle.cayenne.util.IDUtil; 74 import org.objectstyle.cayenne.util.Util; 75 76 import com.caucho.services.server.Service; 77 78 87 public class HessianServiceHandler implements HessianService, Service { 88 89 private static final Logger logObj = Logger.getLogger(HessianServiceHandler.class); 90 91 protected Map commandHandlers; 92 protected DataDomain domain; 93 94 97 public void init(ServletConfig config) throws ServletException { 98 99 logObj.debug("CayenneHessianService is starting"); 101 102 Configuration cayenneConfig = new DefaultConfiguration( 103 Configuration.DEFAULT_DOMAIN_FILE); 104 105 try { 106 cayenneConfig.initialize(); 107 cayenneConfig.didInitialize(); 108 } 109 catch (Exception ex) { 110 throw new ServletException ("Error starting Cayenne", ex); 111 } 112 113 this.domain = cayenneConfig.getDomain(); 115 this.commandHandlers = Collections.synchronizedMap(new HashMap ()); 116 logObj.debug("CayenneHessianService started"); 117 } 118 119 122 public void destroy() { 123 logObj.debug("CayenneHessianService destroyed"); 124 this.commandHandlers = null; 125 } 126 127 public String establishSession(String userName, String password) { 128 logObj.debug("CayenneHessianService - session requested by client"); 129 130 132 String id = makeId(); 133 134 synchronized (commandHandlers) { 135 commandHandlers.put(id, new ServerObjectContext(domain)); 136 } 137 138 logObj.debug("CayenneHessianService - established client session: " + id); 139 return id; 140 } 141 142 public Object processMessage(String sessionId, ClientMessage command) 143 throws Throwable { 144 145 logObj.debug("processMessage, sessionId: " + sessionId); 146 147 ClientMessageHandler handler; 148 synchronized (commandHandlers) { 149 handler = (ClientMessageHandler) commandHandlers.get(sessionId); 150 } 151 152 if (handler == null) { 154 throw new CayenneRuntimeException("Invalid sessionId: " + sessionId); 155 } 156 157 try { 159 return command.onReceive(handler); 160 } 161 catch (Throwable th) { 162 logObj.debug("error processing message", Util.unwindException(th)); 163 throw th; 164 } 165 } 166 167 private String makeId() { 168 byte[] bytes = IDUtil.pseudoUniqueByteSequence(32); 169 170 final String digits = "0123456789ABCDEF"; 173 174 StringBuffer buffer = new StringBuffer (bytes.length * 2); 175 176 for (int i = 0; i < bytes.length; i++) { 177 byte b = bytes[i]; 178 buffer.append(digits.charAt((b >>> 4) & 0xF)); 179 buffer.append(digits.charAt(b & 0xF)); 180 } 181 182 return buffer.toString(); 183 } 184 } | Popular Tags |