Skip to content

Commit

Permalink
# IGNITE-141 - Marshallers refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Kulichenko committed Mar 5, 2015
1 parent 91740cc commit 4231691
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 39 deletions.
@@ -0,0 +1,60 @@
/*
* 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;

import org.apache.ignite.marshaller.*;
import org.jdk8.backport.*;

import java.io.*;
import java.util.concurrent.*;

/**
* Marshaller context adapter.
*/
public abstract class MarshallerContextAdapter implements MarshallerContext {
/** */
private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties";

/** */
protected final ConcurrentMap<Integer, String> clsNameById = new ConcurrentHashMap8<>();

/**
* Initializes context.
*/
public MarshallerContextAdapter() {
try {
ClassLoader ldr = getClass().getClassLoader();

BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(CLS_NAMES_FILE)));

String line;

while ((line = rdr.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;

String clsName = line.trim();

clsNameById.put(clsName.hashCode(), clsName);
}
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize marshaller context.", e);
}
}
}
Expand Up @@ -20,53 +20,19 @@
import org.apache.ignite.*;
import org.apache.ignite.internal.processors.cache.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.*;
import org.jdk8.backport.*;

import java.io.*;
import java.util.concurrent.*;

/**
* Marshaller context implementation.
*/
public class MarshallerContextImpl implements MarshallerContext {
/** */
private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties";

/** */
private final ConcurrentMap<Integer, String> clsNameById = new ConcurrentHashMap8<>();

public class MarshallerContextImpl extends MarshallerContextAdapter {
/** */
private final CountDownLatch latch = new CountDownLatch(1);

/** */
private volatile GridCacheAdapter<Integer, String> cache;

/**
* Constructor.
*/
MarshallerContextImpl() {
try {
ClassLoader ldr = getClass().getClassLoader();

BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(CLS_NAMES_FILE)));

String line;

while ((line = rdr.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;

String clsName = line.trim();

clsNameById.put(clsName.hashCode(), clsName);
}
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize marshaller context.", e);
}
}

/**
* @param ctx Kernal context.
*/
Expand All @@ -82,8 +48,6 @@ public void onMarshallerCacheReady(GridKernalContext ctx) {
@Override public void registerClass(int id, Class cls) {
if (!clsNameById.containsKey(id)) {
try {
U.debug("REG: " + cls.getName());

if (cache == null)
U.awaitQuiet(latch);

Expand Down
Expand Up @@ -28,6 +28,7 @@
[Lorg.apache.ignite.lang.IgniteBiTuple;
[Lorg.apache.ignite.lang.IgniteFuture;
[Lorg.apache.ignite.lang.IgnitePredicate;
[Ljava.io.Serializable;
[S
[Z

Expand Down
Expand Up @@ -19,7 +19,6 @@

import org.apache.ignite.internal.client.balancer.*;
import org.apache.ignite.internal.client.marshaller.*;
import org.apache.ignite.internal.client.marshaller.jdk.*;
import org.apache.ignite.internal.client.marshaller.optimized.*;
import org.apache.ignite.internal.client.ssl.*;
import org.apache.ignite.internal.util.typedef.*;
Expand Down Expand Up @@ -111,7 +110,7 @@ public class GridClientConfiguration {
private ExecutorService executor;

/** Marshaller. */
private GridClientMarshaller marshaller = new GridClientJdkMarshaller();
private GridClientMarshaller marshaller = new GridClientOptimizedMarshaller();

/** Daemon flag. */
private boolean daemon;
Expand Down
Expand Up @@ -18,8 +18,10 @@
package org.apache.ignite.internal.client.marshaller.optimized;

import org.apache.ignite.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.internal.client.marshaller.*;
import org.apache.ignite.internal.processors.rest.client.message.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.optimized.*;

import java.io.*;
Expand All @@ -41,6 +43,8 @@ public class GridClientOptimizedMarshaller implements GridClientMarshaller {
*/
public GridClientOptimizedMarshaller() {
opMarsh = new OptimizedMarshaller();

opMarsh.setContext(new ClientMarshallerContext());
}

/**
Expand All @@ -55,6 +59,7 @@ public GridClientOptimizedMarshaller() {
public GridClientOptimizedMarshaller(boolean requireSer, int poolSize) throws IOException {
opMarsh = new OptimizedMarshaller();

opMarsh.setContext(new ClientMarshallerContext());
opMarsh.setRequireSerializable(requireSer);
opMarsh.setPoolSize(poolSize);
}
Expand Down Expand Up @@ -92,4 +97,22 @@ public GridClientOptimizedMarshaller(boolean requireSer, int poolSize) throws IO
throw new IOException(e);
}
}

/**
*/
private static class ClientMarshallerContext extends MarshallerContextAdapter {
/** {@inheritDoc} */
@Override public void registerClass(int id, Class cls) {
// No-op.
}

/** {@inheritDoc} */
@Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException {
String clsName = clsNameById.get(id);

assert clsName != null : id;

return U.forName(clsName, ldr);
}
}
}

0 comments on commit 4231691

Please sign in to comment.