Skip to content
Permalink
Browse files
Enhance TypeDescriptor caching / retrieval
  • Loading branch information
ngmr committed Jun 5, 2015
1 parent ad04c68 commit 5a31410bf59dcadf27c57209e6208a91e7823515
Showing 3 changed files with 83 additions and 15 deletions.
@@ -17,10 +17,9 @@
*/
package org.apache.yoko.rmi.impl;

import org.omg.CORBA.AttributeDescription;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.ValueDefPackage.FullValueDescription;
import org.omg.CORBA.ValueMember;
import org.omg.CORBA.ValueDefPackage.FullValueDescription;

/**
* @author krab
@@ -22,6 +22,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;

import org.omg.CORBA.portable.InputStream;
@@ -35,10 +36,68 @@ public abstract class TypeDescriptor extends ModelElement {

protected RemoteInterfaceDescriptor remoteDescriptor;

private FullKey _key;

public Class getJavaClass() {
return _java_class;
}

public final FullKey getKey() {
if (null == _key) {
_key = new FullKey(getRepositoryID(), getJavaClass());
}
return _key;
}

public static class SimpleKey {
private final String repid;

public SimpleKey(String repid) {
this.repid = repid;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((repid == null) ? 0 : repid.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof SimpleKey)) return false;
return Objects.equals(repid, ((SimpleKey)obj).repid);
}
}

public static final class FullKey extends SimpleKey {
private final Class<?> localType;

public FullKey(String repid, Class<?> localType) {
super(repid);
this.localType = localType;
}

@Override
public int hashCode() {
// must just be the same as SimpleKey's hashCode
return super.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof SimpleKey)) return false;
if (obj instanceof FullKey &&
!!!Objects.equals(localType, ((FullKey)obj).localType)) return false;
return super.equals(obj);
}
}

@Override
public String toString() {
return String.format("%s{class=\"%s\",repId=\"%s\"}",
@@ -39,40 +39,50 @@

import javax.rmi.CORBA.ClassDesc;

import org.apache.yoko.rmi.util.ByteBuffer;
import org.apache.yoko.rmi.util.ByteString;
import org.apache.yoko.rmi.util.SearchKey;
import org.apache.yoko.rmi.util.WeakKey;
import org.omg.CORBA.MARSHAL;
import org.omg.CORBA.ValueDefPackage.FullValueDescription;
import org.omg.CORBA.portable.IDLEntity;
import org.omg.SendingContext.CodeBase;
import org.omg.SendingContext.CodeBaseHelper;
import org.omg.SendingContext.RunTime;
import org.apache.yoko.rmi.util.SearchKey;
import org.apache.yoko.rmi.util.WeakKey;

import org.apache.yoko.rmi.impl.TypeDescriptor.FullKey;
import org.apache.yoko.rmi.impl.TypeDescriptor.SimpleKey;

public class TypeRepository {
static final Logger logger = Logger.getLogger(TypeRepository.class.getName());

org.omg.CORBA.ORB orb;

private static final class TypeDescriptorCache {
private final ConcurrentMap<WeakKey<String>, WeakReference<TypeDescriptor>> map = new ConcurrentHashMap<>();
private final ReferenceQueue<String> staleKeys = new ReferenceQueue<>();
private final ConcurrentMap<WeakKey<FullKey>, WeakReference<TypeDescriptor>> map =
new ConcurrentHashMap<>();
private final ReferenceQueue<FullKey> staleKeys = new ReferenceQueue<>();

public TypeDescriptor get(String repid) {
cleanStaleKeys();
WeakReference<TypeDescriptor> ref =
map.get(new SearchKey<SimpleKey>(new SimpleKey(repid)));
return (null == ref) ? null : ref.get();
}

public TypeDescriptor get(String repId) {
public TypeDescriptor get(String repid, Class<?> localType) {
cleanStaleKeys();
WeakReference<TypeDescriptor> ref = map.get(new SearchKey<String>(repId));
WeakReference<TypeDescriptor> ref =
map.get(new SearchKey<FullKey>(new FullKey(repid, localType)));
return (null == ref) ? null : ref.get();
}

public void put(TypeDescriptor typeDesc) {
cleanStaleKeys();
final WeakReference<TypeDescriptor> value = new WeakReference<>(typeDesc);
map.putIfAbsent(new WeakKey<String>(typeDesc.getRepositoryID(), staleKeys), value);
map.putIfAbsent(new WeakKey<FullKey>(typeDesc.getKey(), staleKeys), value);
}

private void cleanStaleKeys() {
for (Reference<? extends String> staleKey = staleKeys.poll(); staleKey != null; staleKey = staleKeys.poll()) {
for (Reference<? extends Object> staleKey = staleKeys.poll(); staleKey != null; staleKey = staleKeys.poll()) {
map.remove(staleKey);
}
}
@@ -314,13 +324,13 @@ public ValueDescriptor getDescriptor(Class<?> clz, String repid,
return (ValueDescriptor) getDescriptor(clz);
}

ValueDescriptor clzdesc = (ValueDescriptor) repIdDescriptors.get(repid);
ValueDescriptor clzdesc = (ValueDescriptor) repIdDescriptors.get(repid, clz);
if (clzdesc != null) {
return clzdesc;
}

if (clz != null) {
logger.fine("Requesting type descriptor for class " + clz.getName() + " with repid " + repid);
logger.fine("Requesting type descriptor for class " + clz.getName() + " with repid " + repid);
// special handling for array value types.
if (clz.isArray()) {
//TODO don't we need to look up the FVD for the array element?
@@ -339,7 +349,7 @@ public ValueDescriptor getDescriptor(Class<?> clz, String repid,
// and padding and these can't be reliably identified without this remote info. cf YOKO-434.
}

logger.fine("Requesting type descriptor for repid " + repid);
logger.fine("Requesting type descriptor for repid " + repid);
CodeBase codebase = CodeBaseHelper.narrow(runtime);
if (codebase == null) {
throw new MARSHAL("cannot locate RunTime CodeBase");

0 comments on commit 5a31410

Please sign in to comment.