CAY-2372. OSGI, web-module with tests.
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/24b45fd4 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/24b45fd4 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/24b45fd4 Branch: refs/heads/master Commit: 24b45fd479a65cc48c1ed129c65174408c7f9897 Parents: b3a065f Author: Arseni Bulatski <ancars...@gmail.com> Authored: Tue Oct 31 12:08:30 2017 +0300 Committer: Arseni Bulatski <ancars...@gmail.com> Committed: Wed Nov 22 10:20:07 2017 +0300 ---------------------------------------------------------------------- cayenne-client/pom.xml | 7 +- cayenne-rop-server/pom.xml | 68 +++ .../rop/HessianROPSerializationService.java | 69 +++ .../org/apache/cayenne/rop/ROPConstants.java | 28 + .../apache/cayenne/rop/ROPRequestContext.java | 107 ++++ .../cayenne/rop/ROPSerializationService.java | 39 ++ .../java/org/apache/cayenne/rop/ROPServlet.java | 135 +++++ ...rverHessianSerializationServiceProvider.java | 37 ++ .../cayenne/rop/ServerHttpRemoteService.java | 47 ++ .../cayenne/rop/server/ROPServerModule.java | 62 +++ .../apache/cayenne/rop/server/MockModule1.java | 30 ++ .../apache/cayenne/rop/server/MockModule2.java | 32 ++ .../cayenne/rop/server/MockRequestHandler.java | 35 ++ .../server/ROPHessianServlet_ConfigModule.java | 38 ++ .../cayenne/rop/server/ROPServletTest.java | 182 +++++++ .../rop/server/ROPServerModule.java | 62 --- .../configuration/web/CayenneFilter.java | 124 ----- .../configuration/web/RequestHandler.java | 35 -- .../web/SessionContextRequestHandler.java | 88 ---- .../web/StatelessContextRequestHandler.java | 73 --- .../configuration/web/WebConfiguration.java | 191 ------- .../cayenne/configuration/web/WebModule.java | 35 -- .../cayenne/configuration/web/WebUtil.java | 54 -- .../rop/HessianROPSerializationService.java | 69 --- .../org/apache/cayenne/rop/ROPConstants.java | 28 - .../apache/cayenne/rop/ROPRequestContext.java | 107 ---- .../cayenne/rop/ROPSerializationService.java | 39 -- .../java/org/apache/cayenne/rop/ROPServlet.java | 135 ----- ...rverHessianSerializationServiceProvider.java | 37 -- .../cayenne/rop/ServerHttpRemoteService.java | 47 -- .../server/ROPHessianServlet_ConfigModule.java | 38 -- .../rop/server/ROPServletTest.java | 186 ------- .../configuration/web/CayenneFilterTest.java | 179 ------- .../web/CayenneFilter_DispatchModule.java | 29 -- .../CayenneFilter_DispatchRequestHandler.java | 45 -- .../cayenne/configuration/web/MockModule1.java | 30 -- .../cayenne/configuration/web/MockModule2.java | 30 -- .../configuration/web/MockRequestHandler.java | 33 -- .../web/ServletContextHandlerTest.java | 124 ----- .../configuration/web/WebConfigurationTest.java | 214 -------- .../configuration/web/WebModuleTest.java | 40 -- .../cayenne/configuration/web/WebUtilTest.java | 50 -- cayenne-web/hs_err_pid22580.log | 515 ++++++++++++++++++ cayenne-web/hs_err_pid22611.log | 515 ++++++++++++++++++ cayenne-web/hs_err_pid22668.log | 515 ++++++++++++++++++ cayenne-web/hs_err_pid22674.log | 522 +++++++++++++++++++ cayenne-web/hs_err_pid22678.log | 522 +++++++++++++++++++ cayenne-web/hs_err_pid22694.log | 522 +++++++++++++++++++ cayenne-web/hs_err_pid23170.log | 522 +++++++++++++++++++ cayenne-web/hs_err_pid23415.log | 522 +++++++++++++++++++ cayenne-web/hs_err_pid23423.log | 522 +++++++++++++++++++ cayenne-web/pom.xml | 48 +- .../org/apache/cayenne/web/CayenneFilter.java | 124 +++++ ...iguration.server.CayenneServerModuleProvider | 20 + .../apache/cayenne/web/CayenneFilterTest.java | 173 ++++++ .../web/CayenneFilter_DispatchModule.java | 29 ++ .../CayenneFilter_DispatchRequestHandler.java | 45 ++ .../org/apache/cayenne/web/MockModule1.java | 30 ++ .../org/apache/cayenne/web/MockModule2.java | 31 ++ .../apache/cayenne/web/MockRequestHandler.java | 33 ++ .../cayenne/web/ServletContextHandlerTest.java | 124 +++++ .../cayenne/web/WebConfigurationTest.java | 214 ++++++++ .../cayenne/web/WebModuleProviderTest.java | 30 ++ .../org/apache/cayenne/web/WebModuleTest.java | 40 ++ .../org/apache/cayenne/web/WebUtilTest.java | 50 ++ pom.xml | 4 + .../cayenne/tutorial/Http2ROPServlet.java | 6 +- 67 files changed, 6572 insertions(+), 2144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-client/pom.xml ---------------------------------------------------------------------- diff --git a/cayenne-client/pom.xml b/cayenne-client/pom.xml index da8ba0e..c8d6aac 100644 --- a/cayenne-client/pom.xml +++ b/cayenne-client/pom.xml @@ -74,7 +74,12 @@ <artifactId>slf4j-simple</artifactId> <scope>test</scope> </dependency> - </dependencies> + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-rop-server</artifactId> + <version>4.1.M2-SNAPSHOT</version> + </dependency> + </dependencies> <build> <plugins> http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/pom.xml ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/pom.xml b/cayenne-rop-server/pom.xml new file mode 100644 index 0000000..4d810d8 --- /dev/null +++ b/cayenne-rop-server/pom.xml @@ -0,0 +1,68 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>cayenne-parent</artifactId> + <groupId>org.apache.cayenne</groupId> + <version>4.1.M2-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>cayenne-rop-server</artifactId> + <name>cayenne-rop-server</name> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-server</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.caucho</groupId> + <artifactId>hessian</artifactId> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>RELEASE</version> + </dependency> + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-web</artifactId> + <version>${project.version}</version> + </dependency> + <!-- TEST DEPENDENCIES --> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cayenne.build-tools</groupId> + <artifactId>cayenne-test-utilities</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-server</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.mockrunner</groupId> + <artifactId>mockrunner-servlet</artifactId> + </dependency> + </dependencies> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java new file mode 100644 index 0000000..af3c656 --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java @@ -0,0 +1,69 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +import com.caucho.hessian.io.HessianInput; +import com.caucho.hessian.io.HessianOutput; +import com.caucho.hessian.io.SerializerFactory; + +import java.io.*; + +public class HessianROPSerializationService implements ROPSerializationService { + + protected SerializerFactory serializerFactory; + + public HessianROPSerializationService(SerializerFactory serializerFactory) { + this.serializerFactory = serializerFactory; + } + + @Override + public byte[] serialize(Object object) throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + HessianOutput out = new HessianOutput(bytes); + out.setSerializerFactory(serializerFactory); + out.writeObject(object); + out.flush(); + + return bytes.toByteArray(); + } + + @Override + public void serialize(Object object, OutputStream outputStream) throws IOException { + HessianOutput out = new HessianOutput(outputStream); + out.setSerializerFactory(serializerFactory); + out.writeObject(object); + out.flush(); + } + + @Override + public <T> T deserialize(byte[] serializedObject, Class<T> objectClass) throws IOException { + HessianInput in = new HessianInput(new ByteArrayInputStream(serializedObject)); + in.setSerializerFactory(serializerFactory); + + return objectClass.cast(in.readObject()); + } + + @Override + public <T> T deserialize(InputStream input, Class<T> objectClass) throws IOException { + HessianInput in = new HessianInput(input); + in.setSerializerFactory(serializerFactory); + + return objectClass.cast(in.readObject()); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java new file mode 100644 index 0000000..e076eee --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java @@ -0,0 +1,28 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +public class ROPConstants { + + public static final String OPERATION_PARAMETER = "operation"; + public static final String SESSION_NAME_PARAMETER = "session_name"; + + public static final String ESTABLISH_SESSION_OPERATION = "establish_session"; + public static final String ESTABLISH_SHARED_SESSION_OPERATION = "establish_shared_session"; +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java new file mode 100644 index 0000000..126b5d2 --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java @@ -0,0 +1,107 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ + +package org.apache.cayenne.rop; + +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +public class ROPRequestContext { + + private static final ThreadLocal<ROPRequestContext> localContext = new ThreadLocal<>(); + + private String serviceId; + private String objectId; + private ServletRequest request; + private ServletResponse response; + private int count; + + private ROPRequestContext() { + } + + public static void start(String serviceId, String objectId, ServletRequest request, ServletResponse response) { + ROPRequestContext context = localContext.get(); + + if (context == null) { + context = new ROPRequestContext(); + localContext.set(context); + } + + context.serviceId = serviceId; + context.objectId = objectId; + context.request = request; + context.response = response; + context.count++; + } + + public static ROPRequestContext getROPRequestContext() { + return localContext.get(); + } + + public static String getContextServiceId() { + ROPRequestContext context = localContext.get(); + + if (context != null) { + return context.serviceId; + } else { + return null; + } + } + + public static String getContextObjectId() { + ROPRequestContext context = localContext.get(); + + if (context != null) { + return context.objectId; + } else { + return null; + } + } + + public static ServletRequest getContextRequest() { + ROPRequestContext context = localContext.get(); + + if (context != null) { + return context.request; + } else { + return null; + } + } + + public static ServletResponse getContextResponse() { + ROPRequestContext context = localContext.get(); + + if (context != null) { + return context.response; + } else { + return null; + } + } + + public static void end() { + ROPRequestContext context = localContext.get(); + + if (context != null && --context.count == 0) { + context.request = null; + context.response = null; + + localContext.set(null); + } + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPSerializationService.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPSerializationService.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPSerializationService.java new file mode 100644 index 0000000..99cccbf --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPSerializationService.java @@ -0,0 +1,39 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * ROP serialization service + * + * @since 4.0 + */ +public interface ROPSerializationService { + + byte[] serialize(Object object) throws IOException; + + void serialize(Object object, OutputStream outputStream) throws IOException; + + <T> T deserialize(InputStream inputStream, Class<T> objectClass) throws IOException; + + <T> T deserialize(byte[] serializedObject, Class<T> objectClass) throws IOException; +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPServlet.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPServlet.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPServlet.java new file mode 100644 index 0000000..daa854c --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ROPServlet.java @@ -0,0 +1,135 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.rop.server.ROPServerModule; +import org.apache.cayenne.configuration.server.ServerRuntime; +import org.apache.cayenne.web.WebConfiguration; +import org.apache.cayenne.web.WebUtil; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.remote.ClientMessage; +import org.apache.cayenne.remote.RemoteService; +import org.apache.cayenne.remote.RemoteSession; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Collection; +import java.util.Map; + +public class ROPServlet extends HttpServlet { + + protected ServletContext servletContext; + protected RemoteService remoteService; + protected ROPSerializationService serializationService; + + @Override + public void init(ServletConfig configuration) throws ServletException { + + checkAlreadyConfigured(configuration.getServletContext()); + + this.servletContext = configuration.getServletContext(); + + WebConfiguration configAdapter = new WebConfiguration(configuration); + + String configurationLocation = configAdapter.getConfigurationLocation(); + Map<String, String> eventBridgeParameters = configAdapter.getOtherParameters(); + + Collection<Module> modules = configAdapter.createModules(new ROPServerModule( + eventBridgeParameters)); + + ServerRuntime runtime = ServerRuntime.builder() + .addConfig(configurationLocation) + .addModules(modules) + .build(); + + this.remoteService = runtime.getInjector().getInstance(RemoteService.class); + this.serializationService = runtime.getInjector().getInstance(ROPSerializationService.class); + + WebUtil.setCayenneRuntime(servletContext, runtime); + super.init(configuration); + } + + protected void checkAlreadyConfigured(ServletContext context) throws ServletException { + // sanity check + if (WebUtil.getCayenneRuntime(context) != null) { + throw new ServletException( + "CayenneRuntime is already configured in the servlet environment"); + } + } + + @Override + public void destroy() { + super.destroy(); + + CayenneRuntime runtime = WebUtil.getCayenneRuntime(servletContext); + if (runtime != null) { + runtime.shutdown(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String serviceId = req.getPathInfo(); + String objectId = req.getParameter("id"); + + if (objectId == null) { + objectId = req.getParameter("ejbid"); + } + + ROPRequestContext.start(serviceId, objectId, req, resp); + + String operation = req.getParameter(ROPConstants.OPERATION_PARAMETER); + + if (operation != null) { + switch (operation) { + case ROPConstants.ESTABLISH_SESSION_OPERATION: + RemoteSession session = remoteService.establishSession(); + serializationService.serialize(session, resp.getOutputStream()); + break; + case ROPConstants.ESTABLISH_SHARED_SESSION_OPERATION: + String sessionName = req.getParameter(ROPConstants.SESSION_NAME_PARAMETER); + RemoteSession sharedSession = remoteService.establishSharedSession(sessionName); + + serializationService.serialize(sharedSession, resp.getOutputStream()); + break; + default: + throw new ServletException("Unknown operation: " + operation); + } + } else { + Object response = remoteService.processMessage( + serializationService.deserialize(req.getInputStream(), ClientMessage.class)); + + serializationService.serialize(response, resp.getOutputStream()); + } + } catch (RuntimeException | ServletException e) { + throw e; + } catch (Throwable e) { + throw new ServletException(e); + } finally { + ROPRequestContext.end(); + } + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHessianSerializationServiceProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHessianSerializationServiceProvider.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHessianSerializationServiceProvider.java new file mode 100644 index 0000000..ec0c21c --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHessianSerializationServiceProvider.java @@ -0,0 +1,37 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +import org.apache.cayenne.di.DIRuntimeException; +import org.apache.cayenne.di.Provider; +import org.apache.cayenne.remote.hessian.HessianConfig; +import org.apache.cayenne.remote.hessian.service.ServerSerializerFactory; + +public class ServerHessianSerializationServiceProvider implements Provider<ROPSerializationService> { + + public static final String[] SERVER_SERIALIZER_FACTORIES = new String[] { + ServerSerializerFactory.class.getName() + }; + + @Override + public ROPSerializationService get() throws DIRuntimeException { + return new HessianROPSerializationService( + HessianConfig.createFactory(SERVER_SERIALIZER_FACTORIES, null)); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHttpRemoteService.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHttpRemoteService.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHttpRemoteService.java new file mode 100644 index 0000000..9099b04 --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/ServerHttpRemoteService.java @@ -0,0 +1,47 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop; + +import org.apache.cayenne.configuration.Constants; +import org.apache.cayenne.configuration.ObjectContextFactory; +import org.apache.cayenne.di.Inject; +import org.apache.cayenne.remote.service.HttpRemoteService; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.Map; + +public class ServerHttpRemoteService extends HttpRemoteService { + + public ServerHttpRemoteService(@Inject ObjectContextFactory contextFactory, + @Inject(Constants.SERVER_ROP_EVENT_BRIDGE_PROPERTIES_MAP) Map<String, String> eventBridgeProperties) { + super(contextFactory, eventBridgeProperties); + } + + @Override + protected HttpSession getSession(boolean create) { + HttpServletRequest request = (HttpServletRequest) ROPRequestContext.getContextRequest(); + if (request == null) { + throw new IllegalStateException( + "Attempt to access HttpSession outside the request scope."); + } + + return request.getSession(create); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/server/ROPServerModule.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/server/ROPServerModule.java b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/server/ROPServerModule.java new file mode 100644 index 0000000..af77b19 --- /dev/null +++ b/cayenne-rop-server/src/main/java/org/apache/cayenne/rop/server/ROPServerModule.java @@ -0,0 +1,62 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + +import org.apache.cayenne.configuration.Constants; +import org.apache.cayenne.di.Binder; +import org.apache.cayenne.di.MapBuilder; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.remote.RemoteService; +import org.apache.cayenne.rop.ROPSerializationService; +import org.apache.cayenne.rop.ServerHessianSerializationServiceProvider; +import org.apache.cayenne.rop.ServerHttpRemoteService; + +import java.util.Map; + +/** + * A DI module that defines services for the server-side of an ROP application based on + * Caucho Hessian. + * + * @since 3.1 + */ +public class ROPServerModule implements Module { + + protected Map<String, String> eventBridgeProperties; + + /** + * @since 4.0 + */ + public static MapBuilder<String> contributeROPBridgeProperties(Binder binder) { + return binder.bindMap(String.class, Constants.SERVER_ROP_EVENT_BRIDGE_PROPERTIES_MAP); + } + + public ROPServerModule(Map<String, String> eventBridgeProperties) { + this.eventBridgeProperties = eventBridgeProperties; + } + + public void configure(Binder binder) { + + MapBuilder<String> mapBuilder = contributeROPBridgeProperties(binder); + mapBuilder.putAll(eventBridgeProperties); + + binder.bind(RemoteService.class).to(ServerHttpRemoteService.class); + binder.bind(ROPSerializationService.class).toProvider(ServerHessianSerializationServiceProvider.class); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule1.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule1.java b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule1.java new file mode 100644 index 0000000..f10bec6 --- /dev/null +++ b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule1.java @@ -0,0 +1,30 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + +import org.apache.cayenne.di.Binder; +import org.apache.cayenne.di.Module; + + +public class MockModule1 implements Module { + + public void configure(Binder binder) { + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule2.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule2.java b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule2.java new file mode 100644 index 0000000..8dce6a8 --- /dev/null +++ b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockModule2.java @@ -0,0 +1,32 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + + +import org.apache.cayenne.di.Binder; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.web.RequestHandler; + +public class MockModule2 implements Module { + + public void configure(Binder binder) { + binder.bind(RequestHandler.class).to(MockRequestHandler.class); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockRequestHandler.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockRequestHandler.java b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockRequestHandler.java new file mode 100644 index 0000000..b57c513 --- /dev/null +++ b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/MockRequestHandler.java @@ -0,0 +1,35 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + +import org.apache.cayenne.web.RequestHandler; + +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + + +public class MockRequestHandler implements RequestHandler { + + public void requestEnd(ServletRequest request, ServletResponse response) { + } + + public void requestStart(ServletRequest request, ServletResponse response) { + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPHessianServlet_ConfigModule.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPHessianServlet_ConfigModule.java b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPHessianServlet_ConfigModule.java new file mode 100644 index 0000000..b25da89 --- /dev/null +++ b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPHessianServlet_ConfigModule.java @@ -0,0 +1,38 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + +import org.apache.cayenne.DataChannel; +import org.apache.cayenne.access.DataDomain; +import org.apache.cayenne.di.Binder; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.remote.MockRemoteService; +import org.apache.cayenne.remote.RemoteService; + +public class ROPHessianServlet_ConfigModule implements Module { + + public void configure(Binder binder) { + + DataDomain domain = new DataDomain("x"); + binder.bind(DataChannel.class).toInstance(domain); + binder.bind(DataDomain.class).toInstance(domain); + binder.bind(RemoteService.class).to(MockRemoteService.class); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPServletTest.java ---------------------------------------------------------------------- diff --git a/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPServletTest.java b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPServletTest.java new file mode 100644 index 0000000..400b9e4 --- /dev/null +++ b/cayenne-rop-server/src/test/java/org/apache/cayenne/rop/server/ROPServletTest.java @@ -0,0 +1,182 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.rop.server; + +import com.mockrunner.mock.web.MockServletConfig; +import com.mockrunner.mock.web.MockServletContext; +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.configuration.Constants; +import org.apache.cayenne.configuration.server.ServerModule; +import org.apache.cayenne.web.*; +import org.apache.cayenne.di.Key; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.rop.ROPServlet; +import org.junit.After; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.junit.Assert.*; + +public class ROPServletTest { + + private CayenneRuntime runtime; + + @After + public void shutdownRuntime() { + if(runtime != null) { + runtime.shutdown(); + } + } + + @Test + public void testInitWithServletName() throws Exception { + + MockServletConfig config = new MockServletConfig(); + config.setServletName("cayenne-org.apache.cayenne.configuration.rop.server.test-config"); + + MockServletContext context = new MockServletContext(); + config.setServletContext(context); + + ROPServlet servlet = new ROPServlet(); + + assertNull(WebUtil.getCayenneRuntime(context)); + servlet.init(config); + + runtime = WebUtil.getCayenneRuntime(context); + assertNotNull(runtime); + + List<String> locations = runtime.getInjector().getInstance( + Key.getListOf(String.class, Constants.SERVER_PROJECT_LOCATIONS_LIST)); + assertEquals(Arrays.asList("cayenne-org.apache.cayenne.configuration.rop.server.test-config.xml"), locations); + } + + @Test + public void testInitWithLocation() throws Exception { + + String location = "cayenne-org.apache.cayenne.configuration.rop.server.test-config.xml"; + MockServletConfig config = new MockServletConfig(); + config.setServletName("abc"); + config.setInitParameter("configuration-location", location); + + MockServletContext context = new MockServletContext(); + config.setServletContext(context); + + ROPServlet servlet = new ROPServlet(); + servlet.init(config); + + runtime = WebUtil.getCayenneRuntime(context); + assertNotNull(runtime); + List<String> locations = runtime.getInjector().getInstance( + Key.getListOf(String.class, Constants.SERVER_PROJECT_LOCATIONS_LIST)); + + assertEquals(Arrays.asList(location), locations); + } + +// @Test +// public void testInitWithStandardModules() throws Exception { +// +// String name = "cayenne-org.apache.cayenne.configuration.rop.server.test-config"; +// +// MockServletConfig config = new MockServletConfig(); +// config.setServletName(name); +// +// MockServletContext context = new MockServletContext(); +// config.setServletContext(context); +// +// ROPServlet servlet = new ROPServlet(); +// servlet.init(config); +// +// runtime = WebUtil.getCayenneRuntime(context); +// assertNotNull(runtime); +// +// List<String> locations = runtime.getInjector().getInstance( +// Key.getListOf(String.class, Constants.SERVER_PROJECT_LOCATIONS_LIST)); +// +// assertEquals(Arrays.asList(name + ".xml"), locations); +// +// Collection<Module> modules = runtime.getModules(); +// assertEquals(3, modules.size()); +// Object[] marray = modules.toArray(); +// +// assertTrue(marray[0] instanceof ServerModule); +// // [2] is an inner class +// assertTrue(marray[1] instanceof ROPServerModule); +// } + +// @Test +// public void testInitWithExtraModules() throws Exception { +// +// String name = "cayenne-org.apache.cayenne.configuration.rop.server.test-config"; +// +// MockServletConfig config = new MockServletConfig(); +// config.setServletName(name); +// config.setInitParameter("extra-modules", MockModule1.class.getName() + "," + MockModule2.class.getName()); +// +// MockServletContext context = new MockServletContext(); +// config.setServletContext(context); +// +// ROPServlet servlet = new ROPServlet(); +// servlet.init(config); +// +// runtime = WebUtil.getCayenneRuntime(context); +// assertNotNull(runtime); +// +// Collection<Module> modules = runtime.getModules(); +// assertEquals(5, modules.size()); +// +// Object[] marray = modules.toArray(); +// +// assertTrue(marray[0] instanceof ServerModule); +// // [1] is an inner class +// assertTrue(marray[1] instanceof ROPServerModule); +// assertTrue(marray[2] instanceof MockModule1); +// assertTrue(marray[3] instanceof MockModule2); +// +// RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class); +// assertTrue(handler instanceof MockRequestHandler); +// } + +// @Test +// public void testInitHessianService() throws Exception { +// +// MockServletConfig config = new MockServletConfig(); +// config.setServletName("abc"); +// +// MockServletContext context = new MockServletContext(); +// config.setServletContext(context); +// config.setInitParameter("extra-modules", ROPHessianServlet_ConfigModule.class.getName()); +// +// ROPServlet servlet = new ROPServlet(); +// +// servlet.init(config); +// runtime = WebUtil.getCayenneRuntime(context); +// Collection<Module> modules = runtime.getModules(); +// assertEquals(4, modules.size()); +// +// Object[] marray = modules.toArray(); +// +// assertTrue(marray[2] instanceof ROPHessianServlet_ConfigModule); +// +// // TODO: mock servlet request to check that the right service instance +// // is invoked +// } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/rop/server/ROPServerModule.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/rop/server/ROPServerModule.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/rop/server/ROPServerModule.java deleted file mode 100644 index ad93183..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/rop/server/ROPServerModule.java +++ /dev/null @@ -1,62 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.rop.server; - -import org.apache.cayenne.configuration.Constants; -import org.apache.cayenne.di.Binder; -import org.apache.cayenne.di.MapBuilder; -import org.apache.cayenne.di.Module; -import org.apache.cayenne.remote.RemoteService; -import org.apache.cayenne.rop.ROPSerializationService; -import org.apache.cayenne.rop.ServerHessianSerializationServiceProvider; -import org.apache.cayenne.rop.ServerHttpRemoteService; - -import java.util.Map; - -/** - * A DI module that defines services for the server-side of an ROP application based on - * Caucho Hessian. - * - * @since 3.1 - */ -public class ROPServerModule implements Module { - - protected Map<String, String> eventBridgeProperties; - - /** - * @since 4.0 - */ - public static MapBuilder<String> contributeROPBridgeProperties(Binder binder) { - return binder.bindMap(String.class, Constants.SERVER_ROP_EVENT_BRIDGE_PROPERTIES_MAP); - } - - public ROPServerModule(Map<String, String> eventBridgeProperties) { - this.eventBridgeProperties = eventBridgeProperties; - } - - public void configure(Binder binder) { - - MapBuilder<String> mapBuilder = contributeROPBridgeProperties(binder); - mapBuilder.putAll(eventBridgeProperties); - - binder.bind(RemoteService.class).to(ServerHttpRemoteService.class); - binder.bind(ROPSerializationService.class).toProvider(ServerHessianSerializationServiceProvider.class); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java deleted file mode 100644 index 5637146..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java +++ /dev/null @@ -1,124 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import org.apache.cayenne.configuration.CayenneRuntime; -import org.apache.cayenne.configuration.server.ServerModule; -import org.apache.cayenne.configuration.server.ServerRuntime; -import org.apache.cayenne.di.Module; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; - -/** - * A filter that creates a Cayenne server runtime, possibly including custom modules. By - * default runtime includes {@link ServerModule} and {@link WebModule}. Any custom modules - * are loaded after the two standard ones to allow custom service overrides. Filter - * initialization parameters: - * <ul> - * <li>configuration-location - (optional) a name of Cayenne configuration XML file that - * will be used to load Cayenne stack. If missing, the filter name will be used to derive - * the location. ".xml" extension will be appended to the filter name to get the location, - * so a filter named "cayenne-foo" will result in location "cayenne-foo.xml". - * <li>extra-modules - (optional) a comma or space-separated list of class names, with - * each class implementing {@link Module} interface. These are the custom modules loaded - * after the two standard ones that allow users to override any Cayenne runtime aspects, - * e.g. {@link RequestHandler}. Each custom module must have a no-arg constructor. - * </ul> - * <p> - * CayenneFilter is a great utility to quickly start a Cayenne application. More advanced - * apps most likely will not use it, relying on their own configuration mechanism (such as - * Guice, Spring, etc.) - * - * @since 3.1 - */ -public class CayenneFilter implements Filter { - - protected ServletContext servletContext; - - public void init(FilterConfig config) throws ServletException { - - checkAlreadyConfigured(config.getServletContext()); - - this.servletContext = config.getServletContext(); - - WebConfiguration configAdapter = new WebConfiguration(config); - - String configurationLocation = configAdapter.getConfigurationLocation(); - Collection<Module> modules = configAdapter.createModules(new WebModule()); - modules.addAll(getAdditionalModules()); - - ServerRuntime runtime = ServerRuntime.builder() - .addConfig(configurationLocation) - .addModules(modules).build(); - - WebUtil.setCayenneRuntime(config.getServletContext(), runtime); - } - - /** - * Subclasses may override this to specify additional modules that should be included when creating the CayenneRuntime - * (in addition to those specified in the web.xml file). - * - * @since 4.0 - */ - protected Collection<Module> getAdditionalModules() { - return new ArrayList<>(); - } - - protected void checkAlreadyConfigured(ServletContext context) throws ServletException { - // sanity check - if (WebUtil.getCayenneRuntime(context) != null) { - throw new ServletException( - "CayenneRuntime is already configured in the servlet environment"); - } - } - - public void destroy() { - CayenneRuntime runtime = WebUtil.getCayenneRuntime(servletContext); - - if (runtime != null) { - runtime.shutdown(); - } - } - - public void doFilter( - ServletRequest request, - ServletResponse response, - FilterChain chain) throws IOException, ServletException { - - CayenneRuntime runtime = WebUtil.getCayenneRuntime(servletContext); - RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class); - - handler.requestStart(request, response); - try { - chain.doFilter(request, response); - } - finally { - handler.requestEnd(request, response); - } - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java deleted file mode 100644 index 86f41cc..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java +++ /dev/null @@ -1,35 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -/** - * A service invoked by {@link CayenneFilter} that provides a callback mechanism to bind - * appropriate ObjectContext to the request thread, handle transactions, etc. - * - * @since 3.1 - */ -public interface RequestHandler { - - void requestStart(ServletRequest request, ServletResponse response); - - void requestEnd(ServletRequest request, ServletResponse response); -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java deleted file mode 100644 index cf26fc9..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java +++ /dev/null @@ -1,88 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -import org.apache.cayenne.BaseContext; -import org.apache.cayenne.ObjectContext; -import org.apache.cayenne.configuration.CayenneRuntime; -import org.apache.cayenne.configuration.ObjectContextFactory; -import org.apache.cayenne.di.Inject; -import org.apache.cayenne.di.Injector; - -/** - * Default implementation of the {@link RequestHandler} that stores per-user - * {@link ObjectContext} in a web session and binds it to request thread. Note that using - * this handler would force {@link HttpSession} creation, that may not be desirable in - * many cases. Also session-bound context may result in a race condition with two user - * requests updating the same persistent objects in parallel. - * <p> - * User applications in most cases should provide a custom RequestHandler that implements - * a smarter app-specific strategy for providing ObjectContext. - * <p> - * For stateless (per request) context creation use {@link StatelessContextRequestHandler}. - * - * @since 3.1 - */ -public class SessionContextRequestHandler implements RequestHandler { - - static final String SESSION_CONTEXT_KEY = SessionContextRequestHandler.class - .getName() - + ".SESSION_CONTEXT"; - - // using injector to lookup services instead of injecting them directly for lazy - // startup and "late binding" - @Inject - private Injector injector; - - public void requestStart(ServletRequest request, ServletResponse response) { - - CayenneRuntime.bindThreadInjector(injector); - - if (request instanceof HttpServletRequest) { - - // this forces session creation if it does not exist yet - HttpSession session = ((HttpServletRequest) request).getSession(); - - ObjectContext context; - synchronized (session) { - context = (ObjectContext) session.getAttribute(SESSION_CONTEXT_KEY); - - if (context == null) { - context = injector - .getInstance(ObjectContextFactory.class) - .createContext(); - session.setAttribute(SESSION_CONTEXT_KEY, context); - } - } - - BaseContext.bindThreadObjectContext(context); - } - } - - public void requestEnd(ServletRequest request, ServletResponse response) { - CayenneRuntime.bindThreadInjector(null); - BaseContext.bindThreadObjectContext(null); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/StatelessContextRequestHandler.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/StatelessContextRequestHandler.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/StatelessContextRequestHandler.java deleted file mode 100644 index ef54465..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/StatelessContextRequestHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import org.apache.cayenne.BaseContext; -import org.apache.cayenne.ObjectContext; -import org.apache.cayenne.configuration.CayenneRuntime; -import org.apache.cayenne.configuration.ObjectContextFactory; -import org.apache.cayenne.di.Inject; -import org.apache.cayenne.di.Injector; -import org.apache.cayenne.di.Module; - -/** - * <p>Stateless implementation of {@link RequestHandler} that creates a new - * {@link ObjectContext} for each request and binds it to the request thread. - * <p> - * This is an alternative to the session-based request handler - * {@link SessionContextRequestHandler} which is the default. - * <p> - * The request handler can be used by injecting it with a custom @{link Module}, like so: - * -<pre><code> -import org.apache.cayenne.configuration.web.RequestHandler; -import org.apache.cayenne.configuration.web.StatelessContextRequestHandler; -import org.apache.cayenne.di.Binder; -import org.apache.cayenne.di.Module; -public class AppModule implements Module { - public void configure(Binder binder) { - binder.bind(RequestHandler.class).to(StatelessContextRequestHandler.class); - } -} -</code></pre> - * - * @since 4.0 - */ -public class StatelessContextRequestHandler implements RequestHandler { - - // using injector to lookup services instead of injecting them directly for lazy - // startup and "late binding" - @Inject - private Injector injector; - - public void requestStart(ServletRequest request, ServletResponse response) { - CayenneRuntime.bindThreadInjector(injector); - ObjectContext context = injector.getInstance(ObjectContextFactory.class).createContext(); - BaseContext.bindThreadObjectContext(context); - } - - public void requestEnd(ServletRequest request, ServletResponse response) { - CayenneRuntime.bindThreadInjector(null); - BaseContext.bindThreadObjectContext(null); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebConfiguration.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebConfiguration.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebConfiguration.java deleted file mode 100644 index f349ac7..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebConfiguration.java +++ /dev/null @@ -1,191 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import org.apache.cayenne.di.Module; -import org.apache.cayenne.util.Util; - -import javax.servlet.FilterConfig; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.StringTokenizer; - -/** - * A class that provides access to common Cayenne web configuration parameters retrieved - * either from a FilterConfig or a ServletConfig configuration. - * - * @since 3.1 - */ -public class WebConfiguration { - - static final String CONFIGURATION_LOCATION_PARAMETER = "configuration-location"; - static final String EXTRA_MODULES_PARAMETER = "extra-modules"; - - private FilterConfig configuration; - - public WebConfiguration(final ServletConfig servletConfiguration) { - this.configuration = new FilterConfig() { - - public ServletContext getServletContext() { - return servletConfiguration.getServletContext(); - } - - @SuppressWarnings("all") - public Enumeration getInitParameterNames() { - return servletConfiguration.getInitParameterNames(); - } - - public String getInitParameter(String name) { - return servletConfiguration.getInitParameter(name); - } - - public String getFilterName() { - return servletConfiguration.getServletName(); - } - }; - } - - public WebConfiguration(FilterConfig filterConfiguration) { - this.configuration = filterConfiguration; - } - - /** - * Returns a non-null location of an XML Cayenne configuration, extracted from the - * filter or servlet configuration parameters. - */ - public String getConfigurationLocation() { - String configurationLocation = configuration - .getInitParameter(CONFIGURATION_LOCATION_PARAMETER); - - if (configurationLocation != null) { - return configurationLocation; - } - - String name = configuration.getFilterName(); - - if (name == null) { - return null; - } - - if (!name.endsWith(".xml")) { - name = name + ".xml"; - } - - return name; - } - - /** - * Creates and returns a collection of modules made of provided standard modules and - * extra custom modules specified via an optional "extra-modules" init parameter. The - * value of the parameter is expected to be a comma or space-separated list of class - * names, with each class implementing {@link Module} interface. Each custom module - * must have a no-arg constructor. If a module of this type is already in the modules - * collection, such module is skipped. - */ - public Collection<Module> createModules(Module... standardModules) - throws ServletException { - - Set<String> existingModules = new HashSet<>(); - Collection<Module> modules = new ArrayList<>(); - - if (standardModules != null) { - for (Module module : standardModules) { - modules.add(module); - existingModules.add(module.getClass().getName()); - } - } - - String extraModules = configuration.getInitParameter(EXTRA_MODULES_PARAMETER); - if (extraModules != null) { - - StringTokenizer toks = new StringTokenizer(extraModules, ", \n\r"); - while (toks.hasMoreTokens()) { - String moduleName = toks.nextToken(); - - if (!existingModules.add(moduleName)) { - continue; - } - - Module module; - try { - module = (Module) Util.getJavaClass(moduleName).newInstance(); - } - catch (Exception e) { - String message = String - .format( - "Error instantiating custom DI module '%s' by filter '%s': %s", - moduleName, - getClass().getName(), - e.getMessage()); - throw new ServletException(message, e); - } - - modules.add(module); - } - } - - return modules; - } - - /** - * Returns a map of all init parameters from the underlying FilterConfig or - * ServletConfig object. - */ - public Map<String, String> getParameters() { - Enumeration<?> en = configuration.getInitParameterNames(); - - if (!en.hasMoreElements()) { - return Collections.EMPTY_MAP; - } - - Map<String, String> parameters = new HashMap<>(); - while (en.hasMoreElements()) { - String key = (String) en.nextElement(); - parameters.put(key, configuration.getInitParameter(key)); - } - - return parameters; - } - - /** - * Returns servlet or filter init parameters, excluding those recognized by - * WebConfiguration. Namely 'configuration-location' and 'extra-modules' parameters - * are removed from the returned map. - */ - public Map<String, String> getOtherParameters() { - - Map<String, String> parameters = getParameters(); - - if (!parameters.isEmpty()) { - parameters.remove(CONFIGURATION_LOCATION_PARAMETER); - parameters.remove(EXTRA_MODULES_PARAMETER); - } - - return parameters; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebModule.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebModule.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebModule.java deleted file mode 100644 index 292df4c..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebModule.java +++ /dev/null @@ -1,35 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import org.apache.cayenne.di.Binder; -import org.apache.cayenne.di.Module; - -/** - * @since 3.1 - */ -public class WebModule implements Module { - - public void configure(Binder binder) { - binder - .bind(RequestHandler.class) - .to(SessionContextRequestHandler.class) - .withoutScope(); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java deleted file mode 100644 index dfeb3c4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java +++ /dev/null @@ -1,54 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.configuration.web; - -import javax.servlet.ServletContext; - -import org.apache.cayenne.configuration.CayenneRuntime; - -/** - * A helper class to retrieve and store {@link CayenneRuntime} in the - * {@link ServletContext}. All Cayenne web configuration objects, such as - * {@link CayenneFilter} and {@link org.apache.cayenne.rop.ROPServlet}, are using this class to access - * runtime. - * - * @since 3.1 - */ -public class WebUtil { - - static final String CAYENNE_RUNTIME_KEY = WebUtil.class.getName() - + ".CAYENNE_RUNTIME"; - - /** - * Retrieves CayenneRuntime previously stored in provided context via - * {@link #setCayenneRuntime(ServletContext, CayenneRuntime)}. May return null if no - * runtime was stored. - */ - public static CayenneRuntime getCayenneRuntime(ServletContext context) { - return (CayenneRuntime) context.getAttribute(CAYENNE_RUNTIME_KEY); - } - - /** - * Stores {@link CayenneRuntime} in the servlet context. It can be later retrieve via - * {@link #getCayenneRuntime(ServletContext)}. - */ - public static void setCayenneRuntime(ServletContext context, CayenneRuntime runtime) { - context.setAttribute(CAYENNE_RUNTIME_KEY, runtime); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java b/cayenne-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java deleted file mode 100644 index af3c656..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/rop/HessianROPSerializationService.java +++ /dev/null @@ -1,69 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.rop; - -import com.caucho.hessian.io.HessianInput; -import com.caucho.hessian.io.HessianOutput; -import com.caucho.hessian.io.SerializerFactory; - -import java.io.*; - -public class HessianROPSerializationService implements ROPSerializationService { - - protected SerializerFactory serializerFactory; - - public HessianROPSerializationService(SerializerFactory serializerFactory) { - this.serializerFactory = serializerFactory; - } - - @Override - public byte[] serialize(Object object) throws IOException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - HessianOutput out = new HessianOutput(bytes); - out.setSerializerFactory(serializerFactory); - out.writeObject(object); - out.flush(); - - return bytes.toByteArray(); - } - - @Override - public void serialize(Object object, OutputStream outputStream) throws IOException { - HessianOutput out = new HessianOutput(outputStream); - out.setSerializerFactory(serializerFactory); - out.writeObject(object); - out.flush(); - } - - @Override - public <T> T deserialize(byte[] serializedObject, Class<T> objectClass) throws IOException { - HessianInput in = new HessianInput(new ByteArrayInputStream(serializedObject)); - in.setSerializerFactory(serializerFactory); - - return objectClass.cast(in.readObject()); - } - - @Override - public <T> T deserialize(InputStream input, Class<T> objectClass) throws IOException { - HessianInput in = new HessianInput(input); - in.setSerializerFactory(serializerFactory); - - return objectClass.cast(in.readObject()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java b/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java deleted file mode 100644 index e076eee..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPConstants.java +++ /dev/null @@ -1,28 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.rop; - -public class ROPConstants { - - public static final String OPERATION_PARAMETER = "operation"; - public static final String SESSION_NAME_PARAMETER = "session_name"; - - public static final String ESTABLISH_SESSION_OPERATION = "establish_session"; - public static final String ESTABLISH_SHARED_SESSION_OPERATION = "establish_shared_session"; -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/24b45fd4/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java b/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java deleted file mode 100644 index 126b5d2..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/rop/ROPRequestContext.java +++ /dev/null @@ -1,107 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ - -package org.apache.cayenne.rop; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -public class ROPRequestContext { - - private static final ThreadLocal<ROPRequestContext> localContext = new ThreadLocal<>(); - - private String serviceId; - private String objectId; - private ServletRequest request; - private ServletResponse response; - private int count; - - private ROPRequestContext() { - } - - public static void start(String serviceId, String objectId, ServletRequest request, ServletResponse response) { - ROPRequestContext context = localContext.get(); - - if (context == null) { - context = new ROPRequestContext(); - localContext.set(context); - } - - context.serviceId = serviceId; - context.objectId = objectId; - context.request = request; - context.response = response; - context.count++; - } - - public static ROPRequestContext getROPRequestContext() { - return localContext.get(); - } - - public static String getContextServiceId() { - ROPRequestContext context = localContext.get(); - - if (context != null) { - return context.serviceId; - } else { - return null; - } - } - - public static String getContextObjectId() { - ROPRequestContext context = localContext.get(); - - if (context != null) { - return context.objectId; - } else { - return null; - } - } - - public static ServletRequest getContextRequest() { - ROPRequestContext context = localContext.get(); - - if (context != null) { - return context.request; - } else { - return null; - } - } - - public static ServletResponse getContextResponse() { - ROPRequestContext context = localContext.get(); - - if (context != null) { - return context.response; - } else { - return null; - } - } - - public static void end() { - ROPRequestContext context = localContext.get(); - - if (context != null && --context.count == 0) { - context.request = null; - context.response = null; - - localContext.set(null); - } - } -}