You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/01/20 21:19:56 UTC
git commit: [OLINGO-129] First working with HSQLDB
Updated Branches:
refs/heads/Olingo-129_PocJpaDataStore [created] b0e0a32f2
[OLINGO-129] First working with HSQLDB
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/b0e0a32f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/b0e0a32f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/b0e0a32f
Branch: refs/heads/Olingo-129_PocJpaDataStore
Commit: b0e0a32f2a16ae9d92bff4241f0f4757550c9664
Parents: 2216461
Author: Michael Bolz <mi...@apache.org>
Authored: Mon Jan 20 20:08:15 2014 +0100
Committer: Michael Bolz <mi...@apache.org>
Committed: Mon Jan 20 21:19:17 2014 +0100
----------------------------------------------------------------------
.gitignore | 1 +
.../processor/api/AnnotationServiceFactory.java | 4 +-
.../annotation-processor-core/pom.xml | 25 +-
.../core/datasource/AnnotationInMemoryDs.java | 12 +-
.../processor/core/datasource/DataStore.java | 290 ++-----------------
.../core/datasource/DataStoreException.java | 35 +++
.../core/datasource/DataStoreFactory.java | 36 +++
.../core/datasource/InMemoryDataStore.java | 290 +++++++++++++++++++
.../core/datasource/JpaAnnotationDataStore.java | 134 +++++++++
.../datasource/AnnotationsInMemoryDsTest.java | 6 +-
.../annotation-processor-ref/pom.xml | 44 ++-
.../ref/AnnotationRefServiceFactory.java | 41 ++-
.../processor/ref/model/jpa/Animal.java | 66 +++++
.../src/main/resources/META-INF/persistence.xml | 37 +++
.../src/main/webapp/WEB-INF/web.xml | 2 +-
15 files changed, 723 insertions(+), 300 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index e000e0d..19cc1bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@ classes
.DS_Store
nb-configuration.xml
context.xml
+glassfish-web.xml
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-api/src/main/java/org/apache/olingo/odata2/annotation/processor/api/AnnotationServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-api/src/main/java/org/apache/olingo/odata2/annotation/processor/api/AnnotationServiceFactory.java b/odata2-annotation-processor/annotation-processor-api/src/main/java/org/apache/olingo/odata2/annotation/processor/api/AnnotationServiceFactory.java
index 38fe73a..4583aab 100644
--- a/odata2-annotation-processor/annotation-processor-api/src/main/java/org/apache/olingo/odata2/annotation/processor/api/AnnotationServiceFactory.java
+++ b/odata2-annotation-processor/annotation-processor-api/src/main/java/org/apache/olingo/odata2/annotation/processor/api/AnnotationServiceFactory.java
@@ -80,7 +80,7 @@ public abstract class AnnotationServiceFactory {
* <code>org.apache.olingo.olingo-odata2-api-annotation</code> module
* (see package <code>org.apache.olingo.odata2.api.annotation.edm</code>) to define the model and access the data.
*
- * @param modelPackage classes (which are annotated) which will be used for EDM definition and data access.
+ * @param annotatedClasses classes (which are annotated) which will be used for EDM definition and data access.
* @return service an {@link ODataService} based on on an EDM and Processor which are using annotations
* for model definition and data access.
* @throws ODataException if an error during initialization occurs
@@ -107,7 +107,7 @@ public abstract class AnnotationServiceFactory {
* <code>org.apache.olingo.olingo-odata2-api-annotation</code> module
* (see package <code>org.apache.olingo.odata2.api.annotation.edm</code>) to define the model and access the data.
*
- * @param modelPackage classes (which are annotated) which will be used for EDM definition and data access.
+ * @param annotatedClasses classes (which are annotated) which will be used for EDM definition and data access.
* @return service an {@link ODataService} based on on an EDM and Processor which are using annotations
* for model definition and data access.
* @throws ODataException if an error during initialization occurs
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/pom.xml
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/pom.xml b/odata2-annotation-processor/annotation-processor-core/pom.xml
index 8ed093e..e3e0586 100644
--- a/odata2-annotation-processor/annotation-processor-core/pom.xml
+++ b/odata2-annotation-processor/annotation-processor-core/pom.xml
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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. -->
+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. -->
<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">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>olingo-odata2-annotation-processor-core-incubating</artifactId>
@@ -52,5 +52,10 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>javax.persistence</artifactId>
+ <version>2.1.0</version>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationInMemoryDs.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationInMemoryDs.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationInMemoryDs.java
index 2096785..ee8c602 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationInMemoryDs.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationInMemoryDs.java
@@ -22,7 +22,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.olingo.odata2.annotation.processor.core.datasource.DataStore.DataStoreException;
import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper;
import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper.AnnotatedNavInfo;
import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper.ODataAnnotationException;
@@ -44,6 +43,7 @@ public class AnnotationInMemoryDs implements DataSource {
private static final AnnotationHelper ANNOTATION_HELPER = new AnnotationHelper();
private final Map<String, DataStore<Object>> dataStores = new HashMap<String, DataStore<Object>>();
+ private final DataStoreFactory dataStoreFactory;
private final boolean persistInMemory;
public AnnotationInMemoryDs(final Collection<Class<?>> annotatedClasses) throws ODataException {
@@ -53,6 +53,8 @@ public class AnnotationInMemoryDs implements DataSource {
public AnnotationInMemoryDs(final Collection<Class<?>> annotatedClasses, final boolean persistInMemory)
throws ODataException {
this.persistInMemory = persistInMemory;
+ this.dataStoreFactory = new DataStoreFactory();
+
init(annotatedClasses);
}
@@ -62,6 +64,8 @@ public class AnnotationInMemoryDs implements DataSource {
public AnnotationInMemoryDs(final String packageToScan, final boolean persistInMemory) throws ODataException {
this.persistInMemory = persistInMemory;
+ this.dataStoreFactory = new DataStoreFactory();
+
List<Class<?>> foundClasses = ClassHelper.loadClasses(packageToScan, new ClassHelper.ClassValidator() {
@Override
public boolean isClassValid(final Class<?> c) {
@@ -78,14 +82,14 @@ public class AnnotationInMemoryDs implements DataSource {
for (Class<?> clz : annotatedClasses) {
String entitySetName = ANNOTATION_HELPER.extractEntitySetName(clz);
if(entitySetName != null) {
- DataStore<Object> dhs = (DataStore<Object>) DataStore.createInMemory(clz, persistInMemory);
+ DataStore<Object> dhs = (DataStore<Object>) dataStoreFactory.createInstance(clz, persistInMemory);
dataStores.put(entitySetName, dhs);
} else if(!ANNOTATION_HELPER.isEdmAnnotated(clz)) {
throw new ODataException("Found not annotated class during DataStore initilization of type: "
+ clz.getName());
}
}
- } catch (DataStore.DataStoreException e) {
+ } catch (DataStoreException e) {
throw new ODataException("Error in DataStore initilization with message: " + e.getMessage(), e);
}
}
@@ -377,7 +381,7 @@ public class AnnotationInMemoryDs implements DataSource {
/**
* Returns corresponding DataStore for EdmEntitySet or if no data store is registered an
- * ODataRuntimeException is thrown.
+ ODataRuntimeException is thrown.
* Never returns NULL.
*
* @param entitySet for which the corresponding DataStore is returned
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java
index 57b3d8c..2db386b 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java
@@ -1,159 +1,49 @@
-/*******************************************************************************
- * 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.
- ******************************************************************************/
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.odata2.annotation.processor.core.datasource;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper;
-import org.apache.olingo.odata2.annotation.processor.core.util.ClassHelper;
-import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
-import org.apache.olingo.odata2.api.exception.ODataApplicationException;
-import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
/**
*
+ * @author michael
*/
-public class DataStore<T> {
-
- private static final AnnotationHelper ANNOTATION_HELPER = new AnnotationHelper();
- private final Map<KeyElement, T> dataStore;
- private final Class<T> dataTypeClass;
- private final KeyAccess keyAccess;
-
- private static class InMemoryDataStore {
- private static final Map<Class<?>, DataStore<?>> c2ds = new HashMap<Class<?>, DataStore<?>>();
-
- @SuppressWarnings("unchecked")
- static synchronized DataStore<?> getInstance(final Class<?> clz, final boolean createNewInstance)
- throws DataStoreException {
- DataStore<?> ds = c2ds.get(clz);
- if (createNewInstance || ds == null) {
- ds = new DataStore<Object>((Class<Object>) clz);
- c2ds.put(clz, ds);
- }
- return ds;
- }
- }
+public interface DataStore<T> {
- @SuppressWarnings("unchecked")
- public static <T> DataStore<T> createInMemory(final Class<T> clazz) throws DataStoreException {
- return (DataStore<T>) InMemoryDataStore.getInstance(clazz, true);
- }
+ T create(final T object) throws DataStoreException;
- @SuppressWarnings("unchecked")
- public static <T> DataStore<T> createInMemory(final Class<T> clazz, final boolean keepExisting)
- throws DataStoreException {
- return (DataStore<T>) InMemoryDataStore.getInstance(clazz, !keepExisting);
- }
+ T createInstance();
- private DataStore(final Map<KeyElement, T> wrapStore, final Class<T> clz) throws DataStoreException {
- dataStore = Collections.synchronizedMap(wrapStore);
- dataTypeClass = clz;
- keyAccess = new KeyAccess(clz);
- }
+ T delete(final T object);
- private DataStore(final Class<T> clz) throws DataStoreException {
- this(new HashMap<KeyElement, T>(), clz);
- }
+ Class<T> getDataTypeClass();
- public Class<T> getDataTypeClass() {
- return dataTypeClass;
- }
+ String getEntityTypeName();
- public String getEntityTypeName() {
- return ANNOTATION_HELPER.extractEntityTypeName(dataTypeClass);
- }
-
- public T createInstance() {
- try {
- return dataTypeClass.newInstance();
- } catch (InstantiationException e) {
- throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
- } catch (IllegalAccessException e) {
- throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
- }
- }
-
- public T read(final T obj) {
- KeyElement objKeys = getKeys(obj);
- return dataStore.get(objKeys);
- }
-
- public Collection<T> read() {
- return Collections.unmodifiableCollection(dataStore.values());
- }
-
- public T create(final T object) throws DataStoreException {
- KeyElement keyElement = getKeys(object);
- return create(object, keyElement);
- }
-
- private T create(final T object, final KeyElement keyElement) throws DataStoreException {
- synchronized (dataStore) {
- if (keyElement.keyValuesMissing() || dataStore.containsKey(keyElement)) {
- KeyElement newKey = createSetAndGetKeys(object);
- return this.create(object, newKey);
- }
- dataStore.put(keyElement, object);
- }
- return object;
- }
-
- public T update(final T object) {
- KeyElement keyElement = getKeys(object);
- synchronized (dataStore) {
- dataStore.remove(keyElement);
- dataStore.put(keyElement, object);
- }
- return object;
- }
-
- public T delete(final T object) {
- KeyElement keyElement = getKeys(object);
- synchronized (dataStore) {
- return dataStore.remove(keyElement);
- }
- }
-
/**
* Are the key values equal for both instances.
* If all compared key values are <code>null</code> this also means equal.
- *
+ *
* @param first first instance to check for key equal
* @param second second instance to check for key equal
* @return <code>true</code> if object instance have equal keys set.
*/
- public boolean isKeyEqual(final T first, final T second) {
- KeyElement firstKeys = getKeys(first);
- KeyElement secondKeys = getKeys(second);
-
- return firstKeys.equals(secondKeys);
- }
-
+ boolean isKeyEqual(final T first, final T second);
+
/**
* Are the key values equal for both instances.
* If all compared key values are <code>null</code> this also means equal.
@@ -161,134 +51,16 @@ public class DataStore<T> {
* and that both are from the same class as this {@link DataStore} (see {@link #dataTypeClass}).
* For the equal check on {@link #dataTypeClass} instances without validation see
* {@link #isKeyEqual(Object, Object)}.
- *
+ *
* @param first first instance to check for key equal
* @param second second instance to check for key equal
* @return <code>true</code> if object instance have equal keys set.
*/
- @SuppressWarnings("unchecked")
- public boolean isKeyEqualChecked(Object first, Object second) throws DataStoreException {
- if(first == null || second == null) {
- throw new DataStoreException("Tried to compare null values which is not allowed.");
- } else if(first.getClass() != dataTypeClass) {
- throw new DataStoreException("First value is no instance from required class '" + dataTypeClass + "'.");
- } else if(second.getClass() != dataTypeClass) {
- throw new DataStoreException("Second value is no instance from required class '" + dataTypeClass + "'.");
- }
-
- return isKeyEqual((T) first, (T) second);
- }
-
-
- private class KeyElement {
- private int cachedHashCode = 42;
- private final List<Object> keyValues;
-
- public KeyElement(final int size) {
- keyValues = new ArrayList<Object>(size);
- }
-
- private void addValue(final Object keyValue) {
- keyValues.add(keyValue);
- cachedHashCode = 89 * cachedHashCode + (keyValue != null ? keyValue.hashCode() : 0);
- }
-
- boolean keyValuesMissing() {
- return keyValues.contains(null);
- }
-
- @Override
- public int hashCode() {
- return cachedHashCode;
- }
-
- @Override
- public boolean equals(final Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- @SuppressWarnings("unchecked")
- final KeyElement other = (KeyElement) obj;
- if (this.keyValues != other.keyValues && (this.keyValues == null || !this.keyValues.equals(other.keyValues))) {
- return false;
- }
- return true;
- }
-
- @Override
- public String toString() {
- return "KeyElement{" + "cachedHashCode=" + cachedHashCode + ", keyValues=" + keyValues + '}';
- }
- }
-
- private class KeyAccess {
- final List<Field> keyFields;
- final AtomicInteger idCounter = new AtomicInteger(1);
-
- KeyAccess(final Class<?> clazz) throws DataStoreException {
- keyFields = ANNOTATION_HELPER.getAnnotatedFields(clazz, EdmKey.class);
- if (keyFields.isEmpty()) {
- throw new DataStoreException("No EdmKey annotated fields found for class " + clazz);
- }
- }
-
- KeyElement getKeyValues(final T object) {
- KeyElement keyElement = new KeyElement(keyFields.size());
- for (Field field : keyFields) {
- Object keyValue = ClassHelper.getFieldValue(object, field);
- keyElement.addValue(keyValue);
- }
-
- return keyElement;
- }
-
- KeyElement createSetAndGetKeys(final T object) throws DataStoreException {
- KeyElement keyElement = new KeyElement(keyFields.size());
- for (Field field : keyFields) {
- Object key = createKey(field);
- ClassHelper.setFieldValue(object, field, key);
- keyElement.addValue(key);
- }
-
- return keyElement;
- }
-
- private Object createKey(final Field field) {
- Class<?> type = field.getType();
-
- if (type == String.class) {
- return String.valueOf(idCounter.getAndIncrement());
- } else if (type == Integer.class || type == int.class) {
- return Integer.valueOf(idCounter.getAndIncrement());
- } else if (type == Long.class || type == long.class) {
- return Long.valueOf(idCounter.getAndIncrement());
- }
-
- throw new UnsupportedOperationException("Automated key generation for type '" + type
- + "' is not supported (caused on field '" + field + "').");
- }
- }
-
- private KeyElement getKeys(final T object) {
- return keyAccess.getKeyValues(object);
- }
-
- private KeyElement createSetAndGetKeys(final T object) throws DataStoreException {
- return keyAccess.createSetAndGetKeys(object);
- }
+ boolean isKeyEqualChecked(Object first, Object second) throws DataStoreException;
- public static class DataStoreException extends ODataApplicationException {
- private static final long serialVersionUID = 42L;
+ T read(final T obj);
- public DataStoreException(final String message) {
- this(message, null);
- }
+ Collection<T> read();
- public DataStoreException(final String message, final Throwable cause) {
- super(message, Locale.ENGLISH, cause);
- }
- }
+ T update(final T object);
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreException.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreException.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreException.java
new file mode 100644
index 0000000..95299f4
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreException.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.odata2.annotation.processor.core.datasource;
+
+import java.util.Locale;
+import org.apache.olingo.odata2.api.exception.ODataApplicationException;
+
+/**
+ *
+ */
+public class DataStoreException extends ODataApplicationException {
+
+ private static final long serialVersionUID = 42L;
+
+ public DataStoreException(final String message) {
+ this(message, null);
+ }
+
+ public DataStoreException(final String message, final Throwable cause) {
+ super(message, Locale.ENGLISH, cause);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreFactory.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreFactory.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreFactory.java
new file mode 100644
index 0000000..739ec94
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStoreFactory.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.odata2.annotation.processor.core.datasource;
+
+import javax.persistence.Entity;
+
+/**
+ *
+ */
+public class DataStoreFactory {
+
+ public DataStore<?> createInstance(Class<?> clz, boolean keepPersistent) throws DataStoreException {
+ if(isJpaAnnotated(clz)) {
+ return JpaAnnotationDataStore.createInstance(clz);
+ }
+ return InMemoryDataStore.createInMemory(clz, keepPersistent);
+ }
+
+ private boolean isJpaAnnotated(Class<?> clz) {
+ return clz.getAnnotation(Entity.class) != null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/InMemoryDataStore.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/InMemoryDataStore.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/InMemoryDataStore.java
new file mode 100644
index 0000000..b50ada1
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/InMemoryDataStore.java
@@ -0,0 +1,290 @@
+/*******************************************************************************
+ * 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.odata2.annotation.processor.core.datasource;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper;
+import org.apache.olingo.odata2.annotation.processor.core.util.ClassHelper;
+import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+
+/**
+ *
+ */
+public class InMemoryDataStore<T> implements DataStore<T> {
+
+ private static final AnnotationHelper ANNOTATION_HELPER = new AnnotationHelper();
+ private final Map<KeyElement, T> dataStore;
+ private final Class<T> dataTypeClass;
+ private final KeyAccess keyAccess;
+
+ private static class InMemoryDataStoreHolder {
+ private static final Map<Class<?>, InMemoryDataStore<?>> c2ds = new HashMap<Class<?>, InMemoryDataStore<?>>();
+
+ @SuppressWarnings("unchecked")
+ static synchronized InMemoryDataStore<?> getInstance(final Class<?> clz, final boolean createNewInstance)
+ throws DataStoreException {
+ InMemoryDataStore<?> ds = c2ds.get(clz);
+ if (createNewInstance || ds == null) {
+ ds = new InMemoryDataStore<Object>((Class<Object>) clz);
+ c2ds.put(clz, ds);
+ }
+ return ds;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> InMemoryDataStore<T> createInMemory(final Class<T> clazz) throws DataStoreException {
+ return (InMemoryDataStore<T>) InMemoryDataStoreHolder.getInstance(clazz, true);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> InMemoryDataStore<T> createInMemory(final Class<T> clazz, final boolean keepExisting)
+ throws DataStoreException {
+ return (InMemoryDataStore<T>) InMemoryDataStoreHolder.getInstance(clazz, !keepExisting);
+ }
+
+ private InMemoryDataStore(final Map<KeyElement, T> wrapStore, final Class<T> clz) throws DataStoreException {
+ dataStore = Collections.synchronizedMap(wrapStore);
+ dataTypeClass = clz;
+ keyAccess = new KeyAccess(clz);
+ }
+
+ private InMemoryDataStore(final Class<T> clz) throws DataStoreException {
+ this(new HashMap<KeyElement, T>(), clz);
+ }
+
+ @Override
+ public Class<T> getDataTypeClass() {
+ return dataTypeClass;
+ }
+
+ @Override
+ public String getEntityTypeName() {
+ return ANNOTATION_HELPER.extractEntityTypeName(dataTypeClass);
+ }
+
+ @Override
+ public T createInstance() {
+ try {
+ return dataTypeClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
+ } catch (IllegalAccessException e) {
+ throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
+ }
+ }
+
+ @Override
+ public T read(final T obj) {
+ KeyElement objKeys = getKeys(obj);
+ return dataStore.get(objKeys);
+ }
+
+ @Override
+ public Collection<T> read() {
+ return Collections.unmodifiableCollection(dataStore.values());
+ }
+
+ @Override
+ public T create(final T object) throws DataStoreException {
+ KeyElement keyElement = getKeys(object);
+ return create(object, keyElement);
+ }
+
+ private T create(final T object, final KeyElement keyElement) throws DataStoreException {
+ synchronized (dataStore) {
+ if (keyElement.keyValuesMissing() || dataStore.containsKey(keyElement)) {
+ KeyElement newKey = createSetAndGetKeys(object);
+ return this.create(object, newKey);
+ }
+ dataStore.put(keyElement, object);
+ }
+ return object;
+ }
+
+ @Override
+ public T update(final T object) {
+ KeyElement keyElement = getKeys(object);
+ synchronized (dataStore) {
+ dataStore.remove(keyElement);
+ dataStore.put(keyElement, object);
+ }
+ return object;
+ }
+
+ @Override
+ public T delete(final T object) {
+ KeyElement keyElement = getKeys(object);
+ synchronized (dataStore) {
+ return dataStore.remove(keyElement);
+ }
+ }
+
+ /**
+ * Are the key values equal for both instances.
+ * If all compared key values are <code>null</code> this also means equal.
+ *
+ * @param first first instance to check for key equal
+ * @param second second instance to check for key equal
+ * @return <code>true</code> if object instance have equal keys set.
+ */
+ @Override
+ public boolean isKeyEqual(final T first, final T second) {
+ KeyElement firstKeys = getKeys(first);
+ KeyElement secondKeys = getKeys(second);
+
+ return firstKeys.equals(secondKeys);
+ }
+
+ /**
+ * Are the key values equal for both instances.
+ * If all compared key values are <code>null</code> this also means equal.
+ * Before object (keys) are compared it is validated that both object instance are NOT null
+ * and that both are from the same class as this {@link InMemoryDataStore} (see {@link #dataTypeClass}).
+ * For the equal check on {@link #dataTypeClass} instances without validation see
+ * {@link #isKeyEqual(Object, Object)}.
+ *
+ * @param first first instance to check for key equal
+ * @param second second instance to check for key equal
+ * @return <code>true</code> if object instance have equal keys set.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean isKeyEqualChecked(Object first, Object second) throws DataStoreException {
+ if(first == null || second == null) {
+ throw new DataStoreException("Tried to compare null values which is not allowed.");
+ } else if(first.getClass() != dataTypeClass) {
+ throw new DataStoreException("First value is no instance from required class '" + dataTypeClass + "'.");
+ } else if(second.getClass() != dataTypeClass) {
+ throw new DataStoreException("Second value is no instance from required class '" + dataTypeClass + "'.");
+ }
+
+ return isKeyEqual((T) first, (T) second);
+ }
+
+
+ private class KeyElement {
+ private int cachedHashCode = 42;
+ private final List<Object> keyValues;
+
+ public KeyElement(final int size) {
+ keyValues = new ArrayList<Object>(size);
+ }
+
+ private void addValue(final Object keyValue) {
+ keyValues.add(keyValue);
+ cachedHashCode = 89 * cachedHashCode + (keyValue != null ? keyValue.hashCode() : 0);
+ }
+
+ boolean keyValuesMissing() {
+ return keyValues.contains(null);
+ }
+
+ @Override
+ public int hashCode() {
+ return cachedHashCode;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ @SuppressWarnings("unchecked")
+ final KeyElement other = (KeyElement) obj;
+ if (this.keyValues != other.keyValues && (this.keyValues == null || !this.keyValues.equals(other.keyValues))) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "KeyElement{" + "cachedHashCode=" + cachedHashCode + ", keyValues=" + keyValues + '}';
+ }
+ }
+
+ private class KeyAccess {
+ final List<Field> keyFields;
+ final AtomicInteger idCounter = new AtomicInteger(1);
+
+ KeyAccess(final Class<?> clazz) throws DataStoreException {
+ keyFields = ANNOTATION_HELPER.getAnnotatedFields(clazz, EdmKey.class);
+ if (keyFields.isEmpty()) {
+ throw new DataStoreException("No EdmKey annotated fields found for class " + clazz);
+ }
+ }
+
+ KeyElement getKeyValues(final T object) {
+ KeyElement keyElement = new KeyElement(keyFields.size());
+ for (Field field : keyFields) {
+ Object keyValue = ClassHelper.getFieldValue(object, field);
+ keyElement.addValue(keyValue);
+ }
+
+ return keyElement;
+ }
+
+ KeyElement createSetAndGetKeys(final T object) throws DataStoreException {
+ KeyElement keyElement = new KeyElement(keyFields.size());
+ for (Field field : keyFields) {
+ Object key = createKey(field);
+ ClassHelper.setFieldValue(object, field, key);
+ keyElement.addValue(key);
+ }
+
+ return keyElement;
+ }
+
+ private Object createKey(final Field field) {
+ Class<?> type = field.getType();
+
+ if (type == String.class) {
+ return String.valueOf(idCounter.getAndIncrement());
+ } else if (type == Integer.class || type == int.class) {
+ return Integer.valueOf(idCounter.getAndIncrement());
+ } else if (type == Long.class || type == long.class) {
+ return Long.valueOf(idCounter.getAndIncrement());
+ }
+
+ throw new UnsupportedOperationException("Automated key generation for type '" + type
+ + "' is not supported (caused on field '" + field + "').");
+ }
+ }
+
+ private KeyElement getKeys(final T object) {
+ return keyAccess.getKeyValues(object);
+ }
+
+ private KeyElement createSetAndGetKeys(final T object) throws DataStoreException {
+ return keyAccess.createSetAndGetKeys(object);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/JpaAnnotationDataStore.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/JpaAnnotationDataStore.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/JpaAnnotationDataStore.java
new file mode 100644
index 0000000..fe32483
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/JpaAnnotationDataStore.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.odata2.annotation.processor.core.datasource;
+
+import java.util.Collection;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+import javax.persistence.TypedQuery;
+import org.apache.olingo.odata2.annotation.processor.core.util.AnnotationHelper;
+import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+
+/**
+ *
+ */
+public class JpaAnnotationDataStore<T> implements DataStore<T> {
+
+ public static DataStore<?> createInstance(Class<?> clz) {
+ return new JpaAnnotationDataStore<Object>((Class<Object>) clz);
+ }
+
+ protected Class<T> dataTypeClass;
+ private static final AnnotationHelper ANNOTATION_HELPER = new AnnotationHelper();
+
+ protected EntityManager entityManager;
+
+ public JpaAnnotationDataStore(final Class<T> clz) {
+ EntityManagerFactory emf = Persistence.createEntityManagerFactory("AnimalPersistence");
+ entityManager = emf.createEntityManager();
+ this.dataTypeClass = clz;
+ }
+
+ @Override
+ public Class<T> getDataTypeClass() {
+ return dataTypeClass;
+ }
+
+ @Override
+ public String getEntityTypeName() {
+ return ANNOTATION_HELPER.extractEntityTypeName(dataTypeClass);
+ }
+
+ @Override
+ public T createInstance() {
+ try {
+ return dataTypeClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
+ } catch (IllegalAccessException e) {
+ throw new ODataRuntimeException("Unable to create instance of class '" + dataTypeClass + "'.", e);
+ }
+ }
+
+ @Override
+ public T create(T object) throws DataStoreException {
+ EntityTransaction t = this.entityManager.getTransaction();
+ try {
+ t.begin();
+ this.entityManager.persist(object);
+ this.entityManager.flush();
+ t.commit();
+ } catch(Exception e) {
+ if(t != null && t.isActive()) {
+ t.rollback();
+ }
+ }
+
+ return object;
+ }
+
+ @Override
+ public T delete(T object) {
+ EntityTransaction t = this.entityManager.getTransaction();
+ try {
+ t.begin();
+ object = this.entityManager.merge(object);
+ this.entityManager.remove(object);
+ this.entityManager.flush();
+ t.commit();
+
+ return object;
+ } catch(Exception e) {
+ if(t != null && t.isActive()) {
+ t.rollback();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public boolean isKeyEqual(T first, T second) {
+ return ANNOTATION_HELPER.keyMatch(first, second);
+ }
+
+ @Override
+ public boolean isKeyEqualChecked(Object first, Object second) throws DataStoreException {
+ return ANNOTATION_HELPER.keyMatch(first, second);
+ }
+
+ @Override
+ public T read(T obj) {
+ Object key = ANNOTATION_HELPER.getValueForField(obj, EdmKey.class);
+ return this.entityManager.find(dataTypeClass, key);
+ }
+
+ @Override
+ public Collection<T> read() {
+ TypedQuery<T> query = entityManager.createQuery(
+ "select t from " + dataTypeClass.getSimpleName() + " t", dataTypeClass);
+// Query query = entityManager.createQuery(
+// "select t from " + dataTypeClass.getSimpleName() + " t");
+ return query.getResultList();
+ }
+
+ @Override
+ public T update(T object) {
+ return this.entityManager.merge(object);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
index ddf4ce5..a7e7e13 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
@@ -68,7 +68,7 @@ public class AnnotationsInMemoryDsTest {
ANNOTATED_ENTITY_SET_CLASSES.add(Manager.class);
ANNOTATED_ENTITY_SET_CLASSES.add(Photo.class);
ANNOTATED_ENTITY_SET_CLASSES.add(Room.class);
- ANNOTATED_ENTITY_SET_CLASSES.add(Team. class);
+ ANNOTATED_ENTITY_SET_CLASSES.add(Team.class);
ANNOTATED_MODEL_CLASSES.addAll(ANNOTATED_ENTITY_SET_CLASSES);
ANNOTATED_MODEL_CLASSES.add(Location.class);
@@ -746,8 +746,8 @@ public class AnnotationsInMemoryDsTest {
@Test
public void writeRelations() throws Exception {
- DataStore<Building> buildingStore = DataStore.createInMemory(Building.class, true);
- DataStore<Room> roomStore = DataStore.createInMemory(Room.class, true);
+ DataStore<Building> buildingStore = InMemoryDataStore.createInMemory(Building.class, true);
+ DataStore<Room> roomStore = InMemoryDataStore.createInMemory(Room.class, true);
EdmEntitySet buildingsEntitySet = createMockedEdmEntitySet("Buildings");
EdmEntitySet roomsEntitySet = createMockedEdmEntitySet("Rooms");
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-ref/pom.xml
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/pom.xml b/odata2-annotation-processor/annotation-processor-ref/pom.xml
index 4614af0..5327a53 100644
--- a/odata2-annotation-processor/annotation-processor-ref/pom.xml
+++ b/odata2-annotation-processor/annotation-processor-ref/pom.xml
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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. -->
+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. -->
<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">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>olingo-odata2-annotation-processor-ref-incubating</artifactId>
@@ -34,6 +34,30 @@
<dependencies>
<dependency>
+ <!-- required because of auto detection of web facet 2.5 -->
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>javax.persistence</artifactId>
+ <version>2.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>1.8.0.10</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>eclipselink</artifactId>
+ <version>2.5.1</version>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-api-incubating</artifactId>
<version>${project.version}</version>
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/AnnotationRefServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/AnnotationRefServiceFactory.java b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/AnnotationRefServiceFactory.java
index ccaca81..fb6b82e 100644
--- a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/AnnotationRefServiceFactory.java
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/AnnotationRefServiceFactory.java
@@ -15,18 +15,23 @@
package org.apache.olingo.odata2.annotation.processor.ref;
import java.util.Calendar;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.olingo.odata2.annotation.processor.api.AnnotationServiceFactory;
-import org.apache.olingo.odata2.annotation.processor.core.datasource.DataStore;
-import org.apache.olingo.odata2.annotation.processor.core.datasource.DataStore.DataStoreException;
+import org.apache.olingo.odata2.annotation.processor.core.datasource.InMemoryDataStore;
+import org.apache.olingo.odata2.annotation.processor.core.datasource.DataStoreException;
import org.apache.olingo.odata2.annotation.processor.ref.model.Building;
+import org.apache.olingo.odata2.annotation.processor.ref.model.City;
import org.apache.olingo.odata2.annotation.processor.ref.model.Employee;
import org.apache.olingo.odata2.annotation.processor.ref.model.Location;
import org.apache.olingo.odata2.annotation.processor.ref.model.Manager;
import org.apache.olingo.odata2.annotation.processor.ref.model.Photo;
+import org.apache.olingo.odata2.annotation.processor.ref.model.RefBase;
import org.apache.olingo.odata2.annotation.processor.ref.model.ResourceHelper;
import org.apache.olingo.odata2.annotation.processor.ref.model.Room;
import org.apache.olingo.odata2.annotation.processor.ref.model.Team;
+import org.apache.olingo.odata2.annotation.processor.ref.model.jpa.Animal;
import org.apache.olingo.odata2.api.ODataCallback;
import org.apache.olingo.odata2.api.ODataDebugCallback;
import org.apache.olingo.odata2.api.ODataService;
@@ -54,12 +59,26 @@ public class AnnotationRefServiceFactory extends ODataServiceFactory {
*/
private static class AnnotationInstances {
final static String MODEL_PACKAGE = "org.apache.olingo.odata2.annotation.processor.ref.model";
-
+ final static Set<Class<?>> ANNOTATED_MODEL_CLASSES = new HashSet<Class<?>>();
+ static {
+ ANNOTATED_MODEL_CLASSES.add(Building.class);
+ ANNOTATED_MODEL_CLASSES.add(City.class);
+ ANNOTATED_MODEL_CLASSES.add(Employee.class);
+ ANNOTATED_MODEL_CLASSES.add(Location.class);
+ ANNOTATED_MODEL_CLASSES.add(Manager.class);
+ ANNOTATED_MODEL_CLASSES.add(Photo.class);
+ ANNOTATED_MODEL_CLASSES.add(RefBase.class);
+ ANNOTATED_MODEL_CLASSES.add(Room.class);
+ ANNOTATED_MODEL_CLASSES.add(Team.class);
+ // JPA
+ ANNOTATED_MODEL_CLASSES.add(Animal.class);
+ }
final static ODataService ANNOTATION_ODATA_SERVICE;
static {
try {
- ANNOTATION_ODATA_SERVICE = AnnotationServiceFactory.createAnnotationService(MODEL_PACKAGE);
+// ANNOTATION_ODATA_SERVICE = AnnotationServiceFactory.createAnnotationService(MODEL_PACKAGE);
+ ANNOTATION_ODATA_SERVICE = AnnotationServiceFactory.createAnnotationService(ANNOTATED_MODEL_CLASSES);
initializeSampleData();
} catch (ODataApplicationException ex) {
throw new RuntimeException("Exception during sample data generation.", ex);
@@ -116,19 +135,19 @@ public class AnnotationRefServiceFactory extends ODataServiceFactory {
}
- private static <T> DataStore<T> getDataStore(Class<T> clz) throws DataStoreException {
- return DataStore.createInMemory(clz, true);
+ private static <T> InMemoryDataStore<T> getDataStore(Class<T> clz) throws DataStoreException {
+ return InMemoryDataStore.createInMemory(clz, true);
}
private static void initializeSampleData() throws ODataApplicationException {
- DataStore<Team> teamDs = getDataStore(Team.class);
+ InMemoryDataStore<Team> teamDs = getDataStore(Team.class);
teamDs.create(createTeam("Team Alpha", true));
teamDs.create(createTeam("Team Beta", false));
teamDs.create(createTeam("Team Gamma", false));
teamDs.create(createTeam("Team Omega", true));
teamDs.create(createTeam("Team Zeta", true));
- DataStore<Building> buildingsDs = getDataStore(Building.class);
+ InMemoryDataStore<Building> buildingsDs = getDataStore(Building.class);
Building redBuilding = createBuilding("Red Building");
buildingsDs.create(redBuilding);
Building greenBuilding = createBuilding("Green Building");
@@ -138,13 +157,13 @@ public class AnnotationRefServiceFactory extends ODataServiceFactory {
Building yellowBuilding = createBuilding("Yellow Building");
buildingsDs.create(yellowBuilding);
- DataStore<Photo> photoDs = getDataStore(Photo.class);
+ InMemoryDataStore<Photo> photoDs = getDataStore(Photo.class);
photoDs.create(createPhoto("Small picture", ResourceHelper.Format.GIF));
photoDs.create(createPhoto("Medium picture", ResourceHelper.Format.PNG));
photoDs.create(createPhoto("Big picture", ResourceHelper.Format.JPEG));
photoDs.create(createPhoto("Huge picture", ResourceHelper.Format.BMP));
- DataStore<Room> roomDs = getDataStore(Room.class);
+ InMemoryDataStore<Room> roomDs = getDataStore(Room.class);
roomDs.create(createRoom("Tiny red room", 5, 1, redBuilding));
roomDs.create(createRoom("Small red room", 20, 1, redBuilding));
roomDs.create(createRoom("Small green room", 20, 1, greenBuilding));
@@ -152,7 +171,7 @@ public class AnnotationRefServiceFactory extends ODataServiceFactory {
roomDs.create(createRoom("Huge blue room", 120, 1, blueBuilding));
roomDs.create(createRoom("Huge yellow room", 120, 1, yellowBuilding));
- DataStore<Employee> employeeDataStore = getDataStore(Employee.class);
+ InMemoryDataStore<Employee> employeeDataStore = getDataStore(Employee.class);
employeeDataStore.create(createEmployee("first Employee",
new Location("Norge", "8392", "Ä"), 42, null,
photoDs.read().iterator().next().getImage(), photoDs.read().iterator().next().getImageType(),
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/jpa/Animal.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/jpa/Animal.java b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/jpa/Animal.java
new file mode 100644
index 0000000..8418343
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/jpa/Animal.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.odata2.annotation.processor.ref.model.jpa;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import org.apache.olingo.odata2.api.annotation.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
+
+@EdmEntitySet
+@EdmEntityType
+@Entity
+public class Animal implements Serializable {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ @EdmKey
+ @EdmProperty
+ private String id;
+ @EdmProperty
+ private String name;
+ @EdmProperty
+ private Integer age;
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-ref/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/resources/META-INF/persistence.xml b/odata2-annotation-processor/annotation-processor-ref/src/main/resources/META-INF/persistence.xml
new file mode 100644
index 0000000..61ecfed
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/resources/META-INF/persistence.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
+ <persistence-unit name="AnimalPersistence" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+ <class>org.apache.olingo.odata2.annotation.processor.ref.model.jpa.Animal</class>
+ <properties>
+ <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
+ <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:org.apache.olingo.annotation.jpa.sample"/>
+<!-- <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:/tmp/db/olingo;shutdown=true"/>-->
+ <property name="javax.persistence.jdbc.user" value="sa"/>
+ <property name="javax.persistence.jdbc.password" value=""/>
+ <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.HSQLPlatform"/>
+ <property name="eclipselink.logging.level" value="ALL"/>
+ <property name="eclipselink.orm.throw.exceptions" value="true"/>
+ <property name="eclipselink.ddl-generation" value="create-tables"/>
+ <property name="eclipselink.ddl-generation.output-mode" value="database"/>
+ </properties>
+ </persistence-unit>
+</persistence>
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/b0e0a32f/odata2-annotation-processor/annotation-processor-web/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-web/src/main/webapp/WEB-INF/web.xml b/odata2-annotation-processor/annotation-processor-web/src/main/webapp/WEB-INF/web.xml
index 2e4635e..df856bb 100644
--- a/odata2-annotation-processor/annotation-processor-web/src/main/webapp/WEB-INF/web.xml
+++ b/odata2-annotation-processor/annotation-processor-web/src/main/webapp/WEB-INF/web.xml
@@ -36,7 +36,7 @@
<init-param>
<param-name>org.apache.olingo.odata2.service.factory</param-name>
<param-value>org.apache.olingo.odata2.annotation.processor.ref.AnnotationRefServiceFactory</param-value>
- </init-param>
+ </init-param>
<load-on-startup>1</load-on-startup>
</servlet>