/* * 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.cache; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteException; import org.jetbrains.annotations.Nullable; /** * */ public class CacheObjectImpl extends CacheObjectAdapter { /** */ private static final long serialVersionUID = 0L; /** * */ public CacheObjectImpl() { // No-op. } /** * @param val Value. * @param valBytes Value bytes. */ public CacheObjectImpl(Object val, byte[] valBytes) { assert val != null || valBytes != null; this.val = val; this.valBytes = valBytes; } /** {@inheritDoc} */ @Override public boolean isPlatformType() { return true; } /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { cpy = cpy && needCopy(ctx); try { if (cpy) { if (valBytes == null) { assert val != null; valBytes = ctx.processor().marshal(ctx, val); } ClassLoader clsLdr; if (val != null) clsLdr = val.getClass().getClassLoader(); else if (ctx.kernalContext().config().isPeerClassLoadingEnabled()) clsLdr = ctx.kernalContext().cache().context().deploy().globalLoader(); else clsLdr = null; return (T)ctx.processor().unmarshal(ctx, valBytes, clsLdr); } if (val != null) return (T)val; assert valBytes != null; Object val = ctx.processor().unmarshal(ctx, valBytes, ctx.kernalContext().config().isPeerClassLoadingEnabled() ? ctx.kernalContext().cache().context().deploy().globalLoader() : null); if (ctx.storeValue()) this.val = val; return (T)val; } catch (IgniteCheckedException e) { throw new IgniteException("Failed to unmarshall object.", e); } } /** {@inheritDoc} */ @Override public byte[] valueBytes(CacheObjectContext ctx) throws IgniteCheckedException { if (valBytes == null) valBytes = ctx.processor().marshal(ctx, val); return valBytes; } /** {@inheritDoc} */ @Override public void prepareMarshal(CacheObjectContext ctx) throws IgniteCheckedException { assert val != null || valBytes != null; if (valBytes == null) valBytes = ctx.kernalContext().cacheObjects().marshal(ctx, val); } /** {@inheritDoc} */ @Override public void finishUnmarshal(CacheObjectContext ctx, ClassLoader ldr) throws IgniteCheckedException { assert val != null || valBytes != null; if (val == null && ctx.storeValue()) val = ctx.processor().unmarshal(ctx, valBytes, ldr); } /** {@inheritDoc} */ @Override public void onAckReceived() { // No-op. } /** {@inheritDoc} */ @Override public short directType() { return 89; } /** {@inheritDoc} */ @Override public int hashCode() { assert false; return super.hashCode(); } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { assert false; return super.equals(obj); } /** {@inheritDoc} */ @Override public CacheObject prepareForCache(CacheObjectContext ctx) { return this; } }