Repository: ignite Updated Branches: refs/heads/ignite-2977 fcb23411e -> c3b1970c6
IGNITE-2977: Improved factories handling. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/c3b1970c Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/c3b1970c Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/c3b1970c Branch: refs/heads/ignite-2977 Commit: c3b1970c675dbfc61be900d8fc9fc16d6b5c39dd Parents: fcb2341 Author: vozerov-gridgain <[email protected]> Authored: Wed Apr 13 12:33:10 2016 +0300 Committer: vozerov-gridgain <[email protected]> Committed: Wed Apr 13 12:33:10 2016 +0300 ---------------------------------------------------------------------- .../PlatformJavaObjectFactoryProxy.java | 32 +++++++------ .../PlatformJavaObjectSingletonFactory.java | 48 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/c3b1970c/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 ecce23d..11f282c 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 @@ -52,8 +52,8 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz /** Factory type. */ private int factoryTyp; - /** Factory class name. */ - private String factoryClsName; + /** Class name. */ + private String clsName; /** Optional payload for special factory types. */ @GridToStringExclude @@ -74,14 +74,14 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz * Constructor. * * @param factoryTyp Factory type. - * @param factoryClsName Factory class name. + * @param clsName Class name. * @param payload Payload. * @param props Properties. */ - public PlatformJavaObjectFactoryProxy(int factoryTyp, @Nullable String factoryClsName, @Nullable Object payload, + public PlatformJavaObjectFactoryProxy(int factoryTyp, @Nullable String clsName, @Nullable Object payload, @Nullable Map<String, Object> props) { this.factoryTyp = factoryTyp; - this.factoryClsName = factoryClsName; + this.clsName = clsName; this.payload = payload; this.props = props; } @@ -94,7 +94,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz */ public PlatformJavaObjectFactory factory(GridKernalContext ctx) { // Create factory. - PlatformJavaObjectFactory res; + Object res; switch (factoryTyp) { case TYP_DEFAULT: @@ -103,7 +103,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz break; case TYP_USER: - res = PlatformUtils.createJavaObject(factoryClsName); + res = PlatformUtils.createJavaObject(clsName); break; @@ -114,10 +114,14 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz // Initialize factory. if (res instanceof PlatformJavaObjectFactoryEx) ((PlatformJavaObjectFactoryEx)res).initialize(payload, props); - else - PlatformUtils.initializeJavaObject(res, factoryClsName, props, ctx); + else { + PlatformUtils.initializeJavaObject(res, clsName, props, ctx); + + if (!(res instanceof PlatformJavaObjectFactory)) + res = new PlatformJavaObjectSingletonFactory<>(res); + } - return res; + return (PlatformJavaObjectFactory)res; } /** {@inheritDoc} */ @@ -125,7 +129,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz BinaryRawWriterEx rawWriter = (BinaryRawWriterEx)writer.rawWriter(); rawWriter.writeInt(factoryTyp); - rawWriter.writeString(factoryClsName); + rawWriter.writeString(clsName); rawWriter.writeObjectDetached(payload); if (props != null) { @@ -145,7 +149,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz BinaryRawReaderEx rawReader = (BinaryRawReaderEx)reader.rawReader(); factoryTyp = rawReader.readInt(); - factoryClsName = rawReader.readString(); + clsName = rawReader.readString(); payload = rawReader.readObjectDetached(); int propsSize = rawReader.readInt(); @@ -165,7 +169,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz /** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(factoryTyp); - U.writeString(out, factoryClsName); + U.writeString(out, clsName); out.writeObject(payload); U.writeMap(out, props); } @@ -173,7 +177,7 @@ public class PlatformJavaObjectFactoryProxy implements Externalizable, Binaryliz /** {@inheritDoc} */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { factoryTyp = in.readInt(); - factoryClsName = U.readString(in); + clsName = U.readString(in); payload = in.readObject(); props = U.readMap(in); } http://git-wip-us.apache.org/repos/asf/ignite/blob/c3b1970c/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectSingletonFactory.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectSingletonFactory.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectSingletonFactory.java new file mode 100644 index 0000000..165b7d1 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/PlatformJavaObjectSingletonFactory.java @@ -0,0 +1,48 @@ +/* + * 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.util.typedef.internal.S; +import org.apache.ignite.platform.PlatformJavaObjectFactory; + +/** + * Singleton factory. + */ +public class PlatformJavaObjectSingletonFactory<T> implements PlatformJavaObjectFactory<T> { + /** Instance. */ + private final T instance; + + /** + * Constructor. + * + * @param instance Instance. + */ + public PlatformJavaObjectSingletonFactory(T instance) { + this.instance = instance; + } + + /** {@inheritDoc} */ + @Override public T create() { + return instance; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(PlatformJavaObjectSingletonFactory.class, this); + } +}
