Repository: olingo-odata4 Updated Branches: refs/heads/OLINGO-549-ODataV4-JPA 9ac7b1931 -> e3a20bb9e
[OLINGO-619] Initial version of ODataJPAServlet Signed-off-by: Chandan V A <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/e3a20bb9 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/e3a20bb9 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/e3a20bb9 Branch: refs/heads/OLINGO-549-ODataV4-JPA Commit: e3a20bb9e8b309fcbaa6b2a7cb623c2f1a5e1a73 Parents: 9ac7b19 Author: Chandan V A <[email protected]> Authored: Sun Aug 23 12:52:24 2015 +0530 Committer: Chandan V A <[email protected]> Committed: Sun Aug 23 12:52:24 2015 +0530 ---------------------------------------------------------------------- ext/odata-jpa/odata-jpa-api/pom.xml | 6 +- .../jpa/api/ODataJPAAbstractEdmProvider.java | 25 ++++++ .../apache/olingo/jpa/api/ODataJPAContext.java | 39 +++++++++ .../olingo/jpa/api/ODataJPAProcessor.java | 25 ++++++ .../apache/olingo/jpa/api/ODataJPAService.java | 51 +++++++++++ .../apache/olingo/jpa/api/ODataJPAServlet.java | 92 +++++++++++++++++++- .../olingo/jpa/api/ODataJPATransaction.java | 32 +++++++ .../jpa/api/exception/ODataJPAException.java | 37 ++++++++ .../olingo/jpa/api/factory/ODataJPAFactory.java | 42 +++++++++ 9 files changed, 347 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/pom.xml ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/pom.xml b/ext/odata-jpa/odata-jpa-api/pom.xml index 1e154b8..0dd4a24 100644 --- a/ext/odata-jpa/odata-jpa-api/pom.xml +++ b/ext/odata-jpa/odata-jpa-api/pom.xml @@ -21,7 +21,7 @@ </parent> <artifactId>odata-jpa-api</artifactId> - <name>${project-artifactId}</name> + <name>${project.artifactId}</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> @@ -41,5 +41,9 @@ <groupId>org.apache.olingo</groupId> <artifactId>odata-commons-api</artifactId> </dependency> + <dependency> + <groupId>org.apache.olingo</groupId> + <artifactId>odata-server-api</artifactId> + </dependency> </dependencies> </project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAAbstractEdmProvider.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAAbstractEdmProvider.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAAbstractEdmProvider.java new file mode 100644 index 0000000..f2ec2d3 --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAAbstractEdmProvider.java @@ -0,0 +1,25 @@ +/* + * 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.olingo.jpa.api; + +import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider; + +public abstract class ODataJPAAbstractEdmProvider extends CsdlAbstractEdmProvider { + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAContext.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAContext.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAContext.java new file mode 100644 index 0000000..575b5de --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAContext.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.olingo.jpa.api; + +/** + * This class does the compilation of context objects required for OData JPA + * Runtime. The context object should be properly initialized with values else + * the behavior of processor and EDM provider can result in exception. + * + * Following are the mandatory parameter to be set into the context object + * <ol> + * <li>Persistence Unit Name</li> + * <li>An instance of Java Persistence Entity Manager Factory</li> + * </ol> + * + */ +public interface ODataJPAContext { + + String getPersistenceUnitName(); + + void setPersistenceUnitName(); + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAProcessor.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAProcessor.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAProcessor.java new file mode 100644 index 0000000..659b1b5 --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAProcessor.java @@ -0,0 +1,25 @@ +/* + * 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.olingo.jpa.api; + +import org.apache.olingo.server.api.processor.EntityCollectionProcessor; + +public interface ODataJPAProcessor extends EntityCollectionProcessor { + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAService.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAService.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAService.java new file mode 100644 index 0000000..a4e93e5 --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAService.java @@ -0,0 +1,51 @@ +/* + * 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.olingo.jpa.api; + +public class ODataJPAService { + + private String persistenceUnitName; + private ODataJPAAbstractEdmProvider odataJPAEdmProvider; + private ODataJPAProcessor odataJPAProcessor; + + public ODataJPAProcessor getODataJPAProcessor() { + return odataJPAProcessor; + } + + public void setODataJPAProcessor(ODataJPAProcessor odataJPAProcessor) { + this.odataJPAProcessor = odataJPAProcessor; + } + + public ODataJPAAbstractEdmProvider getODataJPAEdmProvider() { + return odataJPAEdmProvider; + } + + public void setODataJPAEdmProvider(ODataJPAAbstractEdmProvider odataJPAEdmProvider) { + this.odataJPAEdmProvider = odataJPAEdmProvider; + } + + public String getPersistenceUnitName() { + return persistenceUnitName; + } + + public void setPersistenceUnitName(String pUnitName) { + this.persistenceUnitName = pUnitName; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAServlet.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAServlet.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAServlet.java index 6cf038f..ee25548 100644 --- a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAServlet.java +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPAServlet.java @@ -18,13 +18,103 @@ */ package org.apache.olingo.jpa.api; +import java.io.IOException; +import java.util.ArrayList; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.olingo.jpa.api.exception.ODataJPAException; +import org.apache.olingo.jpa.api.factory.ODataJPAFactory; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataHttpHandler; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.edmx.EdmxReference; -public class ODataJPAServlet extends HttpServlet { +/** + * The class {@link org.apache.olingo.jpa.api.ODataJPAServlet} is an abstract Servlet that + * <ul> + * <li> Creates an instance of Edm Provider</li> + * <li> Creates an instance of OData processor</li> + * <li> Handles OData requests and response</li> + * </ul> + * + * <p>Applications exposing JPA application as OData application can extend this servlet and should implement the + * abstract method <b>initializeODataJPAContext</b> + * + * <p>Applications can pass the persistence unit name which needs to be transformed into OData service as + * a value for servlet's init parameter - <b>persitence.unit</b> in Web application descriptor (web.xml)</p> + * + * <p> + * Applications can also pass their own ODataJPAProcessor by overriding the method getODataJPAService in this servlet + * and return an instance of {@link org.apache.olingo.jpa.api.ODataJPAService} </p> + */ +public abstract class ODataJPAServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; + private static final String PERSISTENCE_UNIT_NAME = "persistence.unit"; + private static final String ODATA_JPA_TRANSACTION = "odata.jpa.transaction"; + private String persistenceUnitName; + private String transaction; + private ODataJPAService odataJPAService = null; + private ODataJPAContext odataJPAContext = null; + private ODataJPAFactory odataJPAFactory = null; + + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + setPersistenceUnitName(config.getInitParameter(PERSISTENCE_UNIT_NAME)); + transaction = config.getInitParameter(ODATA_JPA_TRANSACTION); + odataJPAFactory = ODataJPAFactory.newInstance(); + try { + odataJPAService = getODataJPAService(); + } catch (ODataJPAException e) { + throw new ServletException(e); + } + } + + @Override + protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + OData odata = OData.newInstance(); + ServiceMetadata edm = + odata.createServiceMetadata(odataJPAService.getODataJPAEdmProvider(), new ArrayList<EdmxReference>()); + ODataHttpHandler handler = odata.createHandler(edm); + handler.register(odataJPAService.getODataJPAProcessor()); + handler.process(req, resp); + } + + protected ODataJPAService getODataJPAService() throws ODataJPAException { + odataJPAService = new ODataJPAService(); + odataJPAContext = getODataJPAContext(); + initializeODataJPAContext(odataJPAContext); + odataJPAService.setODataJPAEdmProvider(odataJPAFactory.getODataJPAEdmProvider(persistenceUnitName)); + odataJPAService.setODataJPAProcessor(odataJPAFactory.getODataJPAProcessor(odataJPAContext)); + return odataJPAService; + } + + /** + * @return the persistencUnitName + */ + protected String getPersistenceUnitName() { + return persistenceUnitName; + } + + /** + * @param persistencUnitName the persistencUnitName to set + */ + private void setPersistenceUnitName(String persistencUnitName) { + this.persistenceUnitName = persistencUnitName; + } + + protected abstract void initializeODataJPAContext(ODataJPAContext odataJPAContext2); + private ODataJPAContext getODataJPAContext() { + return odataJPAFactory.newODataJPAContext(); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPATransaction.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPATransaction.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPATransaction.java new file mode 100644 index 0000000..77faa37 --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/ODataJPATransaction.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.olingo.jpa.api; + +/** + * <p> + * The interface provides methods for handling transactions. OData JPA processor defaults to "RESOURCE LOCAL" + * application managed transaction. + * </p> + * <p> Applications can override this behavior by implementing this interface. The implementation can + * be registered with OData JPA processor using the servelt init parameter <b>odata.jpa.transaction</b></p> + * + */ +public interface ODataJPATransaction { + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/exception/ODataJPAException.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/exception/ODataJPAException.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/exception/ODataJPAException.java new file mode 100644 index 0000000..6cc4276 --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/exception/ODataJPAException.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.olingo.jpa.api.exception; + +import org.apache.olingo.server.api.ODataTranslatedException; + +/** + * Root exception for OData Extension for JPA + */ +public abstract class ODataJPAException extends ODataTranslatedException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + protected ODataJPAException(String developmentMessage, Throwable cause, MessageKey messageKey, String[] parameters) { + super(developmentMessage, cause, messageKey, parameters); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e3a20bb9/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/factory/ODataJPAFactory.java ---------------------------------------------------------------------- diff --git a/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/factory/ODataJPAFactory.java b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/factory/ODataJPAFactory.java new file mode 100644 index 0000000..dd13b5d --- /dev/null +++ b/ext/odata-jpa/odata-jpa-api/src/main/java/org/apache/olingo/jpa/api/factory/ODataJPAFactory.java @@ -0,0 +1,42 @@ +/* + * 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.olingo.jpa.api.factory; + +import java.util.ServiceLoader; + +import org.apache.olingo.jpa.api.ODataJPAAbstractEdmProvider; +import org.apache.olingo.jpa.api.ODataJPAContext; +import org.apache.olingo.jpa.api.ODataJPAProcessor; +import org.apache.olingo.jpa.api.exception.ODataJPAException; + +public abstract class ODataJPAFactory { + + public static ODataJPAFactory newInstance() { + ServiceLoader<ODataJPAFactory> odataJPAFactoryLoader = ServiceLoader.load(ODataJPAFactory.class); + ODataJPAFactory odataJPAFactory = odataJPAFactoryLoader.iterator().next(); + return odataJPAFactory; + } + + public abstract ODataJPAAbstractEdmProvider getODataJPAEdmProvider(String pUnitName) throws ODataJPAException; + + public abstract ODataJPAProcessor getODataJPAProcessor(ODataJPAContext odataJPAContext) throws ODataJPAException; + + public abstract ODataJPAContext newODataJPAContext(); + +}
