ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject ignite git commit: IGNITE-2977: Initial implementation of Java factories.
Date Tue, 12 Apr 2016 10:29:30 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-2977 b1d49b3ec -> 4e0f1e491


IGNITE-2977: Initial implementation of Java factories.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/4e0f1e49
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/4e0f1e49
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/4e0f1e49

Branch: refs/heads/ignite-2977
Commit: 4e0f1e491f7dbf154fe17c3790adc380794df9e5
Parents: b1d49b3
Author: vozerov-gridgain <vozerov@gridgain.com>
Authored: Tue Apr 12 13:29:24 2016 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Tue Apr 12 13:29:24 2016 +0300

----------------------------------------------------------------------
 .../PlatformDefaultJavaObjectFactory.java       |  58 ++++++++
 .../platform/PlatformJavaObjectFactoryEx.java   |  36 +++++
 .../PlatformJavaObjectFactoryProxy.java         | 131 +++++++------------
 .../platform/utils/PlatformUtils.java           |  72 ++++++++++
 4 files changed, 214 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/4e0f1e49/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformDefaultJavaObjectFactory.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformDefaultJavaObjectFactory.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformDefaultJavaObjectFactory.java
new file mode 100644
index 0000000..c22cbd9
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformDefaultJavaObjectFactory.java
@@ -0,0 +1,58 @@
+/*
+ * 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.ignite.internal.processors.platform;
+
+import org.apache.ignite.internal.processors.platform.utils.PlatformUtils;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Map;
+
+/**
+ * Default Java object factory implementation.
+ */
+public class PlatformDefaultJavaObjectFactory<T> implements PlatformJavaObjectFactoryEx<T>
{
+    /** Class name. */
+    private String clsName;
+
+    /** Properties. */
+    private Map<String, Object> props;
+
+    /** {@inheritDoc} */
+    @Override public void initialize(@Nullable Object payload, @Nullable Map<String, Object>
props) {
+        assert payload != null && payload instanceof String;
+
+        clsName = (String)payload;
+
+        this.props = props;
+    }
+
+    /** {@inheritDoc} */
+    @Override public T create() {
+        T res = PlatformUtils.createJavaObject(clsName);
+
+        PlatformUtils.initializeJavaObject(res, clsName, props, null);
+
+        return res;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(PlatformDefaultJavaObjectFactory.class, this);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/4e0f1e49/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryEx.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryEx.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryEx.java
new file mode 100644
index 0000000..9c56e34
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryEx.java
@@ -0,0 +1,36 @@
+/*
+ * 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.ignite.internal.processors.platform;
+
+import org.apache.ignite.platform.PlatformJavaObjectFactory;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Map;
+
+/**
+ * Extended Java object factory interface to handle special cases.
+ */
+public interface PlatformJavaObjectFactoryEx<T> extends PlatformJavaObjectFactory<T>
{
+    /**
+     * Initialize factory.
+     *
+     * @param payload Optional payload.
+     * @param props Optional properties.
+     */
+    public void initialize(@Nullable Object payload, @Nullable Map<String, Object>
props);
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/4e0f1e49/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryProxy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryProxy.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryProxy.java
index 706be79..6984328 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryProxy.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectFactoryProxy.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.internal.processors.platform;
 
-import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryReader;
@@ -26,34 +25,43 @@ import org.apache.ignite.binary.Binarylizable;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.binary.BinaryRawReaderEx;
 import org.apache.ignite.internal.binary.BinaryRawWriterEx;
+import org.apache.ignite.internal.processors.platform.utils.PlatformUtils;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.platform.PlatformJavaObjectFactory;
-import org.jetbrains.annotations.Nullable;
 
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Map;
 
 /**
  * Wrapper for Java object factory.
  */
-public class PlatformJavaObjectFactoryProxy implements PlatformJavaObjectFactory, Externalizable,
Binarylizable {
-    /** Class name. */
-    private String clsName;
+public class PlatformJavaObjectFactoryProxy implements Externalizable, Binarylizable {
+    /** User-defined type. */
+    private static final int TYP_USER = 0;
+
+    /** Default factory. */
+    private static final int TYP_DEFAULT = 1;
+
+    /** Factory type. */
+    private int factoryType;
+
+    /** Factory class name. */
+    private String factoryClsName;
+
+    /** Optional payload for special factory types. */
+    @GridToStringExclude
+    private Object payload;
 
     /** Properties. */
     @GridToStringExclude
     private Map<String, Object> props;
 
-    /** Whether object is to be created directly. */
-    private boolean direct;
-
     /**
      * Default constructor.
      */
@@ -61,12 +69,6 @@ public class PlatformJavaObjectFactoryProxy implements PlatformJavaObjectFactory
         // No-op.
     }
 
-    /** {@inheritDoc} */
-    @Override public Object create() {
-        assert direct : "Should be called only in direct mode.";
-
-        return create0(null);
-    }
     /**
      * Get factory instance.
      *
@@ -74,80 +76,40 @@ public class PlatformJavaObjectFactoryProxy implements PlatformJavaObjectFactory
      * @return Factory instance.
      */
     public PlatformJavaObjectFactory factory(GridKernalContext ctx) {
-        if (direct)
-            return this;
-        else
-            return create0(ctx);
-    }
-
-    /**
-     * Internal create routine.
-     *
-     * @param ctx Optional context for injections.
-     * @return Object.
-     */
-    @SuppressWarnings("unchecked")
-    private <T> T create0(@Nullable GridKernalContext ctx) {
-        if (clsName == null)
-            throw new IgniteException("Java object/factory class name is not set.");
-
-        Class cls = U.classForName(clsName, null);
-
-        if (cls == null)
-            throw new IgniteException("Java object/factory class is not found (is it in the
classpath?): " +
-                clsName);
-
-        Object res;
-
-        try {
-            res =  cls.newInstance();
-        }
-        catch (ReflectiveOperationException e) {
-            throw new IgniteException("Failed to instantiate Java object/factory class (does
it have public " +
-                "default constructor?): " + clsName, e);
-        }
+        // Create factory.
+        PlatformJavaObjectFactory res;
 
-        if (props != null) {
-            for (Map.Entry<String, Object> prop : props.entrySet()) {
-                String fieldName = prop.getKey();
+        switch (factoryType) {
+            case TYP_DEFAULT:
+                res = new PlatformDefaultJavaObjectFactory();
 
-                if (fieldName == null)
-                    throw new IgniteException("Java object/factory field name cannot be null:
" + clsName);
+                break;
 
-                Field field = U.findField(cls, fieldName);
+            case TYP_USER:
+                res = PlatformUtils.createJavaObject(factoryClsName);
 
-                if (field == null)
-                    throw new IgniteException("Java object/factory class field is not found
[" +
-                        "className=" + clsName + ", fieldName=" + fieldName + ']');
+                break;
 
-                try {
-                    field.set(res, prop.getValue());
-                }
-                catch (ReflectiveOperationException e) {
-                    throw new IgniteException("Failed to set Java object/factory field [className="
+ clsName +
-                        ", fieldName=" + fieldName + ']', e);
-                }
-            }
+            default:
+                throw new IgniteException("Unsupported Java object factory type: " + factoryType);
         }
 
-        if (ctx != null) {
-            try {
-                ctx.resource().injectGeneric(res);
-            }
-            catch (IgniteCheckedException e) {
-                throw new IgniteException("Failed to inject resources to Java factory: "
+ clsName, e);
-            }
-        }
+        // Initialize factory.
+        if (res instanceof PlatformJavaObjectFactoryEx)
+            ((PlatformJavaObjectFactoryEx)res).initialize(payload, props);
+        else
+            PlatformUtils.initializeJavaObject(res, factoryClsName, props, ctx);
 
-        return (T)res;
+        return res;
     }
 
     /** {@inheritDoc} */
     @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException {
         BinaryRawWriterEx rawWriter = (BinaryRawWriterEx)writer.rawWriter();
 
-        rawWriter.writeString(clsName);
-        rawWriter.writeBoolean(direct);
+        rawWriter.writeInt(factoryType);
+        rawWriter.writeString(factoryClsName);
+        rawWriter.writeObjectDetached(payload);
 
         if (props != null) {
             rawWriter.writeInt(props.size());
@@ -158,19 +120,20 @@ public class PlatformJavaObjectFactoryProxy implements PlatformJavaObjectFactory
             }
         }
         else
-            rawWriter.writeInt(-1);
+            rawWriter.writeInt(0);
     }
 
     /** {@inheritDoc} */
     @Override public void readBinary(BinaryReader reader) throws BinaryObjectException {
         BinaryRawReaderEx rawReader = (BinaryRawReaderEx)reader.rawReader();
 
-        clsName = rawReader.readString();
-        direct = rawReader.readBoolean();
+        factoryType = rawReader.readInt();
+        factoryClsName = rawReader.readString();
+        payload = rawReader.readObjectDetached();
 
         int propsSize = rawReader.readInt();
 
-        if (propsSize >= 0) {
+        if (propsSize > 0) {
             props = new HashMap<>(propsSize);
 
             for (int i = 0; i < propsSize; i++) {
@@ -184,16 +147,18 @@ public class PlatformJavaObjectFactoryProxy implements PlatformJavaObjectFactory
 
     /** {@inheritDoc} */
     @Override public void writeExternal(ObjectOutput out) throws IOException {
-        U.writeString(out, clsName);
+        out.writeInt(factoryType);
+        U.writeString(out, factoryClsName);
+        out.writeObject(payload);
         U.writeMap(out, props);
-        out.writeBoolean(direct);
     }
 
     /** {@inheritDoc} */
     @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
-        clsName = U.readString(in);
+        factoryType = in.readInt();
+        factoryClsName = U.readString(in);
+        payload = in.readObject();
         props = U.readMap(in);
-        direct = in.readBoolean();
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/4e0f1e49/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/utils/PlatformUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/utils/PlatformUtils.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/utils/PlatformUtils.java
index 04b1a17..f1047be 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/utils/PlatformUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/utils/PlatformUtils.java
@@ -53,6 +53,7 @@ import org.jetbrains.annotations.Nullable;
 import javax.cache.CacheException;
 import javax.cache.event.CacheEntryEvent;
 import javax.cache.event.CacheEntryListenerException;
+import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.security.Timestamp;
 import java.util.ArrayList;
@@ -884,6 +885,8 @@ public class PlatformUtils {
     private static Collection<Object> unwrapKnownCollection(Collection<Object>
col) {
         Collection<Object> col0 = BinaryUtils.newKnownCollection(col);
 
+        assert col0 != null;
+
         for (Object obj : col)
             col0.add(unwrapBinary(obj));
 
@@ -919,6 +922,75 @@ public class PlatformUtils {
 
         return map0;
     }
+
+    /**
+     * Create Java object.
+     *
+     * @param clsName Class name.
+     * @return Instance.
+     */
+    public static <T> T createJavaObject(String clsName) {
+        if (clsName == null)
+            throw new IgniteException("Java object/factory class name is not set.");
+
+        Class cls = U.classForName(clsName, null);
+
+        if (cls == null)
+            throw new IgniteException("Java object/factory class is not found (is it in the
classpath?): " +
+                clsName);
+
+        try {
+            return (T)cls.newInstance();
+        }
+        catch (ReflectiveOperationException e) {
+            throw new IgniteException("Failed to instantiate Java object/factory class (does
it have public " +
+                "default constructor?): " + clsName, e);
+        }
+    }
+
+    /**
+     * Initialize Java object or object factory.
+     *
+     * @param obj Object.
+     * @param clsName Class name.
+     * @param props Properties (optional).
+     * @param ctx Kernal context (optional).
+     */
+    public static void initializeJavaObject(Object obj, String clsName, @Nullable Map<String,
Object> props,
+        @Nullable GridKernalContext ctx) {
+        if (props != null) {
+            for (Map.Entry<String, Object> prop : props.entrySet()) {
+                String fieldName = prop.getKey();
+
+                if (fieldName == null)
+                    throw new IgniteException("Java object/factory field name cannot be null:
" + clsName);
+
+                Field field = U.findField(obj.getClass(), fieldName);
+
+                if (field == null)
+                    throw new IgniteException("Java object/factory class field is not found
[" +
+                        "className=" + clsName + ", fieldName=" + fieldName + ']');
+
+                try {
+                    field.set(obj, prop.getValue());
+                }
+                catch (ReflectiveOperationException e) {
+                    throw new IgniteException("Failed to set Java object/factory field [className="
+ clsName +
+                        ", fieldName=" + fieldName + ']', e);
+                }
+            }
+        }
+
+        if (ctx != null) {
+            try {
+                ctx.resource().injectGeneric(obj);
+            }
+            catch (IgniteCheckedException e) {
+                throw new IgniteException("Failed to inject resources to Java factory: "
+ clsName, e);
+            }
+        }
+    }
+
     /**
      * Private constructor.
      */


Mime
View raw message